aboutsummaryrefslogtreecommitdiff
path: root/src/libthread/daemonize.c
blob: da8ec977bbd6f64da83052545f1d6b002d84862d (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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#include <u.h>
#include <sys/time.h>
#include <sys/resource.h>
#include "threadimpl.h"

#undef waitpid
#undef pipe
#undef wait

static int sigpid;
static int threadpassfd;

static void
child(void)
{
	int status, pid;
	struct rlimit rl;

	notedisable("sys: child");
	pid = waitpid(sigpid, &status, 0);
	if(pid < 0){
		fprint(2, "%s: wait: %r\n", argv0);
		_exit(97);
	}
	if(WIFEXITED(status))
		 _exit(WEXITSTATUS(status));
	if(WIFSIGNALED(status)){
		/*
		 * Make sure we don't scribble over the nice
		 * core file that our child just wrote out.
		 */
		rl.rlim_cur = 0;
		rl.rlim_max = 0;
		setrlimit(RLIMIT_CORE, &rl);

		signal(WTERMSIG(status), SIG_DFL);
		raise(WTERMSIG(status));
		_exit(98);	/* not reached */
	}
	fprint(2, "%s: wait pid %d status 0x%ux\n", pid, status);
	_exit(99);
}

static void
sigpass(int sig)
{
	if(sig == SIGCHLD)
		child();
	else
		kill(sigpid, sig);
}

void
_threadsetupdaemonize(void)
{
	int i, n, pid;
	int p[2];
	char buf[20];

	sigpid = 1;

	threadlinklibrary();

	if(pipe(p) < 0)
		sysfatal("passer pipe: %r");

	/* hide these somewhere they won't cause harm */
	if(dup(p[0], 98) < 0 || dup(p[1], 99) < 0)
		sysfatal("passer pipe dup: %r");
	close(p[0]);
	close(p[1]);
	p[0] = 98;
	p[1] = 99;

	/* close on exec */
	if(fcntl(p[0], F_SETFD, 1) < 0 || fcntl(p[1], F_SETFD, 1) < 0)
		sysfatal("passer pipe pipe fcntl: %r");

	noteenable("sys: child");
	signal(SIGCHLD, sigpass);
	switch(pid = fork()){
	case -1:
		sysfatal("passer fork: %r");
	default:
		close(p[1]);
		break;
	case 0:
		for(i=0; i<100; i++) sched_yield();
		notedisable("sys: child");
		signal(SIGCHLD, SIG_DFL);
		rfork(RFNOTEG);
		close(p[0]);
		threadpassfd = p[1];
		return;
	}

	sigpid = pid;
	for(i=0; i<NSIG; i++){
		struct sigaction sa;

		memset(&sa, 0, sizeof sa);
		sa.sa_handler = sigpass;
		sa.sa_flags |= SA_RESTART;
		sigaction(i, &sa, nil);
	}

	for(;;){
		n = read(p[0], buf, sizeof buf-1);
		if(n == 0){	/* program exited */
			child();
		}
		if(n > 0)
			break;
		print("passer read: %r\n");
	}
	buf[n] = 0;
	_exit(atoi(buf));
}

void
_threaddaemonize(void)
{
	if(threadpassfd >= 0){
		write(threadpassfd, "0", 1);
		close(threadpassfd);
		threadpassfd = -1;
	}
}