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 * $FreeBSD: head/usr.sbin/daemon/daemon.c 255707 2013-09-19 18:00:05Z trociny $ 30 */ 31 32 33 #include <sys/param.h> 34 #include <sys/mman.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 47 static void dummy_sighandler(int); 48 static void restrict_process(const char *); 49 static int wait_child(pid_t pid, sigset_t *mask); 50 static void usage(void); 51 52 int 53 main(int argc, char *argv[]) 54 { 55 struct pidfh *ppfh, *pfh; 56 sigset_t mask, oldmask; 57 int ch, nochdir, noclose, restart, serrno; 58 const char *pidfile, *ppidfile, *user; 59 pid_t otherpid, pid; 60 61 nochdir = noclose = 1; 62 restart = 0; 63 ppidfile = pidfile = user = NULL; 64 while ((ch = getopt(argc, argv, "cfp:P: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 'P': 76 ppidfile = optarg; 77 break; 78 case 'r': 79 restart = 1; 80 break; 81 case 'u': 82 user = optarg; 83 break; 84 default: 85 usage(); 86 } 87 } 88 argc -= optind; 89 argv += optind; 90 91 if (argc == 0) 92 usage(); 93 94 ppfh = pfh = NULL; 95 /* 96 * Try to open the pidfile before calling daemon(3), 97 * to be able to report the error intelligently 98 */ 99 if (pidfile != NULL) { 100 pfh = pidfile_open(pidfile, 0600, &otherpid); 101 if (pfh == NULL) { 102 if (errno == EEXIST) { 103 errx(3, "process already running, pid: %d", 104 otherpid); 105 } 106 err(2, "pidfile ``%s''", pidfile); 107 } 108 } 109 /* Do the same for actual daemon process. */ 110 if (ppidfile != NULL) { 111 ppfh = pidfile_open(ppidfile, 0600, &otherpid); 112 if (ppfh == NULL) { 113 serrno = errno; 114 pidfile_remove(pfh); 115 errno = serrno; 116 if (errno == EEXIST) { 117 errx(3, "process already running, pid: %d", 118 otherpid); 119 } 120 err(2, "ppidfile ``%s''", ppidfile); 121 } 122 } 123 124 if (daemon(nochdir, noclose) == -1) { 125 warn("daemon"); 126 goto exit; 127 } 128 /* Write out parent pidfile if needed. */ 129 pidfile_write(ppfh); 130 131 /* 132 * If the pidfile or restart option is specified the daemon 133 * executes the command in a forked process and wait on child 134 * exit to remove the pidfile or restart the command. Normally 135 * we don't want the monitoring daemon to be terminated 136 * leaving the running process and the stale pidfile, so we 137 * catch SIGTERM and forward it to the children expecting to 138 * get SIGCHLD eventually. 139 */ 140 pid = -1; 141 if (pidfile != NULL || restart) { 142 /* 143 * Restore default action for SIGTERM in case the 144 * parent process decided to ignore it. 145 */ 146 if (signal(SIGTERM, SIG_DFL) == SIG_ERR) { 147 warn("signal"); 148 goto exit; 149 } 150 /* 151 * Because SIGCHLD is ignored by default, setup dummy handler 152 * for it, so we can mask it. 153 */ 154 if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) { 155 warn("signal"); 156 goto exit; 157 } 158 /* 159 * Block interesting signals. 160 */ 161 sigemptyset(&mask); 162 sigaddset(&mask, SIGTERM); 163 sigaddset(&mask, SIGCHLD); 164 if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) { 165 warn("sigprocmask"); 166 goto exit; 167 } 168 /* 169 * Try to protect against pageout kill. Ignore the 170 * error, madvise(2) will fail only if a process does 171 * not have superuser privileges. 172 * MADV_PROTECT not supported on DragonFly 173 * (void)madvise(NULL, 0, MADV_PROTECT); 174 */ 175 restart: 176 /* 177 * Spawn a child to exec the command, so in the parent 178 * we could wait for it to exit and remove pidfile. 179 */ 180 pid = fork(); 181 if (pid == -1) { 182 warn("fork"); 183 goto exit; 184 } 185 } 186 if (pid <= 0) { 187 if (pid == 0) { 188 /* Restore old sigmask in the child. */ 189 if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 190 err(1, "sigprocmask"); 191 } 192 /* Now that we are the child, write out the pid. */ 193 pidfile_write(pfh); 194 195 if (user != NULL) 196 restrict_process(user); 197 198 execvp(argv[0], argv); 199 200 /* 201 * execvp() failed -- report the error. The child is 202 * now running, so the exit status doesn't matter. 203 */ 204 err(1, "%s", argv[0]); 205 } 206 207 setproctitle("%s[%d]", argv[0], pid); 208 if (wait_child(pid, &mask) == 0 && restart) { 209 sleep(1); 210 goto restart; 211 } 212 exit: 213 pidfile_remove(pfh); 214 pidfile_remove(ppfh); 215 exit(1); /* If daemon(3) succeeded exit status does not matter. */ 216 } 217 218 static void 219 dummy_sighandler(int sig __unused) 220 { 221 /* Nothing to do. */ 222 } 223 224 static void 225 restrict_process(const char *user) 226 { 227 struct passwd *pw = NULL; 228 229 pw = getpwnam(user); 230 if (pw == NULL) 231 errx(1, "unknown user: %s", user); 232 233 if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 234 errx(1, "failed to set user environment"); 235 } 236 237 static int 238 wait_child(pid_t pid, sigset_t *mask) 239 { 240 int terminate, signo; 241 242 terminate = 0; 243 for (;;) { 244 if (sigwait(mask, &signo) == -1) { 245 warn("sigwaitinfo"); 246 return (-1); 247 } 248 switch (signo) { 249 case SIGCHLD: 250 if (waitpid(pid, NULL, WNOHANG) == -1) { 251 warn("waitpid"); 252 return (-1); 253 } 254 return (terminate); 255 case SIGTERM: 256 terminate = 1; 257 if (kill(pid, signo) == -1) { 258 warn("kill"); 259 return (-1); 260 } 261 continue; 262 default: 263 warnx("sigwaitinfo: invalid signal: %d", signo); 264 return (-1); 265 } 266 } 267 } 268 269 static void 270 usage(void) 271 { 272 (void)fprintf(stderr, 273 "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " 274 "[-u user]\n command arguments ...\n"); 275 exit(1); 276 } 277