daemon: style changes
This is not a functional change. - Clean up whitespace (spaces where there should be tabs) - Break up lines that are longer than 80 Reviewed by: kevans Pull Request: https://github.com/freebsd/freebsd-src/pull/672
This commit is contained in:
parent
dda4d97289
commit
39ea4280e4
@ -138,7 +138,7 @@ usage(int exitcode)
|
|||||||
" --syslog-tag -T <tag> Set syslog tag\n"
|
" --syslog-tag -T <tag> Set syslog tag\n"
|
||||||
" --help -h Show this help\n");
|
" --help -h Show this help\n");
|
||||||
|
|
||||||
exit(exitcode);
|
exit(exitcode);
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
int
|
||||||
@ -165,7 +165,7 @@ main(int argc, char *argv[])
|
|||||||
.keep_fds_open = 1,
|
.keep_fds_open = 1,
|
||||||
.output_fd = -1,
|
.output_fd = -1,
|
||||||
.output_filename = NULL
|
.output_filename = NULL
|
||||||
};
|
};
|
||||||
struct pidfh *parent_pidfh = NULL;
|
struct pidfh *parent_pidfh = NULL;
|
||||||
struct pidfh *child_pidfh = NULL;
|
struct pidfh *child_pidfh = NULL;
|
||||||
sigset_t mask_orig;
|
sigset_t mask_orig;
|
||||||
@ -190,17 +190,18 @@ main(int argc, char *argv[])
|
|||||||
log_reopen = true;
|
log_reopen = true;
|
||||||
break;
|
break;
|
||||||
case 'l':
|
case 'l':
|
||||||
logparams.syslog_facility = get_log_mapping(optarg, facilitynames);
|
logparams.syslog_facility = get_log_mapping(optarg,
|
||||||
|
facilitynames);
|
||||||
if (logparams.syslog_facility == -1) {
|
if (logparams.syslog_facility == -1) {
|
||||||
errx(5, "unrecognized syslog facility");
|
errx(5, "unrecognized syslog facility");
|
||||||
}
|
}
|
||||||
logparams.syslog_enabled = true;
|
logparams.syslog_enabled = true;
|
||||||
break;
|
break;
|
||||||
case 'm':
|
case 'm':
|
||||||
stdmask = strtol(optarg, &p, 10);
|
stdmask = strtol(optarg, &p, 10);
|
||||||
if (p == optarg || stdmask < 0 || stdmask > 3) {
|
if (p == optarg || stdmask < 0 || stdmask > 3) {
|
||||||
errx(6, "unrecognized listening mask");
|
errx(6, "unrecognized listening mask");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 'o':
|
case 'o':
|
||||||
logparams.output_filename = optarg;
|
logparams.output_filename = optarg;
|
||||||
@ -218,13 +219,14 @@ main(int argc, char *argv[])
|
|||||||
restart = strtol(optarg, &p, 0);
|
restart = strtol(optarg, &p, 0);
|
||||||
if (p == optarg || restart < 1) {
|
if (p == optarg || restart < 1) {
|
||||||
errx(6, "invalid restart delay");
|
errx(6, "invalid restart delay");
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
case 's':
|
case 's':
|
||||||
logparams.syslog_priority = get_log_mapping(optarg, prioritynames);
|
logparams.syslog_priority = get_log_mapping(optarg,
|
||||||
|
prioritynames);
|
||||||
if (logparams.syslog_priority == -1) {
|
if (logparams.syslog_priority == -1) {
|
||||||
errx(4, "unrecognized syslog priority");
|
errx(4, "unrecognized syslog priority");
|
||||||
}
|
}
|
||||||
logparams.syslog_enabled = true;
|
logparams.syslog_enabled = true;
|
||||||
break;
|
break;
|
||||||
case 'S':
|
case 'S':
|
||||||
@ -240,9 +242,9 @@ main(int argc, char *argv[])
|
|||||||
case 'u':
|
case 'u':
|
||||||
user = optarg;
|
user = optarg;
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
usage(0);
|
usage(0);
|
||||||
__builtin_unreachable();
|
__builtin_unreachable();
|
||||||
default:
|
default:
|
||||||
usage(1);
|
usage(1);
|
||||||
}
|
}
|
||||||
@ -252,22 +254,23 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (argc == 0) {
|
if (argc == 0) {
|
||||||
usage(1);
|
usage(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!title) {
|
if (!title) {
|
||||||
title = argv[0];
|
title = argv[0];
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logparams.output_filename) {
|
if (logparams.output_filename) {
|
||||||
logparams.output_fd = open_log(logparams.output_filename);
|
logparams.output_fd = open_log(logparams.output_filename);
|
||||||
if (logparams.output_fd == -1) {
|
if (logparams.output_fd == -1) {
|
||||||
err(7, "open");
|
err(7, "open");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (logparams.syslog_enabled) {
|
if (logparams.syslog_enabled) {
|
||||||
openlog(logparams.syslog_tag, LOG_PID | LOG_NDELAY, logparams.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),
|
||||||
@ -359,7 +362,7 @@ main(int argc, char *argv[])
|
|||||||
restart:
|
restart:
|
||||||
if (pipe(pfd)) {
|
if (pipe(pfd)) {
|
||||||
err(1, "pipe");
|
err(1, "pipe");
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* Spawn a child to exec the command.
|
* Spawn a child to exec the command.
|
||||||
*/
|
*/
|
||||||
@ -387,7 +390,7 @@ main(int argc, char *argv[])
|
|||||||
|
|
||||||
if (user != NULL) {
|
if (user != NULL) {
|
||||||
restrict_process(user);
|
restrict_process(user);
|
||||||
}
|
}
|
||||||
/*
|
/*
|
||||||
* When forking, the child gets the original sigmask,
|
* When forking, the child gets the original sigmask,
|
||||||
* and dup'd pipes.
|
* and dup'd pipes.
|
||||||
@ -396,21 +399,21 @@ main(int argc, char *argv[])
|
|||||||
close(pfd[0]);
|
close(pfd[0]);
|
||||||
if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) {
|
if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) {
|
||||||
err(1, "sigprogmask");
|
err(1, "sigprogmask");
|
||||||
}
|
}
|
||||||
if (stdmask & STDERR_FILENO) {
|
if (stdmask & STDERR_FILENO) {
|
||||||
if (dup2(pfd[1], STDERR_FILENO) == -1) {
|
if (dup2(pfd[1], STDERR_FILENO) == -1) {
|
||||||
err(1, "dup2");
|
err(1, "dup2");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (stdmask & STDOUT_FILENO) {
|
if (stdmask & STDOUT_FILENO) {
|
||||||
if (dup2(pfd[1], STDOUT_FILENO) == -1) {
|
if (dup2(pfd[1], STDOUT_FILENO) == -1) {
|
||||||
err(1, "dup2");
|
err(1, "dup2");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pfd[1] != STDERR_FILENO &&
|
if (pfd[1] != STDERR_FILENO &&
|
||||||
pfd[1] != STDOUT_FILENO) {
|
pfd[1] != STDOUT_FILENO) {
|
||||||
close(pfd[1]);
|
close(pfd[1]);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
execvp(argv[0], argv);
|
execvp(argv[0], argv);
|
||||||
/*
|
/*
|
||||||
@ -471,7 +474,7 @@ main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
if (restart && !terminate) {
|
if (restart && !terminate) {
|
||||||
daemon_sleep(restart, 0);
|
daemon_sleep(restart, 0);
|
||||||
}
|
}
|
||||||
if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) {
|
if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) {
|
||||||
warn("sigprocmask");
|
warn("sigprocmask");
|
||||||
goto exit;
|
goto exit;
|
||||||
@ -487,7 +490,7 @@ main(int argc, char *argv[])
|
|||||||
close(pfd[1]);
|
close(pfd[1]);
|
||||||
if (logparams.syslog_enabled) {
|
if (logparams.syslog_enabled) {
|
||||||
closelog();
|
closelog();
|
||||||
}
|
}
|
||||||
pidfile_remove(child_pidfh);
|
pidfile_remove(child_pidfh);
|
||||||
pidfile_remove(parent_pidfh);
|
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. */
|
||||||
@ -501,7 +504,7 @@ daemon_sleep(time_t secs, long nsecs)
|
|||||||
while (!terminate && nanosleep(&ts, &ts) == -1) {
|
while (!terminate && nanosleep(&ts, &ts) == -1) {
|
||||||
if (errno != EINTR) {
|
if (errno != EINTR) {
|
||||||
err(1, "nanosleep");
|
err(1, "nanosleep");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -545,7 +548,7 @@ get_log_mapping(const char *str, const CODE *c)
|
|||||||
for (cp = c; cp->c_name; cp++)
|
for (cp = c; cp->c_name; cp++)
|
||||||
if (strcmp(cp->c_name, str) == 0) {
|
if (strcmp(cp->c_name, str) == 0) {
|
||||||
return cp->c_val;
|
return cp->c_val;
|
||||||
}
|
}
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -557,11 +560,11 @@ restrict_process(const char *user)
|
|||||||
pw = getpwnam(user);
|
pw = getpwnam(user);
|
||||||
if (pw == NULL) {
|
if (pw == NULL) {
|
||||||
errx(1, "unknown user: %s", user);
|
errx(1, "unknown user: %s", user);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
|
if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
|
||||||
errx(1, "failed to set user environment");
|
errx(1, "failed to set user environment");
|
||||||
}
|
}
|
||||||
|
|
||||||
setenv("USER", pw->pw_name, 1);
|
setenv("USER", pw->pw_name, 1);
|
||||||
setenv("HOME", pw->pw_dir, 1);
|
setenv("HOME", pw->pw_dir, 1);
|
||||||
@ -587,7 +590,7 @@ listen_child(int fd, struct log_params *logpar)
|
|||||||
|
|
||||||
if (do_log_reopen) {
|
if (do_log_reopen) {
|
||||||
reopen_log(logpar);
|
reopen_log(logpar);
|
||||||
}
|
}
|
||||||
rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1);
|
rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1);
|
||||||
if (rv > 0) {
|
if (rv > 0) {
|
||||||
unsigned char *cp;
|
unsigned char *cp;
|
||||||
@ -610,7 +613,7 @@ listen_child(int fd, struct log_params *logpar)
|
|||||||
/* Wait until the buffer is full. */
|
/* Wait until the buffer is full. */
|
||||||
if (bytes_read < LBUF_SIZE - 1) {
|
if (bytes_read < LBUF_SIZE - 1) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
do_output(buf, bytes_read, logpar);
|
do_output(buf, bytes_read, logpar);
|
||||||
bytes_read = 0;
|
bytes_read = 0;
|
||||||
return 1;
|
return 1;
|
||||||
@ -644,17 +647,19 @@ do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
|
|||||||
|
|
||||||
if (len < 1) {
|
if (len < 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (logpar->syslog_enabled) {
|
if (logpar->syslog_enabled) {
|
||||||
syslog(logpar->syslog_priority, "%.*s", (int)len, buf);
|
syslog(logpar->syslog_priority, "%.*s", (int)len, buf);
|
||||||
}
|
}
|
||||||
if (logpar->output_fd != -1) {
|
if (logpar->output_fd != -1) {
|
||||||
if (write(logpar->output_fd, buf, len) == -1)
|
if (write(logpar->output_fd, buf, len) == -1)
|
||||||
warn("write");
|
warn("write");
|
||||||
}
|
}
|
||||||
if (logpar->keep_fds_open && !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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
@ -666,7 +671,7 @@ handle_term(int signo)
|
|||||||
{
|
{
|
||||||
if (pid > 0 && !child_gone) {
|
if (pid > 0 && !child_gone) {
|
||||||
kill(pid, signo);
|
kill(pid, signo);
|
||||||
}
|
}
|
||||||
terminate = 1;
|
terminate = 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -709,7 +714,7 @@ reopen_log(struct log_params *logparams)
|
|||||||
outfd = open_log(logparams->output_filename);
|
outfd = open_log(logparams->output_filename);
|
||||||
if (logparams->output_fd >= 0) {
|
if (logparams->output_fd >= 0) {
|
||||||
close(logparams->output_fd);
|
close(logparams->output_fd);
|
||||||
}
|
}
|
||||||
logparams->output_fd = outfd;
|
logparams->output_fd = outfd;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user