-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchild.c
411 lines (366 loc) · 10.3 KB
/
child.c
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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
/**
* This file is part of mozplugger a fork of plugger, for list of developers
* see the README file.
*
* 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, USA.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <unistd.h>
#include <signal.h>
#include <errno.h>
#include <fcntl.h>
#include <sysexits.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include "cmd_flags.h"
#include "debug.h"
/* Time to wait for a process to exit */
#define KILL_TIMEOUT_USEC 100000
static int sig_rd_fd = -1;
static int sig_wr_fd = -1;
static int chld_rd_fd = -1; /* fd to read stdout of child */
/**
* Restore the SIG CHLD handler to default behaviour
*
* @return None
*/
void restore_SIGCHLD_to_default(void)
{
if(sig_wr_fd >= 0)
{
signal(SIGCHLD, SIG_DFL);
close(sig_wr_fd);
sig_wr_fd = -1;
}
if(sig_rd_fd >= 0)
{
close(sig_rd_fd);
sig_rd_fd = -1;
}
}
/**
* Convert signal to message in pipe - Callback function attached as a signal
* handler. This sends a message over a pipe about the signal that has
* just happened. This allows signals to be handled by select()
*
* @param[in] sig The signal
*
* @return nothing
*/
static void sig_to_fd(int sig)
{
D("Signal %i routed to pipe\n", sig);
if(write(sig_wr_fd, &sig, sizeof(int)) != sizeof(int))
{
D("Signal write to pipe failed\n");
}
}
/**
* This function creates a signal handler for SIGCHLD that causes a file descriptor
* to be ready when the signal occurs. This makes it easy to handle the signal
* within a select loop.
*
* @return The fd
*/
int redirect_SIGCHLD_to_fd(void)
{
int fds[2];
if(pipe(fds) != 0)
{
D("Failed to create pipe to handle signals");
sig_rd_fd = sig_wr_fd = -1;
return -1;
}
sig_rd_fd = fds[0];
sig_wr_fd = fds[1];
/* Re-direct SIGCHLD events through a pipe to be see by select() */
signal(SIGCHLD, sig_to_fd);
return sig_rd_fd;
}
/**
* This function is called to get the value of the file descriptor for the
* SIGCHLD events.
*
* @return file descriptor
*/
int get_SIGCHLD_fd(void)
{
return sig_rd_fd;
}
/**
* This function is called to handle the SIGCHLD file descriptor when select
* detects an event on that file descriptor.
*
* @return None
*/
void handle_SIGCHLD_event(void)
{
int sig;
read(sig_rd_fd, &sig, sizeof(int));
D("Seen SIGCHLD event\n");
}
/**
* This function is called to get the value of the file descriptor for the
* stdout of the child.
*
* @return file descriptor
*/
int get_chld_out_fd(void)
{
return chld_rd_fd;
}
/**
* Handle the event of the child writing to stdout / stderr
*
* @param[in] fd
*
* @return false if failed
*/
bool handle_chld_out_event(int fd)
{
static char linebuf[256];
static int pos = 0;
int n;
int left = sizeof(linebuf) - pos - 1;
if(left <= 0)
{
D("chld stdout > 256 without \n");
D("[CHLD]:%s\n", linebuf);
pos = 0;
left = sizeof(linebuf) -1;
}
if((n = read(fd, &linebuf[pos], left)) > 0)
{
char * end;
pos += n;
linebuf[pos] = '\0';
while( (end = strchr(linebuf, '\n')) != NULL)
{
char * p = end;
while((p >= linebuf) && ((*p == '\r') || (*p == '\n') || (*p == ' ') || (*p == '\t')))
{
p--;
}
p[1] = '\0';
if(p > linebuf)
{
D("[CHLD]:%s\n", linebuf);
}
pos -= (&end[1] - linebuf);
memmove(linebuf, &end[1], pos+1);
}
return true;
}
else
{
if(pos > 0)
{
D("[CHLD]:%s\n", linebuf);
}
pos = 0;
return false;
}
}
/**
* Wrapper for execlp() that calls the application.
*
* WARNING: This function runs in the daughter process so one must assume the
* daughter uses a copy (including) heap memory of the parent's memory space
* i.e. any write to memory here does not affect the parent memory space.
* Since Linux uses copy-on-write, best leave memory read-only and once execlp
* is called, all daughter memory is wiped anyway (except the stack).
*
* @param[in] command Pointer to list of arguments
* @param[in] flags The flags
*
* @return Process ID
*/
pid_t spawn_app(char * command, const int flags)
{
int fds[2] = {-1, -1};
pid_t pid;
/* For debug connect a pipe to the stdout / stderr of the child */
if( (((flags & H_DAEMON)) == 0) && is_debugging())
{
pipe(fds);
}
pid = fork();
if(pid == 0)
{
int maxFd;
int i;
char * app_argv[4];
/* Group child and any grand children under the same process group */
if(setpgid(pid, 0) != 0)
{
D("Failed to set process group ID\n");
}
app_argv[0] = "/bin/sh";
app_argv[1] = "-c";
app_argv[2] = command;
app_argv[3] = 0;
/* Redirect stdout & stderr to /dev/null */
if(( (flags & (H_NOISY | H_DAEMON)) != 0) && (fds[1] <= 0))
{
fds[1] = open("/dev/null", O_RDONLY);
}
if(fds[1] > 0)
{
D("Redirecting stdout and stderr to %i\n", fds[1]);
dup2(fds[1], 1);
dup2(fds[1], 2);
close(fds[1]);
close(fds[0]);
}
/* For a DAEMON we must also redirect STDIN to /dev/null
* otherwise we can get some weird behaviour especially with realplayer
* (I suspect it uses stdin to communicate with its peers) */
if( (flags & H_DAEMON) != 0)
{
const int ifd = open("/dev/null", O_WRONLY);
D("Redirecting stdin also\n");
if(ifd == -1)
{
exit(EX_UNAVAILABLE);
}
dup2(ifd, 0);
close(ifd);
}
D("Running %s\n", command);
close_debug();
/* Close up those file descriptors */
close(sig_rd_fd);
close(sig_wr_fd);
maxFd = sysconf(_SC_OPEN_MAX);
for(i = 3; i <= maxFd; i++)
{
close(i);
}
// fprintf(stderr, "Started\n");
execvp(app_argv[0], app_argv);
D("Execvp failed. (errno=%d)\n", errno);
exit(EX_UNAVAILABLE);
}
else
{
if(fds[1] > 0)
{
close(fds[1]);
chld_rd_fd = fds[0];
}
}
return pid;
}
/**
* Wait for the child
*
* @param[in] pid The child pid
*
* @return (-2) If app crashed, (-1) If app exited but with error,
* (0) If child dettached or exited successfully, (1) If still running
*/
int wait_child(pid_t pid)
{
int retVal = 1;
int status;
if(waitpid(pid, &status, WNOHANG) != 0)
{
while(chld_rd_fd >= 0)
{
fd_set fds;
struct timeval timeout;
timeout.tv_usec = 100000;
timeout.tv_sec = 0;
FD_ZERO(&fds);
FD_SET(chld_rd_fd, &fds);
if(select(chld_rd_fd + 1, &fds, NULL, NULL, &timeout) > 0)
{
if(!handle_chld_out_event(chld_rd_fd))
{
close(chld_rd_fd);
chld_rd_fd = -1;
}
}
else
{
close(chld_rd_fd);
chld_rd_fd = -1;
}
}
/* If Application completed in a bad way, then lets give up now */
if (!WIFEXITED(status))
{
D("Process dumped core or something...\n");
retVal = -2;
}
else if (WEXITSTATUS(status))
{
D("Process exited with status: %d\n", WEXITSTATUS(status));
retVal = -1;
}
else
{
D("Child has stopped or detached\n");
retVal = 0;
}
}
// D("wait_child retVal=%i\n", retVal);
return retVal;
}
/**
* Adaptive kill(), keep calling kill with higher and higher signal level,
*
* NOTE: kill return zero on successfully sending the signal so the code
* only exits the nested ifs when kill no longer can send the signal!
*
* @param[in] pid The process ID of process to kill
*/
void kill_app(pid_t pid)
{
int status;
D("Killing PID %d and associated process group with with SIGTERM\n", pid);
kill(pid, SIGTERM); /* Signal the child */
if(kill(-pid, SIGTERM) == 0) /* Signal the childs progress group */
{
usleep(KILL_TIMEOUT_USEC);
/* Child is still alive */
D("Killing process group %d with SIGTERM\n", pid);
if( kill(-pid, SIGTERM) == 0) /* child got signal */
{
usleep(KILL_TIMEOUT_USEC);
/* Child is still alive */
D("Killing process group %d with SIGTERM\n", pid);
if( kill(-pid, SIGTERM) == 0) /* child got signal */
{
usleep(KILL_TIMEOUT_USEC);
/* Child still ALIVE? */
D("Killing PID %d with SIGKILL\n", pid);
kill(pid, SIGKILL);
kill(-pid, SIGKILL);
}
}
}
D("Waiting for kids\n");
while(waitpid(-1, &status, WNOHANG) > 0);
if(chld_rd_fd >= 0)
{
close(chld_rd_fd);
}
}