2 * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
12 * 3. Berkeley Software Design Inc's name may not be used to endorse or
13 * promote products derived from this software without specific prior
16 * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
31 #include <sys/cdefs.h>
32 __FBSDID("$FreeBSD$");
34 #include <sys/param.h>
41 #include <login_cap.h>
48 static void dummy_sighandler(int);
49 static void restrict_process(const char *);
50 static int wait_child(pid_t pid, sigset_t *mask);
51 static void usage(void);
54 main(int argc, char *argv[])
56 struct pidfh *ppfh, *pfh;
57 sigset_t mask, oldmask;
58 int ch, nochdir, noclose, restart, serrno;
59 const char *pidfile, *ppidfile, *user;
62 nochdir = noclose = 1;
64 ppidfile = pidfile = user = NULL;
65 while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) {
97 * Try to open the pidfile before calling daemon(3),
98 * to be able to report the error intelligently
100 if (pidfile != NULL) {
101 pfh = pidfile_open(pidfile, 0600, &otherpid);
103 if (errno == EEXIST) {
104 errx(3, "process already running, pid: %d",
107 err(2, "pidfile ``%s''", pidfile);
110 /* Do the same for actual daemon process. */
111 if (ppidfile != NULL) {
112 ppfh = pidfile_open(ppidfile, 0600, &otherpid);
117 if (errno == EEXIST) {
118 errx(3, "process already running, pid: %d",
121 err(2, "ppidfile ``%s''", ppidfile);
125 if (daemon(nochdir, noclose) == -1) {
129 /* Write out parent pidfile if needed. */
133 * If the pidfile or restart option is specified the daemon
134 * executes the command in a forked process and wait on child
135 * exit to remove the pidfile or restart the command. Normally
136 * we don't want the monitoring daemon to be terminated
137 * leaving the running process and the stale pidfile, so we
138 * catch SIGTERM and forward it to the children expecting to
139 * get SIGCHLD eventually.
142 if (pidfile != NULL || ppidfile != NULL || restart) {
144 * Restore default action for SIGTERM in case the
145 * parent process decided to ignore it.
147 if (signal(SIGTERM, SIG_DFL) == SIG_ERR) {
152 * Because SIGCHLD is ignored by default, setup dummy handler
153 * for it, so we can mask it.
155 if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) {
160 * Block interesting signals.
163 sigaddset(&mask, SIGTERM);
164 sigaddset(&mask, SIGCHLD);
165 if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) {
170 * Try to protect against pageout kill. Ignore the
171 * error, madvise(2) will fail only if a process does
172 * not have superuser privileges.
174 (void)madvise(NULL, 0, MADV_PROTECT);
177 * Spawn a child to exec the command, so in the parent
178 * we could wait for it to exit and remove pidfile.
188 /* Restore old sigmask in the child. */
189 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1)
190 err(1, "sigprocmask");
192 /* Now that we are the child, write out the pid. */
196 restrict_process(user);
198 execvp(argv[0], argv);
201 * execvp() failed -- report the error. The child is
202 * now running, so the exit status doesn't matter.
204 err(1, "%s", argv[0]);
207 setproctitle("%s[%d]", argv[0], pid);
208 if (wait_child(pid, &mask) == 0 && restart) {
214 pidfile_remove(ppfh);
215 exit(1); /* If daemon(3) succeeded exit status does not matter. */
219 dummy_sighandler(int sig __unused)
225 restrict_process(const char *user)
227 struct passwd *pw = NULL;
231 errx(1, "unknown user: %s", user);
233 if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0)
234 errx(1, "failed to set user environment");
238 wait_child(pid_t pid, sigset_t *mask)
240 int terminate, signo;
244 if (sigwait(mask, &signo) == -1) {
250 if (waitpid(pid, NULL, WNOHANG) == -1) {
257 if (kill(pid, signo) == -1) {
263 warnx("sigwaitinfo: invalid signal: %d", signo);
272 (void)fprintf(stderr,
273 "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] "
274 "[-u user]\n command arguments ...\n");