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> 352ad43027SMikolaj Golub #include <sys/wait.h> 36bd06a3ecSMike Barcroft 37bd06a3ecSMike Barcroft #include <err.h> 38846be7bdSPoul-Henning Kamp #include <errno.h> 39c6262cb6SPawel Jakub Dawidek #include <libutil.h> 40e6d4b388STom Rhodes #include <login_cap.h> 41195fc497SMikolaj Golub #include <pwd.h> 42195fc497SMikolaj Golub #include <signal.h> 43bd06a3ecSMike Barcroft #include <stdio.h> 44bd06a3ecSMike Barcroft #include <stdlib.h> 45bd06a3ecSMike Barcroft #include <unistd.h> 46bd06a3ecSMike Barcroft 47195fc497SMikolaj Golub static void dummy_sighandler(int); 48e6d4b388STom Rhodes static void restrict_process(const char *); 49*b6193c24SMikolaj Golub static int wait_child(pid_t pid, sigset_t *mask); 50bd06a3ecSMike Barcroft static void usage(void); 51bd06a3ecSMike Barcroft 52bd06a3ecSMike Barcroft int 53bd06a3ecSMike Barcroft main(int argc, char *argv[]) 54bd06a3ecSMike Barcroft { 5515543aadSTom Rhodes struct pidfh *pfh = NULL; 56195fc497SMikolaj Golub sigset_t mask, oldmask; 57*b6193c24SMikolaj Golub int ch, nochdir, noclose, restart; 58e6d4b388STom Rhodes const char *pidfile, *user; 592ad43027SMikolaj Golub pid_t otherpid, pid; 60bd06a3ecSMike Barcroft 61bd06a3ecSMike Barcroft nochdir = noclose = 1; 62*b6193c24SMikolaj Golub restart = 0; 63e6d4b388STom Rhodes pidfile = user = NULL; 64*b6193c24SMikolaj Golub while ((ch = getopt(argc, argv, "-cfp:ru:")) != -1) { 65bd06a3ecSMike Barcroft switch (ch) { 66bd06a3ecSMike Barcroft case 'c': 67bd06a3ecSMike Barcroft nochdir = 0; 68bd06a3ecSMike Barcroft break; 69bd06a3ecSMike Barcroft case 'f': 70bd06a3ecSMike Barcroft noclose = 0; 71bd06a3ecSMike Barcroft break; 72846be7bdSPoul-Henning Kamp case 'p': 73846be7bdSPoul-Henning Kamp pidfile = optarg; 74846be7bdSPoul-Henning Kamp break; 75*b6193c24SMikolaj Golub case 'r': 76*b6193c24SMikolaj Golub restart = 1; 77*b6193c24SMikolaj Golub break; 78e6d4b388STom Rhodes case 'u': 79e6d4b388STom Rhodes user = optarg; 80e6d4b388STom Rhodes break; 81bd06a3ecSMike Barcroft default: 82bd06a3ecSMike Barcroft usage(); 83bd06a3ecSMike Barcroft } 84bd06a3ecSMike Barcroft } 85bd06a3ecSMike Barcroft argc -= optind; 86bd06a3ecSMike Barcroft argv += optind; 87bd06a3ecSMike Barcroft 88bd06a3ecSMike Barcroft if (argc == 0) 89bd06a3ecSMike Barcroft usage(); 9012d7249eSTom Rhodes 912ad43027SMikolaj Golub pfh = NULL; 92846be7bdSPoul-Henning Kamp /* 93846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 94846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 95846be7bdSPoul-Henning Kamp */ 962ad43027SMikolaj Golub if (pidfile != NULL) { 97c6262cb6SPawel Jakub Dawidek pfh = pidfile_open(pidfile, 0600, &otherpid); 98c6262cb6SPawel Jakub Dawidek if (pfh == NULL) { 99c6262cb6SPawel Jakub Dawidek if (errno == EEXIST) { 100c6262cb6SPawel Jakub Dawidek errx(3, "process already running, pid: %d", 101c6262cb6SPawel Jakub Dawidek otherpid); 102c6262cb6SPawel Jakub Dawidek } 103846be7bdSPoul-Henning Kamp err(2, "pidfile ``%s''", pidfile); 104846be7bdSPoul-Henning Kamp } 105c6262cb6SPawel Jakub Dawidek } 106846be7bdSPoul-Henning Kamp 107bd06a3ecSMike Barcroft if (daemon(nochdir, noclose) == -1) 108bd06a3ecSMike Barcroft err(1, NULL); 109846be7bdSPoul-Henning Kamp 110195fc497SMikolaj Golub /* 111*b6193c24SMikolaj Golub * If the pidfile or restart option is specified the daemon 112*b6193c24SMikolaj Golub * executes the command in a forked process and wait on child 113*b6193c24SMikolaj Golub * exit to remove the pidfile or restart the command. Normally 114*b6193c24SMikolaj Golub * we don't want the monitoring daemon to be terminated 115*b6193c24SMikolaj Golub * leaving the running process and the stale pidfile, so we 116*b6193c24SMikolaj Golub * catch SIGTERM and forward it to the children expecting to 117*b6193c24SMikolaj Golub * get SIGCHLD eventually. 118195fc497SMikolaj Golub */ 119195fc497SMikolaj Golub pid = -1; 120*b6193c24SMikolaj Golub if (pidfile != NULL || restart) { 1212ad43027SMikolaj Golub /* 122195fc497SMikolaj Golub * Restore default action for SIGTERM in case the 123195fc497SMikolaj Golub * parent process decided to ignore it. 124195fc497SMikolaj Golub */ 125195fc497SMikolaj Golub if (signal(SIGTERM, SIG_DFL) == SIG_ERR) 126195fc497SMikolaj Golub err(1, "signal"); 127195fc497SMikolaj Golub /* 128195fc497SMikolaj Golub * Because SIGCHLD is ignored by default, setup dummy handler 129195fc497SMikolaj Golub * for it, so we can mask it. 130195fc497SMikolaj Golub */ 131195fc497SMikolaj Golub if (signal(SIGCHLD, dummy_sighandler) == SIG_ERR) 132195fc497SMikolaj Golub err(1, "signal"); 133195fc497SMikolaj Golub /* 134195fc497SMikolaj Golub * Block interesting signals. 135195fc497SMikolaj Golub */ 136195fc497SMikolaj Golub sigemptyset(&mask); 137195fc497SMikolaj Golub sigaddset(&mask, SIGTERM); 138195fc497SMikolaj Golub sigaddset(&mask, SIGCHLD); 139195fc497SMikolaj Golub if (sigprocmask(SIG_SETMASK, &mask, &oldmask) == -1) 140195fc497SMikolaj Golub err(1, "sigprocmask"); 141*b6193c24SMikolaj Golub restart: 142195fc497SMikolaj Golub /* 1432ad43027SMikolaj Golub * Spawn a child to exec the command, so in the parent 1442ad43027SMikolaj Golub * we could wait for it to exit and remove pidfile. 1452ad43027SMikolaj Golub */ 1462ad43027SMikolaj Golub pid = fork(); 1472ad43027SMikolaj Golub if (pid == -1) { 1482ad43027SMikolaj Golub pidfile_remove(pfh); 1492ad43027SMikolaj Golub err(1, "fork"); 1502ad43027SMikolaj Golub } 1512ad43027SMikolaj Golub } 152195fc497SMikolaj Golub if (pid <= 0) { 1532ad43027SMikolaj Golub if (pid == 0) { 154195fc497SMikolaj Golub /* Restore old sigmask in the child. */ 155195fc497SMikolaj Golub if (sigprocmask(SIG_SETMASK, &oldmask, NULL) == -1) 156195fc497SMikolaj Golub err(1, "sigprocmask"); 157195fc497SMikolaj Golub } 1582ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 159c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 160846be7bdSPoul-Henning Kamp 1612ad43027SMikolaj Golub if (user != NULL) 1622ad43027SMikolaj Golub restrict_process(user); 1632ad43027SMikolaj Golub 164bd06a3ecSMike Barcroft execvp(argv[0], argv); 165bd06a3ecSMike Barcroft 166846be7bdSPoul-Henning Kamp /* 1672ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 1682ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 169846be7bdSPoul-Henning Kamp */ 1702ad43027SMikolaj Golub err(1, "%s", argv[0]); 1712ad43027SMikolaj Golub } 1722ad43027SMikolaj Golub setproctitle("%s[%d]", argv[0], pid); 173*b6193c24SMikolaj Golub if (wait_child(pid, &mask) == 0 && restart) { 174*b6193c24SMikolaj Golub sleep(1); 175*b6193c24SMikolaj Golub goto restart; 176*b6193c24SMikolaj Golub } 177c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 1782ad43027SMikolaj Golub exit(0); /* Exit status does not matter. */ 179bd06a3ecSMike Barcroft } 180bd06a3ecSMike Barcroft 181bd06a3ecSMike Barcroft static void 182195fc497SMikolaj Golub dummy_sighandler(int sig __unused) 183195fc497SMikolaj Golub { 184195fc497SMikolaj Golub /* Nothing to do. */ 185195fc497SMikolaj Golub } 186195fc497SMikolaj Golub 187195fc497SMikolaj Golub static void 188e6d4b388STom Rhodes restrict_process(const char *user) 18912d7249eSTom Rhodes { 19012d7249eSTom Rhodes struct passwd *pw = NULL; 19112d7249eSTom Rhodes 192e6d4b388STom Rhodes pw = getpwnam(user); 193e6d4b388STom Rhodes if (pw == NULL) 194e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 19512d7249eSTom Rhodes 196e6d4b388STom Rhodes if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) 197e6d4b388STom Rhodes errx(1, "failed to set user environment"); 19812d7249eSTom Rhodes } 19912d7249eSTom Rhodes 200*b6193c24SMikolaj Golub static int 201195fc497SMikolaj Golub wait_child(pid_t pid, sigset_t *mask) 2022ad43027SMikolaj Golub { 203*b6193c24SMikolaj Golub int terminate, signo; 2042ad43027SMikolaj Golub 205*b6193c24SMikolaj Golub terminate = 0; 206195fc497SMikolaj Golub for (;;) { 207195fc497SMikolaj Golub if (sigwait(mask, &signo) == -1) { 208195fc497SMikolaj Golub warn("sigwaitinfo"); 209*b6193c24SMikolaj Golub return (-1); 210195fc497SMikolaj Golub } 211195fc497SMikolaj Golub switch (signo) { 212195fc497SMikolaj Golub case SIGCHLD: 213*b6193c24SMikolaj Golub return (terminate); 214195fc497SMikolaj Golub case SIGTERM: 215*b6193c24SMikolaj Golub terminate = 1; 216195fc497SMikolaj Golub if (kill(pid, signo) == -1) { 217195fc497SMikolaj Golub warn("kill"); 218*b6193c24SMikolaj Golub return (-1); 219195fc497SMikolaj Golub } 220195fc497SMikolaj Golub continue; 221195fc497SMikolaj Golub default: 222195fc497SMikolaj Golub warnx("sigwaitinfo: invalid signal: %d", signo); 223*b6193c24SMikolaj Golub return (-1); 2242ad43027SMikolaj Golub } 2252ad43027SMikolaj Golub } 2262ad43027SMikolaj Golub } 2272ad43027SMikolaj Golub 2282ad43027SMikolaj Golub static void 229bd06a3ecSMike Barcroft usage(void) 230bd06a3ecSMike Barcroft { 231846be7bdSPoul-Henning Kamp (void)fprintf(stderr, 232e6d4b388STom Rhodes "usage: daemon [-cf] [-p pidfile] [-u user] command " 23312d7249eSTom Rhodes "arguments ...\n"); 234bd06a3ecSMike Barcroft exit(1); 235bd06a3ecSMike Barcroft } 236