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 { 63*e70444c6SIhor Antonov const char *output_filename; 64*e70444c6SIhor Antonov int syslog_priority; 6553d49b37SJilles Tjoelker int noclose; 66*e70444c6SIhor Antonov int output_fd; 67f2f9d31dSIhor 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; 14697022e90SIhor Antonov bool log_reopen = false; 147e745dc22SIhor Antonov char *p = NULL; 148e745dc22SIhor Antonov const char *pidfile = NULL; 149*e70444c6SIhor Antonov const char *syslog_tag = "daemon"; 150e745dc22SIhor Antonov const char *ppidfile = NULL; 151e745dc22SIhor Antonov const char *title = NULL; 152e745dc22SIhor Antonov const char *user = NULL; 153e745dc22SIhor Antonov int ch = 0; 154e745dc22SIhor Antonov int child_eof = 0; 155*e70444c6SIhor Antonov int syslog_facility = LOG_DAEMON; 156e745dc22SIhor Antonov int nochdir = 1; 157e745dc22SIhor Antonov int pfd[2] = { -1, -1 }; 158e745dc22SIhor Antonov int restart = 0; 159e745dc22SIhor Antonov int stdmask = STDOUT_FILENO | STDERR_FILENO; 160*e70444c6SIhor Antonov struct log_params logpar = { 161*e70444c6SIhor Antonov .syslog_enabled = false, 162*e70444c6SIhor Antonov .syslog_priority = LOG_NOTICE, 163*e70444c6SIhor Antonov .noclose = 1, 164*e70444c6SIhor Antonov .output_fd = -1, 165*e70444c6SIhor Antonov .output_filename = NULL 166*e70444c6SIhor Antonov }; 167e745dc22SIhor Antonov struct pidfh *ppfh = NULL; 168e745dc22SIhor Antonov struct pidfh *pfh = NULL; 169e745dc22SIhor Antonov sigset_t mask_orig; 170e745dc22SIhor Antonov sigset_t mask_read; 171e745dc22SIhor Antonov sigset_t mask_term; 172e745dc22SIhor Antonov sigset_t mask_susp; 173bd06a3ecSMike Barcroft 174e745dc22SIhor Antonov sigemptyset(&mask_susp); 175e745dc22SIhor Antonov sigemptyset(&mask_read); 176e745dc22SIhor Antonov sigemptyset(&mask_term); 17784866cefSIhor Antonov sigemptyset(&mask_orig); 178e745dc22SIhor Antonov 1790a402ad2SIhor Antonov while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { 180bd06a3ecSMike Barcroft switch (ch) { 181bd06a3ecSMike Barcroft case 'c': 182bd06a3ecSMike Barcroft nochdir = 0; 183bd06a3ecSMike Barcroft break; 184bd06a3ecSMike Barcroft case 'f': 185*e70444c6SIhor Antonov logpar.noclose = 0; 186bd06a3ecSMike Barcroft break; 1874cd407ecSMaxim Sobolev case 'H': 18897022e90SIhor Antonov log_reopen = true; 1894cd407ecSMaxim Sobolev break; 19053d49b37SJilles Tjoelker case 'l': 191*e70444c6SIhor Antonov syslog_facility = get_log_mapping(optarg, facilitynames); 192*e70444c6SIhor Antonov if (syslog_facility == -1) { 19353d49b37SJilles Tjoelker errx(5, "unrecognized syslog facility"); 1946b4ef4b1SIhor Antonov } 195*e70444c6SIhor Antonov logpar.syslog_enabled = true; 19653d49b37SJilles Tjoelker break; 19753d49b37SJilles Tjoelker case 'm': 19853d49b37SJilles Tjoelker stdmask = strtol(optarg, &p, 10); 1996b4ef4b1SIhor Antonov if (p == optarg || stdmask < 0 || stdmask > 3) { 20053d49b37SJilles Tjoelker errx(6, "unrecognized listening mask"); 2016b4ef4b1SIhor Antonov } 20253d49b37SJilles Tjoelker break; 20353d49b37SJilles Tjoelker case 'o': 204*e70444c6SIhor Antonov logpar.output_filename = optarg; 20553d49b37SJilles Tjoelker break; 206846be7bdSPoul-Henning Kamp case 'p': 207846be7bdSPoul-Henning Kamp pidfile = optarg; 208846be7bdSPoul-Henning Kamp break; 20932b17786SJohn-Mark Gurney case 'P': 21032b17786SJohn-Mark Gurney ppidfile = optarg; 21132b17786SJohn-Mark Gurney break; 212b6193c24SMikolaj Golub case 'r': 213b6193c24SMikolaj Golub restart = 1; 214b6193c24SMikolaj Golub break; 21537820b87SIan Lepore case 'R': 21637820b87SIan Lepore restart = strtol(optarg, &p, 0); 2176b4ef4b1SIhor Antonov if (p == optarg || restart < 1) { 21837820b87SIan Lepore errx(6, "invalid restart delay"); 2196b4ef4b1SIhor Antonov } 22037820b87SIan Lepore break; 22153d49b37SJilles Tjoelker case 's': 222*e70444c6SIhor Antonov logpar.syslog_priority = get_log_mapping(optarg, prioritynames); 223*e70444c6SIhor Antonov if (logpar.syslog_priority == -1) { 22453d49b37SJilles Tjoelker errx(4, "unrecognized syslog priority"); 2256b4ef4b1SIhor Antonov } 226*e70444c6SIhor Antonov logpar.syslog_enabled = true; 22753d49b37SJilles Tjoelker break; 22853d49b37SJilles Tjoelker case 'S': 229*e70444c6SIhor Antonov logpar.syslog_enabled = true; 23053d49b37SJilles Tjoelker break; 231112bfcf5SConrad Meyer case 't': 232112bfcf5SConrad Meyer title = optarg; 233112bfcf5SConrad Meyer break; 23453d49b37SJilles Tjoelker case 'T': 235*e70444c6SIhor Antonov syslog_tag = optarg; 236*e70444c6SIhor Antonov logpar.syslog_enabled = true; 23753d49b37SJilles Tjoelker break; 238e6d4b388STom Rhodes case 'u': 239e6d4b388STom Rhodes user = optarg; 240e6d4b388STom Rhodes break; 2410a402ad2SIhor Antonov case 'h': 2420a402ad2SIhor Antonov usage(0); 2430a402ad2SIhor Antonov __builtin_unreachable(); 244bd06a3ecSMike Barcroft default: 2450a402ad2SIhor Antonov usage(1); 246bd06a3ecSMike Barcroft } 247bd06a3ecSMike Barcroft } 248bd06a3ecSMike Barcroft argc -= optind; 249bd06a3ecSMike Barcroft argv += optind; 250bd06a3ecSMike Barcroft 2516b4ef4b1SIhor Antonov if (argc == 0) { 2520a402ad2SIhor Antonov usage(1); 2536b4ef4b1SIhor Antonov } 25412d7249eSTom Rhodes 2556b4ef4b1SIhor Antonov if (!title) { 25653d49b37SJilles Tjoelker title = argv[0]; 2576b4ef4b1SIhor Antonov } 25853d49b37SJilles Tjoelker 259*e70444c6SIhor Antonov if (logpar.output_filename) { 260*e70444c6SIhor Antonov logpar.output_fd = open_log(logpar.output_filename); 261*e70444c6SIhor Antonov if (logpar.output_fd == -1) { 26253d49b37SJilles Tjoelker err(7, "open"); 26353d49b37SJilles Tjoelker } 2646b4ef4b1SIhor Antonov } 26553d49b37SJilles Tjoelker 266*e70444c6SIhor Antonov if (logpar.syslog_enabled) { 267*e70444c6SIhor Antonov openlog(syslog_tag, LOG_PID | LOG_NDELAY, syslog_facility); 2686b4ef4b1SIhor Antonov } 26953d49b37SJilles Tjoelker 270846be7bdSPoul-Henning Kamp /* 271846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 272846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 273846be7bdSPoul-Henning Kamp */ 27453d49b37SJilles Tjoelker open_pid_files(pidfile, ppidfile, &pfh, &ppfh); 275*e70444c6SIhor Antonov if (daemon(nochdir, logpar.noclose) == -1) { 2769da0ef13SMikolaj Golub warn("daemon"); 2779da0ef13SMikolaj Golub goto exit; 2789da0ef13SMikolaj Golub } 2799da0ef13SMikolaj Golub /* Write out parent pidfile if needed. */ 2809da0ef13SMikolaj Golub pidfile_write(ppfh); 281203df05bSIhor Antonov 282195fc497SMikolaj Golub /* 283203df05bSIhor Antonov * Supervision mode is enabled if one of the following options are used: 284203df05bSIhor Antonov * --child-pidfile -p 285203df05bSIhor Antonov * --supervisor-pidfile -P 286203df05bSIhor Antonov * --restart -r / --restart-delay -R 287203df05bSIhor Antonov * --syslog -S 288203df05bSIhor Antonov * --syslog-facility -l 289203df05bSIhor Antonov * --syslog-priority -s 290203df05bSIhor Antonov * --syslog-tag -T 291203df05bSIhor Antonov * 292203df05bSIhor Antonov * In supervision mode daemon executes the command in a forked process 293203df05bSIhor Antonov * and observes the child by waiting for SIGCHILD. In supervision mode 294203df05bSIhor Antonov * daemon must never exit before the child, this is necessary to prevent 295203df05bSIhor Antonov * orphaning the child and leaving a stale pid file. 296203df05bSIhor Antonov * To achieve this daemon catches SIGTERM and 297203df05bSIhor Antonov * forwards it to the child, expecting to get SIGCHLD eventually. 298195fc497SMikolaj Golub */ 299203df05bSIhor Antonov supervision_enabled = pidfile != NULL || 300203df05bSIhor Antonov ppidfile != NULL || 301203df05bSIhor Antonov restart != 0 || 302*e70444c6SIhor Antonov logpar.output_fd != -1 || 303*e70444c6SIhor Antonov logpar.syslog_enabled == true; 304203df05bSIhor Antonov 305203df05bSIhor Antonov if (supervision_enabled) { 306259ed21dSIhor Antonov struct sigaction act_term = { 0 }; 307259ed21dSIhor Antonov struct sigaction act_chld = { 0 }; 308259ed21dSIhor Antonov struct sigaction act_hup = { 0 }; 30953d49b37SJilles Tjoelker 31053d49b37SJilles Tjoelker /* Avoid PID racing with SIGCHLD and SIGTERM. */ 31153d49b37SJilles Tjoelker act_term.sa_handler = handle_term; 31253d49b37SJilles Tjoelker sigemptyset(&act_term.sa_mask); 31353d49b37SJilles Tjoelker sigaddset(&act_term.sa_mask, SIGCHLD); 31453d49b37SJilles Tjoelker 31553d49b37SJilles Tjoelker act_chld.sa_handler = handle_chld; 31653d49b37SJilles Tjoelker sigemptyset(&act_chld.sa_mask); 31753d49b37SJilles Tjoelker sigaddset(&act_chld.sa_mask, SIGTERM); 31853d49b37SJilles Tjoelker 3194cd407ecSMaxim Sobolev act_hup.sa_handler = handle_hup; 3204cd407ecSMaxim Sobolev sigemptyset(&act_hup.sa_mask); 3214cd407ecSMaxim Sobolev 32253d49b37SJilles Tjoelker /* Block these when avoiding racing before sigsuspend(). */ 32353d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGTERM); 32453d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGCHLD); 32553d49b37SJilles Tjoelker /* Block SIGTERM when we lack a valid child PID. */ 32653d49b37SJilles Tjoelker sigaddset(&mask_term, SIGTERM); 3272ad43027SMikolaj Golub /* 32853d49b37SJilles Tjoelker * When reading, we wish to avoid SIGCHLD. SIGTERM 32953d49b37SJilles Tjoelker * has to be caught, otherwise we'll be stuck until 33053d49b37SJilles Tjoelker * the read() returns - if it returns. 331195fc497SMikolaj Golub */ 33253d49b37SJilles Tjoelker sigaddset(&mask_read, SIGCHLD); 33353d49b37SJilles Tjoelker /* Block SIGTERM to avoid racing until we have forked. */ 33453d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) { 3359da0ef13SMikolaj Golub warn("sigprocmask"); 3369da0ef13SMikolaj Golub goto exit; 3379da0ef13SMikolaj Golub } 33853d49b37SJilles Tjoelker if (sigaction(SIGTERM, &act_term, NULL) == -1) { 33953d49b37SJilles Tjoelker warn("sigaction"); 34053d49b37SJilles Tjoelker goto exit; 34153d49b37SJilles Tjoelker } 34253d49b37SJilles Tjoelker if (sigaction(SIGCHLD, &act_chld, NULL) == -1) { 34353d49b37SJilles Tjoelker warn("sigaction"); 34453d49b37SJilles Tjoelker goto exit; 34553d49b37SJilles Tjoelker } 34653c49998SMikolaj Golub /* 34753c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 34853c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 34953c49998SMikolaj Golub * not have superuser privileges. 35053c49998SMikolaj Golub */ 35153c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 352*e70444c6SIhor Antonov if (log_reopen && logpar.output_fd >= 0 && 3534cd407ecSMaxim Sobolev sigaction(SIGHUP, &act_hup, NULL) == -1) { 3544cd407ecSMaxim Sobolev warn("sigaction"); 3554cd407ecSMaxim Sobolev goto exit; 3564cd407ecSMaxim Sobolev } 357b6193c24SMikolaj Golub restart: 3586b4ef4b1SIhor Antonov if (pipe(pfd)) { 35953d49b37SJilles Tjoelker err(1, "pipe"); 3606b4ef4b1SIhor Antonov } 361195fc497SMikolaj Golub /* 36253d49b37SJilles Tjoelker * Spawn a child to exec the command. 3632ad43027SMikolaj Golub */ 36453d49b37SJilles Tjoelker child_gone = 0; 3652ad43027SMikolaj Golub pid = fork(); 3662ad43027SMikolaj Golub if (pid == -1) { 3679da0ef13SMikolaj Golub warn("fork"); 3689da0ef13SMikolaj Golub goto exit; 36953d49b37SJilles Tjoelker } else if (pid > 0) { 37053d49b37SJilles Tjoelker /* 37153d49b37SJilles Tjoelker * Unblock SIGTERM after we know we have a valid 37253d49b37SJilles Tjoelker * child PID to signal. 37353d49b37SJilles Tjoelker */ 37453d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { 37553d49b37SJilles Tjoelker warn("sigprocmask"); 37653d49b37SJilles Tjoelker goto exit; 37753d49b37SJilles Tjoelker } 37853d49b37SJilles Tjoelker close(pfd[1]); 37953d49b37SJilles Tjoelker pfd[1] = -1; 3802ad43027SMikolaj Golub } 3812ad43027SMikolaj Golub } 382195fc497SMikolaj Golub if (pid <= 0) { 3832ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 384c6262cb6SPawel Jakub Dawidek pidfile_write(pfh); 385846be7bdSPoul-Henning Kamp 3866b4ef4b1SIhor Antonov if (user != NULL) { 3872ad43027SMikolaj Golub restrict_process(user); 3886b4ef4b1SIhor Antonov } 38953d49b37SJilles Tjoelker /* 39053d49b37SJilles Tjoelker * When forking, the child gets the original sigmask, 39153d49b37SJilles Tjoelker * and dup'd pipes. 39253d49b37SJilles Tjoelker */ 39353d49b37SJilles Tjoelker if (pid == 0) { 39453d49b37SJilles Tjoelker close(pfd[0]); 3956b4ef4b1SIhor Antonov if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) { 39653d49b37SJilles Tjoelker err(1, "sigprogmask"); 3976b4ef4b1SIhor Antonov } 39853d49b37SJilles Tjoelker if (stdmask & STDERR_FILENO) { 3996b4ef4b1SIhor Antonov if (dup2(pfd[1], STDERR_FILENO) == -1) { 40053d49b37SJilles Tjoelker err(1, "dup2"); 40153d49b37SJilles Tjoelker } 4026b4ef4b1SIhor Antonov } 40353d49b37SJilles Tjoelker if (stdmask & STDOUT_FILENO) { 4046b4ef4b1SIhor Antonov if (dup2(pfd[1], STDOUT_FILENO) == -1) { 40553d49b37SJilles Tjoelker err(1, "dup2"); 40653d49b37SJilles Tjoelker } 4076b4ef4b1SIhor Antonov } 40853d49b37SJilles Tjoelker if (pfd[1] != STDERR_FILENO && 4096b4ef4b1SIhor Antonov pfd[1] != STDOUT_FILENO) { 41053d49b37SJilles Tjoelker close(pfd[1]); 41153d49b37SJilles Tjoelker } 4126b4ef4b1SIhor Antonov } 413bd06a3ecSMike Barcroft execvp(argv[0], argv); 414846be7bdSPoul-Henning Kamp /* 4152ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 4162ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 417846be7bdSPoul-Henning Kamp */ 4182ad43027SMikolaj Golub err(1, "%s", argv[0]); 4192ad43027SMikolaj Golub } 42053d49b37SJilles Tjoelker setproctitle("%s[%d]", title, (int)pid); 42153d49b37SJilles Tjoelker /* 42253d49b37SJilles Tjoelker * As we have closed the write end of pipe for parent process, 42353d49b37SJilles Tjoelker * we might detect the child's exit by reading EOF. The child 42453d49b37SJilles Tjoelker * might have closed its stdout and stderr, so we must wait for 42553d49b37SJilles Tjoelker * the SIGCHLD to ensure that the process is actually gone. 42653d49b37SJilles Tjoelker */ 42753d49b37SJilles Tjoelker for (;;) { 42853d49b37SJilles Tjoelker /* 42953d49b37SJilles Tjoelker * We block SIGCHLD when listening, but SIGTERM we accept 43053d49b37SJilles Tjoelker * so the read() won't block if we wish to depart. 43153d49b37SJilles Tjoelker * 43253d49b37SJilles Tjoelker * Upon receiving SIGTERM, we have several options after 43353d49b37SJilles Tjoelker * sending the SIGTERM to our child: 43453d49b37SJilles Tjoelker * - read until EOF 43553d49b37SJilles Tjoelker * - read until EOF but only for a while 43653d49b37SJilles Tjoelker * - bail immediately 43753d49b37SJilles Tjoelker * 43853d49b37SJilles Tjoelker * We go for the third, as otherwise we have no guarantee 43953d49b37SJilles Tjoelker * that we won't block indefinitely if the child refuses 44053d49b37SJilles Tjoelker * to depart. To handle the second option, a different 44153d49b37SJilles Tjoelker * approach would be needed (procctl()?) 44253d49b37SJilles Tjoelker */ 44353d49b37SJilles Tjoelker if (child_gone && child_eof) { 44453d49b37SJilles Tjoelker break; 44553d49b37SJilles Tjoelker } else if (terminate) { 44653d49b37SJilles Tjoelker goto exit; 44753d49b37SJilles Tjoelker } else if (!child_eof) { 44853d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) { 44953d49b37SJilles Tjoelker warn("sigprocmask"); 45053d49b37SJilles Tjoelker goto exit; 45153d49b37SJilles Tjoelker } 45253d49b37SJilles Tjoelker child_eof = !listen_child(pfd[0], &logpar); 45353d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { 45453d49b37SJilles Tjoelker warn("sigprocmask"); 45553d49b37SJilles Tjoelker goto exit; 45653d49b37SJilles Tjoelker } 45753d49b37SJilles Tjoelker } else { 45853d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) { 45953d49b37SJilles Tjoelker warn("sigprocmask"); 46053d49b37SJilles Tjoelker goto exit; 46153d49b37SJilles Tjoelker } 46253d49b37SJilles Tjoelker while (!terminate && !child_gone) 46353d49b37SJilles Tjoelker sigsuspend(&mask_orig); 46453d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) { 46553d49b37SJilles Tjoelker warn("sigprocmask"); 46653d49b37SJilles Tjoelker goto exit; 46753d49b37SJilles Tjoelker } 46853d49b37SJilles Tjoelker } 46953d49b37SJilles Tjoelker } 4706b4ef4b1SIhor Antonov if (restart && !terminate) { 47109a3675dSConrad Meyer daemon_sleep(restart, 0); 4726b4ef4b1SIhor Antonov } 47353d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) { 47453d49b37SJilles Tjoelker warn("sigprocmask"); 47553d49b37SJilles Tjoelker goto exit; 47653d49b37SJilles Tjoelker } 47753d49b37SJilles Tjoelker if (restart && !terminate) { 47853d49b37SJilles Tjoelker close(pfd[0]); 47953d49b37SJilles Tjoelker pfd[0] = -1; 480b6193c24SMikolaj Golub goto restart; 481b6193c24SMikolaj Golub } 4829da0ef13SMikolaj Golub exit: 483*e70444c6SIhor Antonov close(logpar.output_fd); 48453d49b37SJilles Tjoelker close(pfd[0]); 48553d49b37SJilles Tjoelker close(pfd[1]); 486*e70444c6SIhor Antonov if (logpar.syslog_enabled) { 48753d49b37SJilles Tjoelker closelog(); 4886b4ef4b1SIhor Antonov } 489c6262cb6SPawel Jakub Dawidek pidfile_remove(pfh); 49032b17786SJohn-Mark Gurney pidfile_remove(ppfh); 4919da0ef13SMikolaj Golub exit(1); /* If daemon(3) succeeded exit status does not matter. */ 492bd06a3ecSMike Barcroft } 493bd06a3ecSMike Barcroft 494bd06a3ecSMike Barcroft static void 49553d49b37SJilles Tjoelker daemon_sleep(time_t secs, long nsecs) 496195fc497SMikolaj Golub { 49753d49b37SJilles Tjoelker struct timespec ts = { secs, nsecs }; 49809a3675dSConrad Meyer 49909a3675dSConrad Meyer while (!terminate && nanosleep(&ts, &ts) == -1) { 5006b4ef4b1SIhor Antonov if (errno != EINTR) { 50153d49b37SJilles Tjoelker err(1, "nanosleep"); 50253d49b37SJilles Tjoelker } 50353d49b37SJilles Tjoelker } 5046b4ef4b1SIhor Antonov } 50553d49b37SJilles Tjoelker 50653d49b37SJilles Tjoelker static void 50753d49b37SJilles Tjoelker open_pid_files(const char *pidfile, const char *ppidfile, 50853d49b37SJilles Tjoelker struct pidfh **pfh, struct pidfh **ppfh) 50953d49b37SJilles Tjoelker { 51053d49b37SJilles Tjoelker pid_t fpid; 51153d49b37SJilles Tjoelker int serrno; 51253d49b37SJilles Tjoelker 51353d49b37SJilles Tjoelker if (pidfile) { 51453d49b37SJilles Tjoelker *pfh = pidfile_open(pidfile, 0600, &fpid); 51553d49b37SJilles Tjoelker if (*pfh == NULL) { 51653d49b37SJilles Tjoelker if (errno == EEXIST) { 51753d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 51853d49b37SJilles Tjoelker fpid); 51953d49b37SJilles Tjoelker } 52053d49b37SJilles Tjoelker err(2, "pidfile ``%s''", pidfile); 52153d49b37SJilles Tjoelker } 52253d49b37SJilles Tjoelker } 52353d49b37SJilles Tjoelker /* Do the same for the actual daemon process. */ 52453d49b37SJilles Tjoelker if (ppidfile) { 52553d49b37SJilles Tjoelker *ppfh = pidfile_open(ppidfile, 0600, &fpid); 52653d49b37SJilles Tjoelker if (*ppfh == NULL) { 52753d49b37SJilles Tjoelker serrno = errno; 52853d49b37SJilles Tjoelker pidfile_remove(*pfh); 52953d49b37SJilles Tjoelker errno = serrno; 53053d49b37SJilles Tjoelker if (errno == EEXIST) { 53153d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 53253d49b37SJilles Tjoelker fpid); 53353d49b37SJilles Tjoelker } 53453d49b37SJilles Tjoelker err(2, "ppidfile ``%s''", ppidfile); 53553d49b37SJilles Tjoelker } 53653d49b37SJilles Tjoelker } 53753d49b37SJilles Tjoelker } 53853d49b37SJilles Tjoelker 53953d49b37SJilles Tjoelker static int 54053d49b37SJilles Tjoelker get_log_mapping(const char *str, const CODE *c) 54153d49b37SJilles Tjoelker { 54253d49b37SJilles Tjoelker const CODE *cp; 54353d49b37SJilles Tjoelker for (cp = c; cp->c_name; cp++) 5446b4ef4b1SIhor Antonov if (strcmp(cp->c_name, str) == 0) { 54553d49b37SJilles Tjoelker return cp->c_val; 5466b4ef4b1SIhor Antonov } 54753d49b37SJilles Tjoelker return -1; 548195fc497SMikolaj Golub } 549195fc497SMikolaj Golub 550195fc497SMikolaj Golub static void 551e6d4b388STom Rhodes restrict_process(const char *user) 55212d7249eSTom Rhodes { 55312d7249eSTom Rhodes struct passwd *pw = NULL; 55412d7249eSTom Rhodes 555e6d4b388STom Rhodes pw = getpwnam(user); 5566b4ef4b1SIhor Antonov if (pw == NULL) { 557e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 5586b4ef4b1SIhor Antonov } 55912d7249eSTom Rhodes 5606b4ef4b1SIhor Antonov if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) { 561e6d4b388STom Rhodes errx(1, "failed to set user environment"); 5626b4ef4b1SIhor Antonov } 5636b3ad1d7SMaxim Sobolev 5646b3ad1d7SMaxim Sobolev setenv("USER", pw->pw_name, 1); 5656b3ad1d7SMaxim Sobolev setenv("HOME", pw->pw_dir, 1); 5666b3ad1d7SMaxim Sobolev setenv("SHELL", *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL, 1); 56712d7249eSTom Rhodes } 56812d7249eSTom Rhodes 56953d49b37SJilles Tjoelker /* 57053d49b37SJilles Tjoelker * We try to collect whole lines terminated by '\n'. Otherwise we collect a 57153d49b37SJilles Tjoelker * full buffer, and then output it. 57253d49b37SJilles Tjoelker * 57353d49b37SJilles Tjoelker * Return value of 0 is assumed to mean EOF or error, and 1 indicates to 57453d49b37SJilles Tjoelker * continue reading. 57553d49b37SJilles Tjoelker */ 576b6193c24SMikolaj Golub static int 57753d49b37SJilles Tjoelker listen_child(int fd, struct log_params *logpar) 5782ad43027SMikolaj Golub { 57953d49b37SJilles Tjoelker static unsigned char buf[LBUF_SIZE]; 58053d49b37SJilles Tjoelker static size_t bytes_read = 0; 58153d49b37SJilles Tjoelker int rv; 5822ad43027SMikolaj Golub 58353d49b37SJilles Tjoelker assert(logpar); 58453d49b37SJilles Tjoelker assert(bytes_read < LBUF_SIZE - 1); 58553d49b37SJilles Tjoelker 5866b4ef4b1SIhor Antonov if (do_log_reopen) { 5874cd407ecSMaxim Sobolev reopen_log(logpar); 5886b4ef4b1SIhor Antonov } 58953d49b37SJilles Tjoelker rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1); 59053d49b37SJilles Tjoelker if (rv > 0) { 59153d49b37SJilles Tjoelker unsigned char *cp; 59253d49b37SJilles Tjoelker 59353d49b37SJilles Tjoelker bytes_read += rv; 59453d49b37SJilles Tjoelker assert(bytes_read <= LBUF_SIZE - 1); 59553d49b37SJilles Tjoelker /* Always NUL-terminate just in case. */ 59653d49b37SJilles Tjoelker buf[LBUF_SIZE - 1] = '\0'; 59753d49b37SJilles Tjoelker /* 59853d49b37SJilles Tjoelker * Chomp line by line until we run out of buffer. 59953d49b37SJilles Tjoelker * This does not take NUL characters into account. 60053d49b37SJilles Tjoelker */ 60153d49b37SJilles Tjoelker while ((cp = memchr(buf, '\n', bytes_read)) != NULL) { 60253d49b37SJilles Tjoelker size_t bytes_line = cp - buf + 1; 60353d49b37SJilles Tjoelker assert(bytes_line <= bytes_read); 60453d49b37SJilles Tjoelker do_output(buf, bytes_line, logpar); 60553d49b37SJilles Tjoelker bytes_read -= bytes_line; 60653d49b37SJilles Tjoelker memmove(buf, cp + 1, bytes_read); 607195fc497SMikolaj Golub } 60853d49b37SJilles Tjoelker /* Wait until the buffer is full. */ 6096b4ef4b1SIhor Antonov if (bytes_read < LBUF_SIZE - 1) { 61053d49b37SJilles Tjoelker return 1; 6116b4ef4b1SIhor Antonov } 61253d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 61353d49b37SJilles Tjoelker bytes_read = 0; 61453d49b37SJilles Tjoelker return 1; 61553d49b37SJilles Tjoelker } else if (rv == -1) { 61653d49b37SJilles Tjoelker /* EINTR should trigger another read. */ 61753d49b37SJilles Tjoelker if (errno == EINTR) { 61853d49b37SJilles Tjoelker return 1; 61953d49b37SJilles Tjoelker } else { 62053d49b37SJilles Tjoelker warn("read"); 62153d49b37SJilles Tjoelker return 0; 622c60d51f9SMikolaj Golub } 62353d49b37SJilles Tjoelker } 62453d49b37SJilles Tjoelker /* Upon EOF, we have to flush what's left of the buffer. */ 62553d49b37SJilles Tjoelker if (bytes_read > 0) { 62653d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 62753d49b37SJilles Tjoelker bytes_read = 0; 62853d49b37SJilles Tjoelker } 62953d49b37SJilles Tjoelker return 0; 63053d49b37SJilles Tjoelker } 63153d49b37SJilles Tjoelker 63253d49b37SJilles Tjoelker /* 63353d49b37SJilles Tjoelker * The default behavior is to stay silent if the user wants to redirect 63453d49b37SJilles Tjoelker * output to a file and/or syslog. If neither are provided, then we bounce 63553d49b37SJilles Tjoelker * everything back to parent's stdout. 63653d49b37SJilles Tjoelker */ 63753d49b37SJilles Tjoelker static void 63853d49b37SJilles Tjoelker do_output(const unsigned char *buf, size_t len, struct log_params *logpar) 63953d49b37SJilles Tjoelker { 64053d49b37SJilles Tjoelker assert(len <= LBUF_SIZE); 64153d49b37SJilles Tjoelker assert(logpar); 64253d49b37SJilles Tjoelker 6436b4ef4b1SIhor Antonov if (len < 1) { 64453d49b37SJilles Tjoelker return; 6456b4ef4b1SIhor Antonov } 646f2f9d31dSIhor Antonov if (logpar->syslog_enabled) { 647*e70444c6SIhor Antonov syslog(logpar->syslog_priority, "%.*s", (int)len, buf); 6486b4ef4b1SIhor Antonov } 649*e70444c6SIhor Antonov if (logpar->output_fd != -1) { 650*e70444c6SIhor Antonov if (write(logpar->output_fd, buf, len) == -1) 65153d49b37SJilles Tjoelker warn("write"); 65253d49b37SJilles Tjoelker } 653*e70444c6SIhor Antonov if (logpar->noclose && !logpar->syslog_enabled && logpar->output_fd == -1) { 65453d49b37SJilles Tjoelker printf("%.*s", (int)len, buf); 65553d49b37SJilles Tjoelker } 6566b4ef4b1SIhor Antonov } 65753d49b37SJilles Tjoelker 65853d49b37SJilles Tjoelker /* 65953d49b37SJilles Tjoelker * We use the global PID acquired directly from fork. If there is no valid 66053d49b37SJilles Tjoelker * child pid, the handler should be blocked and/or child_gone == 1. 66153d49b37SJilles Tjoelker */ 66253d49b37SJilles Tjoelker static void 66353d49b37SJilles Tjoelker handle_term(int signo) 66453d49b37SJilles Tjoelker { 6656b4ef4b1SIhor Antonov if (pid > 0 && !child_gone) { 66653d49b37SJilles Tjoelker kill(pid, signo); 6676b4ef4b1SIhor Antonov } 668b6193c24SMikolaj Golub terminate = 1; 669195fc497SMikolaj Golub } 67053d49b37SJilles Tjoelker 67153d49b37SJilles Tjoelker static void 6724cd407ecSMaxim Sobolev handle_chld(int signo __unused) 67353d49b37SJilles Tjoelker { 6744cd407ecSMaxim Sobolev 67553d49b37SJilles Tjoelker for (;;) { 67653d49b37SJilles Tjoelker int rv = waitpid(-1, NULL, WNOHANG); 67753d49b37SJilles Tjoelker if (pid == rv) { 67853d49b37SJilles Tjoelker child_gone = 1; 67953d49b37SJilles Tjoelker break; 68053d49b37SJilles Tjoelker } else if (rv == -1 && errno != EINTR) { 68153d49b37SJilles Tjoelker warn("waitpid"); 68253d49b37SJilles Tjoelker return; 6832ad43027SMikolaj Golub } 6842ad43027SMikolaj Golub } 6852ad43027SMikolaj Golub } 6862ad43027SMikolaj Golub 6872ad43027SMikolaj Golub static void 6884cd407ecSMaxim Sobolev handle_hup(int signo __unused) 6894cd407ecSMaxim Sobolev { 6904cd407ecSMaxim Sobolev 6914cd407ecSMaxim Sobolev do_log_reopen = 1; 6924cd407ecSMaxim Sobolev } 6934cd407ecSMaxim Sobolev 6944cd407ecSMaxim Sobolev static int 6954cd407ecSMaxim Sobolev open_log(const char *outfn) 6964cd407ecSMaxim Sobolev { 6974cd407ecSMaxim Sobolev 6984cd407ecSMaxim Sobolev return open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600); 6994cd407ecSMaxim Sobolev } 7004cd407ecSMaxim Sobolev 7014cd407ecSMaxim Sobolev static void 7024cd407ecSMaxim Sobolev reopen_log(struct log_params *lpp) 7034cd407ecSMaxim Sobolev { 7044cd407ecSMaxim Sobolev int outfd; 7054cd407ecSMaxim Sobolev 7064cd407ecSMaxim Sobolev do_log_reopen = 0; 707*e70444c6SIhor Antonov outfd = open_log(lpp->output_filename); 708*e70444c6SIhor Antonov if (lpp->output_fd >= 0) { 709*e70444c6SIhor Antonov close(lpp->output_fd); 7106b4ef4b1SIhor Antonov } 711*e70444c6SIhor Antonov lpp->output_fd = outfd; 7124cd407ecSMaxim Sobolev } 7134cd407ecSMaxim Sobolev 714