]> pd.if.org Git - startuptools/blob - daemon.c
fix empty module list handling
[startuptools] / daemon.c
1 /*
2  * daemonize and exec
3  */
4
5 #include <stdlib.h>
6 #include <stdio.h>
7
8 #include <sys/types.h>
9 #include <sys/stat.h>
10 #include <fcntl.h>
11
12 #include <unistd.h>
13
14 #include "exec.c"
15
16 int main(int ac, char *av[]) {
17         pid_t pid, sid;
18
19         pid = fork();
20         if (pid < 0) {
21                 fprintf(stderr, "fork fail!\n");
22                 exit(EXIT_FAILURE);
23         }
24
25         if (pid > 0) {
26                 exit(EXIT_SUCCESS);
27         }
28
29         umask(0);
30
31         sid = setsid();
32         if (sid < 0) {
33                 exit(EXIT_SUCCESS);
34         }
35
36         pid = fork();
37         if (pid < 0) {
38                 fprintf(stderr, "fork fail!\n");
39                 exit(EXIT_FAILURE);
40         }
41         if (pid > 0) {
42                 /* fprintf(stderr, "pid = %d\n", pid); */
43                 exit(EXIT_SUCCESS);
44         }
45
46         /* reset the file descriptors */
47         if (isatty(0)) {
48                 close(0);
49                 open("/dev/null", O_RDONLY);
50         }
51         if (isatty(1)) {
52                 close(0);
53                 open("/dev/null", O_WRONLY);
54         }
55         if (isatty(2)) {
56                 close(0);
57                 open("/dev/null", O_WRONLY);
58         }
59
60         if (chdir("/") < 0) {
61                 exit(EXIT_FAILURE);
62         }
63
64         do_exec(1, ac, av);
65
66         /* shouldn't get here... */
67         return EXIT_FAILURE;
68 }