daemon.c revision 231911
1/*- 2 * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 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 14 * written permission. 15 * 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 26 * SUCH DAMAGE. 27 * 28 * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp 29 */ 30 31#include <sys/cdefs.h> 32__FBSDID("$FreeBSD: head/usr.sbin/daemon/daemon.c 231911 2012-02-19 10:36:29Z trociny $"); 33 34#include <sys/param.h> 35#include <sys/wait.h> 36 37#include <err.h> 38#include <errno.h> 39#include <libutil.h> 40#include <login_cap.h> 41#include <pwd.h> 42#include <signal.h> 43#include <stdio.h> 44#include <stdlib.h> 45#include <unistd.h> 46 47static void dummy_sighandler(int); 48static void restrict_process(const char *); 49static int wait_child(pid_t pid, sigset_t *mask); 50static void usage(void); 51 52int 53main(int argc, char *argv[]) 54{ 55 struct pidfh *pfh = NULL; 56 sigset_t mask, oldmask; 57 int ch, nochdir, noclose, restart; 58 const char *pidfile, *user; 59 pid_t otherpid, pid; 60 61 nochdir = noclose = 1; 62 restart = 0; 63 pidfile = user = NULL; 64 while ((ch = getopt(argc, argv, "-cfp:ru:")) != -1) { 65 switch (ch) { 66 case 'c': 67 nochdir = 0; 68 break; 69 case 'f': 70 noclose = 0; 71 break; 72 case 'p': 73 pidfile = optarg; 74 break; 75 case 'r': 76 restart = 1; 77 break; 78 case 'u': 79 user = optarg; 80 break; 81 default: 82 usage(); 83 } 84 } 85 argc -= optind; 86 argv += optind; 87 88 if (argc == 0) 89 usage(); 90 91 pfh = NULL; 92 /* 93 * Try to open the pidfile before calling daemon(3), 94 * to be able to report the error intelligently 95 */ 96 if (pidfile != NULL) { 97 pfh = pidfile_open(pidfile, 0600, &otherpid); 98 if (pfh == NULL) { 99 if (errno == EEXIST) { 100 errx(3, "process already running, pid: %d", 101 otherpid); 102 } 103 err(2, "pidfile ``%s''", pidfile); 104 } 105 } 106 107 if (daemon(nochdir, noclose) == -1) 108 err(1, NULL); 109 110 /* 111 * If the pidfile or restart option is specified the daemon 112 * executes the command in a forked process and wait on child 113 * exit to remove the pidfile or restart the command. Normally 114 * we don't want the monitoring daemon to be terminated 115 * leaving the running process and the stale pidfile, so we 116 * catch SIGTERM and forward it to the children expecting to 117 * get SIGCHLD eventually. 118 */ 119 pid = -1; 120 if (pidfile != NULL || restart) { 121 /* 122 * Restore default action for SIGTERM in case the 123 * parent process decided to ignore it. 124 */ 125 if (signal(SIGTERM, SIG_DFL) == SIG_ERR) 126 err(1, "signal"); 127 /* 128 * Because SIGCHLD is ignored by default, setup dummy handler 129 * for it, so we can mask it. 130 */ 131 if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) 132 err(1, "signal"); 133 /* 134 * Block interesting signals. 135 */ 136 sigemptyset(&mask); 137 sigaddset(&mask, SIGTERM); 138 sigaddset(&mask, SIGCHLD); 139 if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) 140 err(1, "sigprocmask"); 141restart: 142 /* 143 * Spawn a child to exec the command, so in the parent 144 * we could wait for it to exit and remove pidfile. 145 */ 146 pid = fork(); 147 if (pid == -1) { 148 pidfile_remove(pfh); 149 err(1, "fork"); 150 } 151 } 152 if (pid <= 0) { 153 if (pid == 0) { 154 /* Restore old sigmask in the child. */ 155 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 156 err(1, "sigprocmask"); 157 } 158 /* Now that we are the child, write out the pid. */ 159 pidfile_write(pfh); 160 161 if (user != NULL) 162 restrict_process(user); 163 164 execvp(argv[0], argv); 165 166 /* 167 * execvp() failed -- report the error. The child is 168 * now running, so the exit status doesn't matter. 169 */ 170 err(1, "%s", argv[0]); 171 } 172 setproctitle("%s[%d]", argv[0], pid); 173 if (wait_child(pid, &mask) == 0 && restart) { 174 sleep(1); 175 goto restart; 176 } 177 pidfile_remove(pfh); 178 exit(0); /* Exit status does not matter. */ 179} 180 181static void 182dummy_sighandler(int sig __unused) 183{ 184 /* Nothing to do. */ 185} 186 187static void 188restrict_process(const char *user) 189{ 190 struct passwd *pw = NULL; 191 192 pw = getpwnam(user); 193 if (pw == NULL) 194 errx(1, "unknown user: %s", user); 195 196 if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 197 errx(1, "failed to set user environment"); 198} 199 200static int 201wait_child(pid_t pid, sigset_t *mask) 202{ 203 int terminate, signo; 204 205 terminate = 0; 206 for (;;) { 207 if (sigwait(mask, &signo) == -1) { 208 warn("sigwaitinfo"); 209 return (-1); 210 } 211 switch (signo) { 212 case SIGCHLD: 213 return (terminate); 214 case SIGTERM: 215 terminate = 1; 216 if (kill(pid, signo) == -1) { 217 warn("kill"); 218 return (-1); 219 } 220 continue; 221 default: 222 warnx("sigwaitinfo: invalid signal: %d", signo); 223 return (-1); 224 } 225 } 226} 227 228static void 229usage(void) 230{ 231 (void)fprintf(stderr, 232 "usage: daemon [-cf] [-p pidfile] [-u user] command " 233 "arguments ...\n"); 234 exit(1); 235} 236