1bd06a3ecSMike Barcroft /*- 21de7b4b8SPedro F. Giffuni * SPDX-License-Identifier: BSD-3-Clause 31de7b4b8SPedro F. Giffuni * 4bd06a3ecSMike Barcroft * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved. 5bd06a3ecSMike Barcroft * 6bd06a3ecSMike Barcroft * Redistribution and use in source and binary forms, with or without 7bd06a3ecSMike Barcroft * modification, are permitted provided that the following conditions 8bd06a3ecSMike Barcroft * are met: 9bd06a3ecSMike Barcroft * 1. Redistributions of source code must retain the above copyright 10bd06a3ecSMike Barcroft * notice, this list of conditions and the following disclaimer. 11bd06a3ecSMike Barcroft * 2. Redistributions in binary form must reproduce the above copyright 12bd06a3ecSMike Barcroft * notice, this list of conditions and the following disclaimer in the 13bd06a3ecSMike Barcroft * documentation and/or other materials provided with the distribution. 14bd06a3ecSMike Barcroft * 3. Berkeley Software Design Inc's name may not be used to endorse or 15bd06a3ecSMike Barcroft * promote products derived from this software without specific prior 16bd06a3ecSMike Barcroft * written permission. 17bd06a3ecSMike Barcroft * 18bd06a3ecSMike Barcroft * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND 19bd06a3ecSMike Barcroft * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 20bd06a3ecSMike Barcroft * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 21bd06a3ecSMike Barcroft * ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE 22bd06a3ecSMike Barcroft * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 23bd06a3ecSMike Barcroft * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 24bd06a3ecSMike Barcroft * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25bd06a3ecSMike Barcroft * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 26bd06a3ecSMike Barcroft * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 27bd06a3ecSMike Barcroft * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 28bd06a3ecSMike Barcroft * SUCH DAMAGE. 29bd06a3ecSMike Barcroft * 30bd06a3ecSMike Barcroft * From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp 31bd06a3ecSMike Barcroft */ 32bd06a3ecSMike Barcroft 3354ede02dSPhilippe Charnier #include <sys/cdefs.h> 3454ede02dSPhilippe Charnier __FBSDID("$FreeBSD$"); 3554ede02dSPhilippe Charnier 36c6262cb6SPawel Jakub Dawidek #include <sys/param.h> 3753c49998SMikolaj Golub #include <sys/mman.h> 382ad43027SMikolaj Golub #include <sys/wait.h> 39bd06a3ecSMike Barcroft 4053d49b37SJilles Tjoelker #include <fcntl.h> 41bd06a3ecSMike Barcroft #include <err.h> 42846be7bdSPoul-Henning Kamp #include <errno.h> 430a402ad2SIhor Antonov #include <getopt.h> 44c6262cb6SPawel Jakub Dawidek #include <libutil.h> 45e6d4b388STom Rhodes #include <login_cap.h> 466b3ad1d7SMaxim Sobolev #include <paths.h> 47195fc497SMikolaj Golub #include <pwd.h> 48195fc497SMikolaj Golub #include <signal.h> 49bd06a3ecSMike Barcroft #include <stdio.h> 50bd06a3ecSMike Barcroft #include <stdlib.h> 51bd06a3ecSMike Barcroft #include <unistd.h> 5253d49b37SJilles Tjoelker #include <string.h> 5353d49b37SJilles Tjoelker #include <strings.h> 5453d49b37SJilles Tjoelker #define SYSLOG_NAMES 5553d49b37SJilles Tjoelker #include <syslog.h> 5653d49b37SJilles Tjoelker #include <time.h> 5753d49b37SJilles Tjoelker #include <assert.h> 58bd06a3ecSMike Barcroft 5953d49b37SJilles Tjoelker #define LBUF_SIZE 4096 6053d49b37SJilles Tjoelker 6153d49b37SJilles Tjoelker struct log_params { 6253d49b37SJilles Tjoelker int dosyslog; 6353d49b37SJilles Tjoelker int logpri; 6453d49b37SJilles Tjoelker int noclose; 6553d49b37SJilles Tjoelker int outfd; 664cd407ecSMaxim Sobolev const char *outfn; 6753d49b37SJilles Tjoelker }; 6853d49b37SJilles Tjoelker 69e6d4b388STom Rhodes static void restrict_process(const char *); 7053d49b37SJilles Tjoelker static void handle_term(int); 7153d49b37SJilles Tjoelker static void handle_chld(int); 724cd407ecSMaxim Sobolev static void handle_hup(int); 734cd407ecSMaxim Sobolev static int open_log(const char *); 744cd407ecSMaxim Sobolev static void reopen_log(struct log_params *); 7553d49b37SJilles Tjoelker static int listen_child(int, struct log_params *); 7653d49b37SJilles Tjoelker static int get_log_mapping(const char *, const CODE *); 7753d49b37SJilles Tjoelker static void open_pid_files(const char *, const char *, struct pidfh **, 7853d49b37SJilles Tjoelker struct pidfh **); 7953d49b37SJilles Tjoelker static void do_output(const unsigned char *, size_t, struct log_params *); 8053d49b37SJilles Tjoelker static void daemon_sleep(time_t, long); 81bd06a3ecSMike Barcroft 82*e745dc22SIhor Antonov static volatile sig_atomic_t terminate = 0; 83*e745dc22SIhor Antonov static volatile sig_atomic_t child_gone = 0; 84*e745dc22SIhor Antonov static volatile sig_atomic_t pid = 0; 85*e745dc22SIhor Antonov static volatile sig_atomic_t do_log_reopen = 0; 8653d49b37SJilles Tjoelker 870a402ad2SIhor Antonov static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; 880a402ad2SIhor Antonov 890a402ad2SIhor Antonov static const struct option longopts[] = { 900a402ad2SIhor Antonov { "change-dir", no_argument, NULL, 'c' }, 910a402ad2SIhor Antonov { "close-fds", no_argument, NULL, 'f' }, 920a402ad2SIhor Antonov { "sighup", no_argument, NULL, 'H' }, 930a402ad2SIhor Antonov { "syslog", no_argument, NULL, 'S' }, 940a402ad2SIhor Antonov { "output-file", required_argument, NULL, 'o' }, 950a402ad2SIhor Antonov { "output-mask", required_argument, NULL, 'm' }, 960a402ad2SIhor Antonov { "child-pidfile", required_argument, NULL, 'p' }, 970a402ad2SIhor Antonov { "supervisor-pidfile", required_argument, NULL, 'P' }, 980a402ad2SIhor Antonov { "restart", no_argument, NULL, 'r' }, 990a402ad2SIhor Antonov { "restart-delay", required_argument, NULL, 'R' }, 1000a402ad2SIhor Antonov { "title", required_argument, NULL, 't' }, 1010a402ad2SIhor Antonov { "user", required_argument, NULL, 'u' }, 1020a402ad2SIhor Antonov { "syslog-priority", required_argument, NULL, 's' }, 1030a402ad2SIhor Antonov { "syslog-facility", required_argument, NULL, 'l' }, 1040a402ad2SIhor Antonov { "syslog-tag", required_argument, NULL, 'T' }, 1050a402ad2SIhor Antonov { "help", no_argument, NULL, 'h' }, 1060a402ad2SIhor Antonov { NULL, 0, NULL, 0 } 1070a402ad2SIhor Antonov }; 1080a402ad2SIhor Antonov 1090a402ad2SIhor Antonov static _Noreturn void 1100a402ad2SIhor Antonov usage(int exitcode) 1110a402ad2SIhor Antonov { 1120a402ad2SIhor Antonov (void)fprintf(stderr, 1130a402ad2SIhor Antonov "usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n" 1140a402ad2SIhor Antonov " [-u user] [-o output_file] [-t title]\n" 1150a402ad2SIhor Antonov " [-l syslog_facility] [-s syslog_priority]\n" 1160a402ad2SIhor Antonov " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" 1170a402ad2SIhor Antonov "command arguments ...\n"); 1180a402ad2SIhor Antonov 1190a402ad2SIhor Antonov (void)fprintf(stderr, 1200a402ad2SIhor Antonov " --change-dir -c Change the current working directory to root\n" 1210a402ad2SIhor Antonov " --close-fds -f Set stdin, stdout, stderr to /dev/null\n" 1220a402ad2SIhor Antonov " --sighup -H Close and re-open output file on SIGHUP\n" 1230a402ad2SIhor Antonov " --syslog -S Send output to syslog\n" 1240a402ad2SIhor Antonov " --output-file -o <file> Append output of the child process to file\n" 1250a402ad2SIhor Antonov " --output-mask -m <mask> What to send to syslog/file\n" 1260a402ad2SIhor Antonov " 1=stdout, 2=stderr, 3=both\n" 1270a402ad2SIhor Antonov " --child-pidfile -p <file> Write PID of the child process to file\n" 1280a402ad2SIhor Antonov " --supervisor-pidfile -P <file> Write PID of the supervisor process to file\n" 1290a402ad2SIhor Antonov " --restart -r Restart child if it terminates (1 sec delay)\n" 1300a402ad2SIhor Antonov " --restart-delay -R <N> Restart child if it terminates after N sec\n" 1310a402ad2SIhor Antonov " --title -t <title> Set the title of the supervisor process\n" 1320a402ad2SIhor Antonov " --user -u <user> Drop privileges, run as given user\n" 1330a402ad2SIhor Antonov " --syslog-priority -s <prio> Set syslog priority\n" 1340a402ad2SIhor Antonov " --syslog-facility -l <flty> Set syslog facility\n" 1350a402ad2SIhor Antonov " --syslog-tag -T <tag> Set syslog tag\n" 1360a402ad2SIhor Antonov " --help -h Show this help\n"); 1370a402ad2SIhor Antonov 1380a402ad2SIhor Antonov exit(exitcode); 1390a402ad2SIhor Antonov } 1400a402ad2SIhor Antonov 141bd06a3ecSMike Barcroft int 142bd06a3ecSMike Barcroft main(int argc, char *argv[]) 143bd06a3ecSMike Barcroft { 144*e745dc22SIhor Antonov char *p = NULL; 145*e745dc22SIhor Antonov const char *pidfile = NULL; 146*e745dc22SIhor Antonov const char *logtag = "daemon"; 147*e745dc22SIhor Antonov const char *outfn = NULL; 148*e745dc22SIhor Antonov const char *ppidfile = NULL; 149*e745dc22SIhor Antonov const char *title = NULL; 150*e745dc22SIhor Antonov const char *user = NULL; 151*e745dc22SIhor Antonov int ch = 0; 152*e745dc22SIhor Antonov int child_eof = 0; 153*e745dc22SIhor Antonov int dosyslog = 0; 154*e745dc22SIhor Antonov int log_reopen = 0; 155*e745dc22SIhor Antonov int logfac = LOG_DAEMON; 156*e745dc22SIhor Antonov int logpri = LOG_NOTICE; 157*e745dc22SIhor Antonov int nochdir = 1; 158*e745dc22SIhor Antonov int noclose = 1; 159*e745dc22SIhor Antonov int outfd = -1; 160*e745dc22SIhor Antonov int pfd[2] = { -1, -1 }; 161*e745dc22SIhor Antonov int restart = 0; 162*e745dc22SIhor Antonov int stdmask = STDOUT_FILENO | STDERR_FILENO; 163*e745dc22SIhor Antonov struct log_params logpar = { 0 }; 164*e745dc22SIhor Antonov struct pidfh *ppfh = NULL; 165*e745dc22SIhor Antonov struct pidfh *pfh = NULL; 166*e745dc22SIhor Antonov sigset_t mask_orig; 167*e745dc22SIhor Antonov sigset_t mask_read; 168*e745dc22SIhor Antonov sigset_t mask_term; 169*e745dc22SIhor Antonov sigset_t mask_susp; 170bd06a3ecSMike Barcroft 171*e745dc22SIhor Antonov sigemptyset(&mask_susp); 172*e745dc22SIhor Antonov sigemptyset(&mask_read); 173*e745dc22SIhor Antonov sigemptyset(&mask_term); 174*e745dc22SIhor Antonov 1750a402ad2SIhor Antonov while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { 176bd06a3ecSMike Barcroft switch (ch) { 177bd06a3ecSMike Barcroft case 'c': 178bd06a3ecSMike Barcroft nochdir = 0; 179bd06a3ecSMike Barcroft break; 180bd06a3ecSMike Barcroft case 'f': 181bd06a3ecSMike Barcroft noclose = 0; 182bd06a3ecSMike Barcroft break; 1834cd407ecSMaxim Sobolev case 'H': 1844cd407ecSMaxim Sobolev log_reopen = 1; 1854cd407ecSMaxim Sobolev break; 18653d49b37SJilles Tjoelker case 'l': 18753d49b37SJilles Tjoelker logfac = get_log_mapping(optarg, facilitynames); 1886b4ef4b1SIhor Antonov if (logfac == -1) { 18953d49b37SJilles Tjoelker errx(5, "unrecognized syslog facility"); 1906b4ef4b1SIhor Antonov } 19153d49b37SJilles Tjoelker dosyslog = 1; 19253d49b37SJilles Tjoelker break; 19353d49b37SJilles Tjoelker case 'm': 19453d49b37SJilles Tjoelker stdmask = strtol(optarg, &p, 10); 1956b4ef4b1SIhor Antonov if (p == optarg || stdmask < 0 || stdmask > 3) { 19653d49b37SJilles Tjoelker errx(6, "unrecognized listening mask"); 1976b4ef4b1SIhor Antonov } 19853d49b37SJilles Tjoelker break; 19953d49b37SJilles Tjoelker case 'o': 20053d49b37SJilles Tjoelker outfn = optarg; 20153d49b37SJilles Tjoelker break; 202846be7bdSPoul-Henning Kamp case 'p': 203846be7bdSPoul-Henning Kamp pidfile = optarg; 204846be7bdSPoul-Henning Kamp break; 20532b17786SJohn-Mark Gurney case 'P': 20632b17786SJohn-Mark Gurney ppidfile = optarg; 20732b17786SJohn-Mark Gurney break; 208b6193c24SMikolaj Golub case 'r': 209b6193c24SMikolaj Golub restart = 1; 210b6193c24SMikolaj Golub break; 21137820b87SIan Lepore case 'R': 21237820b87SIan Lepore restart = strtol(optarg, &p, 0); 2136b4ef4b1SIhor Antonov if (p == optarg || restart < 1) { 21437820b87SIan Lepore errx(6, "invalid restart delay"); 2156b4ef4b1SIhor Antonov } 21637820b87SIan Lepore break; 21753d49b37SJilles Tjoelker case 's': 21853d49b37SJilles Tjoelker logpri = get_log_mapping(optarg, prioritynames); 2196b4ef4b1SIhor Antonov if (logpri == -1) { 22053d49b37SJilles Tjoelker errx(4, "unrecognized syslog priority"); 2216b4ef4b1SIhor Antonov } 22253d49b37SJilles Tjoelker dosyslog = 1; 22353d49b37SJilles Tjoelker break; 22453d49b37SJilles Tjoelker case 'S': 22553d49b37SJilles Tjoelker dosyslog = 1; 22653d49b37SJilles Tjoelker break; 227112bfcf5SConrad Meyer case 't': 228112bfcf5SConrad Meyer title = optarg; 229112bfcf5SConrad Meyer break; 23053d49b37SJilles Tjoelker case 'T': 23153d49b37SJilles Tjoelker logtag = optarg; 23253d49b37SJilles Tjoelker dosyslog = 1; 23353d49b37SJilles Tjoelker break; 234e6d4b388STom Rhodes case 'u': 235e6d4b388STom Rhodes user = optarg; 236e6d4b388STom Rhodes break; 2370a402ad2SIhor Antonov case 'h': 2380a402ad2SIhor Antonov usage(0); 2390a402ad2SIhor Antonov __builtin_unreachable(); 240bd06a3ecSMike Barcroft default: 2410a402ad2SIhor Antonov usage(1); 242bd06a3ecSMike Barcroft } 243bd06a3ecSMike Barcroft } 244bd06a3ecSMike Barcroft argc -= optind; 245bd06a3ecSMike Barcroft argv += optind; 246bd06a3ecSMike Barcroft 2476b4ef4b1SIhor Antonov if (argc == 0) { 2480a402ad2SIhor Antonov usage(1); 2496b4ef4b1SIhor Antonov } 25012d7249eSTom Rhodes 2516b4ef4b1SIhor Antonov if (!title) { 25253d49b37SJilles Tjoelker title = argv[0]; 2536b4ef4b1SIhor Antonov } 25453d49b37SJilles Tjoelker 25553d49b37SJilles Tjoelker if (outfn) { 2564cd407ecSMaxim Sobolev outfd = open_log(outfn); 2576b4ef4b1SIhor Antonov if (outfd == -1) { 25853d49b37SJilles Tjoelker err(7, "open"); 25953d49b37SJilles Tjoelker } 2606b4ef4b1SIhor Antonov } 26153d49b37SJilles Tjoelker 2626b4ef4b1SIhor Antonov if (dosyslog) { 26353d49b37SJilles Tjoelker openlog(logtag, LOG_PID | LOG_NDELAY, logfac); 2646b4ef4b1SIhor Antonov } 26553d49b37SJilles Tjoelker 266846be7bdSPoul-Henning Kamp /* 267846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 268846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 269846be7bdSPoul-Henning Kamp */ 27053d49b37SJilles Tjoelker open_pid_files(pidfile, ppidfile, &pfh, &ppfh); 2719da0ef13SMikolaj Golub if (daemon(nochdir, noclose) == -1) { 2729da0ef13SMikolaj Golub warn("daemon"); 2739da0ef13SMikolaj Golub goto exit; 2749da0ef13SMikolaj Golub } 2759da0ef13SMikolaj Golub /* Write out parent pidfile if needed. */ 2769da0ef13SMikolaj Golub pidfile_write(ppfh); 277195fc497SMikolaj Golub /* 278b6193c24SMikolaj Golub * If the pidfile or restart option is specified the daemon 279b6193c24SMikolaj Golub * executes the command in a forked process and wait on child 280b6193c24SMikolaj Golub * exit to remove the pidfile or restart the command. Normally 281b6193c24SMikolaj Golub * we don't want the monitoring daemon to be terminated 282b6193c24SMikolaj Golub * leaving the running process and the stale pidfile, so we 283b6193c24SMikolaj Golub * catch SIGTERM and forward it to the children expecting to 28453d49b37SJilles Tjoelker * get SIGCHLD eventually. We also must fork() to obtain a 28553d49b37SJilles Tjoelker * readable pipe with the child for writing to a log file 28653d49b37SJilles Tjoelker * and syslog. 287195fc497SMikolaj Golub */ 288195fc497SMikolaj Golub pid = -1; 28953d49b37SJilles Tjoelker if (pidfile || ppidfile || restart || outfd != -1 || dosyslog) { 2904cd407ecSMaxim Sobolev struct sigaction act_term, act_chld, act_hup; 29153d49b37SJilles Tjoelker 29253d49b37SJilles Tjoelker /* Avoid PID racing with SIGCHLD and SIGTERM. */ 29353d49b37SJilles Tjoelker memset(&act_term, 0, sizeof(act_term)); 29453d49b37SJilles Tjoelker act_term.sa_handler = handle_term; 29553d49b37SJilles Tjoelker sigemptyset(&act_term.sa_mask); 29653d49b37SJilles Tjoelker sigaddset(&act_term.sa_mask, SIGCHLD); 29753d49b37SJilles Tjoelker 29853d49b37SJilles Tjoelker memset(&act_chld, 0, sizeof(act_chld)); 29953d49b37SJilles Tjoelker act_chld.sa_handler = handle_chld; 30053d49b37SJilles Tjoelker sigemptyset(&act_chld.sa_mask); 30153d49b37SJilles Tjoelker sigaddset(&act_chld.sa_mask, SIGTERM); 30253d49b37SJilles Tjoelker 3034cd407ecSMaxim Sobolev memset(&act_hup, 0, sizeof(act_hup)); 3044cd407ecSMaxim Sobolev act_hup.sa_handler = handle_hup; 3054cd407ecSMaxim Sobolev sigemptyset(&act_hup.sa_mask); 3064cd407ecSMaxim Sobolev 30753d49b37SJilles Tjoelker /* Block these when avoiding racing before sigsuspend(). */ 30853d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGTERM); 30953d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGCHLD); 31053d49b37SJilles Tjoelker /* Block SIGTERM when we lack a valid child PID. */ 31153d49b37SJilles Tjoelker sigaddset(&mask_term, SIGTERM); 3122ad43027SMikolaj Golub /* 31353d49b37SJilles Tjoelker * When reading, we wish to avoid SIGCHLD. SIGTERM 31453d49b37SJilles Tjoelker * has to be caught, otherwise we'll be stuck until 31553d49b37SJilles Tjoelker * the read() returns - if it returns. 316195fc497SMikolaj Golub */ 31753d49b37SJilles Tjoelker sigaddset(&mask_read, SIGCHLD); 31853d49b37SJilles Tjoelker /* Block SIGTERM to avoid racing until we have forked. */ 31953d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) { 3209da0ef13SMikolaj Golub warn("sigprocmask"); 3219da0ef13SMikolaj Golub goto exit; 3229da0ef13SMikolaj Golub } 32353d49b37SJilles Tjoelker if (sigaction(SIGTERM, &act_term, NULL) == -1) { 32453d49b37SJilles Tjoelker warn("sigaction"); 32553d49b37SJilles Tjoelker goto exit; 32653d49b37SJilles Tjoelker } 32753d49b37SJilles Tjoelker if (sigaction(SIGCHLD, &act_chld, NULL) == -1) { 32853d49b37SJilles Tjoelker warn("sigaction"); 32953d49b37SJilles Tjoelker goto exit; 33053d49b37SJilles Tjoelker } 33153c49998SMikolaj Golub /* 33253c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 33353c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 33453c49998SMikolaj Golub * not have superuser privileges. 33553c49998SMikolaj Golub */ 33653c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 33753d49b37SJilles Tjoelker logpar.outfd = outfd; 33853d49b37SJilles Tjoelker logpar.dosyslog = dosyslog; 33953d49b37SJilles Tjoelker logpar.logpri = logpri; 34053d49b37SJilles Tjoelker logpar.noclose = noclose; 3414cd407ecSMaxim Sobolev logpar.outfn = outfn; 3424cd407ecSMaxim Sobolev if (log_reopen && outfd >= 0 && 3434cd407ecSMaxim Sobolev sigaction(SIGHUP, &act_hup, NULL) == -1) { 3444cd407ecSMaxim Sobolev warn("sigaction"); 3454cd407ecSMaxim Sobolev goto exit; 3464cd407ecSMaxim Sobolev } 347b6193c24SMikolaj Golub restart: 3486b4ef4b1SIhor Antonov if (pipe(pfd)) { 34953d49b37SJilles Tjoelker err(1, "pipe"); 3506b4ef4b1SIhor Antonov } 351195fc497SMikolaj Golub /* 35253d49b37SJilles Tjoelker * Spawn a child to exec the command. 3532ad43027SMikolaj Golub */ 35453d49b37SJilles Tjoelker child_gone = 0; 3552ad43027SMikolaj Golub pid = fork(); 3562ad43027SMikolaj Golub if (pid == -1) { 3579da0ef13SMikolaj Golub warn("fork"); 3589da0ef13SMikolaj Golub goto exit; 35953d49b37SJilles Tjoelker } else if (pid > 0) { 36053d49b37SJilles Tjoelker /* 36153d49b37SJilles Tjoelker * Unblock SIGTERM after we know we have a valid 36253d49b37SJilles Tjoelker * child PID to signal. 36353d49b37SJilles Tjoelker */ 36453d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { 36553d49b37SJilles Tjoelker warn("sigprocmask"); 36653d49b37SJilles Tjoelker goto exit; 36753d49b37SJilles Tjoelker } 36853d49b37SJilles Tjoelker close(pfd[1]); 36953d49b37SJilles Tjoelker pfd[1] = -1; 3702ad43027SMikolaj Golub } 3712ad43027SMikolaj Golub } 372195fc497SMikolaj Golub if (pid <= 0) { 3732ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 374c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 375846be7bdSPoul-Henning Kamp 3766b4ef4b1SIhor Antonov if (user != NULL) { 3772ad43027SMikolaj Golub restrict_process(user); 3786b4ef4b1SIhor Antonov } 37953d49b37SJilles Tjoelker /* 38053d49b37SJilles Tjoelker * When forking, the child gets the original sigmask, 38153d49b37SJilles Tjoelker * and dup'd pipes. 38253d49b37SJilles Tjoelker */ 38353d49b37SJilles Tjoelker if (pid == 0) { 38453d49b37SJilles Tjoelker close(pfd[0]); 3856b4ef4b1SIhor Antonov if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) { 38653d49b37SJilles Tjoelker err(1, "sigprogmask"); 3876b4ef4b1SIhor Antonov } 38853d49b37SJilles Tjoelker if (stdmask & STDERR_FILENO) { 3896b4ef4b1SIhor Antonov if (dup2(pfd[1], STDERR_FILENO) == -1) { 39053d49b37SJilles Tjoelker err(1, "dup2"); 39153d49b37SJilles Tjoelker } 3926b4ef4b1SIhor Antonov } 39353d49b37SJilles Tjoelker if (stdmask & STDOUT_FILENO) { 3946b4ef4b1SIhor Antonov if (dup2(pfd[1], STDOUT_FILENO) == -1) { 39553d49b37SJilles Tjoelker err(1, "dup2"); 39653d49b37SJilles Tjoelker } 3976b4ef4b1SIhor Antonov } 39853d49b37SJilles Tjoelker if (pfd[1] != STDERR_FILENO && 3996b4ef4b1SIhor Antonov pfd[1] != STDOUT_FILENO) { 40053d49b37SJilles Tjoelker close(pfd[1]); 40153d49b37SJilles Tjoelker } 4026b4ef4b1SIhor Antonov } 403bd06a3ecSMike Barcroft execvp(argv[0], argv); 404846be7bdSPoul-Henning Kamp /* 4052ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 4062ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 407846be7bdSPoul-Henning Kamp */ 4082ad43027SMikolaj Golub err(1, "%s", argv[0]); 4092ad43027SMikolaj Golub } 41053d49b37SJilles Tjoelker setproctitle("%s[%d]", title, (int)pid); 41153d49b37SJilles Tjoelker /* 41253d49b37SJilles Tjoelker * As we have closed the write end of pipe for parent process, 41353d49b37SJilles Tjoelker * we might detect the child's exit by reading EOF. The child 41453d49b37SJilles Tjoelker * might have closed its stdout and stderr, so we must wait for 41553d49b37SJilles Tjoelker * the SIGCHLD to ensure that the process is actually gone. 41653d49b37SJilles Tjoelker */ 41753d49b37SJilles Tjoelker for (;;) { 41853d49b37SJilles Tjoelker /* 41953d49b37SJilles Tjoelker * We block SIGCHLD when listening, but SIGTERM we accept 42053d49b37SJilles Tjoelker * so the read() won't block if we wish to depart. 42153d49b37SJilles Tjoelker * 42253d49b37SJilles Tjoelker * Upon receiving SIGTERM, we have several options after 42353d49b37SJilles Tjoelker * sending the SIGTERM to our child: 42453d49b37SJilles Tjoelker * - read until EOF 42553d49b37SJilles Tjoelker * - read until EOF but only for a while 42653d49b37SJilles Tjoelker * - bail immediately 42753d49b37SJilles Tjoelker * 42853d49b37SJilles Tjoelker * We go for the third, as otherwise we have no guarantee 42953d49b37SJilles Tjoelker * that we won't block indefinitely if the child refuses 43053d49b37SJilles Tjoelker * to depart. To handle the second option, a different 43153d49b37SJilles Tjoelker * approach would be needed (procctl()?) 43253d49b37SJilles Tjoelker */ 43353d49b37SJilles Tjoelker if (child_gone && child_eof) { 43453d49b37SJilles Tjoelker break; 43553d49b37SJilles Tjoelker } else if (terminate) { 43653d49b37SJilles Tjoelker goto exit; 43753d49b37SJilles Tjoelker } else if (!child_eof) { 43853d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) { 43953d49b37SJilles Tjoelker warn("sigprocmask"); 44053d49b37SJilles Tjoelker goto exit; 44153d49b37SJilles Tjoelker } 44253d49b37SJilles Tjoelker child_eof = !listen_child(pfd[0], &logpar); 44353d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { 44453d49b37SJilles Tjoelker warn("sigprocmask"); 44553d49b37SJilles Tjoelker goto exit; 44653d49b37SJilles Tjoelker } 44753d49b37SJilles Tjoelker } else { 44853d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) { 44953d49b37SJilles Tjoelker warn("sigprocmask"); 45053d49b37SJilles Tjoelker goto exit; 45153d49b37SJilles Tjoelker } 45253d49b37SJilles Tjoelker while (!terminate && !child_gone) 45353d49b37SJilles Tjoelker sigsuspend(&mask_orig); 45453d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) { 45553d49b37SJilles Tjoelker warn("sigprocmask"); 45653d49b37SJilles Tjoelker goto exit; 45753d49b37SJilles Tjoelker } 45853d49b37SJilles Tjoelker } 45953d49b37SJilles Tjoelker } 4606b4ef4b1SIhor Antonov if (restart && !terminate) { 46109a3675dSConrad Meyer daemon_sleep(restart, 0); 4626b4ef4b1SIhor Antonov } 46353d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) { 46453d49b37SJilles Tjoelker warn("sigprocmask"); 46553d49b37SJilles Tjoelker goto exit; 46653d49b37SJilles Tjoelker } 46753d49b37SJilles Tjoelker if (restart && !terminate) { 46853d49b37SJilles Tjoelker close(pfd[0]); 46953d49b37SJilles Tjoelker pfd[0] = -1; 470b6193c24SMikolaj Golub goto restart; 471b6193c24SMikolaj Golub } 4729da0ef13SMikolaj Golub exit: 47353d49b37SJilles Tjoelker close(outfd); 47453d49b37SJilles Tjoelker close(pfd[0]); 47553d49b37SJilles Tjoelker close(pfd[1]); 4766b4ef4b1SIhor Antonov if (dosyslog) { 47753d49b37SJilles Tjoelker closelog(); 4786b4ef4b1SIhor Antonov } 479c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 48032b17786SJohn-Mark Gurney pidfile_remove(ppfh); 4819da0ef13SMikolaj Golub exit(1); /* If daemon(3) succeeded exit status does not matter. */ 482bd06a3ecSMike Barcroft } 483bd06a3ecSMike Barcroft 484bd06a3ecSMike Barcroft static void 48553d49b37SJilles Tjoelker daemon_sleep(time_t secs, long nsecs) 486195fc497SMikolaj Golub { 48753d49b37SJilles Tjoelker struct timespec ts = { secs, nsecs }; 48809a3675dSConrad Meyer 48909a3675dSConrad Meyer while (!terminate && nanosleep(&ts, &ts) == -1) { 4906b4ef4b1SIhor Antonov if (errno != EINTR) { 49153d49b37SJilles Tjoelker err(1, "nanosleep"); 49253d49b37SJilles Tjoelker } 49353d49b37SJilles Tjoelker } 4946b4ef4b1SIhor Antonov } 49553d49b37SJilles Tjoelker 49653d49b37SJilles Tjoelker static void 49753d49b37SJilles Tjoelker open_pid_files(const char *pidfile, const char *ppidfile, 49853d49b37SJilles Tjoelker struct pidfh **pfh, struct pidfh **ppfh) 49953d49b37SJilles Tjoelker { 50053d49b37SJilles Tjoelker pid_t fpid; 50153d49b37SJilles Tjoelker int serrno; 50253d49b37SJilles Tjoelker 50353d49b37SJilles Tjoelker if (pidfile) { 50453d49b37SJilles Tjoelker *pfh = pidfile_open(pidfile, 0600, &fpid); 50553d49b37SJilles Tjoelker if (*pfh == NULL) { 50653d49b37SJilles Tjoelker if (errno == EEXIST) { 50753d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 50853d49b37SJilles Tjoelker fpid); 50953d49b37SJilles Tjoelker } 51053d49b37SJilles Tjoelker err(2, "pidfile ``%s''", pidfile); 51153d49b37SJilles Tjoelker } 51253d49b37SJilles Tjoelker } 51353d49b37SJilles Tjoelker /* Do the same for the actual daemon process. */ 51453d49b37SJilles Tjoelker if (ppidfile) { 51553d49b37SJilles Tjoelker *ppfh = pidfile_open(ppidfile, 0600, &fpid); 51653d49b37SJilles Tjoelker if (*ppfh == NULL) { 51753d49b37SJilles Tjoelker serrno = errno; 51853d49b37SJilles Tjoelker pidfile_remove(*pfh); 51953d49b37SJilles Tjoelker errno = serrno; 52053d49b37SJilles Tjoelker if (errno == EEXIST) { 52153d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 52253d49b37SJilles Tjoelker fpid); 52353d49b37SJilles Tjoelker } 52453d49b37SJilles Tjoelker err(2, "ppidfile ``%s''", ppidfile); 52553d49b37SJilles Tjoelker } 52653d49b37SJilles Tjoelker } 52753d49b37SJilles Tjoelker } 52853d49b37SJilles Tjoelker 52953d49b37SJilles Tjoelker static int 53053d49b37SJilles Tjoelker get_log_mapping(const char *str, const CODE *c) 53153d49b37SJilles Tjoelker { 53253d49b37SJilles Tjoelker const CODE *cp; 53353d49b37SJilles Tjoelker for (cp = c; cp->c_name; cp++) 5346b4ef4b1SIhor Antonov if (strcmp(cp->c_name, str) == 0) { 53553d49b37SJilles Tjoelker return cp->c_val; 5366b4ef4b1SIhor Antonov } 53753d49b37SJilles Tjoelker return -1; 538195fc497SMikolaj Golub } 539195fc497SMikolaj Golub 540195fc497SMikolaj Golub static void 541e6d4b388STom Rhodes restrict_process(const char *user) 54212d7249eSTom Rhodes { 54312d7249eSTom Rhodes struct passwd *pw = NULL; 54412d7249eSTom Rhodes 545e6d4b388STom Rhodes pw = getpwnam(user); 5466b4ef4b1SIhor Antonov if (pw == NULL) { 547e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 5486b4ef4b1SIhor Antonov } 54912d7249eSTom Rhodes 5506b4ef4b1SIhor Antonov if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) { 551e6d4b388STom Rhodes errx(1, "failed to set user environment"); 5526b4ef4b1SIhor Antonov } 5536b3ad1d7SMaxim Sobolev 5546b3ad1d7SMaxim Sobolev setenv("USER", pw->pw_name, 1); 5556b3ad1d7SMaxim Sobolev setenv("HOME", pw->pw_dir, 1); 5566b3ad1d7SMaxim Sobolev setenv("SHELL", *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL, 1); 55712d7249eSTom Rhodes } 55812d7249eSTom Rhodes 55953d49b37SJilles Tjoelker /* 56053d49b37SJilles Tjoelker * We try to collect whole lines terminated by '\n'. Otherwise we collect a 56153d49b37SJilles Tjoelker * full buffer, and then output it. 56253d49b37SJilles Tjoelker * 56353d49b37SJilles Tjoelker * Return value of 0 is assumed to mean EOF or error, and 1 indicates to 56453d49b37SJilles Tjoelker * continue reading. 56553d49b37SJilles Tjoelker */ 566b6193c24SMikolaj Golub static int 56753d49b37SJilles Tjoelker listen_child(int fd, struct log_params *logpar) 5682ad43027SMikolaj Golub { 56953d49b37SJilles Tjoelker static unsigned char buf[LBUF_SIZE]; 57053d49b37SJilles Tjoelker static size_t bytes_read = 0; 57153d49b37SJilles Tjoelker int rv; 5722ad43027SMikolaj Golub 57353d49b37SJilles Tjoelker assert(logpar); 57453d49b37SJilles Tjoelker assert(bytes_read < LBUF_SIZE - 1); 57553d49b37SJilles Tjoelker 5766b4ef4b1SIhor Antonov if (do_log_reopen) { 5774cd407ecSMaxim Sobolev reopen_log(logpar); 5786b4ef4b1SIhor Antonov } 57953d49b37SJilles Tjoelker rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1); 58053d49b37SJilles Tjoelker if (rv > 0) { 58153d49b37SJilles Tjoelker unsigned char *cp; 58253d49b37SJilles Tjoelker 58353d49b37SJilles Tjoelker bytes_read += rv; 58453d49b37SJilles Tjoelker assert(bytes_read <= LBUF_SIZE - 1); 58553d49b37SJilles Tjoelker /* Always NUL-terminate just in case. */ 58653d49b37SJilles Tjoelker buf[LBUF_SIZE - 1] = '\0'; 58753d49b37SJilles Tjoelker /* 58853d49b37SJilles Tjoelker * Chomp line by line until we run out of buffer. 58953d49b37SJilles Tjoelker * This does not take NUL characters into account. 59053d49b37SJilles Tjoelker */ 59153d49b37SJilles Tjoelker while ((cp = memchr(buf, '\n', bytes_read)) != NULL) { 59253d49b37SJilles Tjoelker size_t bytes_line = cp - buf + 1; 59353d49b37SJilles Tjoelker assert(bytes_line <= bytes_read); 59453d49b37SJilles Tjoelker do_output(buf, bytes_line, logpar); 59553d49b37SJilles Tjoelker bytes_read -= bytes_line; 59653d49b37SJilles Tjoelker memmove(buf, cp + 1, bytes_read); 597195fc497SMikolaj Golub } 59853d49b37SJilles Tjoelker /* Wait until the buffer is full. */ 5996b4ef4b1SIhor Antonov if (bytes_read < LBUF_SIZE - 1) { 60053d49b37SJilles Tjoelker return 1; 6016b4ef4b1SIhor Antonov } 60253d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 60353d49b37SJilles Tjoelker bytes_read = 0; 60453d49b37SJilles Tjoelker return 1; 60553d49b37SJilles Tjoelker } else if (rv == -1) { 60653d49b37SJilles Tjoelker /* EINTR should trigger another read. */ 60753d49b37SJilles Tjoelker if (errno == EINTR) { 60853d49b37SJilles Tjoelker return 1; 60953d49b37SJilles Tjoelker } else { 61053d49b37SJilles Tjoelker warn("read"); 61153d49b37SJilles Tjoelker return 0; 612c60d51f9SMikolaj Golub } 61353d49b37SJilles Tjoelker } 61453d49b37SJilles Tjoelker /* Upon EOF, we have to flush what's left of the buffer. */ 61553d49b37SJilles Tjoelker if (bytes_read > 0) { 61653d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 61753d49b37SJilles Tjoelker bytes_read = 0; 61853d49b37SJilles Tjoelker } 61953d49b37SJilles Tjoelker return 0; 62053d49b37SJilles Tjoelker } 62153d49b37SJilles Tjoelker 62253d49b37SJilles Tjoelker /* 62353d49b37SJilles Tjoelker * The default behavior is to stay silent if the user wants to redirect 62453d49b37SJilles Tjoelker * output to a file and/or syslog. If neither are provided, then we bounce 62553d49b37SJilles Tjoelker * everything back to parent's stdout. 62653d49b37SJilles Tjoelker */ 62753d49b37SJilles Tjoelker static void 62853d49b37SJilles Tjoelker do_output(const unsigned char *buf, size_t len, struct log_params *logpar) 62953d49b37SJilles Tjoelker { 63053d49b37SJilles Tjoelker assert(len <= LBUF_SIZE); 63153d49b37SJilles Tjoelker assert(logpar); 63253d49b37SJilles Tjoelker 6336b4ef4b1SIhor Antonov if (len < 1) { 63453d49b37SJilles Tjoelker return; 6356b4ef4b1SIhor Antonov } 6366b4ef4b1SIhor Antonov if (logpar->dosyslog) { 63753d49b37SJilles Tjoelker syslog(logpar->logpri, "%.*s", (int)len, buf); 6386b4ef4b1SIhor Antonov } 63953d49b37SJilles Tjoelker if (logpar->outfd != -1) { 64053d49b37SJilles Tjoelker if (write(logpar->outfd, buf, len) == -1) 64153d49b37SJilles Tjoelker warn("write"); 64253d49b37SJilles Tjoelker } 6436b4ef4b1SIhor Antonov if (logpar->noclose && !logpar->dosyslog && logpar->outfd == -1) { 64453d49b37SJilles Tjoelker printf("%.*s", (int)len, buf); 64553d49b37SJilles Tjoelker } 6466b4ef4b1SIhor Antonov } 64753d49b37SJilles Tjoelker 64853d49b37SJilles Tjoelker /* 64953d49b37SJilles Tjoelker * We use the global PID acquired directly from fork. If there is no valid 65053d49b37SJilles Tjoelker * child pid, the handler should be blocked and/or child_gone == 1. 65153d49b37SJilles Tjoelker */ 65253d49b37SJilles Tjoelker static void 65353d49b37SJilles Tjoelker handle_term(int signo) 65453d49b37SJilles Tjoelker { 6556b4ef4b1SIhor Antonov if (pid > 0 && !child_gone) { 65653d49b37SJilles Tjoelker kill(pid, signo); 6576b4ef4b1SIhor Antonov } 658b6193c24SMikolaj Golub terminate = 1; 659195fc497SMikolaj Golub } 66053d49b37SJilles Tjoelker 66153d49b37SJilles Tjoelker static void 6624cd407ecSMaxim Sobolev handle_chld(int signo __unused) 66353d49b37SJilles Tjoelker { 6644cd407ecSMaxim Sobolev 66553d49b37SJilles Tjoelker for (;;) { 66653d49b37SJilles Tjoelker int rv = waitpid(-1, NULL, WNOHANG); 66753d49b37SJilles Tjoelker if (pid == rv) { 66853d49b37SJilles Tjoelker child_gone = 1; 66953d49b37SJilles Tjoelker break; 67053d49b37SJilles Tjoelker } else if (rv == -1 && errno != EINTR) { 67153d49b37SJilles Tjoelker warn("waitpid"); 67253d49b37SJilles Tjoelker return; 6732ad43027SMikolaj Golub } 6742ad43027SMikolaj Golub } 6752ad43027SMikolaj Golub } 6762ad43027SMikolaj Golub 6772ad43027SMikolaj Golub static void 6784cd407ecSMaxim Sobolev handle_hup(int signo __unused) 6794cd407ecSMaxim Sobolev { 6804cd407ecSMaxim Sobolev 6814cd407ecSMaxim Sobolev do_log_reopen = 1; 6824cd407ecSMaxim Sobolev } 6834cd407ecSMaxim Sobolev 6844cd407ecSMaxim Sobolev static int 6854cd407ecSMaxim Sobolev open_log(const char *outfn) 6864cd407ecSMaxim Sobolev { 6874cd407ecSMaxim Sobolev 6884cd407ecSMaxim Sobolev return open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600); 6894cd407ecSMaxim Sobolev } 6904cd407ecSMaxim Sobolev 6914cd407ecSMaxim Sobolev static void 6924cd407ecSMaxim Sobolev reopen_log(struct log_params *lpp) 6934cd407ecSMaxim Sobolev { 6944cd407ecSMaxim Sobolev int outfd; 6954cd407ecSMaxim Sobolev 6964cd407ecSMaxim Sobolev do_log_reopen = 0; 6974cd407ecSMaxim Sobolev outfd = open_log(lpp->outfn); 6986b4ef4b1SIhor Antonov if (lpp->outfd >= 0) { 6994cd407ecSMaxim Sobolev close(lpp->outfd); 7006b4ef4b1SIhor Antonov } 7014cd407ecSMaxim Sobolev lpp->outfd = outfd; 7024cd407ecSMaxim Sobolev } 7034cd407ecSMaxim Sobolev 704