equal
deleted
inserted
replaced
5 |
5 |
6 #include <stdlib.h> |
6 #include <stdlib.h> |
7 #include <stdio.h> |
7 #include <stdio.h> |
8 #include <string.h> |
8 #include <string.h> |
9 #include <sys/types.h> |
9 #include <sys/types.h> |
|
10 #include <sys/wait.h> |
10 #include <unistd.h> |
11 #include <unistd.h> |
11 |
|
12 |
12 |
13 static void fail(const char *msg) |
13 static void fail(const char *msg) |
14 { |
14 { |
15 fprintf(stderr, "%s\n", msg); |
15 fprintf(stderr, "%s\n", msg); |
16 fflush(stderr); |
16 fflush(stderr); |
32 |
32 |
33 /* setsid */ |
33 /* setsid */ |
34 |
34 |
35 if (setsid() == -1) { |
35 if (setsid() == -1) { |
36 pid_t pid = fork(); |
36 pid_t pid = fork(); |
|
37 int status; |
|
38 |
37 if (pid == -1) fail("Cannot set session id (failed to fork)"); |
39 if (pid == -1) fail("Cannot set session id (failed to fork)"); |
38 else if (pid != 0) exit(0); |
40 else if (pid != 0) { |
|
41 if (waitpid(pid, &status, 0) == -1) { |
|
42 fail("Cannot join forked process"); |
|
43 } |
|
44 |
|
45 if (WIFEXITED(status)) { |
|
46 exit(WEXITSTATUS(status)); |
|
47 } |
|
48 else if (WIFSIGNALED(status)) { |
|
49 exit(128 + WTERMSIG(status)); |
|
50 } |
|
51 else { |
|
52 fail("Unknown status of forked process"); |
|
53 } |
|
54 } |
39 else if (setsid() == -1) fail("Cannot set session id (after fork)"); |
55 else if (setsid() == -1) fail("Cannot set session id (after fork)"); |
40 } |
56 } |
41 |
57 |
42 |
58 |
43 /* report pid */ |
59 /* report pid */ |