File: daemon.c

package info (click to toggle)
dcc 1.2.74-2
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 3,552 kB
  • ctags: 4,041
  • sloc: ansic: 41,034; perl: 2,310; sh: 2,186; makefile: 224
file content (48 lines) | stat: -rw-r--r-- 908 bytes parent folder | download | duplicates (2)
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
45
46
47
48
/* compatibility hack for old systems lacking daemon() */

#include "dcc_config.h"
#include "dcc_paths.h"

#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>

int
dcc_daemon(int nochdir, int noclose)
{
	int retv;

	if (!nochdir) {
		if (chdir("/") == -1)
			perror("chdir /");
	}
	retv = fork();
	if (retv == -1)
		return -1;		/* fork() failed */

	if (retv != 0)
		_exit(0);		/* parent of new child */

	/* fork again after setsid() so that the PID is not the session
	 * group leader so that opening a tty device won't make a
	 * controlling terminal. */
	setsid();
	retv = fork();
	if (retv == -1) {
		perror("fork");		/* second fork() failed */
		exit(1);		/* cannot tell caller */
	}
	if (retv != 0)
		_exit(0);		/* parent of final child */

	if (!noclose) {
		close(0);
		close(1);
		close(2);
		open(_PATH_DEVNULL, O_RDWR, 0666);
		dup2(0, 1);
		dup2(0, 2);
	}
	return 0;
}