xref: /freebsd/usr.sbin/daemon/daemon.c (revision 1de7b4b805ddbf2429da511c053686ac4591ed89)
1bd06a3ecSMike Barcroft /*-
2*1de7b4b8SPedro F. Giffuni  * SPDX-License-Identifier: BSD-3-Clause
3*1de7b4b8SPedro F. Giffuni  *
4bd06a3ecSMike Barcroft  * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
5bd06a3ecSMike Barcroft  *
6bd06a3ecSMike Barcroft  * Redistribution and use in source and binary forms, with or without
7bd06a3ecSMike Barcroft  * modification, are permitted provided that the following conditions
8bd06a3ecSMike Barcroft  * are met:
9bd06a3ecSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
10bd06a3ecSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
11bd06a3ecSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
12bd06a3ecSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
13bd06a3ecSMike Barcroft  *    documentation and/or other materials provided with the distribution.
14bd06a3ecSMike Barcroft  * 3. Berkeley Software Design Inc's name may not be used to endorse or
15bd06a3ecSMike Barcroft  *    promote products derived from this software without specific prior
16bd06a3ecSMike Barcroft  *    written permission.
17bd06a3ecSMike Barcroft  *
18bd06a3ecSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19bd06a3ecSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20bd06a3ecSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21bd06a3ecSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22bd06a3ecSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23bd06a3ecSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24bd06a3ecSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25bd06a3ecSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26bd06a3ecSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27bd06a3ecSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28bd06a3ecSMike Barcroft  * SUCH DAMAGE.
29bd06a3ecSMike Barcroft  *
30bd06a3ecSMike Barcroft  *	From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
31bd06a3ecSMike Barcroft  */
32bd06a3ecSMike Barcroft 
3354ede02dSPhilippe Charnier #include <sys/cdefs.h>
3454ede02dSPhilippe Charnier __FBSDID("$FreeBSD$");
3554ede02dSPhilippe Charnier 
36c6262cb6SPawel Jakub Dawidek #include <sys/param.h>
3753c49998SMikolaj Golub #include <sys/mman.h>
382ad43027SMikolaj Golub #include <sys/wait.h>
39bd06a3ecSMike Barcroft 
4053d49b37SJilles Tjoelker #include <fcntl.h>
41bd06a3ecSMike Barcroft #include <err.h>
42846be7bdSPoul-Henning Kamp #include <errno.h>
43c6262cb6SPawel Jakub Dawidek #include <libutil.h>
44e6d4b388STom Rhodes #include <login_cap.h>
45195fc497SMikolaj Golub #include <pwd.h>
46195fc497SMikolaj Golub #include <signal.h>
47bd06a3ecSMike Barcroft #include <stdio.h>
48bd06a3ecSMike Barcroft #include <stdlib.h>
49bd06a3ecSMike Barcroft #include <unistd.h>
5053d49b37SJilles Tjoelker #include <string.h>
5153d49b37SJilles Tjoelker #include <strings.h>
5253d49b37SJilles Tjoelker #define SYSLOG_NAMES
5353d49b37SJilles Tjoelker #include <syslog.h>
5453d49b37SJilles Tjoelker #include <time.h>
5553d49b37SJilles Tjoelker #include <assert.h>
56bd06a3ecSMike Barcroft 
5753d49b37SJilles Tjoelker #define LBUF_SIZE 4096
5853d49b37SJilles Tjoelker 
5953d49b37SJilles Tjoelker struct log_params {
6053d49b37SJilles Tjoelker 	int dosyslog;
6153d49b37SJilles Tjoelker 	int logpri;
6253d49b37SJilles Tjoelker 	int noclose;
6353d49b37SJilles Tjoelker 	int outfd;
6453d49b37SJilles Tjoelker };
6553d49b37SJilles Tjoelker 
66e6d4b388STom Rhodes static void restrict_process(const char *);
6753d49b37SJilles Tjoelker static void handle_term(int);
6853d49b37SJilles Tjoelker static void handle_chld(int);
6953d49b37SJilles Tjoelker static int  listen_child(int, struct log_params *);
7053d49b37SJilles Tjoelker static int  get_log_mapping(const char *, const CODE *);
7153d49b37SJilles Tjoelker static void open_pid_files(const char *, const char *, struct pidfh **,
7253d49b37SJilles Tjoelker 			   struct pidfh **);
7353d49b37SJilles Tjoelker static void do_output(const unsigned char *, size_t, struct log_params *);
7453d49b37SJilles Tjoelker static void daemon_sleep(time_t, long);
75bd06a3ecSMike Barcroft static void usage(void);
76bd06a3ecSMike Barcroft 
7753d49b37SJilles Tjoelker static volatile sig_atomic_t terminate = 0, child_gone = 0, pid = 0;
7853d49b37SJilles Tjoelker 
79bd06a3ecSMike Barcroft int
80bd06a3ecSMike Barcroft main(int argc, char *argv[])
81bd06a3ecSMike Barcroft {
8253d49b37SJilles Tjoelker 	const char *pidfile, *ppidfile, *title, *user, *outfn, *logtag;
8353d49b37SJilles Tjoelker 	int ch, nochdir, noclose, restart, dosyslog, child_eof;
8453d49b37SJilles Tjoelker 	sigset_t mask_susp, mask_orig, mask_read, mask_term;
8553d49b37SJilles Tjoelker 	struct log_params logpar;
8653d49b37SJilles Tjoelker 	int pfd[2] = { -1, -1 }, outfd = -1;
8753d49b37SJilles Tjoelker 	int stdmask, logpri, logfac;
8832b17786SJohn-Mark Gurney 	struct pidfh *ppfh, *pfh;
8953d49b37SJilles Tjoelker 	char *p;
90bd06a3ecSMike Barcroft 
9153d49b37SJilles Tjoelker 	memset(&logpar, 0, sizeof(logpar));
9253d49b37SJilles Tjoelker 	stdmask = STDOUT_FILENO | STDERR_FILENO;
9353d49b37SJilles Tjoelker 	ppidfile = pidfile = user = NULL;
94bd06a3ecSMike Barcroft 	nochdir = noclose = 1;
9553d49b37SJilles Tjoelker 	logpri = LOG_NOTICE;
9653d49b37SJilles Tjoelker 	logfac = LOG_DAEMON;
9753d49b37SJilles Tjoelker 	logtag = "daemon";
98b6193c24SMikolaj Golub 	restart = 0;
9953d49b37SJilles Tjoelker 	dosyslog = 0;
10053d49b37SJilles Tjoelker 	outfn = NULL;
10153d49b37SJilles Tjoelker 	title = NULL;
10253d49b37SJilles Tjoelker 	while ((ch = getopt(argc, argv, "cfSp:P:ru:o:s:l:t:l:m:T:")) != -1) {
103bd06a3ecSMike Barcroft 		switch (ch) {
104bd06a3ecSMike Barcroft 		case 'c':
105bd06a3ecSMike Barcroft 			nochdir = 0;
106bd06a3ecSMike Barcroft 			break;
107bd06a3ecSMike Barcroft 		case 'f':
108bd06a3ecSMike Barcroft 			noclose = 0;
109bd06a3ecSMike Barcroft 			break;
11053d49b37SJilles Tjoelker 		case 'l':
11153d49b37SJilles Tjoelker 			logfac = get_log_mapping(optarg, facilitynames);
11253d49b37SJilles Tjoelker 			if (logfac == -1)
11353d49b37SJilles Tjoelker 				errx(5, "unrecognized syslog facility");
11453d49b37SJilles Tjoelker 			dosyslog = 1;
11553d49b37SJilles Tjoelker 			break;
11653d49b37SJilles Tjoelker 		case 'm':
11753d49b37SJilles Tjoelker 			stdmask = strtol(optarg, &p, 10);
11853d49b37SJilles Tjoelker 			if (p == optarg || stdmask < 0 || stdmask > 3)
11953d49b37SJilles Tjoelker 				errx(6, "unrecognized listening mask");
12053d49b37SJilles Tjoelker 			break;
12153d49b37SJilles Tjoelker 		case 'o':
12253d49b37SJilles Tjoelker 			outfn = optarg;
12353d49b37SJilles Tjoelker 			break;
124846be7bdSPoul-Henning Kamp 		case 'p':
125846be7bdSPoul-Henning Kamp 			pidfile = optarg;
126846be7bdSPoul-Henning Kamp 			break;
12732b17786SJohn-Mark Gurney 		case 'P':
12832b17786SJohn-Mark Gurney 			ppidfile = optarg;
12932b17786SJohn-Mark Gurney 			break;
130b6193c24SMikolaj Golub 		case 'r':
131b6193c24SMikolaj Golub 			restart = 1;
132b6193c24SMikolaj Golub 			break;
13353d49b37SJilles Tjoelker 		case 's':
13453d49b37SJilles Tjoelker 			logpri = get_log_mapping(optarg, prioritynames);
13553d49b37SJilles Tjoelker 			if (logpri == -1)
13653d49b37SJilles Tjoelker 				errx(4, "unrecognized syslog priority");
13753d49b37SJilles Tjoelker 			dosyslog = 1;
13853d49b37SJilles Tjoelker 			break;
13953d49b37SJilles Tjoelker 		case 'S':
14053d49b37SJilles Tjoelker 			dosyslog = 1;
14153d49b37SJilles Tjoelker 			break;
142112bfcf5SConrad Meyer 		case 't':
143112bfcf5SConrad Meyer 			title = optarg;
144112bfcf5SConrad Meyer 			break;
14553d49b37SJilles Tjoelker 		case 'T':
14653d49b37SJilles Tjoelker 			logtag = optarg;
14753d49b37SJilles Tjoelker 			dosyslog = 1;
14853d49b37SJilles Tjoelker 			break;
149e6d4b388STom Rhodes 		case 'u':
150e6d4b388STom Rhodes 			user = optarg;
151e6d4b388STom Rhodes 			break;
152bd06a3ecSMike Barcroft 		default:
153bd06a3ecSMike Barcroft 			usage();
154bd06a3ecSMike Barcroft 		}
155bd06a3ecSMike Barcroft 	}
156bd06a3ecSMike Barcroft 	argc -= optind;
157bd06a3ecSMike Barcroft 	argv += optind;
158bd06a3ecSMike Barcroft 
159bd06a3ecSMike Barcroft 	if (argc == 0)
160bd06a3ecSMike Barcroft 		usage();
16112d7249eSTom Rhodes 
16253d49b37SJilles Tjoelker 	if (!title)
16353d49b37SJilles Tjoelker 		title = argv[0];
16453d49b37SJilles Tjoelker 
16553d49b37SJilles Tjoelker 	if (outfn) {
16653d49b37SJilles Tjoelker 		outfd = open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600);
16753d49b37SJilles Tjoelker 		if (outfd == -1)
16853d49b37SJilles Tjoelker 			err(7, "open");
16953d49b37SJilles Tjoelker 	}
17053d49b37SJilles Tjoelker 
17153d49b37SJilles Tjoelker 	if (dosyslog)
17253d49b37SJilles Tjoelker 		openlog(logtag, LOG_PID | LOG_NDELAY, logfac);
17353d49b37SJilles Tjoelker 
17432b17786SJohn-Mark Gurney 	ppfh = pfh = NULL;
175846be7bdSPoul-Henning Kamp 	/*
176846be7bdSPoul-Henning Kamp 	 * Try to open the pidfile before calling daemon(3),
177846be7bdSPoul-Henning Kamp 	 * to be able to report the error intelligently
178846be7bdSPoul-Henning Kamp 	 */
17953d49b37SJilles Tjoelker 	open_pid_files(pidfile, ppidfile, &pfh, &ppfh);
1809da0ef13SMikolaj Golub 	if (daemon(nochdir, noclose) == -1) {
1819da0ef13SMikolaj Golub 		warn("daemon");
1829da0ef13SMikolaj Golub 		goto exit;
1839da0ef13SMikolaj Golub 	}
1849da0ef13SMikolaj Golub 	/* Write out parent pidfile if needed. */
1859da0ef13SMikolaj Golub 	pidfile_write(ppfh);
186195fc497SMikolaj Golub 	/*
187b6193c24SMikolaj Golub 	 * If the pidfile or restart option is specified the daemon
188b6193c24SMikolaj Golub 	 * executes the command in a forked process and wait on child
189b6193c24SMikolaj Golub 	 * exit to remove the pidfile or restart the command. Normally
190b6193c24SMikolaj Golub 	 * we don't want the monitoring daemon to be terminated
191b6193c24SMikolaj Golub 	 * leaving the running process and the stale pidfile, so we
192b6193c24SMikolaj Golub 	 * catch SIGTERM and forward it to the children expecting to
19353d49b37SJilles Tjoelker 	 * get SIGCHLD eventually. We also must fork() to obtain a
19453d49b37SJilles Tjoelker 	 * readable pipe with the child for writing to a log file
19553d49b37SJilles Tjoelker 	 * and syslog.
196195fc497SMikolaj Golub 	 */
197195fc497SMikolaj Golub 	pid = -1;
19853d49b37SJilles Tjoelker 	if (pidfile || ppidfile || restart || outfd != -1 || dosyslog) {
19953d49b37SJilles Tjoelker 		struct sigaction act_term, act_chld;
20053d49b37SJilles Tjoelker 
20153d49b37SJilles Tjoelker 		/* Avoid PID racing with SIGCHLD and SIGTERM. */
20253d49b37SJilles Tjoelker 		memset(&act_term, 0, sizeof(act_term));
20353d49b37SJilles Tjoelker 		act_term.sa_handler = handle_term;
20453d49b37SJilles Tjoelker 		sigemptyset(&act_term.sa_mask);
20553d49b37SJilles Tjoelker 		sigaddset(&act_term.sa_mask, SIGCHLD);
20653d49b37SJilles Tjoelker 
20753d49b37SJilles Tjoelker 		memset(&act_chld, 0, sizeof(act_chld));
20853d49b37SJilles Tjoelker 		act_chld.sa_handler = handle_chld;
20953d49b37SJilles Tjoelker 		sigemptyset(&act_chld.sa_mask);
21053d49b37SJilles Tjoelker 		sigaddset(&act_chld.sa_mask, SIGTERM);
21153d49b37SJilles Tjoelker 
21253d49b37SJilles Tjoelker 		/* Block these when avoiding racing before sigsuspend(). */
21353d49b37SJilles Tjoelker 		sigemptyset(&mask_susp);
21453d49b37SJilles Tjoelker 		sigaddset(&mask_susp, SIGTERM);
21553d49b37SJilles Tjoelker 		sigaddset(&mask_susp, SIGCHLD);
21653d49b37SJilles Tjoelker 		/* Block SIGTERM when we lack a valid child PID. */
21753d49b37SJilles Tjoelker 		sigemptyset(&mask_term);
21853d49b37SJilles Tjoelker 		sigaddset(&mask_term, SIGTERM);
2192ad43027SMikolaj Golub 		/*
22053d49b37SJilles Tjoelker 		 * When reading, we wish to avoid SIGCHLD. SIGTERM
22153d49b37SJilles Tjoelker 		 * has to be caught, otherwise we'll be stuck until
22253d49b37SJilles Tjoelker 		 * the read() returns - if it returns.
223195fc497SMikolaj Golub 		 */
22453d49b37SJilles Tjoelker 		sigemptyset(&mask_read);
22553d49b37SJilles Tjoelker 		sigaddset(&mask_read, SIGCHLD);
22653d49b37SJilles Tjoelker 		/* Block SIGTERM to avoid racing until we have forked. */
22753d49b37SJilles Tjoelker 		if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) {
2289da0ef13SMikolaj Golub 			warn("sigprocmask");
2299da0ef13SMikolaj Golub 			goto exit;
2309da0ef13SMikolaj Golub 		}
23153d49b37SJilles Tjoelker 		if (sigaction(SIGTERM, &act_term, NULL) == -1) {
23253d49b37SJilles Tjoelker 			warn("sigaction");
23353d49b37SJilles Tjoelker 			goto exit;
23453d49b37SJilles Tjoelker 		}
23553d49b37SJilles Tjoelker 		if (sigaction(SIGCHLD, &act_chld, NULL) == -1) {
23653d49b37SJilles Tjoelker 			warn("sigaction");
23753d49b37SJilles Tjoelker 			goto exit;
23853d49b37SJilles Tjoelker 		}
23953c49998SMikolaj Golub 		/*
24053c49998SMikolaj Golub 		 * Try to protect against pageout kill. Ignore the
24153c49998SMikolaj Golub 		 * error, madvise(2) will fail only if a process does
24253c49998SMikolaj Golub 		 * not have superuser privileges.
24353c49998SMikolaj Golub 		 */
24453c49998SMikolaj Golub 		(void)madvise(NULL, 0, MADV_PROTECT);
24553d49b37SJilles Tjoelker 		logpar.outfd = outfd;
24653d49b37SJilles Tjoelker 		logpar.dosyslog = dosyslog;
24753d49b37SJilles Tjoelker 		logpar.logpri = logpri;
24853d49b37SJilles Tjoelker 		logpar.noclose = noclose;
249b6193c24SMikolaj Golub restart:
25053d49b37SJilles Tjoelker 		if (pipe(pfd))
25153d49b37SJilles Tjoelker 			err(1, "pipe");
252195fc497SMikolaj Golub 		/*
25353d49b37SJilles Tjoelker 		 * Spawn a child to exec the command.
2542ad43027SMikolaj Golub 		 */
25553d49b37SJilles Tjoelker 		child_gone = 0;
2562ad43027SMikolaj Golub 		pid = fork();
2572ad43027SMikolaj Golub 		if (pid == -1) {
2589da0ef13SMikolaj Golub 			warn("fork");
2599da0ef13SMikolaj Golub 			goto exit;
26053d49b37SJilles Tjoelker 		} else if (pid > 0) {
26153d49b37SJilles Tjoelker 			/*
26253d49b37SJilles Tjoelker 			 * Unblock SIGTERM after we know we have a valid
26353d49b37SJilles Tjoelker 			 * child PID to signal.
26453d49b37SJilles Tjoelker 			 */
26553d49b37SJilles Tjoelker 			if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) {
26653d49b37SJilles Tjoelker 				warn("sigprocmask");
26753d49b37SJilles Tjoelker 				goto exit;
26853d49b37SJilles Tjoelker 			}
26953d49b37SJilles Tjoelker 			close(pfd[1]);
27053d49b37SJilles Tjoelker 			pfd[1] = -1;
2712ad43027SMikolaj Golub 		}
2722ad43027SMikolaj Golub 	}
273195fc497SMikolaj Golub 	if (pid <= 0) {
2742ad43027SMikolaj Golub 		/* Now that we are the child, write out the pid. */
275c6262cb6SPawel Jakub Dawidek 		pidfile_write(pfh);
276846be7bdSPoul-Henning Kamp 
2772ad43027SMikolaj Golub 		if (user != NULL)
2782ad43027SMikolaj Golub 			restrict_process(user);
27953d49b37SJilles Tjoelker 		/*
28053d49b37SJilles Tjoelker 		 * When forking, the child gets the original sigmask,
28153d49b37SJilles Tjoelker 		 * and dup'd pipes.
28253d49b37SJilles Tjoelker 		 */
28353d49b37SJilles Tjoelker 		if (pid == 0) {
28453d49b37SJilles Tjoelker 			close(pfd[0]);
28553d49b37SJilles Tjoelker 			if (sigprocmask(SIG_SETMASK, &mask_orig, NULL))
28653d49b37SJilles Tjoelker 				err(1, "sigprogmask");
28753d49b37SJilles Tjoelker 			if (stdmask & STDERR_FILENO) {
28853d49b37SJilles Tjoelker 				if (dup2(pfd[1], STDERR_FILENO) == -1)
28953d49b37SJilles Tjoelker 					err(1, "dup2");
29053d49b37SJilles Tjoelker 			}
29153d49b37SJilles Tjoelker 			if (stdmask & STDOUT_FILENO) {
29253d49b37SJilles Tjoelker 				if (dup2(pfd[1], STDOUT_FILENO) == -1)
29353d49b37SJilles Tjoelker 					err(1, "dup2");
29453d49b37SJilles Tjoelker 			}
29553d49b37SJilles Tjoelker 			if (pfd[1] != STDERR_FILENO &&
29653d49b37SJilles Tjoelker 			    pfd[1] != STDOUT_FILENO)
29753d49b37SJilles Tjoelker 				close(pfd[1]);
29853d49b37SJilles Tjoelker 		}
299bd06a3ecSMike Barcroft 		execvp(argv[0], argv);
300846be7bdSPoul-Henning Kamp 		/*
3012ad43027SMikolaj Golub 		 * execvp() failed -- report the error. The child is
3022ad43027SMikolaj Golub 		 * now running, so the exit status doesn't matter.
303846be7bdSPoul-Henning Kamp 		 */
3042ad43027SMikolaj Golub 		err(1, "%s", argv[0]);
3052ad43027SMikolaj Golub 	}
30653d49b37SJilles Tjoelker 	setproctitle("%s[%d]", title, (int)pid);
30753d49b37SJilles Tjoelker 	/*
30853d49b37SJilles Tjoelker 	 * As we have closed the write end of pipe for parent process,
30953d49b37SJilles Tjoelker 	 * we might detect the child's exit by reading EOF. The child
31053d49b37SJilles Tjoelker 	 * might have closed its stdout and stderr, so we must wait for
31153d49b37SJilles Tjoelker 	 * the SIGCHLD to ensure that the process is actually gone.
31253d49b37SJilles Tjoelker 	 */
31353d49b37SJilles Tjoelker 	child_eof = 0;
31453d49b37SJilles Tjoelker 	for (;;) {
31553d49b37SJilles Tjoelker 		/*
31653d49b37SJilles Tjoelker 		 * We block SIGCHLD when listening, but SIGTERM we accept
31753d49b37SJilles Tjoelker 		 * so the read() won't block if we wish to depart.
31853d49b37SJilles Tjoelker 		 *
31953d49b37SJilles Tjoelker 		 * Upon receiving SIGTERM, we have several options after
32053d49b37SJilles Tjoelker 		 * sending the SIGTERM to our child:
32153d49b37SJilles Tjoelker 		 * - read until EOF
32253d49b37SJilles Tjoelker 		 * - read until EOF but only for a while
32353d49b37SJilles Tjoelker 		 * - bail immediately
32453d49b37SJilles Tjoelker 		 *
32553d49b37SJilles Tjoelker 		 * We go for the third, as otherwise we have no guarantee
32653d49b37SJilles Tjoelker 		 * that we won't block indefinitely if the child refuses
32753d49b37SJilles Tjoelker 		 * to depart. To handle the second option, a different
32853d49b37SJilles Tjoelker 		 * approach would be needed (procctl()?)
32953d49b37SJilles Tjoelker 		 */
33053d49b37SJilles Tjoelker 		if (child_gone && child_eof) {
33153d49b37SJilles Tjoelker 			break;
33253d49b37SJilles Tjoelker 		} else if (terminate) {
33353d49b37SJilles Tjoelker 			goto exit;
33453d49b37SJilles Tjoelker 		} else if (!child_eof) {
33553d49b37SJilles Tjoelker 			if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) {
33653d49b37SJilles Tjoelker 				warn("sigprocmask");
33753d49b37SJilles Tjoelker 				goto exit;
33853d49b37SJilles Tjoelker 			}
33953d49b37SJilles Tjoelker 			child_eof = !listen_child(pfd[0], &logpar);
34053d49b37SJilles Tjoelker 			if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) {
34153d49b37SJilles Tjoelker 				warn("sigprocmask");
34253d49b37SJilles Tjoelker 				goto exit;
34353d49b37SJilles Tjoelker 			}
34453d49b37SJilles Tjoelker 		} else {
34553d49b37SJilles Tjoelker 			if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) {
34653d49b37SJilles Tjoelker 				warn("sigprocmask");
34753d49b37SJilles Tjoelker 	 			goto exit;
34853d49b37SJilles Tjoelker 			}
34953d49b37SJilles Tjoelker 			while (!terminate && !child_gone)
35053d49b37SJilles Tjoelker 				sigsuspend(&mask_orig);
35153d49b37SJilles Tjoelker 			if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) {
35253d49b37SJilles Tjoelker 				warn("sigprocmask");
35353d49b37SJilles Tjoelker 				goto exit;
35453d49b37SJilles Tjoelker 			}
35553d49b37SJilles Tjoelker 		}
35653d49b37SJilles Tjoelker 	}
35753d49b37SJilles Tjoelker 	if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) {
35853d49b37SJilles Tjoelker 		warn("sigprocmask");
35953d49b37SJilles Tjoelker 		goto exit;
36053d49b37SJilles Tjoelker 	}
36153d49b37SJilles Tjoelker 	if (restart && !terminate) {
36253d49b37SJilles Tjoelker 		daemon_sleep(1, 0);
36353d49b37SJilles Tjoelker 		close(pfd[0]);
36453d49b37SJilles Tjoelker 		pfd[0] = -1;
365b6193c24SMikolaj Golub 		goto restart;
366b6193c24SMikolaj Golub 	}
3679da0ef13SMikolaj Golub exit:
36853d49b37SJilles Tjoelker 	close(outfd);
36953d49b37SJilles Tjoelker 	close(pfd[0]);
37053d49b37SJilles Tjoelker 	close(pfd[1]);
37153d49b37SJilles Tjoelker 	if (dosyslog)
37253d49b37SJilles Tjoelker 		closelog();
373c6262cb6SPawel Jakub Dawidek 	pidfile_remove(pfh);
37432b17786SJohn-Mark Gurney 	pidfile_remove(ppfh);
3759da0ef13SMikolaj Golub 	exit(1); /* If daemon(3) succeeded exit status does not matter. */
376bd06a3ecSMike Barcroft }
377bd06a3ecSMike Barcroft 
378bd06a3ecSMike Barcroft static void
37953d49b37SJilles Tjoelker daemon_sleep(time_t secs, long nsecs)
380195fc497SMikolaj Golub {
38153d49b37SJilles Tjoelker 	struct timespec ts = { secs, nsecs };
38253d49b37SJilles Tjoelker 	while (nanosleep(&ts, &ts) == -1) {
38353d49b37SJilles Tjoelker 		if (errno != EINTR)
38453d49b37SJilles Tjoelker 			err(1, "nanosleep");
38553d49b37SJilles Tjoelker 	}
38653d49b37SJilles Tjoelker }
38753d49b37SJilles Tjoelker 
38853d49b37SJilles Tjoelker static void
38953d49b37SJilles Tjoelker open_pid_files(const char *pidfile, const char *ppidfile,
39053d49b37SJilles Tjoelker 	       struct pidfh **pfh, struct pidfh **ppfh)
39153d49b37SJilles Tjoelker {
39253d49b37SJilles Tjoelker 	pid_t fpid;
39353d49b37SJilles Tjoelker 	int serrno;
39453d49b37SJilles Tjoelker 
39553d49b37SJilles Tjoelker 	if (pidfile) {
39653d49b37SJilles Tjoelker 		*pfh = pidfile_open(pidfile, 0600, &fpid);
39753d49b37SJilles Tjoelker 		if (*pfh == NULL) {
39853d49b37SJilles Tjoelker 			if (errno == EEXIST) {
39953d49b37SJilles Tjoelker 				errx(3, "process already running, pid: %d",
40053d49b37SJilles Tjoelker 				    fpid);
40153d49b37SJilles Tjoelker 			}
40253d49b37SJilles Tjoelker 			err(2, "pidfile ``%s''", pidfile);
40353d49b37SJilles Tjoelker 		}
40453d49b37SJilles Tjoelker 	}
40553d49b37SJilles Tjoelker 	/* Do the same for the actual daemon process. */
40653d49b37SJilles Tjoelker 	if (ppidfile) {
40753d49b37SJilles Tjoelker 		*ppfh = pidfile_open(ppidfile, 0600, &fpid);
40853d49b37SJilles Tjoelker 		if (*ppfh == NULL) {
40953d49b37SJilles Tjoelker 			serrno = errno;
41053d49b37SJilles Tjoelker 			pidfile_remove(*pfh);
41153d49b37SJilles Tjoelker 			errno = serrno;
41253d49b37SJilles Tjoelker 			if (errno == EEXIST) {
41353d49b37SJilles Tjoelker 				errx(3, "process already running, pid: %d",
41453d49b37SJilles Tjoelker 				     fpid);
41553d49b37SJilles Tjoelker 			}
41653d49b37SJilles Tjoelker 			err(2, "ppidfile ``%s''", ppidfile);
41753d49b37SJilles Tjoelker 		}
41853d49b37SJilles Tjoelker 	}
41953d49b37SJilles Tjoelker }
42053d49b37SJilles Tjoelker 
42153d49b37SJilles Tjoelker static int
42253d49b37SJilles Tjoelker get_log_mapping(const char *str, const CODE *c)
42353d49b37SJilles Tjoelker {
42453d49b37SJilles Tjoelker 	const CODE *cp;
42553d49b37SJilles Tjoelker 	for (cp = c; cp->c_name; cp++)
42653d49b37SJilles Tjoelker 		if (strcmp(cp->c_name, str) == 0)
42753d49b37SJilles Tjoelker 			return cp->c_val;
42853d49b37SJilles Tjoelker 	return -1;
429195fc497SMikolaj Golub }
430195fc497SMikolaj Golub 
431195fc497SMikolaj Golub static void
432e6d4b388STom Rhodes restrict_process(const char *user)
43312d7249eSTom Rhodes {
43412d7249eSTom Rhodes 	struct passwd *pw = NULL;
43512d7249eSTom Rhodes 
436e6d4b388STom Rhodes 	pw = getpwnam(user);
437e6d4b388STom Rhodes 	if (pw == NULL)
438e6d4b388STom Rhodes 		errx(1, "unknown user: %s", user);
43912d7249eSTom Rhodes 
440e6d4b388STom Rhodes 	if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0)
441e6d4b388STom Rhodes 		errx(1, "failed to set user environment");
44212d7249eSTom Rhodes }
44312d7249eSTom Rhodes 
44453d49b37SJilles Tjoelker /*
44553d49b37SJilles Tjoelker  * We try to collect whole lines terminated by '\n'. Otherwise we collect a
44653d49b37SJilles Tjoelker  * full buffer, and then output it.
44753d49b37SJilles Tjoelker  *
44853d49b37SJilles Tjoelker  * Return value of 0 is assumed to mean EOF or error, and 1 indicates to
44953d49b37SJilles Tjoelker  * continue reading.
45053d49b37SJilles Tjoelker  */
451b6193c24SMikolaj Golub static int
45253d49b37SJilles Tjoelker listen_child(int fd, struct log_params *logpar)
4532ad43027SMikolaj Golub {
45453d49b37SJilles Tjoelker 	static unsigned char buf[LBUF_SIZE];
45553d49b37SJilles Tjoelker 	static size_t bytes_read = 0;
45653d49b37SJilles Tjoelker 	int rv;
4572ad43027SMikolaj Golub 
45853d49b37SJilles Tjoelker 	assert(logpar);
45953d49b37SJilles Tjoelker 	assert(bytes_read < LBUF_SIZE - 1);
46053d49b37SJilles Tjoelker 
46153d49b37SJilles Tjoelker 	rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1);
46253d49b37SJilles Tjoelker 	if (rv > 0) {
46353d49b37SJilles Tjoelker 		unsigned char *cp;
46453d49b37SJilles Tjoelker 
46553d49b37SJilles Tjoelker 		bytes_read += rv;
46653d49b37SJilles Tjoelker 		assert(bytes_read <= LBUF_SIZE - 1);
46753d49b37SJilles Tjoelker 		/* Always NUL-terminate just in case. */
46853d49b37SJilles Tjoelker 		buf[LBUF_SIZE - 1] = '\0';
46953d49b37SJilles Tjoelker 		/*
47053d49b37SJilles Tjoelker 		 * Chomp line by line until we run out of buffer.
47153d49b37SJilles Tjoelker 		 * This does not take NUL characters into account.
47253d49b37SJilles Tjoelker 		 */
47353d49b37SJilles Tjoelker 		while ((cp = memchr(buf, '\n', bytes_read)) != NULL) {
47453d49b37SJilles Tjoelker 			size_t bytes_line = cp - buf + 1;
47553d49b37SJilles Tjoelker 			assert(bytes_line <= bytes_read);
47653d49b37SJilles Tjoelker 			do_output(buf, bytes_line, logpar);
47753d49b37SJilles Tjoelker 			bytes_read -= bytes_line;
47853d49b37SJilles Tjoelker 			memmove(buf, cp + 1, bytes_read);
479195fc497SMikolaj Golub 		}
48053d49b37SJilles Tjoelker 		/* Wait until the buffer is full. */
48153d49b37SJilles Tjoelker 		if (bytes_read < LBUF_SIZE - 1)
48253d49b37SJilles Tjoelker 			return 1;
48353d49b37SJilles Tjoelker 		do_output(buf, bytes_read, logpar);
48453d49b37SJilles Tjoelker 		bytes_read = 0;
48553d49b37SJilles Tjoelker 		return 1;
48653d49b37SJilles Tjoelker 	} else if (rv == -1) {
48753d49b37SJilles Tjoelker 		/* EINTR should trigger another read. */
48853d49b37SJilles Tjoelker 		if (errno == EINTR) {
48953d49b37SJilles Tjoelker 			return 1;
49053d49b37SJilles Tjoelker 		} else {
49153d49b37SJilles Tjoelker 			warn("read");
49253d49b37SJilles Tjoelker 			return 0;
493c60d51f9SMikolaj Golub 		}
49453d49b37SJilles Tjoelker 	}
49553d49b37SJilles Tjoelker 	/* Upon EOF, we have to flush what's left of the buffer. */
49653d49b37SJilles Tjoelker 	if (bytes_read > 0) {
49753d49b37SJilles Tjoelker 		do_output(buf, bytes_read, logpar);
49853d49b37SJilles Tjoelker 		bytes_read = 0;
49953d49b37SJilles Tjoelker 	}
50053d49b37SJilles Tjoelker 	return 0;
50153d49b37SJilles Tjoelker }
50253d49b37SJilles Tjoelker 
50353d49b37SJilles Tjoelker /*
50453d49b37SJilles Tjoelker  * The default behavior is to stay silent if the user wants to redirect
50553d49b37SJilles Tjoelker  * output to a file and/or syslog. If neither are provided, then we bounce
50653d49b37SJilles Tjoelker  * everything back to parent's stdout.
50753d49b37SJilles Tjoelker  */
50853d49b37SJilles Tjoelker static void
50953d49b37SJilles Tjoelker do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
51053d49b37SJilles Tjoelker {
51153d49b37SJilles Tjoelker 	assert(len <= LBUF_SIZE);
51253d49b37SJilles Tjoelker 	assert(logpar);
51353d49b37SJilles Tjoelker 
51453d49b37SJilles Tjoelker 	if (len < 1)
51553d49b37SJilles Tjoelker 		return;
51653d49b37SJilles Tjoelker 	if (logpar->dosyslog)
51753d49b37SJilles Tjoelker 		syslog(logpar->logpri, "%.*s", (int)len, buf);
51853d49b37SJilles Tjoelker 	if (logpar->outfd != -1) {
51953d49b37SJilles Tjoelker 		if (write(logpar->outfd, buf, len) == -1)
52053d49b37SJilles Tjoelker 			warn("write");
52153d49b37SJilles Tjoelker 	}
52253d49b37SJilles Tjoelker 	if (logpar->noclose && !logpar->dosyslog && logpar->outfd == -1)
52353d49b37SJilles Tjoelker 		printf("%.*s", (int)len, buf);
52453d49b37SJilles Tjoelker }
52553d49b37SJilles Tjoelker 
52653d49b37SJilles Tjoelker /*
52753d49b37SJilles Tjoelker  * We use the global PID acquired directly from fork. If there is no valid
52853d49b37SJilles Tjoelker  * child pid, the handler should be blocked and/or child_gone == 1.
52953d49b37SJilles Tjoelker  */
53053d49b37SJilles Tjoelker static void
53153d49b37SJilles Tjoelker handle_term(int signo)
53253d49b37SJilles Tjoelker {
53353d49b37SJilles Tjoelker 	if (pid > 0 && !child_gone)
53453d49b37SJilles Tjoelker 		kill(pid, signo);
535b6193c24SMikolaj Golub 	terminate = 1;
536195fc497SMikolaj Golub }
53753d49b37SJilles Tjoelker 
53853d49b37SJilles Tjoelker static void
53953d49b37SJilles Tjoelker handle_chld(int signo)
54053d49b37SJilles Tjoelker {
54153d49b37SJilles Tjoelker 	(void)signo;
54253d49b37SJilles Tjoelker 	for (;;) {
54353d49b37SJilles Tjoelker 		int rv = waitpid(-1, NULL, WNOHANG);
54453d49b37SJilles Tjoelker 		if (pid == rv) {
54553d49b37SJilles Tjoelker 			child_gone = 1;
54653d49b37SJilles Tjoelker 			break;
54753d49b37SJilles Tjoelker 		} else if (rv == -1 && errno != EINTR) {
54853d49b37SJilles Tjoelker 			warn("waitpid");
54953d49b37SJilles Tjoelker 			return;
5502ad43027SMikolaj Golub 		}
5512ad43027SMikolaj Golub 	}
5522ad43027SMikolaj Golub }
5532ad43027SMikolaj Golub 
5542ad43027SMikolaj Golub static void
555bd06a3ecSMike Barcroft usage(void)
556bd06a3ecSMike Barcroft {
55753d49b37SJilles Tjoelker 	(void)fprintf(stderr,
55853d49b37SJilles Tjoelker 	    "usage: daemon [-cfrS] [-p child_pidfile] [-P supervisor_pidfile]\n"
55953d49b37SJilles Tjoelker 	    "              [-u user] [-o output_file] [-t title]\n"
56053d49b37SJilles Tjoelker 	    "              [-l syslog_facility] [-s syslog_priority]\n"
56153d49b37SJilles Tjoelker 	    "              [-T syslog_tag] [-m output_mask]\n"
56253d49b37SJilles Tjoelker 	    "command arguments ...\n");
563bd06a3ecSMike Barcroft 	exit(1);
564bd06a3ecSMike Barcroft }
565