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 { 63e70444c6SIhor Antonov const char *output_filename; 646f063672SIhor Antonov const char *syslog_tag; 65e70444c6SIhor Antonov int syslog_priority; 666f063672SIhor Antonov int syslog_facility; 67129ec8f4SIhor Antonov int keep_fds_open; 68e70444c6SIhor Antonov int output_fd; 69f2f9d31dSIhor Antonov bool syslog_enabled; 7053d49b37SJilles Tjoelker }; 7153d49b37SJilles Tjoelker 72e6d4b388STom Rhodes static void restrict_process(const char *); 7353d49b37SJilles Tjoelker static void handle_term(int); 7453d49b37SJilles Tjoelker static void handle_chld(int); 754cd407ecSMaxim Sobolev static void handle_hup(int); 764cd407ecSMaxim Sobolev static int open_log(const char *); 774cd407ecSMaxim Sobolev static void reopen_log(struct log_params *); 7853d49b37SJilles Tjoelker static int listen_child(int, struct log_params *); 7953d49b37SJilles Tjoelker static int get_log_mapping(const char *, const CODE *); 8053d49b37SJilles Tjoelker static void open_pid_files(const char *, const char *, struct pidfh **, 8153d49b37SJilles Tjoelker struct pidfh **); 8253d49b37SJilles Tjoelker static void do_output(const unsigned char *, size_t, struct log_params *); 8353d49b37SJilles Tjoelker static void daemon_sleep(time_t, long); 84bd06a3ecSMike Barcroft 85e745dc22SIhor Antonov static volatile sig_atomic_t terminate = 0; 86e745dc22SIhor Antonov static volatile sig_atomic_t child_gone = 0; 8791b921c7SIhor Antonov static volatile sig_atomic_t pid = -1; 88e745dc22SIhor Antonov static volatile sig_atomic_t do_log_reopen = 0; 8953d49b37SJilles Tjoelker 900a402ad2SIhor Antonov static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h"; 910a402ad2SIhor Antonov 920a402ad2SIhor Antonov static const struct option longopts[] = { 930a402ad2SIhor Antonov { "change-dir", no_argument, NULL, 'c' }, 940a402ad2SIhor Antonov { "close-fds", no_argument, NULL, 'f' }, 950a402ad2SIhor Antonov { "sighup", no_argument, NULL, 'H' }, 960a402ad2SIhor Antonov { "syslog", no_argument, NULL, 'S' }, 970a402ad2SIhor Antonov { "output-file", required_argument, NULL, 'o' }, 980a402ad2SIhor Antonov { "output-mask", required_argument, NULL, 'm' }, 990a402ad2SIhor Antonov { "child-pidfile", required_argument, NULL, 'p' }, 1000a402ad2SIhor Antonov { "supervisor-pidfile", required_argument, NULL, 'P' }, 1010a402ad2SIhor Antonov { "restart", no_argument, NULL, 'r' }, 1020a402ad2SIhor Antonov { "restart-delay", required_argument, NULL, 'R' }, 1030a402ad2SIhor Antonov { "title", required_argument, NULL, 't' }, 1040a402ad2SIhor Antonov { "user", required_argument, NULL, 'u' }, 1050a402ad2SIhor Antonov { "syslog-priority", required_argument, NULL, 's' }, 1060a402ad2SIhor Antonov { "syslog-facility", required_argument, NULL, 'l' }, 1070a402ad2SIhor Antonov { "syslog-tag", required_argument, NULL, 'T' }, 1080a402ad2SIhor Antonov { "help", no_argument, NULL, 'h' }, 1090a402ad2SIhor Antonov { NULL, 0, NULL, 0 } 1100a402ad2SIhor Antonov }; 1110a402ad2SIhor Antonov 1120a402ad2SIhor Antonov static _Noreturn void 1130a402ad2SIhor Antonov usage(int exitcode) 1140a402ad2SIhor Antonov { 1150a402ad2SIhor Antonov (void)fprintf(stderr, 1160a402ad2SIhor Antonov "usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n" 1170a402ad2SIhor Antonov " [-u user] [-o output_file] [-t title]\n" 1180a402ad2SIhor Antonov " [-l syslog_facility] [-s syslog_priority]\n" 1190a402ad2SIhor Antonov " [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n" 1200a402ad2SIhor Antonov "command arguments ...\n"); 1210a402ad2SIhor Antonov 1220a402ad2SIhor Antonov (void)fprintf(stderr, 1230a402ad2SIhor Antonov " --change-dir -c Change the current working directory to root\n" 1240a402ad2SIhor Antonov " --close-fds -f Set stdin, stdout, stderr to /dev/null\n" 1250a402ad2SIhor Antonov " --sighup -H Close and re-open output file on SIGHUP\n" 1260a402ad2SIhor Antonov " --syslog -S Send output to syslog\n" 1270a402ad2SIhor Antonov " --output-file -o <file> Append output of the child process to file\n" 1280a402ad2SIhor Antonov " --output-mask -m <mask> What to send to syslog/file\n" 1290a402ad2SIhor Antonov " 1=stdout, 2=stderr, 3=both\n" 1300a402ad2SIhor Antonov " --child-pidfile -p <file> Write PID of the child process to file\n" 1310a402ad2SIhor Antonov " --supervisor-pidfile -P <file> Write PID of the supervisor process to file\n" 1320a402ad2SIhor Antonov " --restart -r Restart child if it terminates (1 sec delay)\n" 1330a402ad2SIhor Antonov " --restart-delay -R <N> Restart child if it terminates after N sec\n" 1340a402ad2SIhor Antonov " --title -t <title> Set the title of the supervisor process\n" 1350a402ad2SIhor Antonov " --user -u <user> Drop privileges, run as given user\n" 1360a402ad2SIhor Antonov " --syslog-priority -s <prio> Set syslog priority\n" 1370a402ad2SIhor Antonov " --syslog-facility -l <flty> Set syslog facility\n" 1380a402ad2SIhor Antonov " --syslog-tag -T <tag> Set syslog tag\n" 1390a402ad2SIhor Antonov " --help -h Show this help\n"); 1400a402ad2SIhor Antonov 1410a402ad2SIhor Antonov exit(exitcode); 1420a402ad2SIhor Antonov } 1430a402ad2SIhor Antonov 144bd06a3ecSMike Barcroft int 145bd06a3ecSMike Barcroft main(int argc, char *argv[]) 146bd06a3ecSMike Barcroft { 147203df05bSIhor Antonov bool supervision_enabled = false; 14897022e90SIhor Antonov bool log_reopen = false; 149e745dc22SIhor Antonov char *p = NULL; 150129ec8f4SIhor Antonov const char *child_pidfile = NULL; 151129ec8f4SIhor Antonov const char *parent_pidfile = NULL; 152e745dc22SIhor Antonov const char *title = NULL; 153e745dc22SIhor Antonov const char *user = NULL; 154e745dc22SIhor Antonov int ch = 0; 155e745dc22SIhor Antonov int child_eof = 0; 156129ec8f4SIhor Antonov int keep_cur_workdir = 1; 157e745dc22SIhor Antonov int pfd[2] = { -1, -1 }; 158e745dc22SIhor Antonov int restart = 0; 159e745dc22SIhor Antonov int stdmask = STDOUT_FILENO | STDERR_FILENO; 160129ec8f4SIhor Antonov struct log_params logparams = { 161e70444c6SIhor Antonov .syslog_enabled = false, 162e70444c6SIhor Antonov .syslog_priority = LOG_NOTICE, 1636f063672SIhor Antonov .syslog_tag = "daemon", 1646f063672SIhor Antonov .syslog_facility = LOG_DAEMON, 165129ec8f4SIhor Antonov .keep_fds_open = 1, 166e70444c6SIhor Antonov .output_fd = -1, 167e70444c6SIhor Antonov .output_filename = NULL 168e70444c6SIhor Antonov }; 169129ec8f4SIhor Antonov struct pidfh *parent_pidfh = NULL; 170129ec8f4SIhor Antonov struct pidfh *child_pidfh = NULL; 171e745dc22SIhor Antonov sigset_t mask_orig; 172e745dc22SIhor Antonov sigset_t mask_read; 173e745dc22SIhor Antonov sigset_t mask_term; 174e745dc22SIhor Antonov sigset_t mask_susp; 175bd06a3ecSMike Barcroft 176e745dc22SIhor Antonov sigemptyset(&mask_susp); 177e745dc22SIhor Antonov sigemptyset(&mask_read); 178e745dc22SIhor Antonov sigemptyset(&mask_term); 17984866cefSIhor Antonov sigemptyset(&mask_orig); 180e745dc22SIhor Antonov 1810a402ad2SIhor Antonov while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { 182bd06a3ecSMike Barcroft switch (ch) { 183bd06a3ecSMike Barcroft case 'c': 184129ec8f4SIhor Antonov keep_cur_workdir = 0; 185bd06a3ecSMike Barcroft break; 186bd06a3ecSMike Barcroft case 'f': 187129ec8f4SIhor Antonov logparams.keep_fds_open = 0; 188bd06a3ecSMike Barcroft break; 1894cd407ecSMaxim Sobolev case 'H': 19097022e90SIhor Antonov log_reopen = true; 1914cd407ecSMaxim Sobolev break; 19253d49b37SJilles Tjoelker case 'l': 193*39ea4280SIhor Antonov logparams.syslog_facility = get_log_mapping(optarg, 194*39ea4280SIhor Antonov facilitynames); 195129ec8f4SIhor Antonov if (logparams.syslog_facility == -1) { 19653d49b37SJilles Tjoelker errx(5, "unrecognized syslog facility"); 1976b4ef4b1SIhor Antonov } 198129ec8f4SIhor Antonov logparams.syslog_enabled = true; 19953d49b37SJilles Tjoelker break; 20053d49b37SJilles Tjoelker case 'm': 20153d49b37SJilles Tjoelker stdmask = strtol(optarg, &p, 10); 2026b4ef4b1SIhor Antonov if (p == optarg || stdmask < 0 || stdmask > 3) { 20353d49b37SJilles Tjoelker errx(6, "unrecognized listening mask"); 2046b4ef4b1SIhor Antonov } 20553d49b37SJilles Tjoelker break; 20653d49b37SJilles Tjoelker case 'o': 207129ec8f4SIhor Antonov logparams.output_filename = optarg; 20853d49b37SJilles Tjoelker break; 209846be7bdSPoul-Henning Kamp case 'p': 210129ec8f4SIhor Antonov child_pidfile = optarg; 211846be7bdSPoul-Henning Kamp break; 21232b17786SJohn-Mark Gurney case 'P': 213129ec8f4SIhor Antonov parent_pidfile = optarg; 21432b17786SJohn-Mark Gurney break; 215b6193c24SMikolaj Golub case 'r': 216b6193c24SMikolaj Golub restart = 1; 217b6193c24SMikolaj Golub break; 21837820b87SIan Lepore case 'R': 21937820b87SIan Lepore restart = strtol(optarg, &p, 0); 2206b4ef4b1SIhor Antonov if (p == optarg || restart < 1) { 22137820b87SIan Lepore errx(6, "invalid restart delay"); 2226b4ef4b1SIhor Antonov } 22337820b87SIan Lepore break; 22453d49b37SJilles Tjoelker case 's': 225*39ea4280SIhor Antonov logparams.syslog_priority = get_log_mapping(optarg, 226*39ea4280SIhor Antonov prioritynames); 227129ec8f4SIhor Antonov if (logparams.syslog_priority == -1) { 22853d49b37SJilles Tjoelker errx(4, "unrecognized syslog priority"); 2296b4ef4b1SIhor Antonov } 230129ec8f4SIhor Antonov logparams.syslog_enabled = true; 23153d49b37SJilles Tjoelker break; 23253d49b37SJilles Tjoelker case 'S': 233129ec8f4SIhor Antonov logparams.syslog_enabled = true; 23453d49b37SJilles Tjoelker break; 235112bfcf5SConrad Meyer case 't': 236112bfcf5SConrad Meyer title = optarg; 237112bfcf5SConrad Meyer break; 23853d49b37SJilles Tjoelker case 'T': 239129ec8f4SIhor Antonov logparams.syslog_tag = optarg; 240129ec8f4SIhor Antonov logparams.syslog_enabled = true; 24153d49b37SJilles Tjoelker break; 242e6d4b388STom Rhodes case 'u': 243e6d4b388STom Rhodes user = optarg; 244e6d4b388STom Rhodes break; 2450a402ad2SIhor Antonov case 'h': 2460a402ad2SIhor Antonov usage(0); 2470a402ad2SIhor Antonov __builtin_unreachable(); 248bd06a3ecSMike Barcroft default: 2490a402ad2SIhor Antonov usage(1); 250bd06a3ecSMike Barcroft } 251bd06a3ecSMike Barcroft } 252bd06a3ecSMike Barcroft argc -= optind; 253bd06a3ecSMike Barcroft argv += optind; 254bd06a3ecSMike Barcroft 2556b4ef4b1SIhor Antonov if (argc == 0) { 2560a402ad2SIhor Antonov usage(1); 2576b4ef4b1SIhor Antonov } 25812d7249eSTom Rhodes 2596b4ef4b1SIhor Antonov if (!title) { 26053d49b37SJilles Tjoelker title = argv[0]; 2616b4ef4b1SIhor Antonov } 26253d49b37SJilles Tjoelker 263129ec8f4SIhor Antonov if (logparams.output_filename) { 264129ec8f4SIhor Antonov logparams.output_fd = open_log(logparams.output_filename); 265129ec8f4SIhor Antonov if (logparams.output_fd == -1) { 26653d49b37SJilles Tjoelker err(7, "open"); 26753d49b37SJilles Tjoelker } 2686b4ef4b1SIhor Antonov } 26953d49b37SJilles Tjoelker 270129ec8f4SIhor Antonov if (logparams.syslog_enabled) { 271*39ea4280SIhor Antonov openlog(logparams.syslog_tag, LOG_PID | LOG_NDELAY, 272*39ea4280SIhor Antonov logparams.syslog_facility); 2736b4ef4b1SIhor Antonov } 27453d49b37SJilles Tjoelker 275846be7bdSPoul-Henning Kamp /* 276846be7bdSPoul-Henning Kamp * Try to open the pidfile before calling daemon(3), 277846be7bdSPoul-Henning Kamp * to be able to report the error intelligently 278846be7bdSPoul-Henning Kamp */ 279129ec8f4SIhor Antonov open_pid_files(child_pidfile, parent_pidfile, &child_pidfh, &parent_pidfh); 280129ec8f4SIhor Antonov if (daemon(keep_cur_workdir, logparams.keep_fds_open) == -1) { 2819da0ef13SMikolaj Golub warn("daemon"); 2829da0ef13SMikolaj Golub goto exit; 2839da0ef13SMikolaj Golub } 2849da0ef13SMikolaj Golub /* Write out parent pidfile if needed. */ 285129ec8f4SIhor Antonov pidfile_write(parent_pidfh); 286203df05bSIhor Antonov 287195fc497SMikolaj Golub /* 288203df05bSIhor Antonov * Supervision mode is enabled if one of the following options are used: 289203df05bSIhor Antonov * --child-pidfile -p 290203df05bSIhor Antonov * --supervisor-pidfile -P 291203df05bSIhor Antonov * --restart -r / --restart-delay -R 292203df05bSIhor Antonov * --syslog -S 293203df05bSIhor Antonov * --syslog-facility -l 294203df05bSIhor Antonov * --syslog-priority -s 295203df05bSIhor Antonov * --syslog-tag -T 296203df05bSIhor Antonov * 297203df05bSIhor Antonov * In supervision mode daemon executes the command in a forked process 298203df05bSIhor Antonov * and observes the child by waiting for SIGCHILD. In supervision mode 299203df05bSIhor Antonov * daemon must never exit before the child, this is necessary to prevent 300203df05bSIhor Antonov * orphaning the child and leaving a stale pid file. 301203df05bSIhor Antonov * To achieve this daemon catches SIGTERM and 302203df05bSIhor Antonov * forwards it to the child, expecting to get SIGCHLD eventually. 303195fc497SMikolaj Golub */ 304129ec8f4SIhor Antonov supervision_enabled = child_pidfile != NULL || 305129ec8f4SIhor Antonov parent_pidfile != NULL || 306203df05bSIhor Antonov restart != 0 || 307129ec8f4SIhor Antonov logparams.output_fd != -1 || 308129ec8f4SIhor Antonov logparams.syslog_enabled == true; 309203df05bSIhor Antonov 310203df05bSIhor Antonov if (supervision_enabled) { 311259ed21dSIhor Antonov struct sigaction act_term = { 0 }; 312259ed21dSIhor Antonov struct sigaction act_chld = { 0 }; 313259ed21dSIhor Antonov struct sigaction act_hup = { 0 }; 31453d49b37SJilles Tjoelker 31553d49b37SJilles Tjoelker /* Avoid PID racing with SIGCHLD and SIGTERM. */ 31653d49b37SJilles Tjoelker act_term.sa_handler = handle_term; 31753d49b37SJilles Tjoelker sigemptyset(&act_term.sa_mask); 31853d49b37SJilles Tjoelker sigaddset(&act_term.sa_mask, SIGCHLD); 31953d49b37SJilles Tjoelker 32053d49b37SJilles Tjoelker act_chld.sa_handler = handle_chld; 32153d49b37SJilles Tjoelker sigemptyset(&act_chld.sa_mask); 32253d49b37SJilles Tjoelker sigaddset(&act_chld.sa_mask, SIGTERM); 32353d49b37SJilles Tjoelker 3244cd407ecSMaxim Sobolev act_hup.sa_handler = handle_hup; 3254cd407ecSMaxim Sobolev sigemptyset(&act_hup.sa_mask); 3264cd407ecSMaxim Sobolev 32753d49b37SJilles Tjoelker /* Block these when avoiding racing before sigsuspend(). */ 32853d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGTERM); 32953d49b37SJilles Tjoelker sigaddset(&mask_susp, SIGCHLD); 33053d49b37SJilles Tjoelker /* Block SIGTERM when we lack a valid child PID. */ 33153d49b37SJilles Tjoelker sigaddset(&mask_term, SIGTERM); 3322ad43027SMikolaj Golub /* 33353d49b37SJilles Tjoelker * When reading, we wish to avoid SIGCHLD. SIGTERM 33453d49b37SJilles Tjoelker * has to be caught, otherwise we'll be stuck until 33553d49b37SJilles Tjoelker * the read() returns - if it returns. 336195fc497SMikolaj Golub */ 33753d49b37SJilles Tjoelker sigaddset(&mask_read, SIGCHLD); 33853d49b37SJilles Tjoelker /* Block SIGTERM to avoid racing until we have forked. */ 33953d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) { 3409da0ef13SMikolaj Golub warn("sigprocmask"); 3419da0ef13SMikolaj Golub goto exit; 3429da0ef13SMikolaj Golub } 34353d49b37SJilles Tjoelker if (sigaction(SIGTERM, &act_term, NULL) == -1) { 34453d49b37SJilles Tjoelker warn("sigaction"); 34553d49b37SJilles Tjoelker goto exit; 34653d49b37SJilles Tjoelker } 34753d49b37SJilles Tjoelker if (sigaction(SIGCHLD, &act_chld, NULL) == -1) { 34853d49b37SJilles Tjoelker warn("sigaction"); 34953d49b37SJilles Tjoelker goto exit; 35053d49b37SJilles Tjoelker } 35153c49998SMikolaj Golub /* 35253c49998SMikolaj Golub * Try to protect against pageout kill. Ignore the 35353c49998SMikolaj Golub * error, madvise(2) will fail only if a process does 35453c49998SMikolaj Golub * not have superuser privileges. 35553c49998SMikolaj Golub */ 35653c49998SMikolaj Golub (void)madvise(NULL, 0, MADV_PROTECT); 357129ec8f4SIhor Antonov if (log_reopen && logparams.output_fd >= 0 && 3584cd407ecSMaxim Sobolev sigaction(SIGHUP, &act_hup, NULL) == -1) { 3594cd407ecSMaxim Sobolev warn("sigaction"); 3604cd407ecSMaxim Sobolev goto exit; 3614cd407ecSMaxim Sobolev } 362b6193c24SMikolaj Golub restart: 3636b4ef4b1SIhor Antonov if (pipe(pfd)) { 36453d49b37SJilles Tjoelker err(1, "pipe"); 3656b4ef4b1SIhor Antonov } 366195fc497SMikolaj Golub /* 36753d49b37SJilles Tjoelker * Spawn a child to exec the command. 3682ad43027SMikolaj Golub */ 36953d49b37SJilles Tjoelker child_gone = 0; 3702ad43027SMikolaj Golub pid = fork(); 3712ad43027SMikolaj Golub if (pid == -1) { 3729da0ef13SMikolaj Golub warn("fork"); 3739da0ef13SMikolaj Golub goto exit; 37453d49b37SJilles Tjoelker } else if (pid > 0) { 37553d49b37SJilles Tjoelker /* 37653d49b37SJilles Tjoelker * Unblock SIGTERM after we know we have a valid 37753d49b37SJilles Tjoelker * child PID to signal. 37853d49b37SJilles Tjoelker */ 37953d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) { 38053d49b37SJilles Tjoelker warn("sigprocmask"); 38153d49b37SJilles Tjoelker goto exit; 38253d49b37SJilles Tjoelker } 38353d49b37SJilles Tjoelker close(pfd[1]); 38453d49b37SJilles Tjoelker pfd[1] = -1; 3852ad43027SMikolaj Golub } 3862ad43027SMikolaj Golub } 387195fc497SMikolaj Golub if (pid <= 0) { 3882ad43027SMikolaj Golub /* Now that we are the child, write out the pid. */ 389129ec8f4SIhor Antonov pidfile_write(child_pidfh); 390846be7bdSPoul-Henning Kamp 3916b4ef4b1SIhor Antonov if (user != NULL) { 3922ad43027SMikolaj Golub restrict_process(user); 3936b4ef4b1SIhor Antonov } 39453d49b37SJilles Tjoelker /* 39553d49b37SJilles Tjoelker * When forking, the child gets the original sigmask, 39653d49b37SJilles Tjoelker * and dup'd pipes. 39753d49b37SJilles Tjoelker */ 39853d49b37SJilles Tjoelker if (pid == 0) { 39953d49b37SJilles Tjoelker close(pfd[0]); 4006b4ef4b1SIhor Antonov if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) { 40153d49b37SJilles Tjoelker err(1, "sigprogmask"); 4026b4ef4b1SIhor Antonov } 40353d49b37SJilles Tjoelker if (stdmask & STDERR_FILENO) { 4046b4ef4b1SIhor Antonov if (dup2(pfd[1], STDERR_FILENO) == -1) { 40553d49b37SJilles Tjoelker err(1, "dup2"); 40653d49b37SJilles Tjoelker } 4076b4ef4b1SIhor Antonov } 40853d49b37SJilles Tjoelker if (stdmask & STDOUT_FILENO) { 4096b4ef4b1SIhor Antonov if (dup2(pfd[1], STDOUT_FILENO) == -1) { 41053d49b37SJilles Tjoelker err(1, "dup2"); 41153d49b37SJilles Tjoelker } 4126b4ef4b1SIhor Antonov } 41353d49b37SJilles Tjoelker if (pfd[1] != STDERR_FILENO && 4146b4ef4b1SIhor Antonov pfd[1] != STDOUT_FILENO) { 41553d49b37SJilles Tjoelker close(pfd[1]); 41653d49b37SJilles Tjoelker } 4176b4ef4b1SIhor Antonov } 418bd06a3ecSMike Barcroft execvp(argv[0], argv); 419846be7bdSPoul-Henning Kamp /* 4202ad43027SMikolaj Golub * execvp() failed -- report the error. The child is 4212ad43027SMikolaj Golub * now running, so the exit status doesn't matter. 422846be7bdSPoul-Henning Kamp */ 4232ad43027SMikolaj Golub err(1, "%s", argv[0]); 4242ad43027SMikolaj Golub } 42553d49b37SJilles Tjoelker setproctitle("%s[%d]", title, (int)pid); 42653d49b37SJilles Tjoelker /* 42753d49b37SJilles Tjoelker * As we have closed the write end of pipe for parent process, 42853d49b37SJilles Tjoelker * we might detect the child's exit by reading EOF. The child 42953d49b37SJilles Tjoelker * might have closed its stdout and stderr, so we must wait for 43053d49b37SJilles Tjoelker * the SIGCHLD to ensure that the process is actually gone. 43153d49b37SJilles Tjoelker */ 43253d49b37SJilles Tjoelker for (;;) { 43353d49b37SJilles Tjoelker /* 43453d49b37SJilles Tjoelker * We block SIGCHLD when listening, but SIGTERM we accept 43553d49b37SJilles Tjoelker * so the read() won't block if we wish to depart. 43653d49b37SJilles Tjoelker * 43753d49b37SJilles Tjoelker * Upon receiving SIGTERM, we have several options after 43853d49b37SJilles Tjoelker * sending the SIGTERM to our child: 43953d49b37SJilles Tjoelker * - read until EOF 44053d49b37SJilles Tjoelker * - read until EOF but only for a while 44153d49b37SJilles Tjoelker * - bail immediately 44253d49b37SJilles Tjoelker * 44353d49b37SJilles Tjoelker * We go for the third, as otherwise we have no guarantee 44453d49b37SJilles Tjoelker * that we won't block indefinitely if the child refuses 44553d49b37SJilles Tjoelker * to depart. To handle the second option, a different 44653d49b37SJilles Tjoelker * approach would be needed (procctl()?) 44753d49b37SJilles Tjoelker */ 44853d49b37SJilles Tjoelker if (child_gone && child_eof) { 44953d49b37SJilles Tjoelker break; 45053d49b37SJilles Tjoelker } else if (terminate) { 45153d49b37SJilles Tjoelker goto exit; 45253d49b37SJilles Tjoelker } else if (!child_eof) { 45353d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) { 45453d49b37SJilles Tjoelker warn("sigprocmask"); 45553d49b37SJilles Tjoelker goto exit; 45653d49b37SJilles Tjoelker } 457129ec8f4SIhor Antonov child_eof = !listen_child(pfd[0], &logparams); 45853d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { 45953d49b37SJilles Tjoelker warn("sigprocmask"); 46053d49b37SJilles Tjoelker goto exit; 46153d49b37SJilles Tjoelker } 46253d49b37SJilles Tjoelker } else { 46353d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) { 46453d49b37SJilles Tjoelker warn("sigprocmask"); 46553d49b37SJilles Tjoelker goto exit; 46653d49b37SJilles Tjoelker } 46753d49b37SJilles Tjoelker while (!terminate && !child_gone) 46853d49b37SJilles Tjoelker sigsuspend(&mask_orig); 46953d49b37SJilles Tjoelker if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) { 47053d49b37SJilles Tjoelker warn("sigprocmask"); 47153d49b37SJilles Tjoelker goto exit; 47253d49b37SJilles Tjoelker } 47353d49b37SJilles Tjoelker } 47453d49b37SJilles Tjoelker } 4756b4ef4b1SIhor Antonov if (restart && !terminate) { 47609a3675dSConrad Meyer daemon_sleep(restart, 0); 4776b4ef4b1SIhor Antonov } 47853d49b37SJilles Tjoelker if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) { 47953d49b37SJilles Tjoelker warn("sigprocmask"); 48053d49b37SJilles Tjoelker goto exit; 48153d49b37SJilles Tjoelker } 48253d49b37SJilles Tjoelker if (restart && !terminate) { 48353d49b37SJilles Tjoelker close(pfd[0]); 48453d49b37SJilles Tjoelker pfd[0] = -1; 485b6193c24SMikolaj Golub goto restart; 486b6193c24SMikolaj Golub } 4879da0ef13SMikolaj Golub exit: 488129ec8f4SIhor Antonov close(logparams.output_fd); 48953d49b37SJilles Tjoelker close(pfd[0]); 49053d49b37SJilles Tjoelker close(pfd[1]); 491129ec8f4SIhor Antonov if (logparams.syslog_enabled) { 49253d49b37SJilles Tjoelker closelog(); 4936b4ef4b1SIhor Antonov } 494129ec8f4SIhor Antonov pidfile_remove(child_pidfh); 495129ec8f4SIhor Antonov pidfile_remove(parent_pidfh); 4969da0ef13SMikolaj Golub exit(1); /* If daemon(3) succeeded exit status does not matter. */ 497bd06a3ecSMike Barcroft } 498bd06a3ecSMike Barcroft 499bd06a3ecSMike Barcroft static void 50053d49b37SJilles Tjoelker daemon_sleep(time_t secs, long nsecs) 501195fc497SMikolaj Golub { 50253d49b37SJilles Tjoelker struct timespec ts = { secs, nsecs }; 50309a3675dSConrad Meyer 50409a3675dSConrad Meyer while (!terminate && nanosleep(&ts, &ts) == -1) { 5056b4ef4b1SIhor Antonov if (errno != EINTR) { 50653d49b37SJilles Tjoelker err(1, "nanosleep"); 50753d49b37SJilles Tjoelker } 50853d49b37SJilles Tjoelker } 5096b4ef4b1SIhor Antonov } 51053d49b37SJilles Tjoelker 51153d49b37SJilles Tjoelker static void 51253d49b37SJilles Tjoelker open_pid_files(const char *pidfile, const char *ppidfile, 51353d49b37SJilles Tjoelker struct pidfh **pfh, struct pidfh **ppfh) 51453d49b37SJilles Tjoelker { 51553d49b37SJilles Tjoelker pid_t fpid; 51653d49b37SJilles Tjoelker int serrno; 51753d49b37SJilles Tjoelker 51853d49b37SJilles Tjoelker if (pidfile) { 51953d49b37SJilles Tjoelker *pfh = pidfile_open(pidfile, 0600, &fpid); 52053d49b37SJilles Tjoelker if (*pfh == NULL) { 52153d49b37SJilles Tjoelker if (errno == EEXIST) { 52253d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 52353d49b37SJilles Tjoelker fpid); 52453d49b37SJilles Tjoelker } 52553d49b37SJilles Tjoelker err(2, "pidfile ``%s''", pidfile); 52653d49b37SJilles Tjoelker } 52753d49b37SJilles Tjoelker } 52853d49b37SJilles Tjoelker /* Do the same for the actual daemon process. */ 52953d49b37SJilles Tjoelker if (ppidfile) { 53053d49b37SJilles Tjoelker *ppfh = pidfile_open(ppidfile, 0600, &fpid); 53153d49b37SJilles Tjoelker if (*ppfh == NULL) { 53253d49b37SJilles Tjoelker serrno = errno; 53353d49b37SJilles Tjoelker pidfile_remove(*pfh); 53453d49b37SJilles Tjoelker errno = serrno; 53553d49b37SJilles Tjoelker if (errno == EEXIST) { 53653d49b37SJilles Tjoelker errx(3, "process already running, pid: %d", 53753d49b37SJilles Tjoelker fpid); 53853d49b37SJilles Tjoelker } 53953d49b37SJilles Tjoelker err(2, "ppidfile ``%s''", ppidfile); 54053d49b37SJilles Tjoelker } 54153d49b37SJilles Tjoelker } 54253d49b37SJilles Tjoelker } 54353d49b37SJilles Tjoelker 54453d49b37SJilles Tjoelker static int 54553d49b37SJilles Tjoelker get_log_mapping(const char *str, const CODE *c) 54653d49b37SJilles Tjoelker { 54753d49b37SJilles Tjoelker const CODE *cp; 54853d49b37SJilles Tjoelker for (cp = c; cp->c_name; cp++) 5496b4ef4b1SIhor Antonov if (strcmp(cp->c_name, str) == 0) { 55053d49b37SJilles Tjoelker return cp->c_val; 5516b4ef4b1SIhor Antonov } 55253d49b37SJilles Tjoelker return -1; 553195fc497SMikolaj Golub } 554195fc497SMikolaj Golub 555195fc497SMikolaj Golub static void 556e6d4b388STom Rhodes restrict_process(const char *user) 55712d7249eSTom Rhodes { 55812d7249eSTom Rhodes struct passwd *pw = NULL; 55912d7249eSTom Rhodes 560e6d4b388STom Rhodes pw = getpwnam(user); 5616b4ef4b1SIhor Antonov if (pw == NULL) { 562e6d4b388STom Rhodes errx(1, "unknown user: %s", user); 5636b4ef4b1SIhor Antonov } 56412d7249eSTom Rhodes 5656b4ef4b1SIhor Antonov if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) { 566e6d4b388STom Rhodes errx(1, "failed to set user environment"); 5676b4ef4b1SIhor Antonov } 5686b3ad1d7SMaxim Sobolev 5696b3ad1d7SMaxim Sobolev setenv("USER", pw->pw_name, 1); 5706b3ad1d7SMaxim Sobolev setenv("HOME", pw->pw_dir, 1); 5716b3ad1d7SMaxim Sobolev setenv("SHELL", *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL, 1); 57212d7249eSTom Rhodes } 57312d7249eSTom Rhodes 57453d49b37SJilles Tjoelker /* 57553d49b37SJilles Tjoelker * We try to collect whole lines terminated by '\n'. Otherwise we collect a 57653d49b37SJilles Tjoelker * full buffer, and then output it. 57753d49b37SJilles Tjoelker * 57853d49b37SJilles Tjoelker * Return value of 0 is assumed to mean EOF or error, and 1 indicates to 57953d49b37SJilles Tjoelker * continue reading. 58053d49b37SJilles Tjoelker */ 581b6193c24SMikolaj Golub static int 58253d49b37SJilles Tjoelker listen_child(int fd, struct log_params *logpar) 5832ad43027SMikolaj Golub { 58453d49b37SJilles Tjoelker static unsigned char buf[LBUF_SIZE]; 58553d49b37SJilles Tjoelker static size_t bytes_read = 0; 58653d49b37SJilles Tjoelker int rv; 5872ad43027SMikolaj Golub 58853d49b37SJilles Tjoelker assert(logpar); 58953d49b37SJilles Tjoelker assert(bytes_read < LBUF_SIZE - 1); 59053d49b37SJilles Tjoelker 5916b4ef4b1SIhor Antonov if (do_log_reopen) { 5924cd407ecSMaxim Sobolev reopen_log(logpar); 5936b4ef4b1SIhor Antonov } 59453d49b37SJilles Tjoelker rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1); 59553d49b37SJilles Tjoelker if (rv > 0) { 59653d49b37SJilles Tjoelker unsigned char *cp; 59753d49b37SJilles Tjoelker 59853d49b37SJilles Tjoelker bytes_read += rv; 59953d49b37SJilles Tjoelker assert(bytes_read <= LBUF_SIZE - 1); 60053d49b37SJilles Tjoelker /* Always NUL-terminate just in case. */ 60153d49b37SJilles Tjoelker buf[LBUF_SIZE - 1] = '\0'; 60253d49b37SJilles Tjoelker /* 60353d49b37SJilles Tjoelker * Chomp line by line until we run out of buffer. 60453d49b37SJilles Tjoelker * This does not take NUL characters into account. 60553d49b37SJilles Tjoelker */ 60653d49b37SJilles Tjoelker while ((cp = memchr(buf, '\n', bytes_read)) != NULL) { 60753d49b37SJilles Tjoelker size_t bytes_line = cp - buf + 1; 60853d49b37SJilles Tjoelker assert(bytes_line <= bytes_read); 60953d49b37SJilles Tjoelker do_output(buf, bytes_line, logpar); 61053d49b37SJilles Tjoelker bytes_read -= bytes_line; 61153d49b37SJilles Tjoelker memmove(buf, cp + 1, bytes_read); 612195fc497SMikolaj Golub } 61353d49b37SJilles Tjoelker /* Wait until the buffer is full. */ 6146b4ef4b1SIhor Antonov if (bytes_read < LBUF_SIZE - 1) { 61553d49b37SJilles Tjoelker return 1; 6166b4ef4b1SIhor Antonov } 61753d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 61853d49b37SJilles Tjoelker bytes_read = 0; 61953d49b37SJilles Tjoelker return 1; 62053d49b37SJilles Tjoelker } else if (rv == -1) { 62153d49b37SJilles Tjoelker /* EINTR should trigger another read. */ 62253d49b37SJilles Tjoelker if (errno == EINTR) { 62353d49b37SJilles Tjoelker return 1; 62453d49b37SJilles Tjoelker } else { 62553d49b37SJilles Tjoelker warn("read"); 62653d49b37SJilles Tjoelker return 0; 627c60d51f9SMikolaj Golub } 62853d49b37SJilles Tjoelker } 62953d49b37SJilles Tjoelker /* Upon EOF, we have to flush what's left of the buffer. */ 63053d49b37SJilles Tjoelker if (bytes_read > 0) { 63153d49b37SJilles Tjoelker do_output(buf, bytes_read, logpar); 63253d49b37SJilles Tjoelker bytes_read = 0; 63353d49b37SJilles Tjoelker } 63453d49b37SJilles Tjoelker return 0; 63553d49b37SJilles Tjoelker } 63653d49b37SJilles Tjoelker 63753d49b37SJilles Tjoelker /* 63853d49b37SJilles Tjoelker * The default behavior is to stay silent if the user wants to redirect 63953d49b37SJilles Tjoelker * output to a file and/or syslog. If neither are provided, then we bounce 64053d49b37SJilles Tjoelker * everything back to parent's stdout. 64153d49b37SJilles Tjoelker */ 64253d49b37SJilles Tjoelker static void 64353d49b37SJilles Tjoelker do_output(const unsigned char *buf, size_t len, struct log_params *logpar) 64453d49b37SJilles Tjoelker { 64553d49b37SJilles Tjoelker assert(len <= LBUF_SIZE); 64653d49b37SJilles Tjoelker assert(logpar); 64753d49b37SJilles Tjoelker 6486b4ef4b1SIhor Antonov if (len < 1) { 64953d49b37SJilles Tjoelker return; 6506b4ef4b1SIhor Antonov } 651f2f9d31dSIhor Antonov if (logpar->syslog_enabled) { 652e70444c6SIhor Antonov syslog(logpar->syslog_priority, "%.*s", (int)len, buf); 6536b4ef4b1SIhor Antonov } 654e70444c6SIhor Antonov if (logpar->output_fd != -1) { 655e70444c6SIhor Antonov if (write(logpar->output_fd, buf, len) == -1) 65653d49b37SJilles Tjoelker warn("write"); 65753d49b37SJilles Tjoelker } 658*39ea4280SIhor Antonov if (logpar->keep_fds_open && 659*39ea4280SIhor Antonov !logpar->syslog_enabled && 660*39ea4280SIhor Antonov logpar->output_fd == -1) { 66153d49b37SJilles Tjoelker printf("%.*s", (int)len, buf); 66253d49b37SJilles Tjoelker } 6636b4ef4b1SIhor Antonov } 66453d49b37SJilles Tjoelker 66553d49b37SJilles Tjoelker /* 66653d49b37SJilles Tjoelker * We use the global PID acquired directly from fork. If there is no valid 66753d49b37SJilles Tjoelker * child pid, the handler should be blocked and/or child_gone == 1. 66853d49b37SJilles Tjoelker */ 66953d49b37SJilles Tjoelker static void 67053d49b37SJilles Tjoelker handle_term(int signo) 67153d49b37SJilles Tjoelker { 6726b4ef4b1SIhor Antonov if (pid > 0 && !child_gone) { 67353d49b37SJilles Tjoelker kill(pid, signo); 6746b4ef4b1SIhor Antonov } 675b6193c24SMikolaj Golub terminate = 1; 676195fc497SMikolaj Golub } 67753d49b37SJilles Tjoelker 67853d49b37SJilles Tjoelker static void 6794cd407ecSMaxim Sobolev handle_chld(int signo __unused) 68053d49b37SJilles Tjoelker { 6814cd407ecSMaxim Sobolev 68253d49b37SJilles Tjoelker for (;;) { 68353d49b37SJilles Tjoelker int rv = waitpid(-1, NULL, WNOHANG); 68453d49b37SJilles Tjoelker if (pid == rv) { 68553d49b37SJilles Tjoelker child_gone = 1; 68653d49b37SJilles Tjoelker break; 68753d49b37SJilles Tjoelker } else if (rv == -1 && errno != EINTR) { 68853d49b37SJilles Tjoelker warn("waitpid"); 68953d49b37SJilles Tjoelker return; 6902ad43027SMikolaj Golub } 6912ad43027SMikolaj Golub } 6922ad43027SMikolaj Golub } 6932ad43027SMikolaj Golub 6942ad43027SMikolaj Golub static void 6954cd407ecSMaxim Sobolev handle_hup(int signo __unused) 6964cd407ecSMaxim Sobolev { 6974cd407ecSMaxim Sobolev 6984cd407ecSMaxim Sobolev do_log_reopen = 1; 6994cd407ecSMaxim Sobolev } 7004cd407ecSMaxim Sobolev 7014cd407ecSMaxim Sobolev static int 7024cd407ecSMaxim Sobolev open_log(const char *outfn) 7034cd407ecSMaxim Sobolev { 7044cd407ecSMaxim Sobolev 7054cd407ecSMaxim Sobolev return open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600); 7064cd407ecSMaxim Sobolev } 7074cd407ecSMaxim Sobolev 7084cd407ecSMaxim Sobolev static void 709129ec8f4SIhor Antonov reopen_log(struct log_params *logparams) 7104cd407ecSMaxim Sobolev { 7114cd407ecSMaxim Sobolev int outfd; 7124cd407ecSMaxim Sobolev 7134cd407ecSMaxim Sobolev do_log_reopen = 0; 714129ec8f4SIhor Antonov outfd = open_log(logparams->output_filename); 715129ec8f4SIhor Antonov if (logparams->output_fd >= 0) { 716129ec8f4SIhor Antonov close(logparams->output_fd); 7176b4ef4b1SIhor Antonov } 718129ec8f4SIhor Antonov logparams->output_fd = outfd; 7194cd407ecSMaxim Sobolev } 7204cd407ecSMaxim Sobolev 721