daemon: more human-friendly variable names

Reviewed by:	kevans
Pull Request:	https://github.com/freebsd/freebsd-src/pull/669
This commit is contained in:
Ihor Antonov 2023-03-01 21:00:43 -06:00 committed by Kyle Evans
parent 6f0636728b
commit 129ec8f480

View File

@ -64,7 +64,7 @@ struct log_params {
const char *syslog_tag; const char *syslog_tag;
int syslog_priority; int syslog_priority;
int syslog_facility; int syslog_facility;
int noclose; int keep_fds_open;
int output_fd; int output_fd;
bool syslog_enabled; bool syslog_enabled;
}; };
@ -147,27 +147,27 @@ main(int argc, char *argv[])
bool supervision_enabled = false; bool supervision_enabled = false;
bool log_reopen = false; bool log_reopen = false;
char *p = NULL; char *p = NULL;
const char *pidfile = NULL; const char *child_pidfile = NULL;
const char *ppidfile = NULL; const char *parent_pidfile = NULL;
const char *title = NULL; const char *title = NULL;
const char *user = NULL; const char *user = NULL;
int ch = 0; int ch = 0;
int child_eof = 0; int child_eof = 0;
int nochdir = 1; int keep_cur_workdir = 1;
int pfd[2] = { -1, -1 }; int pfd[2] = { -1, -1 };
int restart = 0; int restart = 0;
int stdmask = STDOUT_FILENO | STDERR_FILENO; int stdmask = STDOUT_FILENO | STDERR_FILENO;
struct log_params logpar = { struct log_params logparams = {
.syslog_enabled = false, .syslog_enabled = false,
.syslog_priority = LOG_NOTICE, .syslog_priority = LOG_NOTICE,
.syslog_tag = "daemon", .syslog_tag = "daemon",
.syslog_facility = LOG_DAEMON, .syslog_facility = LOG_DAEMON,
.noclose = 1, .keep_fds_open = 1,
.output_fd = -1, .output_fd = -1,
.output_filename = NULL .output_filename = NULL
}; };
struct pidfh *ppfh = NULL; struct pidfh *parent_pidfh = NULL;
struct pidfh *pfh = NULL; struct pidfh *child_pidfh = NULL;
sigset_t mask_orig; sigset_t mask_orig;
sigset_t mask_read; sigset_t mask_read;
sigset_t mask_term; sigset_t mask_term;
@ -181,20 +181,20 @@ main(int argc, char *argv[])
while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) { while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
switch (ch) { switch (ch) {
case 'c': case 'c':
nochdir = 0; keep_cur_workdir = 0;
break; break;
case 'f': case 'f':
logpar.noclose = 0; logparams.keep_fds_open = 0;
break; break;
case 'H': case 'H':
log_reopen = true; log_reopen = true;
break; break;
case 'l': case 'l':
logpar.syslog_facility = get_log_mapping(optarg, facilitynames); logparams.syslog_facility = get_log_mapping(optarg, facilitynames);
if (logpar.syslog_facility == -1) { if (logparams.syslog_facility == -1) {
errx(5, "unrecognized syslog facility"); errx(5, "unrecognized syslog facility");
} }
logpar.syslog_enabled = true; logparams.syslog_enabled = true;
break; break;
case 'm': case 'm':
stdmask = strtol(optarg, &p, 10); stdmask = strtol(optarg, &p, 10);
@ -203,13 +203,13 @@ main(int argc, char *argv[])
} }
break; break;
case 'o': case 'o':
logpar.output_filename = optarg; logparams.output_filename = optarg;
break; break;
case 'p': case 'p':
pidfile = optarg; child_pidfile = optarg;
break; break;
case 'P': case 'P':
ppidfile = optarg; parent_pidfile = optarg;
break; break;
case 'r': case 'r':
restart = 1; restart = 1;
@ -221,21 +221,21 @@ main(int argc, char *argv[])
} }
break; break;
case 's': case 's':
logpar.syslog_priority = get_log_mapping(optarg, prioritynames); logparams.syslog_priority = get_log_mapping(optarg, prioritynames);
if (logpar.syslog_priority == -1) { if (logparams.syslog_priority == -1) {
errx(4, "unrecognized syslog priority"); errx(4, "unrecognized syslog priority");
} }
logpar.syslog_enabled = true; logparams.syslog_enabled = true;
break; break;
case 'S': case 'S':
logpar.syslog_enabled = true; logparams.syslog_enabled = true;
break; break;
case 't': case 't':
title = optarg; title = optarg;
break; break;
case 'T': case 'T':
logpar.syslog_tag = optarg; logparams.syslog_tag = optarg;
logpar.syslog_enabled = true; logparams.syslog_enabled = true;
break; break;
case 'u': case 'u':
user = optarg; user = optarg;
@ -258,28 +258,28 @@ main(int argc, char *argv[])
title = argv[0]; title = argv[0];
} }
if (logpar.output_filename) { if (logparams.output_filename) {
logpar.output_fd = open_log(logpar.output_filename); logparams.output_fd = open_log(logparams.output_filename);
if (logpar.output_fd == -1) { if (logparams.output_fd == -1) {
err(7, "open"); err(7, "open");
} }
} }
if (logpar.syslog_enabled) { if (logparams.syslog_enabled) {
openlog(logpar.syslog_tag, LOG_PID | LOG_NDELAY, logpar.syslog_facility); openlog(logparams.syslog_tag, LOG_PID | LOG_NDELAY, logparams.syslog_facility);
} }
/* /*
* Try to open the pidfile before calling daemon(3), * Try to open the pidfile before calling daemon(3),
* to be able to report the error intelligently * to be able to report the error intelligently
*/ */
open_pid_files(pidfile, ppidfile, &pfh, &ppfh); open_pid_files(child_pidfile, parent_pidfile, &child_pidfh, &parent_pidfh);
if (daemon(nochdir, logpar.noclose) == -1) { if (daemon(keep_cur_workdir, logparams.keep_fds_open) == -1) {
warn("daemon"); warn("daemon");
goto exit; goto exit;
} }
/* Write out parent pidfile if needed. */ /* Write out parent pidfile if needed. */
pidfile_write(ppfh); pidfile_write(parent_pidfh);
/* /*
* Supervision mode is enabled if one of the following options are used: * Supervision mode is enabled if one of the following options are used:
@ -298,11 +298,11 @@ main(int argc, char *argv[])
* To achieve this daemon catches SIGTERM and * To achieve this daemon catches SIGTERM and
* forwards it to the child, expecting to get SIGCHLD eventually. * forwards it to the child, expecting to get SIGCHLD eventually.
*/ */
supervision_enabled = pidfile != NULL || supervision_enabled = child_pidfile != NULL ||
ppidfile != NULL || parent_pidfile != NULL ||
restart != 0 || restart != 0 ||
logpar.output_fd != -1 || logparams.output_fd != -1 ||
logpar.syslog_enabled == true; logparams.syslog_enabled == true;
if (supervision_enabled) { if (supervision_enabled) {
struct sigaction act_term = { 0 }; struct sigaction act_term = { 0 };
@ -351,7 +351,7 @@ main(int argc, char *argv[])
* not have superuser privileges. * not have superuser privileges.
*/ */
(void)madvise(NULL, 0, MADV_PROTECT); (void)madvise(NULL, 0, MADV_PROTECT);
if (log_reopen && logpar.output_fd >= 0 && if (log_reopen && logparams.output_fd >= 0 &&
sigaction(SIGHUP, &act_hup, NULL) == -1) { sigaction(SIGHUP, &act_hup, NULL) == -1) {
warn("sigaction"); warn("sigaction");
goto exit; goto exit;
@ -383,7 +383,7 @@ main(int argc, char *argv[])
} }
if (pid <= 0) { if (pid <= 0) {
/* Now that we are the child, write out the pid. */ /* Now that we are the child, write out the pid. */
pidfile_write(pfh); pidfile_write(child_pidfh);
if (user != NULL) { if (user != NULL) {
restrict_process(user); restrict_process(user);
@ -451,7 +451,7 @@ main(int argc, char *argv[])
warn("sigprocmask"); warn("sigprocmask");
goto exit; goto exit;
} }
child_eof = !listen_child(pfd[0], &logpar); child_eof = !listen_child(pfd[0], &logparams);
if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) { if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) {
warn("sigprocmask"); warn("sigprocmask");
goto exit; goto exit;
@ -482,14 +482,14 @@ main(int argc, char *argv[])
goto restart; goto restart;
} }
exit: exit:
close(logpar.output_fd); close(logparams.output_fd);
close(pfd[0]); close(pfd[0]);
close(pfd[1]); close(pfd[1]);
if (logpar.syslog_enabled) { if (logparams.syslog_enabled) {
closelog(); closelog();
} }
pidfile_remove(pfh); pidfile_remove(child_pidfh);
pidfile_remove(ppfh); pidfile_remove(parent_pidfh);
exit(1); /* If daemon(3) succeeded exit status does not matter. */ exit(1); /* If daemon(3) succeeded exit status does not matter. */
} }
@ -652,7 +652,7 @@ do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
if (write(logpar->output_fd, buf, len) == -1) if (write(logpar->output_fd, buf, len) == -1)
warn("write"); warn("write");
} }
if (logpar->noclose && !logpar->syslog_enabled && logpar->output_fd == -1) { if (logpar->keep_fds_open && !logpar->syslog_enabled && logpar->output_fd == -1) {
printf("%.*s", (int)len, buf); printf("%.*s", (int)len, buf);
} }
} }
@ -701,15 +701,15 @@ open_log(const char *outfn)
} }
static void static void
reopen_log(struct log_params *lpp) reopen_log(struct log_params *logparams)
{ {
int outfd; int outfd;
do_log_reopen = 0; do_log_reopen = 0;
outfd = open_log(lpp->output_filename); outfd = open_log(logparams->output_filename);
if (lpp->output_fd >= 0) { if (logparams->output_fd >= 0) {
close(lpp->output_fd); close(logparams->output_fd);
} }
lpp->output_fd = outfd; logparams->output_fd = outfd;
} }