xref: /freebsd/usr.sbin/daemon/daemon.c (revision c60d51f964e494a440189ded760551ef036d9531)
1bd06a3ecSMike Barcroft /*-
2bd06a3ecSMike Barcroft  * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
3bd06a3ecSMike Barcroft  *
4bd06a3ecSMike Barcroft  * Redistribution and use in source and binary forms, with or without
5bd06a3ecSMike Barcroft  * modification, are permitted provided that the following conditions
6bd06a3ecSMike Barcroft  * are met:
7bd06a3ecSMike Barcroft  * 1. Redistributions of source code must retain the above copyright
8bd06a3ecSMike Barcroft  *    notice, this list of conditions and the following disclaimer.
9bd06a3ecSMike Barcroft  * 2. Redistributions in binary form must reproduce the above copyright
10bd06a3ecSMike Barcroft  *    notice, this list of conditions and the following disclaimer in the
11bd06a3ecSMike Barcroft  *    documentation and/or other materials provided with the distribution.
12bd06a3ecSMike Barcroft  * 3. Berkeley Software Design Inc's name may not be used to endorse or
13bd06a3ecSMike Barcroft  *    promote products derived from this software without specific prior
14bd06a3ecSMike Barcroft  *    written permission.
15bd06a3ecSMike Barcroft  *
16bd06a3ecSMike Barcroft  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17bd06a3ecSMike Barcroft  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18bd06a3ecSMike Barcroft  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19bd06a3ecSMike Barcroft  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20bd06a3ecSMike Barcroft  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21bd06a3ecSMike Barcroft  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22bd06a3ecSMike Barcroft  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23bd06a3ecSMike Barcroft  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24bd06a3ecSMike Barcroft  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25bd06a3ecSMike Barcroft  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26bd06a3ecSMike Barcroft  * SUCH DAMAGE.
27bd06a3ecSMike Barcroft  *
28bd06a3ecSMike Barcroft  *	From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
29bd06a3ecSMike Barcroft  */
30bd06a3ecSMike Barcroft 
3154ede02dSPhilippe Charnier #include <sys/cdefs.h>
3254ede02dSPhilippe Charnier __FBSDID("$FreeBSD$");
3354ede02dSPhilippe Charnier 
34c6262cb6SPawel Jakub Dawidek #include <sys/param.h>
3553c49998SMikolaj Golub #include <sys/mman.h>
362ad43027SMikolaj Golub #include <sys/wait.h>
37bd06a3ecSMike Barcroft 
38bd06a3ecSMike Barcroft #include <err.h>
39846be7bdSPoul-Henning Kamp #include <errno.h>
40c6262cb6SPawel Jakub Dawidek #include <libutil.h>
41e6d4b388STom Rhodes #include <login_cap.h>
42195fc497SMikolaj Golub #include <pwd.h>
43195fc497SMikolaj Golub #include <signal.h>
44bd06a3ecSMike Barcroft #include <stdio.h>
45bd06a3ecSMike Barcroft #include <stdlib.h>
46bd06a3ecSMike Barcroft #include <unistd.h>
47bd06a3ecSMike Barcroft 
48195fc497SMikolaj Golub static void dummy_sighandler(int);
49e6d4b388STom Rhodes static void restrict_process(const char *);
50b6193c24SMikolaj Golub static int  wait_child(pid_t pid, sigset_t *mask);
51bd06a3ecSMike Barcroft static void usage(void);
52bd06a3ecSMike Barcroft 
53bd06a3ecSMike Barcroft int
54bd06a3ecSMike Barcroft main(int argc, char *argv[])
55bd06a3ecSMike Barcroft {
5615543aadSTom Rhodes 	struct pidfh *pfh = NULL;
57195fc497SMikolaj Golub 	sigset_t mask, oldmask;
58b6193c24SMikolaj Golub 	int ch, nochdir, noclose, restart;
59e6d4b388STom Rhodes 	const char *pidfile, *user;
602ad43027SMikolaj Golub 	pid_t otherpid, pid;
61bd06a3ecSMike Barcroft 
62bd06a3ecSMike Barcroft 	nochdir = noclose = 1;
63b6193c24SMikolaj Golub 	restart = 0;
64e6d4b388STom Rhodes 	pidfile = user = NULL;
65b6193c24SMikolaj Golub 	while ((ch = getopt(argc, argv, "-cfp:ru:")) != -1) {
66bd06a3ecSMike Barcroft 		switch (ch) {
67bd06a3ecSMike Barcroft 		case 'c':
68bd06a3ecSMike Barcroft 			nochdir = 0;
69bd06a3ecSMike Barcroft 			break;
70bd06a3ecSMike Barcroft 		case 'f':
71bd06a3ecSMike Barcroft 			noclose = 0;
72bd06a3ecSMike Barcroft 			break;
73846be7bdSPoul-Henning Kamp 		case 'p':
74846be7bdSPoul-Henning Kamp 			pidfile = optarg;
75846be7bdSPoul-Henning Kamp 			break;
76b6193c24SMikolaj Golub 		case 'r':
77b6193c24SMikolaj Golub 			restart = 1;
78b6193c24SMikolaj Golub 			break;
79e6d4b388STom Rhodes 		case 'u':
80e6d4b388STom Rhodes 			user = optarg;
81e6d4b388STom Rhodes 			break;
82bd06a3ecSMike Barcroft 		default:
83bd06a3ecSMike Barcroft 			usage();
84bd06a3ecSMike Barcroft 		}
85bd06a3ecSMike Barcroft 	}
86bd06a3ecSMike Barcroft 	argc -= optind;
87bd06a3ecSMike Barcroft 	argv += optind;
88bd06a3ecSMike Barcroft 
89bd06a3ecSMike Barcroft 	if (argc == 0)
90bd06a3ecSMike Barcroft 		usage();
9112d7249eSTom Rhodes 
922ad43027SMikolaj Golub 	pfh = NULL;
93846be7bdSPoul-Henning Kamp 	/*
94846be7bdSPoul-Henning Kamp 	 * Try to open the pidfile before calling daemon(3),
95846be7bdSPoul-Henning Kamp 	 * to be able to report the error intelligently
96846be7bdSPoul-Henning Kamp 	 */
972ad43027SMikolaj Golub 	if (pidfile != NULL) {
98c6262cb6SPawel Jakub Dawidek 		pfh = pidfile_open(pidfile, 0600, &otherpid);
99c6262cb6SPawel Jakub Dawidek 		if (pfh == NULL) {
100c6262cb6SPawel Jakub Dawidek 			if (errno == EEXIST) {
101c6262cb6SPawel Jakub Dawidek 				errx(3, "process already running, pid: %d",
102c6262cb6SPawel Jakub Dawidek 				    otherpid);
103c6262cb6SPawel Jakub Dawidek 			}
104846be7bdSPoul-Henning Kamp 			err(2, "pidfile ``%s''", pidfile);
105846be7bdSPoul-Henning Kamp 		}
106c6262cb6SPawel Jakub Dawidek 	}
107846be7bdSPoul-Henning Kamp 
108bd06a3ecSMike Barcroft 	if (daemon(nochdir, noclose) == -1)
109bd06a3ecSMike Barcroft 		err(1, NULL);
110846be7bdSPoul-Henning Kamp 
111195fc497SMikolaj Golub 	/*
112b6193c24SMikolaj Golub 	 * If the pidfile or restart option is specified the daemon
113b6193c24SMikolaj Golub 	 * executes the command in a forked process and wait on child
114b6193c24SMikolaj Golub 	 * exit to remove the pidfile or restart the command. Normally
115b6193c24SMikolaj Golub 	 * we don't want the monitoring daemon to be terminated
116b6193c24SMikolaj Golub 	 * leaving the running process and the stale pidfile, so we
117b6193c24SMikolaj Golub 	 * catch SIGTERM and forward it to the children expecting to
118b6193c24SMikolaj Golub 	 * get SIGCHLD eventually.
119195fc497SMikolaj Golub 	 */
120195fc497SMikolaj Golub 	pid = -1;
121b6193c24SMikolaj Golub 	if (pidfile != NULL || restart) {
1222ad43027SMikolaj Golub 		/*
123195fc497SMikolaj Golub 		 * Restore default action for SIGTERM in case the
124195fc497SMikolaj Golub 		 * parent process decided to ignore it.
125195fc497SMikolaj Golub 		 */
126195fc497SMikolaj Golub 		if (signal(SIGTERM, SIG_DFL) == SIG_ERR)
127195fc497SMikolaj Golub 			err(1, "signal");
128195fc497SMikolaj Golub 		/*
129195fc497SMikolaj Golub 		 * Because SIGCHLD is ignored by default, setup dummy handler
130195fc497SMikolaj Golub 		 * for it, so we can mask it.
131195fc497SMikolaj Golub 		 */
132195fc497SMikolaj Golub 		if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR)
133195fc497SMikolaj Golub 			err(1, "signal");
134195fc497SMikolaj Golub 		/*
135195fc497SMikolaj Golub 		 * Block interesting signals.
136195fc497SMikolaj Golub 		 */
137195fc497SMikolaj Golub 		sigemptyset(&mask);
138195fc497SMikolaj Golub 		sigaddset(&mask, SIGTERM);
139195fc497SMikolaj Golub 		sigaddset(&mask, SIGCHLD);
140195fc497SMikolaj Golub 		if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1)
141195fc497SMikolaj Golub 			err(1, "sigprocmask");
14253c49998SMikolaj Golub 		/*
14353c49998SMikolaj Golub 		 * Try to protect against pageout kill. Ignore the
14453c49998SMikolaj Golub 		 * error, madvise(2) will fail only if a process does
14553c49998SMikolaj Golub 		 * not have superuser privileges.
14653c49998SMikolaj Golub 		 */
14753c49998SMikolaj Golub 		(void)madvise(NULL, 0, MADV_PROTECT);
148b6193c24SMikolaj Golub restart:
149195fc497SMikolaj Golub 		/*
1502ad43027SMikolaj Golub 		 * Spawn a child to exec the command, so in the parent
1512ad43027SMikolaj Golub 		 * we could wait for it to exit and remove pidfile.
1522ad43027SMikolaj Golub 		 */
1532ad43027SMikolaj Golub 		pid = fork();
1542ad43027SMikolaj Golub 		if (pid == -1) {
1552ad43027SMikolaj Golub 			pidfile_remove(pfh);
1562ad43027SMikolaj Golub 			err(1, "fork");
1572ad43027SMikolaj Golub 		}
1582ad43027SMikolaj Golub 	}
159195fc497SMikolaj Golub 	if (pid <= 0) {
1602ad43027SMikolaj Golub 		if (pid == 0) {
161195fc497SMikolaj Golub 			/* Restore old sigmask in the child. */
162195fc497SMikolaj Golub 			if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
163195fc497SMikolaj Golub 				err(1, "sigprocmask");
164195fc497SMikolaj Golub 		}
1652ad43027SMikolaj Golub 		/* Now that we are the child, write out the pid. */
166c6262cb6SPawel Jakub Dawidek 		pidfile_write(pfh);
167846be7bdSPoul-Henning Kamp 
1682ad43027SMikolaj Golub 		if (user != NULL)
1692ad43027SMikolaj Golub 			restrict_process(user);
1702ad43027SMikolaj Golub 
171bd06a3ecSMike Barcroft 		execvp(argv[0], argv);
172bd06a3ecSMike Barcroft 
173846be7bdSPoul-Henning Kamp 		/*
1742ad43027SMikolaj Golub 		 * execvp() failed -- report the error. The child is
1752ad43027SMikolaj Golub 		 * now running, so the exit status doesn't matter.
176846be7bdSPoul-Henning Kamp 		 */
1772ad43027SMikolaj Golub 		err(1, "%s", argv[0]);
1782ad43027SMikolaj Golub 	}
1792ad43027SMikolaj Golub 	setproctitle("%s[%d]", argv[0], pid);
180b6193c24SMikolaj Golub 	if (wait_child(pid, &mask) == 0 && restart) {
181b6193c24SMikolaj Golub 		sleep(1);
182b6193c24SMikolaj Golub 		goto restart;
183b6193c24SMikolaj Golub 	}
184c6262cb6SPawel Jakub Dawidek 	pidfile_remove(pfh);
1852ad43027SMikolaj Golub 	exit(0); /* Exit status does not matter. */
186bd06a3ecSMike Barcroft }
187bd06a3ecSMike Barcroft 
188bd06a3ecSMike Barcroft static void
189195fc497SMikolaj Golub dummy_sighandler(int sig __unused)
190195fc497SMikolaj Golub {
191195fc497SMikolaj Golub 	/* Nothing to do. */
192195fc497SMikolaj Golub }
193195fc497SMikolaj Golub 
194195fc497SMikolaj Golub static void
195e6d4b388STom Rhodes restrict_process(const char *user)
19612d7249eSTom Rhodes {
19712d7249eSTom Rhodes 	struct passwd *pw = NULL;
19812d7249eSTom Rhodes 
199e6d4b388STom Rhodes 	pw = getpwnam(user);
200e6d4b388STom Rhodes 	if (pw == NULL)
201e6d4b388STom Rhodes 		errx(1, "unknown user: %s", user);
20212d7249eSTom Rhodes 
203e6d4b388STom Rhodes 	if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0)
204e6d4b388STom Rhodes 		errx(1, "failed to set user environment");
20512d7249eSTom Rhodes }
20612d7249eSTom Rhodes 
207b6193c24SMikolaj Golub static int
208195fc497SMikolaj Golub wait_child(pid_t pid, sigset_t *mask)
2092ad43027SMikolaj Golub {
210b6193c24SMikolaj Golub 	int terminate, signo;
2112ad43027SMikolaj Golub 
212b6193c24SMikolaj Golub 	terminate = 0;
213195fc497SMikolaj Golub 	for (;;) {
214195fc497SMikolaj Golub 		if (sigwait(mask, &signo) == -1) {
215195fc497SMikolaj Golub 			warn("sigwaitinfo");
216b6193c24SMikolaj Golub 			return (-1);
217195fc497SMikolaj Golub 		}
218195fc497SMikolaj Golub 		switch (signo) {
219195fc497SMikolaj Golub 		case SIGCHLD:
220*c60d51f9SMikolaj Golub 			if (waitpid(pid, NULL, WNOHANG) == -1) {
221*c60d51f9SMikolaj Golub 				warn("waitpid");
222*c60d51f9SMikolaj Golub 				return (-1);
223*c60d51f9SMikolaj Golub 			}
224b6193c24SMikolaj Golub 			return (terminate);
225195fc497SMikolaj Golub 		case SIGTERM:
226b6193c24SMikolaj Golub 			terminate = 1;
227195fc497SMikolaj Golub 			if (kill(pid, signo) == -1) {
228195fc497SMikolaj Golub 				warn("kill");
229b6193c24SMikolaj Golub 				return (-1);
230195fc497SMikolaj Golub 			}
231195fc497SMikolaj Golub 			continue;
232195fc497SMikolaj Golub 		default:
233195fc497SMikolaj Golub 			warnx("sigwaitinfo: invalid signal: %d", signo);
234b6193c24SMikolaj Golub 			return (-1);
2352ad43027SMikolaj Golub 		}
2362ad43027SMikolaj Golub 	}
2372ad43027SMikolaj Golub }
2382ad43027SMikolaj Golub 
2392ad43027SMikolaj Golub static void
240bd06a3ecSMike Barcroft usage(void)
241bd06a3ecSMike Barcroft {
242846be7bdSPoul-Henning Kamp 	(void)fprintf(stderr,
243e6d4b388STom Rhodes 	    "usage: daemon [-cf] [-p pidfile] [-u user] command "
24412d7249eSTom Rhodes 		"arguments ...\n");
245bd06a3ecSMike Barcroft 	exit(1);
246bd06a3ecSMike Barcroft }
247