-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunfg.c
44 lines (39 loc) · 996 Bytes
/
runfg.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
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/prctl.h>
#include <sys/wait.h>
int main(int argc, char **argv) {
int result = EXIT_FAILURE, status;
pid_t child, command;
if (argc < 2) {
fprintf(stderr, "Usage: %s CMD [ARG]...\n", argv[0]);
return 64;
}
if (prctl(PR_SET_CHILD_SUBREAPER, 1) < 0)
err(EXIT_FAILURE, "prctl PR_SET_CHILD_SUBREAPER");
switch (command = fork()) {
case -1:
err(EXIT_FAILURE, "fork");
case 0:
execvp(argv[1], argv + 1);
err(EXIT_FAILURE, "exec %s", argv[1]);
}
while (1) {
child = waitpid(-1, &status, 0);
if (child < 0) {
if (errno == ECHILD)
return result;
if (errno != EINTR)
err(EXIT_FAILURE, "waitpid");
} else if (child == command) {
if (WIFEXITED(status))
result = WEXITSTATUS(status);
if (WIFSIGNALED(status))
result = 128 + WTERMSIG(status);
command = -1;
}
}
}