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 { 5632b17786SJohn-Mark Gurney struct pidfh *ppfh, *pfh; 57195fc497SMikolaj Golub sigset_t mask, oldmask; 58*9da0ef13SMikolaj Golub int ch, nochdir, noclose, restart, serrno; 5932b17786SJohn-Mark Gurney const char *pidfile, *ppidfile, *user; 602ad43027SMikolaj Golub pid_t otherpid, pid; 61bd06a3ecSMike Barcroft 62bd06a3ecSMike Barcroft nochdir = noclose = 1; 63b6193c24SMikolaj Golub restart = 0; 6432b17786SJohn-Mark Gurney ppidfile = pidfile = user = NULL; 6532b17786SJohn-Mark Gurney while ((ch = getopt(argc, argv, "cfp:P: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; 7632b17786SJohn-Mark Gurney case 'P': 7732b17786SJohn-Mark Gurney ppidfile = optarg; 7832b17786SJohn-Mark Gurney break; 79b6193c24SMikolaj Golub case 'r': 80b6193c24SMikolaj Golub restart = 1; 81b6193c24SMikolaj Golub break; 82e6d4b388STom Rhodes case 'u': 83e6d4b388STom Rhodes user = optarg; 84e6d4b388STom Rhodes break; 85bd06a3ecSMike Barcroft default: 86bd06a3ecSMike Barcroft usage(); 87bd06a3ecSMike Barcroft } 88bd06a3ecSMike Barcroft } 89bd06a3ecSMike Barcroft argc -= optind; 90bd06a3ecSMike Barcroft argv += optind; 91bd06a3ecSMike Barcroft 92bd06a3ecSMike Barcroft if (argc == 0) 93bd06a3ecSMike Barcroft usage(); 9412d7249eSTom Rhodes 9532b17786SJohn-Mark Gurney ppfh = pfh = NULL; 96846be7bdSPoul-Henning Kamp /* 97846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 98846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 99846be7bdSPoul-Henning Kamp */ 1002ad43027SMikolaj Golub if (pidfile != NULL) { 101c6262cb6SPawel Jakub Dawidek pfh = pidfile_open(pidfile, 0600, &otherpid); 102c6262cb6SPawel Jakub Dawidek if (pfh == NULL) { 103c6262cb6SPawel Jakub Dawidek if (errno == EEXIST) { 104c6262cb6SPawel Jakub Dawidek errx(3, "process already running, pid: %d", 105c6262cb6SPawel Jakub Dawidek otherpid); 106c6262cb6SPawel Jakub Dawidek } 107846be7bdSPoul-Henning Kamp err(2, "pidfile ``%s''", pidfile); 108846be7bdSPoul-Henning Kamp } 109c6262cb6SPawel Jakub Dawidek } 110*9da0ef13SMikolaj Golub /* Do the same for actual daemon process. */ 11132b17786SJohn-Mark Gurney if (ppidfile != NULL) { 11232b17786SJohn-Mark Gurney ppfh = pidfile_open(ppidfile, 0600, &otherpid); 11332b17786SJohn-Mark Gurney if (ppfh == NULL) { 114*9da0ef13SMikolaj Golub serrno = errno; 115*9da0ef13SMikolaj Golub pidfile_remove(pfh); 116*9da0ef13SMikolaj Golub errno = serrno; 11732b17786SJohn-Mark Gurney if (errno == EEXIST) { 11832b17786SJohn-Mark Gurney errx(3, "process already running, pid: %d", 11932b17786SJohn-Mark Gurney otherpid); 12032b17786SJohn-Mark Gurney } 12132b17786SJohn-Mark Gurney err(2, "ppidfile ``%s''", ppidfile); 12232b17786SJohn-Mark Gurney } 12332b17786SJohn-Mark Gurney } 12432b17786SJohn-Mark Gurney 125*9da0ef13SMikolaj Golub if (daemon(nochdir, noclose) == -1) { 126*9da0ef13SMikolaj Golub warn("daemon"); 127*9da0ef13SMikolaj Golub goto exit; 128*9da0ef13SMikolaj Golub } 129*9da0ef13SMikolaj Golub /* Write out parent pidfile if needed. */ 130*9da0ef13SMikolaj Golub pidfile_write(ppfh); 131846be7bdSPoul-Henning Kamp 132195fc497SMikolaj Golub /* 133b6193c24SMikolaj Golub * If the pidfile or restart option is specified the daemon 134b6193c24SMikolaj Golub * executes the command in a forked process and wait on child 135b6193c24SMikolaj Golub * exit to remove the pidfile or restart the command. Normally 136b6193c24SMikolaj Golub * we don't want the monitoring daemon to be terminated 137b6193c24SMikolaj Golub * leaving the running process and the stale pidfile, so we 138b6193c24SMikolaj Golub * catch SIGTERM and forward it to the children expecting to 139b6193c24SMikolaj Golub * get SIGCHLD eventually. 140195fc497SMikolaj Golub */ 141195fc497SMikolaj Golub pid = -1; 142b6193c24SMikolaj Golub if (pidfile != NULL || restart) { 1432ad43027SMikolaj Golub /* 144195fc497SMikolaj Golub * Restore default action for SIGTERM in case the 145195fc497SMikolaj Golub * parent process decided to ignore it. 146195fc497SMikolaj Golub */ 147*9da0ef13SMikolaj Golub if (signal(SIGTERM, SIG_DFL) == SIG_ERR) { 148*9da0ef13SMikolaj Golub warn("signal"); 149*9da0ef13SMikolaj Golub goto exit; 150*9da0ef13SMikolaj Golub } 151195fc497SMikolaj Golub /* 152195fc497SMikolaj Golub * Because SIGCHLD is ignored by default, setup dummy handler 153195fc497SMikolaj Golub * for it, so we can mask it. 154195fc497SMikolaj Golub */ 155*9da0ef13SMikolaj Golub if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) { 156*9da0ef13SMikolaj Golub warn("signal"); 157*9da0ef13SMikolaj Golub goto exit; 158*9da0ef13SMikolaj Golub } 159195fc497SMikolaj Golub /* 160195fc497SMikolaj Golub * Block interesting signals. 161195fc497SMikolaj Golub */ 162195fc497SMikolaj Golub sigemptyset(&mask); 163195fc497SMikolaj Golub sigaddset(&mask, SIGTERM); 164195fc497SMikolaj Golub sigaddset(&mask, SIGCHLD); 165*9da0ef13SMikolaj Golub if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) { 166*9da0ef13SMikolaj Golub warn("sigprocmask"); 167*9da0ef13SMikolaj Golub goto exit; 168*9da0ef13SMikolaj Golub } 16953c49998SMikolaj Golub /* 17053c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 17153c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 17253c49998SMikolaj Golub * not have superuser privileges. 17353c49998SMikolaj Golub */ 17453c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 175b6193c24SMikolaj Golub restart: 176195fc497SMikolaj Golub /* 1772ad43027SMikolaj Golub * Spawn a child to exec the command, so in the parent 1782ad43027SMikolaj Golub * we could wait for it to exit and remove pidfile. 1792ad43027SMikolaj Golub */ 1802ad43027SMikolaj Golub pid = fork(); 1812ad43027SMikolaj Golub if (pid == -1) { 182*9da0ef13SMikolaj Golub warn("fork"); 183*9da0ef13SMikolaj Golub goto exit; 1842ad43027SMikolaj Golub } 1852ad43027SMikolaj Golub } 186195fc497SMikolaj Golub if (pid <= 0) { 1872ad43027SMikolaj Golub if (pid == 0) { 188195fc497SMikolaj Golub /* Restore old sigmask in the child. */ 189195fc497SMikolaj Golub if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 190195fc497SMikolaj Golub err(1, "sigprocmask"); 191195fc497SMikolaj Golub } 1922ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 193c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 194846be7bdSPoul-Henning Kamp 1952ad43027SMikolaj Golub if (user != NULL) 1962ad43027SMikolaj Golub restrict_process(user); 1972ad43027SMikolaj Golub 198bd06a3ecSMike Barcroft execvp(argv[0], argv); 199bd06a3ecSMike Barcroft 200846be7bdSPoul-Henning Kamp /* 2012ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 2022ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 203846be7bdSPoul-Henning Kamp */ 2042ad43027SMikolaj Golub err(1, "%s", argv[0]); 2052ad43027SMikolaj Golub } 20632b17786SJohn-Mark Gurney 2072ad43027SMikolaj Golub setproctitle("%s[%d]", argv[0], pid); 208b6193c24SMikolaj Golub if (wait_child(pid, &mask) == 0 && restart) { 209b6193c24SMikolaj Golub sleep(1); 210b6193c24SMikolaj Golub goto restart; 211b6193c24SMikolaj Golub } 212*9da0ef13SMikolaj Golub exit: 213c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 21432b17786SJohn-Mark Gurney pidfile_remove(ppfh); 215*9da0ef13SMikolaj Golub exit(1); /* If daemon(3) succeeded exit status does not matter. */ 216bd06a3ecSMike Barcroft } 217bd06a3ecSMike Barcroft 218bd06a3ecSMike Barcroft static void 219195fc497SMikolaj Golub dummy_sighandler(int sig __unused) 220195fc497SMikolaj Golub { 221195fc497SMikolaj Golub /* Nothing to do. */ 222195fc497SMikolaj Golub } 223195fc497SMikolaj Golub 224195fc497SMikolaj Golub static void 225e6d4b388STom Rhodes restrict_process(const char *user) 22612d7249eSTom Rhodes { 22712d7249eSTom Rhodes struct passwd *pw = NULL; 22812d7249eSTom Rhodes 229e6d4b388STom Rhodes pw = getpwnam(user); 230e6d4b388STom Rhodes if (pw == NULL) 231e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 23212d7249eSTom Rhodes 233e6d4b388STom Rhodes if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 234e6d4b388STom Rhodes errx(1, "failed to set user environment"); 23512d7249eSTom Rhodes } 23612d7249eSTom Rhodes 237b6193c24SMikolaj Golub static int 238195fc497SMikolaj Golub wait_child(pid_t pid, sigset_t *mask) 2392ad43027SMikolaj Golub { 240b6193c24SMikolaj Golub int terminate, signo; 2412ad43027SMikolaj Golub 242b6193c24SMikolaj Golub terminate = 0; 243195fc497SMikolaj Golub for (;;) { 244195fc497SMikolaj Golub if (sigwait(mask, &signo) == -1) { 245195fc497SMikolaj Golub warn("sigwaitinfo"); 246b6193c24SMikolaj Golub return (-1); 247195fc497SMikolaj Golub } 248195fc497SMikolaj Golub switch (signo) { 249195fc497SMikolaj Golub case SIGCHLD: 250c60d51f9SMikolaj Golub if (waitpid(pid, NULL, WNOHANG) == -1) { 251c60d51f9SMikolaj Golub warn("waitpid"); 252c60d51f9SMikolaj Golub return (-1); 253c60d51f9SMikolaj Golub } 254b6193c24SMikolaj Golub return (terminate); 255195fc497SMikolaj Golub case SIGTERM: 256b6193c24SMikolaj Golub terminate = 1; 257195fc497SMikolaj Golub if (kill(pid, signo) == -1) { 258195fc497SMikolaj Golub warn("kill"); 259b6193c24SMikolaj Golub return (-1); 260195fc497SMikolaj Golub } 261195fc497SMikolaj Golub continue; 262195fc497SMikolaj Golub default: 263195fc497SMikolaj Golub warnx("sigwaitinfo: invalid signal: %d", signo); 264b6193c24SMikolaj Golub return (-1); 2652ad43027SMikolaj Golub } 2662ad43027SMikolaj Golub } 2672ad43027SMikolaj Golub } 2682ad43027SMikolaj Golub 2692ad43027SMikolaj Golub static void 270bd06a3ecSMike Barcroft usage(void) 271bd06a3ecSMike Barcroft { 272846be7bdSPoul-Henning Kamp (void)fprintf(stderr, 27332b17786SJohn-Mark Gurney "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " 27432b17786SJohn-Mark Gurney "[-u user]\n command arguments ...\n"); 275bd06a3ecSMike Barcroft exit(1); 276bd06a3ecSMike Barcroft } 277