xref: /freebsd/usr.sbin/daemon/daemon.c (revision 195fc497a26717b89f6f24b0dd0449d0f2be2acc)
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>
352ad43027SMikolaj Golub #include <sys/wait.h>
36bd06a3ecSMike Barcroft 
37bd06a3ecSMike Barcroft #include <err.h>
38846be7bdSPoul-Henning Kamp #include <errno.h>
39c6262cb6SPawel Jakub Dawidek #include <libutil.h>
40e6d4b388STom Rhodes #include <login_cap.h>
41*195fc497SMikolaj Golub #include <pwd.h>
42*195fc497SMikolaj Golub #include <signal.h>
43bd06a3ecSMike Barcroft #include <stdio.h>
44bd06a3ecSMike Barcroft #include <stdlib.h>
45bd06a3ecSMike Barcroft #include <unistd.h>
46bd06a3ecSMike Barcroft 
47*195fc497SMikolaj Golub static void dummy_sighandler(int);
48e6d4b388STom Rhodes static void restrict_process(const char *);
49*195fc497SMikolaj Golub static void wait_child(pid_t pid, sigset_t *mask);
50bd06a3ecSMike Barcroft static void usage(void);
51bd06a3ecSMike Barcroft 
52bd06a3ecSMike Barcroft int
53bd06a3ecSMike Barcroft main(int argc, char *argv[])
54bd06a3ecSMike Barcroft {
5515543aadSTom Rhodes 	struct pidfh *pfh = NULL;
56*195fc497SMikolaj Golub 	sigset_t mask, oldmask;
572ad43027SMikolaj Golub 	int ch, nochdir, noclose;
58e6d4b388STom Rhodes 	const char *pidfile, *user;
592ad43027SMikolaj Golub 	pid_t otherpid, pid;
60bd06a3ecSMike Barcroft 
61bd06a3ecSMike Barcroft 	nochdir = noclose = 1;
62e6d4b388STom Rhodes 	pidfile = user = NULL;
6312d5df2bSPeter Wemm 	while ((ch = getopt(argc, argv, "-cfp:u:")) != -1) {
64bd06a3ecSMike Barcroft 		switch (ch) {
65bd06a3ecSMike Barcroft 		case 'c':
66bd06a3ecSMike Barcroft 			nochdir = 0;
67bd06a3ecSMike Barcroft 			break;
68bd06a3ecSMike Barcroft 		case 'f':
69bd06a3ecSMike Barcroft 			noclose = 0;
70bd06a3ecSMike Barcroft 			break;
71846be7bdSPoul-Henning Kamp 		case 'p':
72846be7bdSPoul-Henning Kamp 			pidfile = optarg;
73846be7bdSPoul-Henning Kamp 			break;
74e6d4b388STom Rhodes 		case 'u':
75e6d4b388STom Rhodes 			user = optarg;
76e6d4b388STom Rhodes 			break;
77bd06a3ecSMike Barcroft 		default:
78bd06a3ecSMike Barcroft 			usage();
79bd06a3ecSMike Barcroft 		}
80bd06a3ecSMike Barcroft 	}
81bd06a3ecSMike Barcroft 	argc -= optind;
82bd06a3ecSMike Barcroft 	argv += optind;
83bd06a3ecSMike Barcroft 
84bd06a3ecSMike Barcroft 	if (argc == 0)
85bd06a3ecSMike Barcroft 		usage();
8612d7249eSTom Rhodes 
872ad43027SMikolaj Golub 	pfh = NULL;
88846be7bdSPoul-Henning Kamp 	/*
89846be7bdSPoul-Henning Kamp 	 * Try to open the pidfile before calling daemon(3),
90846be7bdSPoul-Henning Kamp 	 * to be able to report the error intelligently
91846be7bdSPoul-Henning Kamp 	 */
922ad43027SMikolaj Golub 	if (pidfile != NULL) {
93c6262cb6SPawel Jakub Dawidek 		pfh = pidfile_open(pidfile, 0600, &otherpid);
94c6262cb6SPawel Jakub Dawidek 		if (pfh == NULL) {
95c6262cb6SPawel Jakub Dawidek 			if (errno == EEXIST) {
96c6262cb6SPawel Jakub Dawidek 				errx(3, "process already running, pid: %d",
97c6262cb6SPawel Jakub Dawidek 				    otherpid);
98c6262cb6SPawel Jakub Dawidek 			}
99846be7bdSPoul-Henning Kamp 			err(2, "pidfile ``%s''", pidfile);
100846be7bdSPoul-Henning Kamp 		}
101c6262cb6SPawel Jakub Dawidek 	}
102846be7bdSPoul-Henning Kamp 
103bd06a3ecSMike Barcroft 	if (daemon(nochdir, noclose) == -1)
104bd06a3ecSMike Barcroft 		err(1, NULL);
105846be7bdSPoul-Henning Kamp 
106*195fc497SMikolaj Golub 	/*
107*195fc497SMikolaj Golub 	 * If the pidfile option is specified the daemon executes the
108*195fc497SMikolaj Golub 	 * command in a forked process and wait on child exit to
109*195fc497SMikolaj Golub 	 * remove the pidfile. Normally we don't want the monitoring
110*195fc497SMikolaj Golub 	 * daemon to be terminated leaving the running process and the
111*195fc497SMikolaj Golub 	 * stale pidfile, so we catch SIGTERM and pass it to the
112*195fc497SMikolaj Golub 	 * children expecting to get SIGCHLD eventually.
113*195fc497SMikolaj Golub 	 */
114*195fc497SMikolaj Golub 	pid = -1;
1152ad43027SMikolaj Golub 	if (pidfile != NULL) {
1162ad43027SMikolaj Golub 		/*
117*195fc497SMikolaj Golub 		 * Restore default action for SIGTERM in case the
118*195fc497SMikolaj Golub 		 * parent process decided to ignore it.
119*195fc497SMikolaj Golub 		 */
120*195fc497SMikolaj Golub 		if (signal(SIGTERM, SIG_DFL) == SIG_ERR)
121*195fc497SMikolaj Golub 			err(1, "signal");
122*195fc497SMikolaj Golub 		/*
123*195fc497SMikolaj Golub 		 * Because SIGCHLD is ignored by default, setup dummy handler
124*195fc497SMikolaj Golub 		 * for it, so we can mask it.
125*195fc497SMikolaj Golub 		 */
126*195fc497SMikolaj Golub 		if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR)
127*195fc497SMikolaj Golub 			err(1, "signal");
128*195fc497SMikolaj Golub 		/*
129*195fc497SMikolaj Golub 		 * Block interesting signals.
130*195fc497SMikolaj Golub 		 */
131*195fc497SMikolaj Golub 		sigemptyset(&mask);
132*195fc497SMikolaj Golub 		sigaddset(&mask, SIGTERM);
133*195fc497SMikolaj Golub 		sigaddset(&mask, SIGCHLD);
134*195fc497SMikolaj Golub 		if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1)
135*195fc497SMikolaj Golub 			err(1, "sigprocmask");
136*195fc497SMikolaj Golub 		/*
1372ad43027SMikolaj Golub 		 * Spawn a child to exec the command, so in the parent
1382ad43027SMikolaj Golub 		 * we could wait for it to exit and remove pidfile.
1392ad43027SMikolaj Golub 		 */
1402ad43027SMikolaj Golub 		pid = fork();
1412ad43027SMikolaj Golub 		if (pid == -1) {
1422ad43027SMikolaj Golub 			pidfile_remove(pfh);
1432ad43027SMikolaj Golub 			err(1, "fork");
1442ad43027SMikolaj Golub 		}
1452ad43027SMikolaj Golub 	}
146*195fc497SMikolaj Golub 	if (pid <= 0) {
1472ad43027SMikolaj Golub 		if (pid == 0) {
148*195fc497SMikolaj Golub 			/* Restore old sigmask in the child. */
149*195fc497SMikolaj Golub 			if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
150*195fc497SMikolaj Golub 				err(1, "sigprocmask");
151*195fc497SMikolaj Golub 		}
1522ad43027SMikolaj Golub 		/* Now that we are the child, write out the pid. */
153c6262cb6SPawel Jakub Dawidek 		pidfile_write(pfh);
154846be7bdSPoul-Henning Kamp 
1552ad43027SMikolaj Golub 		if (user != NULL)
1562ad43027SMikolaj Golub 			restrict_process(user);
1572ad43027SMikolaj Golub 
158bd06a3ecSMike Barcroft 		execvp(argv[0], argv);
159bd06a3ecSMike Barcroft 
160846be7bdSPoul-Henning Kamp 		/*
1612ad43027SMikolaj Golub 		 * execvp() failed -- report the error. The child is
1622ad43027SMikolaj Golub 		 * now running, so the exit status doesn't matter.
163846be7bdSPoul-Henning Kamp 		 */
1642ad43027SMikolaj Golub 		err(1, "%s", argv[0]);
1652ad43027SMikolaj Golub 	}
1662ad43027SMikolaj Golub 	setproctitle("%s[%d]", argv[0], pid);
167*195fc497SMikolaj Golub 	wait_child(pid, &mask);
168c6262cb6SPawel Jakub Dawidek 	pidfile_remove(pfh);
1692ad43027SMikolaj Golub 	exit(0); /* Exit status does not matter. */
170bd06a3ecSMike Barcroft }
171bd06a3ecSMike Barcroft 
172bd06a3ecSMike Barcroft static void
173*195fc497SMikolaj Golub dummy_sighandler(int sig __unused)
174*195fc497SMikolaj Golub {
175*195fc497SMikolaj Golub 	/* Nothing to do. */
176*195fc497SMikolaj Golub }
177*195fc497SMikolaj Golub 
178*195fc497SMikolaj Golub static void
179e6d4b388STom Rhodes restrict_process(const char *user)
18012d7249eSTom Rhodes {
18112d7249eSTom Rhodes 	struct passwd *pw = NULL;
18212d7249eSTom Rhodes 
183e6d4b388STom Rhodes 	pw = getpwnam(user);
184e6d4b388STom Rhodes 	if (pw == NULL)
185e6d4b388STom Rhodes 		errx(1, "unknown user: %s", user);
18612d7249eSTom Rhodes 
187e6d4b388STom Rhodes 	if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0)
188e6d4b388STom Rhodes 		errx(1, "failed to set user environment");
18912d7249eSTom Rhodes }
19012d7249eSTom Rhodes 
19112d7249eSTom Rhodes static void
192*195fc497SMikolaj Golub wait_child(pid_t pid, sigset_t *mask)
1932ad43027SMikolaj Golub {
194*195fc497SMikolaj Golub 	int signo;
1952ad43027SMikolaj Golub 
196*195fc497SMikolaj Golub 	for (;;) {
197*195fc497SMikolaj Golub 		if (sigwait(mask, &signo) == -1) {
198*195fc497SMikolaj Golub 			warn("sigwaitinfo");
199*195fc497SMikolaj Golub 			return;
200*195fc497SMikolaj Golub 		}
201*195fc497SMikolaj Golub 		switch (signo) {
202*195fc497SMikolaj Golub 		case SIGCHLD:
203*195fc497SMikolaj Golub 			return;
204*195fc497SMikolaj Golub 		case SIGTERM:
205*195fc497SMikolaj Golub 			if (kill(pid, signo) == -1) {
206*195fc497SMikolaj Golub 				warn("kill");
207*195fc497SMikolaj Golub 				return;
208*195fc497SMikolaj Golub 			}
209*195fc497SMikolaj Golub 			continue;
210*195fc497SMikolaj Golub 		default:
211*195fc497SMikolaj Golub 			warnx("sigwaitinfo: invalid signal: %d", signo);
212*195fc497SMikolaj Golub 			return;
2132ad43027SMikolaj Golub 		}
2142ad43027SMikolaj Golub 	}
2152ad43027SMikolaj Golub }
2162ad43027SMikolaj Golub 
2172ad43027SMikolaj Golub static void
218bd06a3ecSMike Barcroft usage(void)
219bd06a3ecSMike Barcroft {
220846be7bdSPoul-Henning Kamp 	(void)fprintf(stderr,
221e6d4b388STom Rhodes 	    "usage: daemon [-cf] [-p pidfile] [-u user] command "
22212d7249eSTom Rhodes 		"arguments ...\n");
223bd06a3ecSMike Barcroft 	exit(1);
224bd06a3ecSMike Barcroft }
225