Mercurial > notdcc
comparison dcclib/daemon.c @ 0:c7f6b056b673
First import of vendor version
author | Peter Gervai <grin@grin.hu> |
---|---|
date | Tue, 10 Mar 2009 13:49:58 +0100 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:c7f6b056b673 |
---|---|
1 /* compatibility hack for old systems lacking daemon() */ | |
2 | |
3 #include "dcc_config.h" | |
4 #include "dcc_paths.h" | |
5 | |
6 #include <stdio.h> | |
7 #include <unistd.h> | |
8 #include <stdlib.h> | |
9 #include <fcntl.h> | |
10 | |
11 int | |
12 dcc_daemon(int nochdir, int noclose) | |
13 { | |
14 int retv; | |
15 | |
16 if (!nochdir) { | |
17 if (chdir("/") == -1) | |
18 perror("chdir /"); | |
19 } | |
20 retv = fork(); | |
21 if (retv == -1) | |
22 return -1; /* fork() failed */ | |
23 | |
24 if (retv != 0) | |
25 _exit(0); /* parent of new child */ | |
26 | |
27 /* fork again after setsid() so that the PID is not the session | |
28 * group leader so that opening a tty device won't make a | |
29 * controlling terminal. */ | |
30 setsid(); | |
31 retv = fork(); | |
32 if (retv == -1) { | |
33 perror("fork"); /* second fork() failed */ | |
34 exit(1); /* cannot tell caller */ | |
35 } | |
36 if (retv != 0) | |
37 _exit(0); /* parent of final child */ | |
38 | |
39 if (!noclose) { | |
40 close(0); | |
41 close(1); | |
42 close(2); | |
43 open(_PATH_DEVNULL, O_RDWR, 0666); | |
44 dup2(0, 1); | |
45 dup2(0, 2); | |
46 } | |
47 return 0; | |
48 } |