File: external.c

package info (click to toggle)
mountapp 3.0-10
  • links: PTS
  • area: main
  • in suites: lenny
  • size: 700 kB
  • ctags: 238
  • sloc: sh: 2,717; ansic: 2,636; makefile: 68
file content (303 lines) | stat: -rw-r--r-- 6,550 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
/*
** Copyright (C) 1998 Steve Borho <steve@borho.org>
**  
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
** 
** This program is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
** 
** You should have received a copy of the GNU General Public License
** along with this program; if not, write to the Free Software 
** Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/

#include "mount.h"

void            newSystem(char *command, MPInfo * mp)
{
    int pid = 0;
   
    if (mp && mp->childPid)
        return;

    pid = fork();
    if (pid == 0)
    {
        char           *argv[4] =
        {"sh", "-c", command, 0};

        execv("/bin/sh", argv);
        exit(127);
    }
    else if ((pid != -1) && mp)
    {
        mp->childPid = pid;
    }
}

void            internalMount(MPInfo * mp)
{
    if (mp->childPid)
        return;

    mp->childPid = fork();

    if (mp->childPid == 0)
    {
        char           *argv[3] =
        {"mount", mp->mountStr, 0};

        execv(MOUNT_CMD, argv);
        exit(127);
    }
    else if (mp->childPid == -1)
    {
        perror("internal mount failed");
        mp->childPid = 0;   
    }
}

void            internalUmount(MPInfo * mp)
{
    if (mp->childPid)
        return;
   
    mp->childPid = fork();

    if (mp->childPid == 0)
    {
        char           *argv[3] =
        {"umount", mp->mountStr, 0};

        execv(UMOUNT_CMD, argv);
        exit(127);
    }
    else if (mp->childPid == -1)
    {
        perror("internal umount failed");
        mp->childPid = 0;
    }
}

void            internalEject(MPInfo * mp)
{
    mp->childPid = fork();
    if (mp->childPid == 0)
    {
        char           *argv[3] =
        {"eject", mp->devStr, 0};

        execv(EJECT_CMD, argv);
        exit(127);
    }
    else if (mp->childPid == -1)
    {
        mp->childPid = 0;
    }
}

void            ejectDrive(MPInfo * mp)
{
    if (mp->childPid)
    {
        fprintf(stderr,
                "eject failed because pid %i is still active for mount %s\n",
                mp->childPid, mp->devStr);
        return;
    }
    
    if (0 == ejectcmd[0])
    {
        internalEject(mp);
    }
    else
    {
        char            cmd[256];
        int             n = (sizeof(cmd) / sizeof(cmd[0]));
       
        snprintf(cmd, n, ejectcmd, mp->mountStr);
        cmd[n - 1] = '\0';
        newSystem(cmd, mp);
    }
}

void            callConfigApp(allInfo *ai)
{
    char buf[256];

    if (!fork())
    {
        char           *argv[5] =
        {"config", "-s", "-m", ai->mpi[ai->curMount]->mountStr, 0};

        sprintf(buf, "%s/mount.conf", BINDIR);

        execv(buf, argv);
        exit(127);
    }
}

void            showHelpPage()
{
    char buf[256];

    if (!fork())
    {
        char           *argv[3] =
        {"config", "-H", 0};

        sprintf(buf, "%s/mount.conf", BINDIR);

        execv(buf, argv);
        exit(127);
    }
}

void            handleReturnCode(MPInfo *mp)
{
    char filebuf[256];
    char *filename = (char *) 0;
    char *title = (char *) 0;

    switch(WEXITSTATUS(mp->childStatus))
    {
    case 2:
        /*
         * Insufficient rights to mount device
         */
        mp->childStatusChar = 'P';
        filename = "noperms.txt";
        title = "Not enough permissions";
        break;
    case 1:
        /*
         * Device busy
         */
        mp->childStatusChar = 'B';
        filename = "devicebusy.txt";
        title = "Drive in use";
        break;
    case 32:
        /*
         * Not ready or directory already in use
         */
        mp->childStatusChar = 'R';
        filename = "notmountable.txt";
        title = "Device not ready";
        break;
    }

    if (aiPtr->newbieMessages && filename != (char *) 0)
    {
        sprintf(filebuf, "%s/%s", ICONDIR, filename);

        if (!fork())
        {
            char           *argv[8] =
            {"xmessage", "-name", title, "-file", filebuf, "-buttons",
                "I gotchya", 0};

            execv(XMESSAGE, argv);
            exit(127);
        }
    }
}

void            handleDeadChild(long pid, int status)
{
    MPInfo *mp = 0;
    int i = 0;

    for (i = 0; i < aiPtr->numMounts ; i++)
    {
       if (aiPtr->mpi[i]->childPid == pid)
       {
           mp = aiPtr->mpi[i];
           break;
       }
    }
   
    if (mp)
    {
        mp->childPid = 0;
        mp->childStatus = status;
        mp->childStatusChar = 0;

        if (i == aiPtr->curMount)
        {
            aiPtr->currentMountChanged = TRUE;
        }
        if (WIFEXITED(status))
        {
            if (WEXITSTATUS(status))
            {
                aiPtr->errorOccured = TRUE;
            }
            else if (mp->performEject)
            {
                mp->performEject = FALSE;
                ejectDrive(mp);
            }
        }
    }
}

#define     P_MAX      5
static long pid[P_MAX];
static int  retStatus[P_MAX];
static int  p_head = 0, p_tail = 0;
/*
 * Handle SIGCHLD signals
 *
 * This function is called by the kernel when one of our child
 * processes dies.  We need to call wait to get the status of
 * the child and save it in a ring buffer so it can be examined
 * later.  Before returning, we re-register the signal handler.
 */
void            noZombie(int sig)
{
    long exitedPid = 0;
    int status = 0;

    exitedPid = wait(&status);

    if ((p_head == p_tail - 1) || (p_head == P_MAX-1 && p_tail == 0))
    {
        /* Ahhhhhhh. Fork bomb */
        return;
    }
    else
    {
        pid[p_head] = exitedPid;
        retStatus[p_head] = status;
        p_head = (p_head == P_MAX-1) ? 0 : ++p_head;
    }

    signal(SIGCHLD, noZombie);
}

/*
 * This function is called from the main loop.  It's job is to
 * empty the ring buffers filled by the SIGCHLD signal handler
 */
void            shovelDeadChildren()
{
    while (p_head != p_tail)
    {
        handleDeadChild(pid[p_tail], retStatus[p_tail]);
        p_tail = (p_tail == P_MAX-1) ? 0 : ++p_tail;
    }
}


/* Signal handler for SIGHUP */
void            sigHUPHandler(int sig)
{
    sigHUP = TRUE;
    signal(SIGHUP, sigHUPHandler);
}