2001-08-30 04:48:02 +00:00
|
|
|
/*-
|
2017-11-27 15:37:16 +00:00
|
|
|
* SPDX-License-Identifier: BSD-3-Clause
|
|
|
|
*
|
2001-08-30 04:48:02 +00:00
|
|
|
* Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Berkeley Software Design Inc's name may not be used to endorse or
|
|
|
|
* promote products derived from this software without specific prior
|
|
|
|
* written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
|
|
|
|
*/
|
|
|
|
|
2003-07-06 12:44:11 +00:00
|
|
|
#include <sys/cdefs.h>
|
|
|
|
__FBSDID("$FreeBSD$");
|
|
|
|
|
2005-08-24 17:24:39 +00:00
|
|
|
#include <sys/param.h>
|
2012-02-19 10:38:55 +00:00
|
|
|
#include <sys/mman.h>
|
2012-02-19 10:20:37 +00:00
|
|
|
#include <sys/wait.h>
|
2001-08-30 04:48:02 +00:00
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
#include <fcntl.h>
|
2001-08-30 04:48:02 +00:00
|
|
|
#include <err.h>
|
2004-06-02 06:48:13 +00:00
|
|
|
#include <errno.h>
|
2023-02-10 04:36:07 +00:00
|
|
|
#include <getopt.h>
|
2005-08-24 17:24:39 +00:00
|
|
|
#include <libutil.h>
|
2007-03-19 12:12:53 +00:00
|
|
|
#include <login_cap.h>
|
2022-08-11 01:44:20 +00:00
|
|
|
#include <paths.h>
|
2012-02-19 10:23:51 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <signal.h>
|
2001-08-30 04:48:02 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
2016-10-22 11:26:22 +00:00
|
|
|
#include <string.h>
|
|
|
|
#include <strings.h>
|
|
|
|
#define SYSLOG_NAMES
|
|
|
|
#include <syslog.h>
|
|
|
|
#include <time.h>
|
|
|
|
#include <assert.h>
|
|
|
|
|
|
|
|
#define LBUF_SIZE 4096
|
|
|
|
|
|
|
|
struct log_params {
|
|
|
|
int dosyslog;
|
|
|
|
int logpri;
|
|
|
|
int noclose;
|
|
|
|
int outfd;
|
2020-09-24 02:44:58 +00:00
|
|
|
const char *outfn;
|
2016-10-22 11:26:22 +00:00
|
|
|
};
|
2001-08-30 04:48:02 +00:00
|
|
|
|
2007-03-19 12:12:53 +00:00
|
|
|
static void restrict_process(const char *);
|
2016-10-22 11:26:22 +00:00
|
|
|
static void handle_term(int);
|
|
|
|
static void handle_chld(int);
|
2020-09-24 02:44:58 +00:00
|
|
|
static void handle_hup(int);
|
|
|
|
static int open_log(const char *);
|
|
|
|
static void reopen_log(struct log_params *);
|
2016-10-22 11:26:22 +00:00
|
|
|
static int listen_child(int, struct log_params *);
|
|
|
|
static int get_log_mapping(const char *, const CODE *);
|
|
|
|
static void open_pid_files(const char *, const char *, struct pidfh **,
|
|
|
|
struct pidfh **);
|
|
|
|
static void do_output(const unsigned char *, size_t, struct log_params *);
|
|
|
|
static void daemon_sleep(time_t, long);
|
2001-08-30 04:48:02 +00:00
|
|
|
|
2020-09-24 02:44:58 +00:00
|
|
|
static volatile sig_atomic_t terminate = 0, child_gone = 0, pid = 0,
|
|
|
|
do_log_reopen = 0;
|
2016-10-22 11:26:22 +00:00
|
|
|
|
2023-02-10 04:36:07 +00:00
|
|
|
static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h";
|
|
|
|
|
|
|
|
static const struct option longopts[] = {
|
|
|
|
{ "change-dir", no_argument, NULL, 'c' },
|
|
|
|
{ "close-fds", no_argument, NULL, 'f' },
|
|
|
|
{ "sighup", no_argument, NULL, 'H' },
|
|
|
|
{ "syslog", no_argument, NULL, 'S' },
|
|
|
|
{ "output-file", required_argument, NULL, 'o' },
|
|
|
|
{ "output-mask", required_argument, NULL, 'm' },
|
|
|
|
{ "child-pidfile", required_argument, NULL, 'p' },
|
|
|
|
{ "supervisor-pidfile", required_argument, NULL, 'P' },
|
|
|
|
{ "restart", no_argument, NULL, 'r' },
|
|
|
|
{ "restart-delay", required_argument, NULL, 'R' },
|
|
|
|
{ "title", required_argument, NULL, 't' },
|
|
|
|
{ "user", required_argument, NULL, 'u' },
|
|
|
|
{ "syslog-priority", required_argument, NULL, 's' },
|
|
|
|
{ "syslog-facility", required_argument, NULL, 'l' },
|
|
|
|
{ "syslog-tag", required_argument, NULL, 'T' },
|
|
|
|
{ "help", no_argument, NULL, 'h' },
|
|
|
|
{ NULL, 0, NULL, 0 }
|
|
|
|
};
|
|
|
|
|
|
|
|
static _Noreturn void
|
|
|
|
usage(int exitcode)
|
|
|
|
{
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
"usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n"
|
|
|
|
" [-u user] [-o output_file] [-t title]\n"
|
|
|
|
" [-l syslog_facility] [-s syslog_priority]\n"
|
|
|
|
" [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n"
|
|
|
|
"command arguments ...\n");
|
|
|
|
|
|
|
|
(void)fprintf(stderr,
|
|
|
|
" --change-dir -c Change the current working directory to root\n"
|
|
|
|
" --close-fds -f Set stdin, stdout, stderr to /dev/null\n"
|
|
|
|
" --sighup -H Close and re-open output file on SIGHUP\n"
|
|
|
|
" --syslog -S Send output to syslog\n"
|
|
|
|
" --output-file -o <file> Append output of the child process to file\n"
|
|
|
|
" --output-mask -m <mask> What to send to syslog/file\n"
|
|
|
|
" 1=stdout, 2=stderr, 3=both\n"
|
|
|
|
" --child-pidfile -p <file> Write PID of the child process to file\n"
|
|
|
|
" --supervisor-pidfile -P <file> Write PID of the supervisor process to file\n"
|
|
|
|
" --restart -r Restart child if it terminates (1 sec delay)\n"
|
|
|
|
" --restart-delay -R <N> Restart child if it terminates after N sec\n"
|
|
|
|
" --title -t <title> Set the title of the supervisor process\n"
|
|
|
|
" --user -u <user> Drop privileges, run as given user\n"
|
|
|
|
" --syslog-priority -s <prio> Set syslog priority\n"
|
|
|
|
" --syslog-facility -l <flty> Set syslog facility\n"
|
|
|
|
" --syslog-tag -T <tag> Set syslog tag\n"
|
|
|
|
" --help -h Show this help\n");
|
|
|
|
|
|
|
|
exit(exitcode);
|
|
|
|
}
|
|
|
|
|
2001-08-30 04:48:02 +00:00
|
|
|
int
|
|
|
|
main(int argc, char *argv[])
|
|
|
|
{
|
2016-10-22 11:26:22 +00:00
|
|
|
const char *pidfile, *ppidfile, *title, *user, *outfn, *logtag;
|
|
|
|
int ch, nochdir, noclose, restart, dosyslog, child_eof;
|
|
|
|
sigset_t mask_susp, mask_orig, mask_read, mask_term;
|
|
|
|
struct log_params logpar;
|
|
|
|
int pfd[2] = { -1, -1 }, outfd = -1;
|
2020-09-24 02:44:58 +00:00
|
|
|
int stdmask, logpri, logfac, log_reopen;
|
2016-10-22 11:26:22 +00:00
|
|
|
struct pidfh *ppfh, *pfh;
|
|
|
|
char *p;
|
2001-08-30 04:48:02 +00:00
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
memset(&logpar, 0, sizeof(logpar));
|
|
|
|
stdmask = STDOUT_FILENO | STDERR_FILENO;
|
|
|
|
ppidfile = pidfile = user = NULL;
|
2001-08-30 04:48:02 +00:00
|
|
|
nochdir = noclose = 1;
|
2016-10-22 11:26:22 +00:00
|
|
|
logpri = LOG_NOTICE;
|
|
|
|
logfac = LOG_DAEMON;
|
|
|
|
logtag = "daemon";
|
2012-02-19 10:36:29 +00:00
|
|
|
restart = 0;
|
2016-10-22 11:26:22 +00:00
|
|
|
dosyslog = 0;
|
2020-09-24 02:44:58 +00:00
|
|
|
log_reopen = 0;
|
2016-10-22 11:26:22 +00:00
|
|
|
outfn = NULL;
|
|
|
|
title = NULL;
|
2023-02-10 04:36:07 +00:00
|
|
|
while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
|
2001-08-30 04:48:02 +00:00
|
|
|
switch (ch) {
|
|
|
|
case 'c':
|
|
|
|
nochdir = 0;
|
|
|
|
break;
|
|
|
|
case 'f':
|
|
|
|
noclose = 0;
|
|
|
|
break;
|
2020-09-24 02:44:58 +00:00
|
|
|
case 'H':
|
|
|
|
log_reopen = 1;
|
|
|
|
break;
|
2016-10-22 11:26:22 +00:00
|
|
|
case 'l':
|
|
|
|
logfac = get_log_mapping(optarg, facilitynames);
|
|
|
|
if (logfac == -1)
|
|
|
|
errx(5, "unrecognized syslog facility");
|
|
|
|
dosyslog = 1;
|
|
|
|
break;
|
|
|
|
case 'm':
|
|
|
|
stdmask = strtol(optarg, &p, 10);
|
|
|
|
if (p == optarg || stdmask < 0 || stdmask > 3)
|
|
|
|
errx(6, "unrecognized listening mask");
|
|
|
|
break;
|
|
|
|
case 'o':
|
|
|
|
outfn = optarg;
|
|
|
|
break;
|
2004-06-02 06:48:13 +00:00
|
|
|
case 'p':
|
|
|
|
pidfile = optarg;
|
|
|
|
break;
|
2013-09-13 16:57:28 +00:00
|
|
|
case 'P':
|
|
|
|
ppidfile = optarg;
|
|
|
|
break;
|
2012-02-19 10:36:29 +00:00
|
|
|
case 'r':
|
|
|
|
restart = 1;
|
|
|
|
break;
|
2018-04-15 21:46:08 +00:00
|
|
|
case 'R':
|
|
|
|
restart = strtol(optarg, &p, 0);
|
|
|
|
if (p == optarg || restart < 1)
|
|
|
|
errx(6, "invalid restart delay");
|
|
|
|
break;
|
2016-10-22 11:26:22 +00:00
|
|
|
case 's':
|
|
|
|
logpri = get_log_mapping(optarg, prioritynames);
|
|
|
|
if (logpri == -1)
|
|
|
|
errx(4, "unrecognized syslog priority");
|
|
|
|
dosyslog = 1;
|
|
|
|
break;
|
|
|
|
case 'S':
|
|
|
|
dosyslog = 1;
|
|
|
|
break;
|
2016-03-02 19:10:39 +00:00
|
|
|
case 't':
|
|
|
|
title = optarg;
|
|
|
|
break;
|
2016-10-22 11:26:22 +00:00
|
|
|
case 'T':
|
|
|
|
logtag = optarg;
|
|
|
|
dosyslog = 1;
|
|
|
|
break;
|
2007-03-19 12:12:53 +00:00
|
|
|
case 'u':
|
|
|
|
user = optarg;
|
|
|
|
break;
|
2023-02-10 04:36:07 +00:00
|
|
|
case 'h':
|
|
|
|
usage(0);
|
|
|
|
__builtin_unreachable();
|
2001-08-30 04:48:02 +00:00
|
|
|
default:
|
2023-02-10 04:36:07 +00:00
|
|
|
usage(1);
|
2001-08-30 04:48:02 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
argc -= optind;
|
|
|
|
argv += optind;
|
|
|
|
|
|
|
|
if (argc == 0)
|
2023-02-10 04:36:07 +00:00
|
|
|
usage(1);
|
2007-03-09 09:33:19 +00:00
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
if (!title)
|
|
|
|
title = argv[0];
|
|
|
|
|
|
|
|
if (outfn) {
|
2020-09-24 02:44:58 +00:00
|
|
|
outfd = open_log(outfn);
|
2016-10-22 11:26:22 +00:00
|
|
|
if (outfd == -1)
|
|
|
|
err(7, "open");
|
|
|
|
}
|
2023-02-10 04:36:07 +00:00
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
if (dosyslog)
|
|
|
|
openlog(logtag, LOG_PID | LOG_NDELAY, logfac);
|
|
|
|
|
2013-09-13 16:57:28 +00:00
|
|
|
ppfh = pfh = NULL;
|
2004-06-02 06:48:13 +00:00
|
|
|
/*
|
|
|
|
* Try to open the pidfile before calling daemon(3),
|
|
|
|
* to be able to report the error intelligently
|
|
|
|
*/
|
2016-10-22 11:26:22 +00:00
|
|
|
open_pid_files(pidfile, ppidfile, &pfh, &ppfh);
|
2013-09-19 18:00:05 +00:00
|
|
|
if (daemon(nochdir, noclose) == -1) {
|
|
|
|
warn("daemon");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
/* Write out parent pidfile if needed. */
|
|
|
|
pidfile_write(ppfh);
|
2012-02-19 10:23:51 +00:00
|
|
|
/*
|
2012-02-19 10:36:29 +00:00
|
|
|
* If the pidfile or restart option is specified the daemon
|
|
|
|
* executes the command in a forked process and wait on child
|
|
|
|
* exit to remove the pidfile or restart the command. Normally
|
|
|
|
* we don't want the monitoring daemon to be terminated
|
|
|
|
* leaving the running process and the stale pidfile, so we
|
|
|
|
* catch SIGTERM and forward it to the children expecting to
|
2016-10-22 11:26:22 +00:00
|
|
|
* get SIGCHLD eventually. We also must fork() to obtain a
|
|
|
|
* readable pipe with the child for writing to a log file
|
|
|
|
* and syslog.
|
2012-02-19 10:23:51 +00:00
|
|
|
*/
|
|
|
|
pid = -1;
|
2016-10-22 11:26:22 +00:00
|
|
|
if (pidfile || ppidfile || restart || outfd != -1 || dosyslog) {
|
2020-09-24 02:44:58 +00:00
|
|
|
struct sigaction act_term, act_chld, act_hup;
|
2016-10-22 11:26:22 +00:00
|
|
|
|
|
|
|
/* Avoid PID racing with SIGCHLD and SIGTERM. */
|
|
|
|
memset(&act_term, 0, sizeof(act_term));
|
|
|
|
act_term.sa_handler = handle_term;
|
|
|
|
sigemptyset(&act_term.sa_mask);
|
|
|
|
sigaddset(&act_term.sa_mask, SIGCHLD);
|
|
|
|
|
|
|
|
memset(&act_chld, 0, sizeof(act_chld));
|
|
|
|
act_chld.sa_handler = handle_chld;
|
|
|
|
sigemptyset(&act_chld.sa_mask);
|
|
|
|
sigaddset(&act_chld.sa_mask, SIGTERM);
|
|
|
|
|
2020-09-24 02:44:58 +00:00
|
|
|
memset(&act_hup, 0, sizeof(act_hup));
|
|
|
|
act_hup.sa_handler = handle_hup;
|
|
|
|
sigemptyset(&act_hup.sa_mask);
|
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
/* Block these when avoiding racing before sigsuspend(). */
|
|
|
|
sigemptyset(&mask_susp);
|
|
|
|
sigaddset(&mask_susp, SIGTERM);
|
|
|
|
sigaddset(&mask_susp, SIGCHLD);
|
|
|
|
/* Block SIGTERM when we lack a valid child PID. */
|
|
|
|
sigemptyset(&mask_term);
|
|
|
|
sigaddset(&mask_term, SIGTERM);
|
2012-02-19 10:23:51 +00:00
|
|
|
/*
|
2016-10-22 11:26:22 +00:00
|
|
|
* When reading, we wish to avoid SIGCHLD. SIGTERM
|
|
|
|
* has to be caught, otherwise we'll be stuck until
|
|
|
|
* the read() returns - if it returns.
|
2012-02-19 10:23:51 +00:00
|
|
|
*/
|
2016-10-22 11:26:22 +00:00
|
|
|
sigemptyset(&mask_read);
|
|
|
|
sigaddset(&mask_read, SIGCHLD);
|
|
|
|
/* Block SIGTERM to avoid racing until we have forked. */
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) {
|
|
|
|
warn("sigprocmask");
|
2013-09-19 18:00:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2016-10-22 11:26:22 +00:00
|
|
|
if (sigaction(SIGTERM, &act_term, NULL) == -1) {
|
|
|
|
warn("sigaction");
|
2013-09-19 18:00:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2016-10-22 11:26:22 +00:00
|
|
|
if (sigaction(SIGCHLD, &act_chld, NULL) == -1) {
|
|
|
|
warn("sigaction");
|
2013-09-19 18:00:05 +00:00
|
|
|
goto exit;
|
|
|
|
}
|
2012-02-19 10:38:55 +00:00
|
|
|
/*
|
|
|
|
* Try to protect against pageout kill. Ignore the
|
|
|
|
* error, madvise(2) will fail only if a process does
|
|
|
|
* not have superuser privileges.
|
|
|
|
*/
|
|
|
|
(void)madvise(NULL, 0, MADV_PROTECT);
|
2016-10-22 11:26:22 +00:00
|
|
|
logpar.outfd = outfd;
|
|
|
|
logpar.dosyslog = dosyslog;
|
|
|
|
logpar.logpri = logpri;
|
|
|
|
logpar.noclose = noclose;
|
2020-09-24 02:44:58 +00:00
|
|
|
logpar.outfn = outfn;
|
|
|
|
if (log_reopen && outfd >= 0 &&
|
|
|
|
sigaction(SIGHUP, &act_hup, NULL) == -1) {
|
|
|
|
warn("sigaction");
|
|
|
|
goto exit;
|
|
|
|
}
|
2012-02-19 10:36:29 +00:00
|
|
|
restart:
|
2016-10-22 11:26:22 +00:00
|
|
|
if (pipe(pfd))
|
|
|
|
err(1, "pipe");
|
2012-02-19 10:20:37 +00:00
|
|
|
/*
|
2016-10-22 11:26:22 +00:00
|
|
|
* Spawn a child to exec the command.
|
2012-02-19 10:20:37 +00:00
|
|
|
*/
|
2016-10-22 11:26:22 +00:00
|
|
|
child_gone = 0;
|
2012-02-19 10:20:37 +00:00
|
|
|
pid = fork();
|
|
|
|
if (pid == -1) {
|
2013-09-19 18:00:05 +00:00
|
|
|
warn("fork");
|
|
|
|
goto exit;
|
2016-10-22 11:26:22 +00:00
|
|
|
} else if (pid > 0) {
|
|
|
|
/*
|
|
|
|
* Unblock SIGTERM after we know we have a valid
|
|
|
|
* child PID to signal.
|
|
|
|
*/
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) {
|
|
|
|
warn("sigprocmask");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
close(pfd[1]);
|
|
|
|
pfd[1] = -1;
|
2012-02-19 10:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
2012-02-19 10:23:51 +00:00
|
|
|
if (pid <= 0) {
|
2012-02-19 10:20:37 +00:00
|
|
|
/* Now that we are the child, write out the pid. */
|
2005-08-24 17:24:39 +00:00
|
|
|
pidfile_write(pfh);
|
2004-06-02 06:48:13 +00:00
|
|
|
|
2012-02-19 10:20:37 +00:00
|
|
|
if (user != NULL)
|
|
|
|
restrict_process(user);
|
2016-10-22 11:26:22 +00:00
|
|
|
/*
|
|
|
|
* When forking, the child gets the original sigmask,
|
|
|
|
* and dup'd pipes.
|
|
|
|
*/
|
|
|
|
if (pid == 0) {
|
|
|
|
close(pfd[0]);
|
|
|
|
if (sigprocmask(SIG_SETMASK, &mask_orig, NULL))
|
|
|
|
err(1, "sigprogmask");
|
|
|
|
if (stdmask & STDERR_FILENO) {
|
|
|
|
if (dup2(pfd[1], STDERR_FILENO) == -1)
|
|
|
|
err(1, "dup2");
|
|
|
|
}
|
|
|
|
if (stdmask & STDOUT_FILENO) {
|
|
|
|
if (dup2(pfd[1], STDOUT_FILENO) == -1)
|
|
|
|
err(1, "dup2");
|
|
|
|
}
|
|
|
|
if (pfd[1] != STDERR_FILENO &&
|
|
|
|
pfd[1] != STDOUT_FILENO)
|
|
|
|
close(pfd[1]);
|
|
|
|
}
|
2012-02-19 10:20:37 +00:00
|
|
|
execvp(argv[0], argv);
|
|
|
|
/*
|
|
|
|
* execvp() failed -- report the error. The child is
|
|
|
|
* now running, so the exit status doesn't matter.
|
|
|
|
*/
|
|
|
|
err(1, "%s", argv[0]);
|
|
|
|
}
|
2016-10-22 11:26:22 +00:00
|
|
|
setproctitle("%s[%d]", title, (int)pid);
|
|
|
|
/*
|
|
|
|
* As we have closed the write end of pipe for parent process,
|
|
|
|
* we might detect the child's exit by reading EOF. The child
|
|
|
|
* might have closed its stdout and stderr, so we must wait for
|
|
|
|
* the SIGCHLD to ensure that the process is actually gone.
|
|
|
|
*/
|
|
|
|
child_eof = 0;
|
|
|
|
for (;;) {
|
|
|
|
/*
|
|
|
|
* We block SIGCHLD when listening, but SIGTERM we accept
|
|
|
|
* so the read() won't block if we wish to depart.
|
|
|
|
*
|
|
|
|
* Upon receiving SIGTERM, we have several options after
|
|
|
|
* sending the SIGTERM to our child:
|
|
|
|
* - read until EOF
|
|
|
|
* - read until EOF but only for a while
|
|
|
|
* - bail immediately
|
|
|
|
*
|
|
|
|
* We go for the third, as otherwise we have no guarantee
|
|
|
|
* that we won't block indefinitely if the child refuses
|
|
|
|
* to depart. To handle the second option, a different
|
|
|
|
* approach would be needed (procctl()?)
|
|
|
|
*/
|
|
|
|
if (child_gone && child_eof) {
|
|
|
|
break;
|
|
|
|
} else if (terminate) {
|
|
|
|
goto exit;
|
|
|
|
} else if (!child_eof) {
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) {
|
|
|
|
warn("sigprocmask");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
child_eof = !listen_child(pfd[0], &logpar);
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) {
|
|
|
|
warn("sigprocmask");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) {
|
|
|
|
warn("sigprocmask");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
while (!terminate && !child_gone)
|
|
|
|
sigsuspend(&mask_orig);
|
|
|
|
if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) {
|
|
|
|
warn("sigprocmask");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2019-06-04 16:07:01 +00:00
|
|
|
if (restart && !terminate)
|
|
|
|
daemon_sleep(restart, 0);
|
2016-10-22 11:26:22 +00:00
|
|
|
if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) {
|
|
|
|
warn("sigprocmask");
|
|
|
|
goto exit;
|
|
|
|
}
|
|
|
|
if (restart && !terminate) {
|
|
|
|
close(pfd[0]);
|
|
|
|
pfd[0] = -1;
|
2012-02-19 10:36:29 +00:00
|
|
|
goto restart;
|
|
|
|
}
|
2013-09-19 18:00:05 +00:00
|
|
|
exit:
|
2016-10-22 11:26:22 +00:00
|
|
|
close(outfd);
|
|
|
|
close(pfd[0]);
|
|
|
|
close(pfd[1]);
|
|
|
|
if (dosyslog)
|
|
|
|
closelog();
|
2012-02-19 10:20:37 +00:00
|
|
|
pidfile_remove(pfh);
|
2013-09-13 16:57:28 +00:00
|
|
|
pidfile_remove(ppfh);
|
2013-09-19 18:00:05 +00:00
|
|
|
exit(1); /* If daemon(3) succeeded exit status does not matter. */
|
2001-08-30 04:48:02 +00:00
|
|
|
}
|
|
|
|
|
2012-02-19 10:23:51 +00:00
|
|
|
static void
|
2016-10-22 11:26:22 +00:00
|
|
|
daemon_sleep(time_t secs, long nsecs)
|
|
|
|
{
|
|
|
|
struct timespec ts = { secs, nsecs };
|
2019-06-04 16:07:01 +00:00
|
|
|
|
|
|
|
while (!terminate && nanosleep(&ts, &ts) == -1) {
|
2016-10-22 11:26:22 +00:00
|
|
|
if (errno != EINTR)
|
|
|
|
err(1, "nanosleep");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
open_pid_files(const char *pidfile, const char *ppidfile,
|
|
|
|
struct pidfh **pfh, struct pidfh **ppfh)
|
|
|
|
{
|
|
|
|
pid_t fpid;
|
|
|
|
int serrno;
|
|
|
|
|
|
|
|
if (pidfile) {
|
|
|
|
*pfh = pidfile_open(pidfile, 0600, &fpid);
|
|
|
|
if (*pfh == NULL) {
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
errx(3, "process already running, pid: %d",
|
|
|
|
fpid);
|
|
|
|
}
|
|
|
|
err(2, "pidfile ``%s''", pidfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Do the same for the actual daemon process. */
|
|
|
|
if (ppidfile) {
|
|
|
|
*ppfh = pidfile_open(ppidfile, 0600, &fpid);
|
|
|
|
if (*ppfh == NULL) {
|
|
|
|
serrno = errno;
|
|
|
|
pidfile_remove(*pfh);
|
|
|
|
errno = serrno;
|
|
|
|
if (errno == EEXIST) {
|
|
|
|
errx(3, "process already running, pid: %d",
|
|
|
|
fpid);
|
|
|
|
}
|
|
|
|
err(2, "ppidfile ``%s''", ppidfile);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
get_log_mapping(const char *str, const CODE *c)
|
2012-02-19 10:23:51 +00:00
|
|
|
{
|
2016-10-22 11:26:22 +00:00
|
|
|
const CODE *cp;
|
|
|
|
for (cp = c; cp->c_name; cp++)
|
|
|
|
if (strcmp(cp->c_name, str) == 0)
|
|
|
|
return cp->c_val;
|
|
|
|
return -1;
|
2012-02-19 10:23:51 +00:00
|
|
|
}
|
|
|
|
|
2007-03-09 09:33:19 +00:00
|
|
|
static void
|
2007-03-19 12:12:53 +00:00
|
|
|
restrict_process(const char *user)
|
2007-03-09 09:33:19 +00:00
|
|
|
{
|
|
|
|
struct passwd *pw = NULL;
|
|
|
|
|
2007-03-19 12:12:53 +00:00
|
|
|
pw = getpwnam(user);
|
|
|
|
if (pw == NULL)
|
|
|
|
errx(1, "unknown user: %s", user);
|
|
|
|
|
|
|
|
if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0)
|
|
|
|
errx(1, "failed to set user environment");
|
2022-08-11 01:44:20 +00:00
|
|
|
|
|
|
|
setenv("USER", pw->pw_name, 1);
|
|
|
|
setenv("HOME", pw->pw_dir, 1);
|
|
|
|
setenv("SHELL", *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL, 1);
|
2007-03-09 09:33:19 +00:00
|
|
|
}
|
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
/*
|
|
|
|
* We try to collect whole lines terminated by '\n'. Otherwise we collect a
|
|
|
|
* full buffer, and then output it.
|
|
|
|
*
|
|
|
|
* Return value of 0 is assumed to mean EOF or error, and 1 indicates to
|
|
|
|
* continue reading.
|
|
|
|
*/
|
2012-02-19 10:36:29 +00:00
|
|
|
static int
|
2016-10-22 11:26:22 +00:00
|
|
|
listen_child(int fd, struct log_params *logpar)
|
2012-02-19 10:20:37 +00:00
|
|
|
{
|
2016-10-22 11:26:22 +00:00
|
|
|
static unsigned char buf[LBUF_SIZE];
|
|
|
|
static size_t bytes_read = 0;
|
|
|
|
int rv;
|
2012-02-19 10:20:37 +00:00
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
assert(logpar);
|
|
|
|
assert(bytes_read < LBUF_SIZE - 1);
|
|
|
|
|
2020-09-24 02:44:58 +00:00
|
|
|
if (do_log_reopen)
|
|
|
|
reopen_log(logpar);
|
2016-10-22 11:26:22 +00:00
|
|
|
rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1);
|
|
|
|
if (rv > 0) {
|
|
|
|
unsigned char *cp;
|
|
|
|
|
|
|
|
bytes_read += rv;
|
|
|
|
assert(bytes_read <= LBUF_SIZE - 1);
|
|
|
|
/* Always NUL-terminate just in case. */
|
|
|
|
buf[LBUF_SIZE - 1] = '\0';
|
|
|
|
/*
|
|
|
|
* Chomp line by line until we run out of buffer.
|
|
|
|
* This does not take NUL characters into account.
|
|
|
|
*/
|
|
|
|
while ((cp = memchr(buf, '\n', bytes_read)) != NULL) {
|
|
|
|
size_t bytes_line = cp - buf + 1;
|
|
|
|
assert(bytes_line <= bytes_read);
|
|
|
|
do_output(buf, bytes_line, logpar);
|
|
|
|
bytes_read -= bytes_line;
|
|
|
|
memmove(buf, cp + 1, bytes_read);
|
2012-02-19 10:23:51 +00:00
|
|
|
}
|
2016-10-22 11:26:22 +00:00
|
|
|
/* Wait until the buffer is full. */
|
|
|
|
if (bytes_read < LBUF_SIZE - 1)
|
|
|
|
return 1;
|
|
|
|
do_output(buf, bytes_read, logpar);
|
|
|
|
bytes_read = 0;
|
|
|
|
return 1;
|
|
|
|
} else if (rv == -1) {
|
|
|
|
/* EINTR should trigger another read. */
|
|
|
|
if (errno == EINTR) {
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
warn("read");
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Upon EOF, we have to flush what's left of the buffer. */
|
|
|
|
if (bytes_read > 0) {
|
|
|
|
do_output(buf, bytes_read, logpar);
|
|
|
|
bytes_read = 0;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* The default behavior is to stay silent if the user wants to redirect
|
|
|
|
* output to a file and/or syslog. If neither are provided, then we bounce
|
|
|
|
* everything back to parent's stdout.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
|
|
|
|
{
|
|
|
|
assert(len <= LBUF_SIZE);
|
|
|
|
assert(logpar);
|
|
|
|
|
|
|
|
if (len < 1)
|
|
|
|
return;
|
|
|
|
if (logpar->dosyslog)
|
|
|
|
syslog(logpar->logpri, "%.*s", (int)len, buf);
|
|
|
|
if (logpar->outfd != -1) {
|
|
|
|
if (write(logpar->outfd, buf, len) == -1)
|
|
|
|
warn("write");
|
|
|
|
}
|
|
|
|
if (logpar->noclose && !logpar->dosyslog && logpar->outfd == -1)
|
|
|
|
printf("%.*s", (int)len, buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* We use the global PID acquired directly from fork. If there is no valid
|
|
|
|
* child pid, the handler should be blocked and/or child_gone == 1.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
handle_term(int signo)
|
|
|
|
{
|
|
|
|
if (pid > 0 && !child_gone)
|
|
|
|
kill(pid, signo);
|
|
|
|
terminate = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2020-09-24 02:44:58 +00:00
|
|
|
handle_chld(int signo __unused)
|
2016-10-22 11:26:22 +00:00
|
|
|
{
|
2020-09-24 02:44:58 +00:00
|
|
|
|
2016-10-22 11:26:22 +00:00
|
|
|
for (;;) {
|
|
|
|
int rv = waitpid(-1, NULL, WNOHANG);
|
|
|
|
if (pid == rv) {
|
|
|
|
child_gone = 1;
|
|
|
|
break;
|
|
|
|
} else if (rv == -1 && errno != EINTR) {
|
|
|
|
warn("waitpid");
|
|
|
|
return;
|
2012-02-19 10:20:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-09-24 02:44:58 +00:00
|
|
|
static void
|
|
|
|
handle_hup(int signo __unused)
|
|
|
|
{
|
|
|
|
|
|
|
|
do_log_reopen = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
open_log(const char *outfn)
|
|
|
|
{
|
|
|
|
|
|
|
|
return open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
reopen_log(struct log_params *lpp)
|
|
|
|
{
|
|
|
|
int outfd;
|
|
|
|
|
|
|
|
do_log_reopen = 0;
|
|
|
|
outfd = open_log(lpp->outfn);
|
|
|
|
if (lpp->outfd >= 0)
|
|
|
|
close(lpp->outfd);
|
|
|
|
lpp->outfd = outfd;
|
|
|
|
}
|
|
|
|
|