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 { 56*32b17786SJohn-Mark Gurney struct pidfh *ppfh, *pfh; 57195fc497SMikolaj Golub sigset_t mask, oldmask; 58b6193c24SMikolaj Golub int ch, nochdir, noclose, restart; 59*32b17786SJohn-Mark Gurney const char *pidfile, *ppidfile, *user; 602ad43027SMikolaj Golub pid_t otherpid, pid; 61bd06a3ecSMike Barcroft 62bd06a3ecSMike Barcroft nochdir = noclose = 1; 63b6193c24SMikolaj Golub restart = 0; 64*32b17786SJohn-Mark Gurney ppfh = pfh = NULL; 65*32b17786SJohn-Mark Gurney ppidfile = pidfile = user = NULL; 66*32b17786SJohn-Mark Gurney while ((ch = getopt(argc, argv, "cfp:P:ru:")) != -1) { 67bd06a3ecSMike Barcroft switch (ch) { 68bd06a3ecSMike Barcroft case 'c': 69bd06a3ecSMike Barcroft nochdir = 0; 70bd06a3ecSMike Barcroft break; 71bd06a3ecSMike Barcroft case 'f': 72bd06a3ecSMike Barcroft noclose = 0; 73bd06a3ecSMike Barcroft break; 74846be7bdSPoul-Henning Kamp case 'p': 75846be7bdSPoul-Henning Kamp pidfile = optarg; 76846be7bdSPoul-Henning Kamp break; 77*32b17786SJohn-Mark Gurney case 'P': 78*32b17786SJohn-Mark Gurney ppidfile = optarg; 79*32b17786SJohn-Mark Gurney break; 80b6193c24SMikolaj Golub case 'r': 81b6193c24SMikolaj Golub restart = 1; 82b6193c24SMikolaj Golub break; 83e6d4b388STom Rhodes case 'u': 84e6d4b388STom Rhodes user = optarg; 85e6d4b388STom Rhodes break; 86bd06a3ecSMike Barcroft default: 87bd06a3ecSMike Barcroft usage(); 88bd06a3ecSMike Barcroft } 89bd06a3ecSMike Barcroft } 90bd06a3ecSMike Barcroft argc -= optind; 91bd06a3ecSMike Barcroft argv += optind; 92bd06a3ecSMike Barcroft 93bd06a3ecSMike Barcroft if (argc == 0) 94bd06a3ecSMike Barcroft usage(); 9512d7249eSTom Rhodes 96*32b17786SJohn-Mark Gurney ppfh = pfh = NULL; 97846be7bdSPoul-Henning Kamp /* 98846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 99846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 100846be7bdSPoul-Henning Kamp */ 1012ad43027SMikolaj Golub if (pidfile != NULL) { 102c6262cb6SPawel Jakub Dawidek pfh = pidfile_open(pidfile, 0600, &otherpid); 103c6262cb6SPawel Jakub Dawidek if (pfh == NULL) { 104c6262cb6SPawel Jakub Dawidek if (errno == EEXIST) { 105c6262cb6SPawel Jakub Dawidek errx(3, "process already running, pid: %d", 106c6262cb6SPawel Jakub Dawidek otherpid); 107c6262cb6SPawel Jakub Dawidek } 108846be7bdSPoul-Henning Kamp err(2, "pidfile ``%s''", pidfile); 109846be7bdSPoul-Henning Kamp } 110c6262cb6SPawel Jakub Dawidek } 111846be7bdSPoul-Henning Kamp 112*32b17786SJohn-Mark Gurney /* do same for actual daemon process */ 113*32b17786SJohn-Mark Gurney if (ppidfile != NULL) { 114*32b17786SJohn-Mark Gurney ppfh = pidfile_open(ppidfile, 0600, &otherpid); 115*32b17786SJohn-Mark Gurney if (ppfh == NULL) { 116*32b17786SJohn-Mark Gurney if (errno == EEXIST) { 117*32b17786SJohn-Mark Gurney errx(3, "process already running, pid: %d", 118*32b17786SJohn-Mark Gurney otherpid); 119*32b17786SJohn-Mark Gurney } 120*32b17786SJohn-Mark Gurney err(2, "ppidfile ``%s''", ppidfile); 121*32b17786SJohn-Mark Gurney } 122*32b17786SJohn-Mark Gurney } 123*32b17786SJohn-Mark Gurney 124bd06a3ecSMike Barcroft if (daemon(nochdir, noclose) == -1) 125bd06a3ecSMike Barcroft err(1, NULL); 126846be7bdSPoul-Henning Kamp 127195fc497SMikolaj Golub /* 128b6193c24SMikolaj Golub * If the pidfile or restart option is specified the daemon 129b6193c24SMikolaj Golub * executes the command in a forked process and wait on child 130b6193c24SMikolaj Golub * exit to remove the pidfile or restart the command. Normally 131b6193c24SMikolaj Golub * we don't want the monitoring daemon to be terminated 132b6193c24SMikolaj Golub * leaving the running process and the stale pidfile, so we 133b6193c24SMikolaj Golub * catch SIGTERM and forward it to the children expecting to 134b6193c24SMikolaj Golub * get SIGCHLD eventually. 135195fc497SMikolaj Golub */ 136195fc497SMikolaj Golub pid = -1; 137b6193c24SMikolaj Golub if (pidfile != NULL || restart) { 1382ad43027SMikolaj Golub /* 139195fc497SMikolaj Golub * Restore default action for SIGTERM in case the 140195fc497SMikolaj Golub * parent process decided to ignore it. 141195fc497SMikolaj Golub */ 142195fc497SMikolaj Golub if (signal(SIGTERM, SIG_DFL) == SIG_ERR) 143195fc497SMikolaj Golub err(1, "signal"); 144195fc497SMikolaj Golub /* 145195fc497SMikolaj Golub * Because SIGCHLD is ignored by default, setup dummy handler 146195fc497SMikolaj Golub * for it, so we can mask it. 147195fc497SMikolaj Golub */ 148195fc497SMikolaj Golub if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) 149195fc497SMikolaj Golub err(1, "signal"); 150195fc497SMikolaj Golub /* 151195fc497SMikolaj Golub * Block interesting signals. 152195fc497SMikolaj Golub */ 153195fc497SMikolaj Golub sigemptyset(&mask); 154195fc497SMikolaj Golub sigaddset(&mask, SIGTERM); 155195fc497SMikolaj Golub sigaddset(&mask, SIGCHLD); 156195fc497SMikolaj Golub if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) 157195fc497SMikolaj Golub err(1, "sigprocmask"); 15853c49998SMikolaj Golub /* 15953c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 16053c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 16153c49998SMikolaj Golub * not have superuser privileges. 16253c49998SMikolaj Golub */ 16353c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 164b6193c24SMikolaj Golub restart: 165195fc497SMikolaj Golub /* 1662ad43027SMikolaj Golub * Spawn a child to exec the command, so in the parent 1672ad43027SMikolaj Golub * we could wait for it to exit and remove pidfile. 1682ad43027SMikolaj Golub */ 1692ad43027SMikolaj Golub pid = fork(); 1702ad43027SMikolaj Golub if (pid == -1) { 1712ad43027SMikolaj Golub pidfile_remove(pfh); 1722ad43027SMikolaj Golub err(1, "fork"); 1732ad43027SMikolaj Golub } 1742ad43027SMikolaj Golub } 175195fc497SMikolaj Golub if (pid <= 0) { 1762ad43027SMikolaj Golub if (pid == 0) { 177195fc497SMikolaj Golub /* Restore old sigmask in the child. */ 178195fc497SMikolaj Golub if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 179195fc497SMikolaj Golub err(1, "sigprocmask"); 180195fc497SMikolaj Golub } 1812ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 182c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 183846be7bdSPoul-Henning Kamp 1842ad43027SMikolaj Golub if (user != NULL) 1852ad43027SMikolaj Golub restrict_process(user); 1862ad43027SMikolaj Golub 187bd06a3ecSMike Barcroft execvp(argv[0], argv); 188bd06a3ecSMike Barcroft 189846be7bdSPoul-Henning Kamp /* 1902ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 1912ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 192846be7bdSPoul-Henning Kamp */ 1932ad43027SMikolaj Golub err(1, "%s", argv[0]); 1942ad43027SMikolaj Golub } 195*32b17786SJohn-Mark Gurney /* write out parent pidfile if needed */ 196*32b17786SJohn-Mark Gurney if (ppidfile != NULL) 197*32b17786SJohn-Mark Gurney pidfile_write(ppfh); 198*32b17786SJohn-Mark Gurney 1992ad43027SMikolaj Golub setproctitle("%s[%d]", argv[0], pid); 200b6193c24SMikolaj Golub if (wait_child(pid, &mask) == 0 && restart) { 201b6193c24SMikolaj Golub sleep(1); 202b6193c24SMikolaj Golub goto restart; 203b6193c24SMikolaj Golub } 204c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 205*32b17786SJohn-Mark Gurney pidfile_remove(ppfh); 2062ad43027SMikolaj Golub exit(0); /* Exit status does not matter. */ 207bd06a3ecSMike Barcroft } 208bd06a3ecSMike Barcroft 209bd06a3ecSMike Barcroft static void 210195fc497SMikolaj Golub dummy_sighandler(int sig __unused) 211195fc497SMikolaj Golub { 212195fc497SMikolaj Golub /* Nothing to do. */ 213195fc497SMikolaj Golub } 214195fc497SMikolaj Golub 215195fc497SMikolaj Golub static void 216e6d4b388STom Rhodes restrict_process(const char *user) 21712d7249eSTom Rhodes { 21812d7249eSTom Rhodes struct passwd *pw = NULL; 21912d7249eSTom Rhodes 220e6d4b388STom Rhodes pw = getpwnam(user); 221e6d4b388STom Rhodes if (pw == NULL) 222e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 22312d7249eSTom Rhodes 224e6d4b388STom Rhodes if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 225e6d4b388STom Rhodes errx(1, "failed to set user environment"); 22612d7249eSTom Rhodes } 22712d7249eSTom Rhodes 228b6193c24SMikolaj Golub static int 229195fc497SMikolaj Golub wait_child(pid_t pid, sigset_t *mask) 2302ad43027SMikolaj Golub { 231b6193c24SMikolaj Golub int terminate, signo; 2322ad43027SMikolaj Golub 233b6193c24SMikolaj Golub terminate = 0; 234195fc497SMikolaj Golub for (;;) { 235195fc497SMikolaj Golub if (sigwait(mask, &signo) == -1) { 236195fc497SMikolaj Golub warn("sigwaitinfo"); 237b6193c24SMikolaj Golub return (-1); 238195fc497SMikolaj Golub } 239195fc497SMikolaj Golub switch (signo) { 240195fc497SMikolaj Golub case SIGCHLD: 241c60d51f9SMikolaj Golub if (waitpid(pid, NULL, WNOHANG) == -1) { 242c60d51f9SMikolaj Golub warn("waitpid"); 243c60d51f9SMikolaj Golub return (-1); 244c60d51f9SMikolaj Golub } 245b6193c24SMikolaj Golub return (terminate); 246195fc497SMikolaj Golub case SIGTERM: 247b6193c24SMikolaj Golub terminate = 1; 248195fc497SMikolaj Golub if (kill(pid, signo) == -1) { 249195fc497SMikolaj Golub warn("kill"); 250b6193c24SMikolaj Golub return (-1); 251195fc497SMikolaj Golub } 252195fc497SMikolaj Golub continue; 253195fc497SMikolaj Golub default: 254195fc497SMikolaj Golub warnx("sigwaitinfo: invalid signal: %d", signo); 255b6193c24SMikolaj Golub return (-1); 2562ad43027SMikolaj Golub } 2572ad43027SMikolaj Golub } 2582ad43027SMikolaj Golub } 2592ad43027SMikolaj Golub 2602ad43027SMikolaj Golub static void 261bd06a3ecSMike Barcroft usage(void) 262bd06a3ecSMike Barcroft { 263846be7bdSPoul-Henning Kamp (void)fprintf(stderr, 264*32b17786SJohn-Mark Gurney "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " 265*32b17786SJohn-Mark Gurney "[-u user]\n command arguments ...\n"); 266bd06a3ecSMike Barcroft exit(1); 267bd06a3ecSMike Barcroft } 268