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; 589da0ef13SMikolaj Golub int ch, nochdir, noclose, restart, serrno; 59*112bfcf5SConrad Meyer const char *pidfile, *ppidfile, *title, *user; 602ad43027SMikolaj Golub pid_t otherpid, pid; 61bd06a3ecSMike Barcroft 62bd06a3ecSMike Barcroft nochdir = noclose = 1; 63b6193c24SMikolaj Golub restart = 0; 64*112bfcf5SConrad Meyer ppidfile = pidfile = title = user = NULL; 65*112bfcf5SConrad Meyer while ((ch = getopt(argc, argv, "cfp:P:rt:u:")) != -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; 82*112bfcf5SConrad Meyer case 't': 83*112bfcf5SConrad Meyer title = optarg; 84*112bfcf5SConrad Meyer break; 85e6d4b388STom Rhodes case 'u': 86e6d4b388STom Rhodes user = optarg; 87e6d4b388STom Rhodes break; 88bd06a3ecSMike Barcroft default: 89bd06a3ecSMike Barcroft usage(); 90bd06a3ecSMike Barcroft } 91bd06a3ecSMike Barcroft } 92bd06a3ecSMike Barcroft argc -= optind; 93bd06a3ecSMike Barcroft argv += optind; 94bd06a3ecSMike Barcroft 95bd06a3ecSMike Barcroft if (argc == 0) 96bd06a3ecSMike Barcroft usage(); 9712d7249eSTom Rhodes 9832b17786SJohn-Mark Gurney ppfh = pfh = NULL; 99846be7bdSPoul-Henning Kamp /* 100846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 101846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 102846be7bdSPoul-Henning Kamp */ 1032ad43027SMikolaj Golub if (pidfile != NULL) { 104c6262cb6SPawel Jakub Dawidek pfh = pidfile_open(pidfile, 0600, &otherpid); 105c6262cb6SPawel Jakub Dawidek if (pfh == NULL) { 106c6262cb6SPawel Jakub Dawidek if (errno == EEXIST) { 107c6262cb6SPawel Jakub Dawidek errx(3, "process already running, pid: %d", 108c6262cb6SPawel Jakub Dawidek otherpid); 109c6262cb6SPawel Jakub Dawidek } 110846be7bdSPoul-Henning Kamp err(2, "pidfile ``%s''", pidfile); 111846be7bdSPoul-Henning Kamp } 112c6262cb6SPawel Jakub Dawidek } 1139da0ef13SMikolaj Golub /* Do the same for actual daemon process. */ 11432b17786SJohn-Mark Gurney if (ppidfile != NULL) { 11532b17786SJohn-Mark Gurney ppfh = pidfile_open(ppidfile, 0600, &otherpid); 11632b17786SJohn-Mark Gurney if (ppfh == NULL) { 1179da0ef13SMikolaj Golub serrno = errno; 1189da0ef13SMikolaj Golub pidfile_remove(pfh); 1199da0ef13SMikolaj Golub errno = serrno; 12032b17786SJohn-Mark Gurney if (errno == EEXIST) { 12132b17786SJohn-Mark Gurney errx(3, "process already running, pid: %d", 12232b17786SJohn-Mark Gurney otherpid); 12332b17786SJohn-Mark Gurney } 12432b17786SJohn-Mark Gurney err(2, "ppidfile ``%s''", ppidfile); 12532b17786SJohn-Mark Gurney } 12632b17786SJohn-Mark Gurney } 12732b17786SJohn-Mark Gurney 1289da0ef13SMikolaj Golub if (daemon(nochdir, noclose) == -1) { 1299da0ef13SMikolaj Golub warn("daemon"); 1309da0ef13SMikolaj Golub goto exit; 1319da0ef13SMikolaj Golub } 1329da0ef13SMikolaj Golub /* Write out parent pidfile if needed. */ 1339da0ef13SMikolaj Golub pidfile_write(ppfh); 134846be7bdSPoul-Henning Kamp 135195fc497SMikolaj Golub /* 136b6193c24SMikolaj Golub * If the pidfile or restart option is specified the daemon 137b6193c24SMikolaj Golub * executes the command in a forked process and wait on child 138b6193c24SMikolaj Golub * exit to remove the pidfile or restart the command. Normally 139b6193c24SMikolaj Golub * we don't want the monitoring daemon to be terminated 140b6193c24SMikolaj Golub * leaving the running process and the stale pidfile, so we 141b6193c24SMikolaj Golub * catch SIGTERM and forward it to the children expecting to 142b6193c24SMikolaj Golub * get SIGCHLD eventually. 143195fc497SMikolaj Golub */ 144195fc497SMikolaj Golub pid = -1; 145ceda7f06SJaakko Heinonen if (pidfile != NULL || ppidfile != NULL || restart) { 1462ad43027SMikolaj Golub /* 147195fc497SMikolaj Golub * Restore default action for SIGTERM in case the 148195fc497SMikolaj Golub * parent process decided to ignore it. 149195fc497SMikolaj Golub */ 1509da0ef13SMikolaj Golub if (signal(SIGTERM, SIG_DFL) == SIG_ERR) { 1519da0ef13SMikolaj Golub warn("signal"); 1529da0ef13SMikolaj Golub goto exit; 1539da0ef13SMikolaj Golub } 154195fc497SMikolaj Golub /* 155195fc497SMikolaj Golub * Because SIGCHLD is ignored by default, setup dummy handler 156195fc497SMikolaj Golub * for it, so we can mask it. 157195fc497SMikolaj Golub */ 1589da0ef13SMikolaj Golub if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) { 1599da0ef13SMikolaj Golub warn("signal"); 1609da0ef13SMikolaj Golub goto exit; 1619da0ef13SMikolaj Golub } 162195fc497SMikolaj Golub /* 163195fc497SMikolaj Golub * Block interesting signals. 164195fc497SMikolaj Golub */ 165195fc497SMikolaj Golub sigemptyset(&mask); 166195fc497SMikolaj Golub sigaddset(&mask, SIGTERM); 167195fc497SMikolaj Golub sigaddset(&mask, SIGCHLD); 1689da0ef13SMikolaj Golub if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) { 1699da0ef13SMikolaj Golub warn("sigprocmask"); 1709da0ef13SMikolaj Golub goto exit; 1719da0ef13SMikolaj Golub } 17253c49998SMikolaj Golub /* 17353c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 17453c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 17553c49998SMikolaj Golub * not have superuser privileges. 17653c49998SMikolaj Golub */ 17753c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 178b6193c24SMikolaj Golub restart: 179195fc497SMikolaj Golub /* 1802ad43027SMikolaj Golub * Spawn a child to exec the command, so in the parent 1812ad43027SMikolaj Golub * we could wait for it to exit and remove pidfile. 1822ad43027SMikolaj Golub */ 1832ad43027SMikolaj Golub pid = fork(); 1842ad43027SMikolaj Golub if (pid == -1) { 1859da0ef13SMikolaj Golub warn("fork"); 1869da0ef13SMikolaj Golub goto exit; 1872ad43027SMikolaj Golub } 1882ad43027SMikolaj Golub } 189195fc497SMikolaj Golub if (pid <= 0) { 1902ad43027SMikolaj Golub if (pid == 0) { 191195fc497SMikolaj Golub /* Restore old sigmask in the child. */ 192195fc497SMikolaj Golub if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 193195fc497SMikolaj Golub err(1, "sigprocmask"); 194195fc497SMikolaj Golub } 1952ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 196c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 197846be7bdSPoul-Henning Kamp 1982ad43027SMikolaj Golub if (user != NULL) 1992ad43027SMikolaj Golub restrict_process(user); 2002ad43027SMikolaj Golub 201bd06a3ecSMike Barcroft execvp(argv[0], argv); 202bd06a3ecSMike Barcroft 203846be7bdSPoul-Henning Kamp /* 2042ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 2052ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 206846be7bdSPoul-Henning Kamp */ 2072ad43027SMikolaj Golub err(1, "%s", argv[0]); 2082ad43027SMikolaj Golub } 20932b17786SJohn-Mark Gurney 210*112bfcf5SConrad Meyer if (title != NULL) 211*112bfcf5SConrad Meyer setproctitle("%s[%d]", title, pid); 212*112bfcf5SConrad Meyer else 2132ad43027SMikolaj Golub setproctitle("%s[%d]", argv[0], pid); 214b6193c24SMikolaj Golub if (wait_child(pid, &mask) == 0 && restart) { 215b6193c24SMikolaj Golub sleep(1); 216b6193c24SMikolaj Golub goto restart; 217b6193c24SMikolaj Golub } 2189da0ef13SMikolaj Golub exit: 219c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 22032b17786SJohn-Mark Gurney pidfile_remove(ppfh); 2219da0ef13SMikolaj Golub exit(1); /* If daemon(3) succeeded exit status does not matter. */ 222bd06a3ecSMike Barcroft } 223bd06a3ecSMike Barcroft 224bd06a3ecSMike Barcroft static void 225195fc497SMikolaj Golub dummy_sighandler(int sig __unused) 226195fc497SMikolaj Golub { 227195fc497SMikolaj Golub /* Nothing to do. */ 228195fc497SMikolaj Golub } 229195fc497SMikolaj Golub 230195fc497SMikolaj Golub static void 231e6d4b388STom Rhodes restrict_process(const char *user) 23212d7249eSTom Rhodes { 23312d7249eSTom Rhodes struct passwd *pw = NULL; 23412d7249eSTom Rhodes 235e6d4b388STom Rhodes pw = getpwnam(user); 236e6d4b388STom Rhodes if (pw == NULL) 237e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 23812d7249eSTom Rhodes 239e6d4b388STom Rhodes if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 240e6d4b388STom Rhodes errx(1, "failed to set user environment"); 24112d7249eSTom Rhodes } 24212d7249eSTom Rhodes 243b6193c24SMikolaj Golub static int 244195fc497SMikolaj Golub wait_child(pid_t pid, sigset_t *mask) 2452ad43027SMikolaj Golub { 246b6193c24SMikolaj Golub int terminate, signo; 2472ad43027SMikolaj Golub 248b6193c24SMikolaj Golub terminate = 0; 249195fc497SMikolaj Golub for (;;) { 250195fc497SMikolaj Golub if (sigwait(mask, &signo) == -1) { 251195fc497SMikolaj Golub warn("sigwaitinfo"); 252b6193c24SMikolaj Golub return (-1); 253195fc497SMikolaj Golub } 254195fc497SMikolaj Golub switch (signo) { 255195fc497SMikolaj Golub case SIGCHLD: 256c60d51f9SMikolaj Golub if (waitpid(pid, NULL, WNOHANG) == -1) { 257c60d51f9SMikolaj Golub warn("waitpid"); 258c60d51f9SMikolaj Golub return (-1); 259c60d51f9SMikolaj Golub } 260b6193c24SMikolaj Golub return (terminate); 261195fc497SMikolaj Golub case SIGTERM: 262b6193c24SMikolaj Golub terminate = 1; 263195fc497SMikolaj Golub if (kill(pid, signo) == -1) { 264195fc497SMikolaj Golub warn("kill"); 265b6193c24SMikolaj Golub return (-1); 266195fc497SMikolaj Golub } 267195fc497SMikolaj Golub continue; 268195fc497SMikolaj Golub default: 269195fc497SMikolaj Golub warnx("sigwaitinfo: invalid signal: %d", signo); 270b6193c24SMikolaj Golub return (-1); 2712ad43027SMikolaj Golub } 2722ad43027SMikolaj Golub } 2732ad43027SMikolaj Golub } 2742ad43027SMikolaj Golub 2752ad43027SMikolaj Golub static void 276bd06a3ecSMike Barcroft usage(void) 277bd06a3ecSMike Barcroft { 278846be7bdSPoul-Henning Kamp (void)fprintf(stderr, 27932b17786SJohn-Mark Gurney "usage: daemon [-cfr] [-p child_pidfile] [-P supervisor_pidfile] " 28032b17786SJohn-Mark Gurney "[-u user]\n command arguments ...\n"); 281bd06a3ecSMike Barcroft exit(1); 282bd06a3ecSMike Barcroft } 283