summaryrefslogtreecommitdiffstats
path: root/daemon.c
blob: dcc566947b78fdcef4547828f5375ecb93a84233 (plain)
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
49
50
51
52
53
54
55
56
57
58
59
60
#include <fcntl.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <errno.h>

#include <sys/param.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <sys/stat.h>

#include <netax25/daemon.h>

int daemon_start(int ignsigcld)
{
	/* Code to initialize a daemon process. Taken from _UNIX Network	*/
	/* Programming_ pp.72-85, by W. Richard Stephens, Prentice		*/
	/* Hall PTR, 1990							*/

	int childpid;

	/* If started by init, don't bother */
	if (getppid() == 1)
		goto out;

	/* Ignore the terminal stop signals */
	signal(SIGTTOU, SIG_IGN);
	signal(SIGTTIN, SIG_IGN);
	signal(SIGTSTP, SIG_IGN);

	/* Fork and let parent exit, insures we're not a process group leader */
	if ((childpid = fork()) < 0) {
		return 0;
	} else if (childpid > 0) {
		exit(0);
	}

	/*
	 * Disassociate from controlling terminal and process group and
	 * ensure the process can't reacquire a new controlling terminal.
	 * We're freshly forked, so setsid can't fail.
	 */
	(void) setsid();

out:
	/* Move the current directory to root, to make sure we aren't on a	*/
	/* mounted filesystem.							*/
	if (chdir("/") < 0)
		return 0;

	/* Clear any inherited file mode creation mask.	*/
	umask(0);

	if (ignsigcld)
		signal(SIGCHLD, SIG_IGN);

	/* That's it, we're a "daemon" process now */
	return 1;
}