daemon: use braces with 'if' consistently

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:41 -06:00 committed by Kyle Evans
parent ceb5f28ba5
commit 6b4ef4b16a

View File

@ -173,14 +173,16 @@ main(int argc, char *argv[])
break; break;
case 'l': case 'l':
logfac = get_log_mapping(optarg, facilitynames); logfac = get_log_mapping(optarg, facilitynames);
if (logfac == -1) if (logfac == -1) {
errx(5, "unrecognized syslog facility"); errx(5, "unrecognized syslog facility");
}
dosyslog = 1; dosyslog = 1;
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':
outfn = optarg; outfn = optarg;
@ -196,13 +198,15 @@ main(int argc, char *argv[])
break; break;
case 'R': case 'R':
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':
logpri = get_log_mapping(optarg, prioritynames); logpri = get_log_mapping(optarg, prioritynames);
if (logpri == -1) if (logpri == -1) {
errx(4, "unrecognized syslog priority"); errx(4, "unrecognized syslog priority");
}
dosyslog = 1; dosyslog = 1;
break; break;
case 'S': case 'S':
@ -228,20 +232,24 @@ main(int argc, char *argv[])
argc -= optind; argc -= optind;
argv += optind; argv += optind;
if (argc == 0) if (argc == 0) {
usage(1); usage(1);
}
if (!title) if (!title) {
title = argv[0]; title = argv[0];
}
if (outfn) { if (outfn) {
outfd = open_log(outfn); outfd = open_log(outfn);
if (outfd == -1) if (outfd == -1) {
err(7, "open"); err(7, "open");
}
} }
if (dosyslog) if (dosyslog) {
openlog(logtag, LOG_PID | LOG_NDELAY, logfac); openlog(logtag, LOG_PID | LOG_NDELAY, logfac);
}
ppfh = pfh = NULL; ppfh = pfh = NULL;
/* /*
@ -329,8 +337,9 @@ main(int argc, char *argv[])
goto exit; goto exit;
} }
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.
*/ */
@ -356,27 +365,32 @@ main(int argc, char *argv[])
/* 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(pfh);
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.
*/ */
if (pid == 0) { if (pid == 0) {
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);
/* /*
@ -436,8 +450,9 @@ 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;
@ -451,8 +466,9 @@ main(int argc, char *argv[])
close(outfd); close(outfd);
close(pfd[0]); close(pfd[0]);
close(pfd[1]); close(pfd[1]);
if (dosyslog) if (dosyslog) {
closelog(); closelog();
}
pidfile_remove(pfh); pidfile_remove(pfh);
pidfile_remove(ppfh); pidfile_remove(ppfh);
exit(1); /* If daemon(3) succeeded exit status does not matter. */ exit(1); /* If daemon(3) succeeded exit status does not matter. */
@ -464,8 +480,9 @@ daemon_sleep(time_t secs, long nsecs)
struct timespec ts = { secs, nsecs }; struct timespec ts = { secs, 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");
}
} }
} }
@ -507,8 +524,9 @@ get_log_mapping(const char *str, const CODE *c)
{ {
const CODE *cp; const CODE *cp;
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;
} }
@ -518,11 +536,13 @@ restrict_process(const char *user)
struct passwd *pw = NULL; struct passwd *pw = NULL;
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);
@ -546,8 +566,9 @@ listen_child(int fd, struct log_params *logpar)
assert(logpar); assert(logpar);
assert(bytes_read < LBUF_SIZE - 1); assert(bytes_read < LBUF_SIZE - 1);
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;
@ -568,8 +589,9 @@ listen_child(int fd, struct log_params *logpar)
memmove(buf, cp + 1, bytes_read); memmove(buf, cp + 1, bytes_read);
} }
/* 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;
@ -601,16 +623,19 @@ do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
assert(len <= LBUF_SIZE); assert(len <= LBUF_SIZE);
assert(logpar); assert(logpar);
if (len < 1) if (len < 1) {
return; return;
if (logpar->dosyslog) }
if (logpar->dosyslog) {
syslog(logpar->logpri, "%.*s", (int)len, buf); syslog(logpar->logpri, "%.*s", (int)len, buf);
}
if (logpar->outfd != -1) { if (logpar->outfd != -1) {
if (write(logpar->outfd, buf, len) == -1) if (write(logpar->outfd, buf, len) == -1)
warn("write"); warn("write");
} }
if (logpar->noclose && !logpar->dosyslog && logpar->outfd == -1) if (logpar->noclose && !logpar->dosyslog && logpar->outfd == -1) {
printf("%.*s", (int)len, buf); printf("%.*s", (int)len, buf);
}
} }
/* /*
@ -620,8 +645,9 @@ do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
static void static void
handle_term(int signo) handle_term(int signo)
{ {
if (pid > 0 && !child_gone) if (pid > 0 && !child_gone) {
kill(pid, signo); kill(pid, signo);
}
terminate = 1; terminate = 1;
} }
@ -662,8 +688,9 @@ reopen_log(struct log_params *lpp)
do_log_reopen = 0; do_log_reopen = 0;
outfd = open_log(lpp->outfn); outfd = open_log(lpp->outfn);
if (lpp->outfd >= 0) if (lpp->outfd >= 0) {
close(lpp->outfd); close(lpp->outfd);
}
lpp->outfd = outfd; lpp->outfd = outfd;
} }