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> 50203df05bSIhor Antonov #include <stdbool.h> 51bd06a3ecSMike Barcroft #include <stdlib.h> 52bd06a3ecSMike Barcroft #include <unistd.h> 5353d49b37SJilles Tjoelker #include <string.h> 5453d49b37SJilles Tjoelker #include <strings.h> 5553d49b37SJilles Tjoelker #define SYSLOG_NAMES 5653d49b37SJilles Tjoelker #include <syslog.h> 5753d49b37SJilles Tjoelker #include <time.h> 5853d49b37SJilles Tjoelker #include <assert.h> 59bd06a3ecSMike Barcroft 6053d49b37SJilles Tjoelker #define LBUF_SIZE 4096 6153d49b37SJilles Tjoelker 6253d49b37SJilles Tjoelker struct log_params { 6353d49b37SJilles Tjoelker int logpri; 6453d49b37SJilles Tjoelker int noclose; 6553d49b37SJilles Tjoelker int outfd; 664cd407ecSMaxim Sobolev const char *outfn; 67*f2f9d31dSIhor Antonov bool syslog_enabled; 6853d49b37SJilles Tjoelker }; 6953d49b37SJilles Tjoelker 70e6d4b388STom Rhodes static void restrict_process(const char *); 7153d49b37SJilles Tjoelker static void handle_term(int); 7253d49b37SJilles Tjoelker static void handle_chld(int); 734cd407ecSMaxim Sobolev static void handle_hup(int); 744cd407ecSMaxim Sobolev static int open_log(const char *); 754cd407ecSMaxim Sobolev static void reopen_log(struct log_params *); 7653d49b37SJilles Tjoelker static int listen_child(int, struct log_params *); 7753d49b37SJilles Tjoelker static int get_log_mapping(const char *, const CODE *); 7853d49b37SJilles Tjoelker static void open_pid_files(const char *, const char *, struct pidfh **, 7953d49b37SJilles Tjoelker struct pidfh **); 8053d49b37SJilles Tjoelker static void do_output(const unsigned char *, size_t, struct log_params *); 8153d49b37SJilles Tjoelker static void daemon_sleep(time_t, long); 82bd06a3ecSMike Barcroft 83e745dc22SIhor Antonov static volatile sig_atomic_t terminate = 0; 84e745dc22SIhor Antonov static volatile sig_atomic_t child_gone = 0; 8591b921c7SIhor Antonov static volatile sig_atomic_t pid = -1; 86e745dc22SIhor Antonov static volatile sig_atomic_t do_log_reopen = 0; 8753d49b37SJilles Tjoelker 880a402ad2SIhor Antonov static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; 890a402ad2SIhor Antonov 900a402ad2SIhor Antonov static const struct option longopts[] = { 910a402ad2SIhor Antonov { "change-dir", no_argument, NULL, 'c' }, 920a402ad2SIhor Antonov { "close-fds", no_argument, NULL, 'f' }, 930a402ad2SIhor Antonov { "sighup", no_argument, NULL, 'H' }, 940a402ad2SIhor Antonov { "syslog", no_argument, NULL, 'S' }, 950a402ad2SIhor Antonov { "output-file", required_argument, NULL, 'o' }, 960a402ad2SIhor Antonov { "output-mask", required_argument, NULL, 'm' }, 970a402ad2SIhor Antonov { "child-pidfile", required_argument, NULL, 'p' }, 980a402ad2SIhor Antonov { "supervisor-pidfile", required_argument, NULL, 'P' }, 990a402ad2SIhor Antonov { "restart", no_argument, NULL, 'r' }, 1000a402ad2SIhor Antonov { "restart-delay", required_argument, NULL, 'R' }, 1010a402ad2SIhor Antonov { "title", required_argument, NULL, 't' }, 1020a402ad2SIhor Antonov { "user", required_argument, NULL, 'u' }, 1030a402ad2SIhor Antonov { "syslog-priority", required_argument, NULL, 's' }, 1040a402ad2SIhor Antonov { "syslog-facility", required_argument, NULL, 'l' }, 1050a402ad2SIhor Antonov { "syslog-tag", required_argument, NULL, 'T' }, 1060a402ad2SIhor Antonov { "help", no_argument, NULL, 'h' }, 1070a402ad2SIhor Antonov { NULL, 0, NULL, 0 } 1080a402ad2SIhor Antonov }; 1090a402ad2SIhor Antonov 1100a402ad2SIhor Antonov static _Noreturn void 1110a402ad2SIhor Antonov usage(int exitcode) 1120a402ad2SIhor Antonov { 1130a402ad2SIhor Antonov (void)fprintf(stderr, 1140a402ad2SIhor Antonov "usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n" 1150a402ad2SIhor Antonov " [-u user] [-o output_file] [-t title]\n" 1160a402ad2SIhor Antonov " [-l syslog_facility] [-s syslog_priority]\n" 1170a402ad2SIhor Antonov " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" 1180a402ad2SIhor Antonov "command arguments ...\n"); 1190a402ad2SIhor Antonov 1200a402ad2SIhor Antonov (void)fprintf(stderr, 1210a402ad2SIhor Antonov " --change-dir -c Change the current working directory to root\n" 1220a402ad2SIhor Antonov " --close-fds -f Set stdin, stdout, stderr to /dev/null\n" 1230a402ad2SIhor Antonov " --sighup -H Close and re-open output file on SIGHUP\n" 1240a402ad2SIhor Antonov " --syslog -S Send output to syslog\n" 1250a402ad2SIhor Antonov " --output-file -o <file> Append output of the child process to file\n" 1260a402ad2SIhor Antonov " --output-mask -m <mask> What to send to syslog/file\n" 1270a402ad2SIhor Antonov " 1=stdout, 2=stderr, 3=both\n" 1280a402ad2SIhor Antonov " --child-pidfile -p <file> Write PID of the child process to file\n" 1290a402ad2SIhor Antonov " --supervisor-pidfile -P <file> Write PID of the supervisor process to file\n" 1300a402ad2SIhor Antonov " --restart -r Restart child if it terminates (1 sec delay)\n" 1310a402ad2SIhor Antonov " --restart-delay -R <N> Restart child if it terminates after N sec\n" 1320a402ad2SIhor Antonov " --title -t <title> Set the title of the supervisor process\n" 1330a402ad2SIhor Antonov " --user -u <user> Drop privileges, run as given user\n" 1340a402ad2SIhor Antonov " --syslog-priority -s <prio> Set syslog priority\n" 1350a402ad2SIhor Antonov " --syslog-facility -l <flty> Set syslog facility\n" 1360a402ad2SIhor Antonov " --syslog-tag -T <tag> Set syslog tag\n" 1370a402ad2SIhor Antonov " --help -h Show this help\n"); 1380a402ad2SIhor Antonov 1390a402ad2SIhor Antonov exit(exitcode); 1400a402ad2SIhor Antonov } 1410a402ad2SIhor Antonov 142bd06a3ecSMike Barcroft int 143bd06a3ecSMike Barcroft main(int argc, char *argv[]) 144bd06a3ecSMike Barcroft { 145203df05bSIhor Antonov bool supervision_enabled = false; 146*f2f9d31dSIhor Antonov bool syslog_enabled = false; 147e745dc22SIhor Antonov char *p = NULL; 148e745dc22SIhor Antonov const char *pidfile = NULL; 149e745dc22SIhor Antonov const char *logtag = "daemon"; 150e745dc22SIhor Antonov const char *outfn = NULL; 151e745dc22SIhor Antonov const char *ppidfile = NULL; 152e745dc22SIhor Antonov const char *title = NULL; 153e745dc22SIhor Antonov const char *user = NULL; 154e745dc22SIhor Antonov int ch = 0; 155e745dc22SIhor Antonov int child_eof = 0; 156e745dc22SIhor Antonov int log_reopen = 0; 157e745dc22SIhor Antonov int logfac = LOG_DAEMON; 158e745dc22SIhor Antonov int logpri = LOG_NOTICE; 159e745dc22SIhor Antonov int nochdir = 1; 160e745dc22SIhor Antonov int noclose = 1; 161e745dc22SIhor Antonov int outfd = -1; 162e745dc22SIhor Antonov int pfd[2] = { -1, -1 }; 163e745dc22SIhor Antonov int restart = 0; 164e745dc22SIhor Antonov int stdmask = STDOUT_FILENO | STDERR_FILENO; 165e745dc22SIhor Antonov struct log_params logpar = { 0 }; 166e745dc22SIhor Antonov struct pidfh *ppfh = NULL; 167e745dc22SIhor Antonov struct pidfh *pfh = NULL; 168e745dc22SIhor Antonov sigset_t mask_orig; 169e745dc22SIhor Antonov sigset_t mask_read; 170e745dc22SIhor Antonov sigset_t mask_term; 171e745dc22SIhor Antonov sigset_t mask_susp; 172bd06a3ecSMike Barcroft 173e745dc22SIhor Antonov sigemptyset(&mask_susp); 174e745dc22SIhor Antonov sigemptyset(&mask_read); 175e745dc22SIhor Antonov sigemptyset(&mask_term); 17684866cefSIhor Antonov sigemptyset(&mask_orig); 177e745dc22SIhor Antonov 1780a402ad2SIhor Antonov while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { 179bd06a3ecSMike Barcroft switch (ch) { 180bd06a3ecSMike Barcroft case 'c': 181bd06a3ecSMike Barcroft nochdir = 0; 182bd06a3ecSMike Barcroft break; 183bd06a3ecSMike Barcroft case 'f': 184bd06a3ecSMike Barcroft noclose = 0; 185bd06a3ecSMike Barcroft break; 1864cd407ecSMaxim Sobolev case 'H': 1874cd407ecSMaxim Sobolev log_reopen = 1; 1884cd407ecSMaxim Sobolev break; 18953d49b37SJilles Tjoelker case 'l': 19053d49b37SJilles Tjoelker logfac = get_log_mapping(optarg, facilitynames); 1916b4ef4b1SIhor Antonov if (logfac == -1) { 19253d49b37SJilles Tjoelker errx(5, "unrecognized syslog facility"); 1936b4ef4b1SIhor Antonov } 194*f2f9d31dSIhor Antonov syslog_enabled = true; 19553d49b37SJilles Tjoelker break; 19653d49b37SJilles Tjoelker case 'm': 19753d49b37SJilles Tjoelker stdmask = strtol(optarg, &p, 10); 1986b4ef4b1SIhor Antonov if (p == optarg || stdmask < 0 || stdmask > 3) { 19953d49b37SJilles Tjoelker errx(6, "unrecognized listening mask"); 2006b4ef4b1SIhor Antonov } 20153d49b37SJilles Tjoelker break; 20253d49b37SJilles Tjoelker case 'o': 20353d49b37SJilles Tjoelker outfn = optarg; 20453d49b37SJilles Tjoelker break; 205846be7bdSPoul-Henning Kamp case 'p': 206846be7bdSPoul-Henning Kamp pidfile = optarg; 207846be7bdSPoul-Henning Kamp break; 20832b17786SJohn-Mark Gurney case 'P': 20932b17786SJohn-Mark Gurney ppidfile = optarg; 21032b17786SJohn-Mark Gurney break; 211b6193c24SMikolaj Golub case 'r': 212b6193c24SMikolaj Golub restart = 1; 213b6193c24SMikolaj Golub break; 21437820b87SIan Lepore case 'R': 21537820b87SIan Lepore restart = strtol(optarg, &p, 0); 2166b4ef4b1SIhor Antonov if (p == optarg || restart < 1) { 21737820b87SIan Lepore errx(6, "invalid restart delay"); 2186b4ef4b1SIhor Antonov } 21937820b87SIan Lepore break; 22053d49b37SJilles Tjoelker case 's': 22153d49b37SJilles Tjoelker logpri = get_log_mapping(optarg, prioritynames); 2226b4ef4b1SIhor Antonov if (logpri == -1) { 22353d49b37SJilles Tjoelker errx(4, "unrecognized syslog priority"); 2246b4ef4b1SIhor Antonov } 225*f2f9d31dSIhor Antonov syslog_enabled = true; 22653d49b37SJilles Tjoelker break; 22753d49b37SJilles Tjoelker case 'S': 228*f2f9d31dSIhor Antonov syslog_enabled = true; 22953d49b37SJilles Tjoelker break; 230112bfcf5SConrad Meyer case 't': 231112bfcf5SConrad Meyer title = optarg; 232112bfcf5SConrad Meyer break; 23353d49b37SJilles Tjoelker case 'T': 23453d49b37SJilles Tjoelker logtag = optarg; 235*f2f9d31dSIhor Antonov syslog_enabled = true; 23653d49b37SJilles Tjoelker break; 237e6d4b388STom Rhodes case 'u': 238e6d4b388STom Rhodes user = optarg; 239e6d4b388STom Rhodes break; 2400a402ad2SIhor Antonov case 'h': 2410a402ad2SIhor Antonov usage(0); 2420a402ad2SIhor Antonov __builtin_unreachable(); 243bd06a3ecSMike Barcroft default: 2440a402ad2SIhor Antonov usage(1); 245bd06a3ecSMike Barcroft } 246bd06a3ecSMike Barcroft } 247bd06a3ecSMike Barcroft argc -= optind; 248bd06a3ecSMike Barcroft argv += optind; 249bd06a3ecSMike Barcroft 2506b4ef4b1SIhor Antonov if (argc == 0) { 2510a402ad2SIhor Antonov usage(1); 2526b4ef4b1SIhor Antonov } 25312d7249eSTom Rhodes 2546b4ef4b1SIhor Antonov if (!title) { 25553d49b37SJilles Tjoelker title = argv[0]; 2566b4ef4b1SIhor Antonov } 25753d49b37SJilles Tjoelker 25853d49b37SJilles Tjoelker if (outfn) { 2594cd407ecSMaxim Sobolev outfd = open_log(outfn); 2606b4ef4b1SIhor Antonov if (outfd == -1) { 26153d49b37SJilles Tjoelker err(7, "open"); 26253d49b37SJilles Tjoelker } 2636b4ef4b1SIhor Antonov } 26453d49b37SJilles Tjoelker 265*f2f9d31dSIhor Antonov if (syslog_enabled) { 26653d49b37SJilles Tjoelker openlog(logtag, LOG_PID | LOG_NDELAY, logfac); 2676b4ef4b1SIhor Antonov } 26853d49b37SJilles Tjoelker 269846be7bdSPoul-Henning Kamp /* 270846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 271846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 272846be7bdSPoul-Henning Kamp */ 27353d49b37SJilles Tjoelker open_pid_files(pidfile, ppidfile, &pfh, &ppfh); 2749da0ef13SMikolaj Golub if (daemon(nochdir, noclose) == -1) { 2759da0ef13SMikolaj Golub warn("daemon"); 2769da0ef13SMikolaj Golub goto exit; 2779da0ef13SMikolaj Golub } 2789da0ef13SMikolaj Golub /* Write out parent pidfile if needed. */ 2799da0ef13SMikolaj Golub pidfile_write(ppfh); 280203df05bSIhor Antonov 281195fc497SMikolaj Golub /* 282203df05bSIhor Antonov * Supervision mode is enabled if one of the following options are used: 283203df05bSIhor Antonov * --child-pidfile -p 284203df05bSIhor Antonov * --supervisor-pidfile -P 285203df05bSIhor Antonov * --restart -r / --restart-delay -R 286203df05bSIhor Antonov * --syslog -S 287203df05bSIhor Antonov * --syslog-facility -l 288203df05bSIhor Antonov * --syslog-priority -s 289203df05bSIhor Antonov * --syslog-tag -T 290203df05bSIhor Antonov * 291203df05bSIhor Antonov * In supervision mode daemon executes the command in a forked process 292203df05bSIhor Antonov * and observes the child by waiting for SIGCHILD. In supervision mode 293203df05bSIhor Antonov * daemon must never exit before the child, this is necessary to prevent 294203df05bSIhor Antonov * orphaning the child and leaving a stale pid file. 295203df05bSIhor Antonov * To achieve this daemon catches SIGTERM and 296203df05bSIhor Antonov * forwards it to the child, expecting to get SIGCHLD eventually. 297195fc497SMikolaj Golub */ 298203df05bSIhor Antonov supervision_enabled = pidfile != NULL || 299203df05bSIhor Antonov ppidfile != NULL || 300203df05bSIhor Antonov restart != 0 || 301203df05bSIhor Antonov outfd != -1 || 302*f2f9d31dSIhor Antonov syslog_enabled == true; 303203df05bSIhor Antonov 304203df05bSIhor Antonov if (supervision_enabled) { 305259ed21dSIhor Antonov struct sigaction act_term = { 0 }; 306259ed21dSIhor Antonov struct sigaction act_chld = { 0 }; 307259ed21dSIhor Antonov struct sigaction act_hup = { 0 }; 30853d49b37SJilles Tjoelker 30953d49b37SJilles Tjoelker /* Avoid PID racing with SIGCHLD and SIGTERM. */ 31053d49b37SJilles Tjoelker act_term.sa_handler = handle_term; 31153d49b37SJilles Tjoelker sigemptyset(&act_term.sa_mask); 31253d49b37SJilles Tjoelker sigaddset(&act_term.sa_mask, SIGCHLD); 31353d49b37SJilles Tjoelker 31453d49b37SJilles Tjoelker act_chld.sa_handler = handle_chld; 31553d49b37SJilles Tjoelker sigemptyset(&act_chld.sa_mask); 31653d49b37SJilles Tjoelker sigaddset(&act_chld.sa_mask, SIGTERM); 31753d49b37SJilles Tjoelker 3184cd407ecSMaxim Sobolev act_hup.sa_handler = handle_hup; 3194cd407ecSMaxim Sobolev sigemptyset(&act_hup.sa_mask); 3204cd407ecSMaxim Sobolev 32153d49b37SJilles Tjoelker /* Block these when avoiding racing before sigsuspend(). */ 32253d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGTERM); 32353d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGCHLD); 32453d49b37SJilles Tjoelker /* Block SIGTERM when we lack a valid child PID. */ 32553d49b37SJilles Tjoelker sigaddset(&mask_term, SIGTERM); 3262ad43027SMikolaj Golub /* 32753d49b37SJilles Tjoelker * When reading, we wish to avoid SIGCHLD. SIGTERM 32853d49b37SJilles Tjoelker * has to be caught, otherwise we'll be stuck until 32953d49b37SJilles Tjoelker * the read() returns - if it returns. 330195fc497SMikolaj Golub */ 33153d49b37SJilles Tjoelker sigaddset(&mask_read, SIGCHLD); 33253d49b37SJilles Tjoelker /* Block SIGTERM to avoid racing until we have forked. */ 33353d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) { 3349da0ef13SMikolaj Golub warn("sigprocmask"); 3359da0ef13SMikolaj Golub goto exit; 3369da0ef13SMikolaj Golub } 33753d49b37SJilles Tjoelker if (sigaction(SIGTERM, &act_term, NULL) == -1) { 33853d49b37SJilles Tjoelker warn("sigaction"); 33953d49b37SJilles Tjoelker goto exit; 34053d49b37SJilles Tjoelker } 34153d49b37SJilles Tjoelker if (sigaction(SIGCHLD, &act_chld, NULL) == -1) { 34253d49b37SJilles Tjoelker warn("sigaction"); 34353d49b37SJilles Tjoelker goto exit; 34453d49b37SJilles Tjoelker } 34553c49998SMikolaj Golub /* 34653c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 34753c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 34853c49998SMikolaj Golub * not have superuser privileges. 34953c49998SMikolaj Golub */ 35053c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 35153d49b37SJilles Tjoelker logpar.outfd = outfd; 352*f2f9d31dSIhor Antonov logpar.syslog_enabled = syslog_enabled; 35353d49b37SJilles Tjoelker logpar.logpri = logpri; 35453d49b37SJilles Tjoelker logpar.noclose = noclose; 3554cd407ecSMaxim Sobolev logpar.outfn = outfn; 3564cd407ecSMaxim Sobolev if (log_reopen && outfd >= 0 && 3574cd407ecSMaxim Sobolev sigaction(SIGHUP, &act_hup, NULL) == -1) { 3584cd407ecSMaxim Sobolev warn("sigaction"); 3594cd407ecSMaxim Sobolev goto exit; 3604cd407ecSMaxim Sobolev } 361b6193c24SMikolaj Golub restart: 3626b4ef4b1SIhor Antonov if (pipe(pfd)) { 36353d49b37SJilles Tjoelker err(1, "pipe"); 3646b4ef4b1SIhor Antonov } 365195fc497SMikolaj Golub /* 36653d49b37SJilles Tjoelker * Spawn a child to exec the command. 3672ad43027SMikolaj Golub */ 36853d49b37SJilles Tjoelker child_gone = 0; 3692ad43027SMikolaj Golub pid = fork(); 3702ad43027SMikolaj Golub if (pid == -1) { 3719da0ef13SMikolaj Golub warn("fork"); 3729da0ef13SMikolaj Golub goto exit; 37353d49b37SJilles Tjoelker } else if (pid > 0) { 37453d49b37SJilles Tjoelker /* 37553d49b37SJilles Tjoelker * Unblock SIGTERM after we know we have a valid 37653d49b37SJilles Tjoelker * child PID to signal. 37753d49b37SJilles Tjoelker */ 37853d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { 37953d49b37SJilles Tjoelker warn("sigprocmask"); 38053d49b37SJilles Tjoelker goto exit; 38153d49b37SJilles Tjoelker } 38253d49b37SJilles Tjoelker close(pfd[1]); 38353d49b37SJilles Tjoelker pfd[1] = -1; 3842ad43027SMikolaj Golub } 3852ad43027SMikolaj Golub } 386195fc497SMikolaj Golub if (pid <= 0) { 3872ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 388c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 389846be7bdSPoul-Henning Kamp 3906b4ef4b1SIhor Antonov if (user != NULL) { 3912ad43027SMikolaj Golub restrict_process(user); 3926b4ef4b1SIhor Antonov } 39353d49b37SJilles Tjoelker /* 39453d49b37SJilles Tjoelker * When forking, the child gets the original sigmask, 39553d49b37SJilles Tjoelker * and dup'd pipes. 39653d49b37SJilles Tjoelker */ 39753d49b37SJilles Tjoelker if (pid == 0) { 39853d49b37SJilles Tjoelker close(pfd[0]); 3996b4ef4b1SIhor Antonov if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) { 40053d49b37SJilles Tjoelker err(1, "sigprogmask"); 4016b4ef4b1SIhor Antonov } 40253d49b37SJilles Tjoelker if (stdmask & STDERR_FILENO) { 4036b4ef4b1SIhor Antonov if (dup2(pfd[1], STDERR_FILENO) == -1) { 40453d49b37SJilles Tjoelker err(1, "dup2"); 40553d49b37SJilles Tjoelker } 4066b4ef4b1SIhor Antonov } 40753d49b37SJilles Tjoelker if (stdmask & STDOUT_FILENO) { 4086b4ef4b1SIhor Antonov if (dup2(pfd[1], STDOUT_FILENO) == -1) { 40953d49b37SJilles Tjoelker err(1, "dup2"); 41053d49b37SJilles Tjoelker } 4116b4ef4b1SIhor Antonov } 41253d49b37SJilles Tjoelker if (pfd[1] != STDERR_FILENO && 4136b4ef4b1SIhor Antonov pfd[1] != STDOUT_FILENO) { 41453d49b37SJilles Tjoelker close(pfd[1]); 41553d49b37SJilles Tjoelker } 4166b4ef4b1SIhor Antonov } 417bd06a3ecSMike Barcroft execvp(argv[0], argv); 418846be7bdSPoul-Henning Kamp /* 4192ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 4202ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 421846be7bdSPoul-Henning Kamp */ 4222ad43027SMikolaj Golub err(1, "%s", argv[0]); 4232ad43027SMikolaj Golub } 42453d49b37SJilles Tjoelker setproctitle("%s[%d]", title, (int)pid); 42553d49b37SJilles Tjoelker /* 42653d49b37SJilles Tjoelker * As we have closed the write end of pipe for parent process, 42753d49b37SJilles Tjoelker * we might detect the child's exit by reading EOF. The child 42853d49b37SJilles Tjoelker * might have closed its stdout and stderr, so we must wait for 42953d49b37SJilles Tjoelker * the SIGCHLD to ensure that the process is actually gone. 43053d49b37SJilles Tjoelker */ 43153d49b37SJilles Tjoelker for (;;) { 43253d49b37SJilles Tjoelker /* 43353d49b37SJilles Tjoelker * We block SIGCHLD when listening, but SIGTERM we accept 43453d49b37SJilles Tjoelker * so the read() won't block if we wish to depart. 43553d49b37SJilles Tjoelker * 43653d49b37SJilles Tjoelker * Upon receiving SIGTERM, we have several options after 43753d49b37SJilles Tjoelker * sending the SIGTERM to our child: 43853d49b37SJilles Tjoelker * - read until EOF 43953d49b37SJilles Tjoelker * - read until EOF but only for a while 44053d49b37SJilles Tjoelker * - bail immediately 44153d49b37SJilles Tjoelker * 44253d49b37SJilles Tjoelker * We go for the third, as otherwise we have no guarantee 44353d49b37SJilles Tjoelker * that we won't block indefinitely if the child refuses 44453d49b37SJilles Tjoelker * to depart. To handle the second option, a different 44553d49b37SJilles Tjoelker * approach would be needed (procctl()?) 44653d49b37SJilles Tjoelker */ 44753d49b37SJilles Tjoelker if (child_gone && child_eof) { 44853d49b37SJilles Tjoelker break; 44953d49b37SJilles Tjoelker } else if (terminate) { 45053d49b37SJilles Tjoelker goto exit; 45153d49b37SJilles Tjoelker } else if (!child_eof) { 45253d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) { 45353d49b37SJilles Tjoelker warn("sigprocmask"); 45453d49b37SJilles Tjoelker goto exit; 45553d49b37SJilles Tjoelker } 45653d49b37SJilles Tjoelker child_eof = !listen_child(pfd[0], &logpar); 45753d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { 45853d49b37SJilles Tjoelker warn("sigprocmask"); 45953d49b37SJilles Tjoelker goto exit; 46053d49b37SJilles Tjoelker } 46153d49b37SJilles Tjoelker } else { 46253d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) { 46353d49b37SJilles Tjoelker warn("sigprocmask"); 46453d49b37SJilles Tjoelker goto exit; 46553d49b37SJilles Tjoelker } 46653d49b37SJilles Tjoelker while (!terminate && !child_gone) 46753d49b37SJilles Tjoelker sigsuspend(&mask_orig); 46853d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) { 46953d49b37SJilles Tjoelker warn("sigprocmask"); 47053d49b37SJilles Tjoelker goto exit; 47153d49b37SJilles Tjoelker } 47253d49b37SJilles Tjoelker } 47353d49b37SJilles Tjoelker } 4746b4ef4b1SIhor Antonov if (restart && !terminate) { 47509a3675dSConrad Meyer daemon_sleep(restart, 0); 4766b4ef4b1SIhor Antonov } 47753d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) { 47853d49b37SJilles Tjoelker warn("sigprocmask"); 47953d49b37SJilles Tjoelker goto exit; 48053d49b37SJilles Tjoelker } 48153d49b37SJilles Tjoelker if (restart && !terminate) { 48253d49b37SJilles Tjoelker close(pfd[0]); 48353d49b37SJilles Tjoelker pfd[0] = -1; 484b6193c24SMikolaj Golub goto restart; 485b6193c24SMikolaj Golub } 4869da0ef13SMikolaj Golub exit: 48753d49b37SJilles Tjoelker close(outfd); 48853d49b37SJilles Tjoelker close(pfd[0]); 48953d49b37SJilles Tjoelker close(pfd[1]); 490*f2f9d31dSIhor Antonov if (syslog_enabled) { 49153d49b37SJilles Tjoelker closelog(); 4926b4ef4b1SIhor Antonov } 493c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 49432b17786SJohn-Mark Gurney pidfile_remove(ppfh); 4959da0ef13SMikolaj Golub exit(1); /* If daemon(3) succeeded exit status does not matter. */ 496bd06a3ecSMike Barcroft } 497bd06a3ecSMike Barcroft 498bd06a3ecSMike Barcroft static void 49953d49b37SJilles Tjoelker daemon_sleep(time_t secs, long nsecs) 500195fc497SMikolaj Golub { 50153d49b37SJilles Tjoelker struct timespec ts = { secs, nsecs }; 50209a3675dSConrad Meyer 50309a3675dSConrad Meyer while (!terminate && nanosleep(&ts, &ts) == -1) { 5046b4ef4b1SIhor Antonov if (errno != EINTR) { 50553d49b37SJilles Tjoelker err(1, "nanosleep"); 50653d49b37SJilles Tjoelker } 50753d49b37SJilles Tjoelker } 5086b4ef4b1SIhor Antonov } 50953d49b37SJilles Tjoelker 51053d49b37SJilles Tjoelker static void 51153d49b37SJilles Tjoelker open_pid_files(const char *pidfile, const char *ppidfile, 51253d49b37SJilles Tjoelker struct pidfh **pfh, struct pidfh **ppfh) 51353d49b37SJilles Tjoelker { 51453d49b37SJilles Tjoelker pid_t fpid; 51553d49b37SJilles Tjoelker int serrno; 51653d49b37SJilles Tjoelker 51753d49b37SJilles Tjoelker if (pidfile) { 51853d49b37SJilles Tjoelker *pfh = pidfile_open(pidfile, 0600, &fpid); 51953d49b37SJilles Tjoelker if (*pfh == NULL) { 52053d49b37SJilles Tjoelker if (errno == EEXIST) { 52153d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 52253d49b37SJilles Tjoelker fpid); 52353d49b37SJilles Tjoelker } 52453d49b37SJilles Tjoelker err(2, "pidfile ``%s''", pidfile); 52553d49b37SJilles Tjoelker } 52653d49b37SJilles Tjoelker } 52753d49b37SJilles Tjoelker /* Do the same for the actual daemon process. */ 52853d49b37SJilles Tjoelker if (ppidfile) { 52953d49b37SJilles Tjoelker *ppfh = pidfile_open(ppidfile, 0600, &fpid); 53053d49b37SJilles Tjoelker if (*ppfh == NULL) { 53153d49b37SJilles Tjoelker serrno = errno; 53253d49b37SJilles Tjoelker pidfile_remove(*pfh); 53353d49b37SJilles Tjoelker errno = serrno; 53453d49b37SJilles Tjoelker if (errno == EEXIST) { 53553d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 53653d49b37SJilles Tjoelker fpid); 53753d49b37SJilles Tjoelker } 53853d49b37SJilles Tjoelker err(2, "ppidfile ``%s''", ppidfile); 53953d49b37SJilles Tjoelker } 54053d49b37SJilles Tjoelker } 54153d49b37SJilles Tjoelker } 54253d49b37SJilles Tjoelker 54353d49b37SJilles Tjoelker static int 54453d49b37SJilles Tjoelker get_log_mapping(const char *str, const CODE *c) 54553d49b37SJilles Tjoelker { 54653d49b37SJilles Tjoelker const CODE *cp; 54753d49b37SJilles Tjoelker for (cp = c; cp->c_name; cp++) 5486b4ef4b1SIhor Antonov if (strcmp(cp->c_name, str) == 0) { 54953d49b37SJilles Tjoelker return cp->c_val; 5506b4ef4b1SIhor Antonov } 55153d49b37SJilles Tjoelker return -1; 552195fc497SMikolaj Golub } 553195fc497SMikolaj Golub 554195fc497SMikolaj Golub static void 555e6d4b388STom Rhodes restrict_process(const char *user) 55612d7249eSTom Rhodes { 55712d7249eSTom Rhodes struct passwd *pw = NULL; 55812d7249eSTom Rhodes 559e6d4b388STom Rhodes pw = getpwnam(user); 5606b4ef4b1SIhor Antonov if (pw == NULL) { 561e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 5626b4ef4b1SIhor Antonov } 56312d7249eSTom Rhodes 5646b4ef4b1SIhor Antonov if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) { 565e6d4b388STom Rhodes errx(1, "failed to set user environment"); 5666b4ef4b1SIhor Antonov } 5676b3ad1d7SMaxim Sobolev 5686b3ad1d7SMaxim Sobolev setenv("USER", pw->pw_name, 1); 5696b3ad1d7SMaxim Sobolev setenv("HOME", pw->pw_dir, 1); 5706b3ad1d7SMaxim Sobolev setenv("SHELL", *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL, 1); 57112d7249eSTom Rhodes } 57212d7249eSTom Rhodes 57353d49b37SJilles Tjoelker /* 57453d49b37SJilles Tjoelker * We try to collect whole lines terminated by '\n'. Otherwise we collect a 57553d49b37SJilles Tjoelker * full buffer, and then output it. 57653d49b37SJilles Tjoelker * 57753d49b37SJilles Tjoelker * Return value of 0 is assumed to mean EOF or error, and 1 indicates to 57853d49b37SJilles Tjoelker * continue reading. 57953d49b37SJilles Tjoelker */ 580b6193c24SMikolaj Golub static int 58153d49b37SJilles Tjoelker listen_child(int fd, struct log_params *logpar) 5822ad43027SMikolaj Golub { 58353d49b37SJilles Tjoelker static unsigned char buf[LBUF_SIZE]; 58453d49b37SJilles Tjoelker static size_t bytes_read = 0; 58553d49b37SJilles Tjoelker int rv; 5862ad43027SMikolaj Golub 58753d49b37SJilles Tjoelker assert(logpar); 58853d49b37SJilles Tjoelker assert(bytes_read < LBUF_SIZE - 1); 58953d49b37SJilles Tjoelker 5906b4ef4b1SIhor Antonov if (do_log_reopen) { 5914cd407ecSMaxim Sobolev reopen_log(logpar); 5926b4ef4b1SIhor Antonov } 59353d49b37SJilles Tjoelker rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1); 59453d49b37SJilles Tjoelker if (rv > 0) { 59553d49b37SJilles Tjoelker unsigned char *cp; 59653d49b37SJilles Tjoelker 59753d49b37SJilles Tjoelker bytes_read += rv; 59853d49b37SJilles Tjoelker assert(bytes_read <= LBUF_SIZE - 1); 59953d49b37SJilles Tjoelker /* Always NUL-terminate just in case. */ 60053d49b37SJilles Tjoelker buf[LBUF_SIZE - 1] = '\0'; 60153d49b37SJilles Tjoelker /* 60253d49b37SJilles Tjoelker * Chomp line by line until we run out of buffer. 60353d49b37SJilles Tjoelker * This does not take NUL characters into account. 60453d49b37SJilles Tjoelker */ 60553d49b37SJilles Tjoelker while ((cp = memchr(buf, '\n', bytes_read)) != NULL) { 60653d49b37SJilles Tjoelker size_t bytes_line = cp - buf + 1; 60753d49b37SJilles Tjoelker assert(bytes_line <= bytes_read); 60853d49b37SJilles Tjoelker do_output(buf, bytes_line, logpar); 60953d49b37SJilles Tjoelker bytes_read -= bytes_line; 61053d49b37SJilles Tjoelker memmove(buf, cp + 1, bytes_read); 611195fc497SMikolaj Golub } 61253d49b37SJilles Tjoelker /* Wait until the buffer is full. */ 6136b4ef4b1SIhor Antonov if (bytes_read < LBUF_SIZE - 1) { 61453d49b37SJilles Tjoelker return 1; 6156b4ef4b1SIhor Antonov } 61653d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 61753d49b37SJilles Tjoelker bytes_read = 0; 61853d49b37SJilles Tjoelker return 1; 61953d49b37SJilles Tjoelker } else if (rv == -1) { 62053d49b37SJilles Tjoelker /* EINTR should trigger another read. */ 62153d49b37SJilles Tjoelker if (errno == EINTR) { 62253d49b37SJilles Tjoelker return 1; 62353d49b37SJilles Tjoelker } else { 62453d49b37SJilles Tjoelker warn("read"); 62553d49b37SJilles Tjoelker return 0; 626c60d51f9SMikolaj Golub } 62753d49b37SJilles Tjoelker } 62853d49b37SJilles Tjoelker /* Upon EOF, we have to flush what's left of the buffer. */ 62953d49b37SJilles Tjoelker if (bytes_read > 0) { 63053d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 63153d49b37SJilles Tjoelker bytes_read = 0; 63253d49b37SJilles Tjoelker } 63353d49b37SJilles Tjoelker return 0; 63453d49b37SJilles Tjoelker } 63553d49b37SJilles Tjoelker 63653d49b37SJilles Tjoelker /* 63753d49b37SJilles Tjoelker * The default behavior is to stay silent if the user wants to redirect 63853d49b37SJilles Tjoelker * output to a file and/or syslog. If neither are provided, then we bounce 63953d49b37SJilles Tjoelker * everything back to parent's stdout. 64053d49b37SJilles Tjoelker */ 64153d49b37SJilles Tjoelker static void 64253d49b37SJilles Tjoelker do_output(const unsigned char *buf, size_t len, struct log_params *logpar) 64353d49b37SJilles Tjoelker { 64453d49b37SJilles Tjoelker assert(len <= LBUF_SIZE); 64553d49b37SJilles Tjoelker assert(logpar); 64653d49b37SJilles Tjoelker 6476b4ef4b1SIhor Antonov if (len < 1) { 64853d49b37SJilles Tjoelker return; 6496b4ef4b1SIhor Antonov } 650*f2f9d31dSIhor Antonov if (logpar->syslog_enabled) { 65153d49b37SJilles Tjoelker syslog(logpar->logpri, "%.*s", (int)len, buf); 6526b4ef4b1SIhor Antonov } 65353d49b37SJilles Tjoelker if (logpar->outfd != -1) { 65453d49b37SJilles Tjoelker if (write(logpar->outfd, buf, len) == -1) 65553d49b37SJilles Tjoelker warn("write"); 65653d49b37SJilles Tjoelker } 657*f2f9d31dSIhor Antonov if (logpar->noclose && !logpar->syslog_enabled && logpar->outfd == -1) { 65853d49b37SJilles Tjoelker printf("%.*s", (int)len, buf); 65953d49b37SJilles Tjoelker } 6606b4ef4b1SIhor Antonov } 66153d49b37SJilles Tjoelker 66253d49b37SJilles Tjoelker /* 66353d49b37SJilles Tjoelker * We use the global PID acquired directly from fork. If there is no valid 66453d49b37SJilles Tjoelker * child pid, the handler should be blocked and/or child_gone == 1. 66553d49b37SJilles Tjoelker */ 66653d49b37SJilles Tjoelker static void 66753d49b37SJilles Tjoelker handle_term(int signo) 66853d49b37SJilles Tjoelker { 6696b4ef4b1SIhor Antonov if (pid > 0 && !child_gone) { 67053d49b37SJilles Tjoelker kill(pid, signo); 6716b4ef4b1SIhor Antonov } 672b6193c24SMikolaj Golub terminate = 1; 673195fc497SMikolaj Golub } 67453d49b37SJilles Tjoelker 67553d49b37SJilles Tjoelker static void 6764cd407ecSMaxim Sobolev handle_chld(int signo __unused) 67753d49b37SJilles Tjoelker { 6784cd407ecSMaxim Sobolev 67953d49b37SJilles Tjoelker for (;;) { 68053d49b37SJilles Tjoelker int rv = waitpid(-1, NULL, WNOHANG); 68153d49b37SJilles Tjoelker if (pid == rv) { 68253d49b37SJilles Tjoelker child_gone = 1; 68353d49b37SJilles Tjoelker break; 68453d49b37SJilles Tjoelker } else if (rv == -1 && errno != EINTR) { 68553d49b37SJilles Tjoelker warn("waitpid"); 68653d49b37SJilles Tjoelker return; 6872ad43027SMikolaj Golub } 6882ad43027SMikolaj Golub } 6892ad43027SMikolaj Golub } 6902ad43027SMikolaj Golub 6912ad43027SMikolaj Golub static void 6924cd407ecSMaxim Sobolev handle_hup(int signo __unused) 6934cd407ecSMaxim Sobolev { 6944cd407ecSMaxim Sobolev 6954cd407ecSMaxim Sobolev do_log_reopen = 1; 6964cd407ecSMaxim Sobolev } 6974cd407ecSMaxim Sobolev 6984cd407ecSMaxim Sobolev static int 6994cd407ecSMaxim Sobolev open_log(const char *outfn) 7004cd407ecSMaxim Sobolev { 7014cd407ecSMaxim Sobolev 7024cd407ecSMaxim Sobolev return open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600); 7034cd407ecSMaxim Sobolev } 7044cd407ecSMaxim Sobolev 7054cd407ecSMaxim Sobolev static void 7064cd407ecSMaxim Sobolev reopen_log(struct log_params *lpp) 7074cd407ecSMaxim Sobolev { 7084cd407ecSMaxim Sobolev int outfd; 7094cd407ecSMaxim Sobolev 7104cd407ecSMaxim Sobolev do_log_reopen = 0; 7114cd407ecSMaxim Sobolev outfd = open_log(lpp->outfn); 7126b4ef4b1SIhor Antonov if (lpp->outfd >= 0) { 7134cd407ecSMaxim Sobolev close(lpp->outfd); 7146b4ef4b1SIhor Antonov } 7154cd407ecSMaxim Sobolev lpp->outfd = outfd; 7164cd407ecSMaxim Sobolev } 7174cd407ecSMaxim Sobolev 718