Mercurial > hg > toybox
comparison lib/pending.c @ 995:893c86bbe452
Add daemonize function to lib for klogd and syslogd
author | Felix Janda <felix.janda@posteo.de> |
---|---|
date | Sat, 10 Aug 2013 20:18:18 +0200 |
parents | 59d4d453296b |
children | 6cb9e7142574 |
comparison
equal
deleted
inserted
replaced
994:00c7b6f4104a | 995:893c86bbe452 |
---|---|
100 char *xastrcat (char *x, char *y) { | 100 char *xastrcat (char *x, char *y) { |
101 x = astrcat (x, y); | 101 x = astrcat (x, y); |
102 if (!x) error_exit ("xastrcat"); | 102 if (!x) error_exit ("xastrcat"); |
103 return x; | 103 return x; |
104 } | 104 } |
105 | |
106 void daemonize(void) | |
107 { | |
108 int fd = open("/dev/null", O_RDWR); | |
109 if (fd < 0) fd = xcreate("/", O_RDONLY, 0666); | |
110 | |
111 pid_t pid = fork(); | |
112 if (pid < 0) perror_exit("DAEMON: failed to fork"); | |
113 if (pid) exit(EXIT_SUCCESS); | |
114 | |
115 setsid(); | |
116 dup2(fd, 0); | |
117 dup2(fd, 1); | |
118 dup2(fd, 2); | |
119 if (fd > 2) close(fd); | |
120 } |