]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - usr.sbin/daemon/daemon.c
daemon: style changes
[FreeBSD/FreeBSD.git] / usr.sbin / daemon / daemon.c
1 /*-
2  * SPDX-License-Identifier: BSD-3-Clause
3  *
4  * Copyright (c) 1999 Berkeley Software Design, Inc. All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions
8  * are met:
9  * 1. Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  * 2. Redistributions in binary form must reproduce the above copyright
12  *    notice, this list of conditions and the following disclaimer in the
13  *    documentation and/or other materials provided with the distribution.
14  * 3. Berkeley Software Design Inc's name may not be used to endorse or
15  *    promote products derived from this software without specific prior
16  *    written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY BERKELEY SOFTWARE DESIGN INC ``AS IS'' AND
19  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED.  IN NO EVENT SHALL BERKELEY SOFTWARE DESIGN INC BE LIABLE
22  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28  * SUCH DAMAGE.
29  *
30  *      From BSDI: daemon.c,v 1.2 1996/08/15 01:11:09 jch Exp
31  */
32
33 #include <sys/cdefs.h>
34 __FBSDID("$FreeBSD$");
35
36 #include <sys/param.h>
37 #include <sys/mman.h>
38 #include <sys/wait.h>
39
40 #include <fcntl.h>
41 #include <err.h>
42 #include <errno.h>
43 #include <getopt.h>
44 #include <libutil.h>
45 #include <login_cap.h>
46 #include <paths.h>
47 #include <pwd.h>
48 #include <signal.h>
49 #include <stdio.h>
50 #include <stdbool.h>
51 #include <stdlib.h>
52 #include <unistd.h>
53 #include <string.h>
54 #include <strings.h>
55 #define SYSLOG_NAMES
56 #include <syslog.h>
57 #include <time.h>
58 #include <assert.h>
59
60 #define LBUF_SIZE 4096
61
62 struct log_params {
63         const char *output_filename;
64         const char *syslog_tag;
65         int syslog_priority;
66         int syslog_facility;
67         int keep_fds_open;
68         int output_fd;
69         bool syslog_enabled;
70 };
71
72 static void restrict_process(const char *);
73 static void handle_term(int);
74 static void handle_chld(int);
75 static void handle_hup(int);
76 static int open_log(const char *);
77 static void reopen_log(struct log_params *);
78 static int  listen_child(int, struct log_params *);
79 static int  get_log_mapping(const char *, const CODE *);
80 static void open_pid_files(const char *, const char *, struct pidfh **,
81                            struct pidfh **);
82 static void do_output(const unsigned char *, size_t, struct log_params *);
83 static void daemon_sleep(time_t, long);
84
85 static volatile sig_atomic_t terminate = 0;
86 static volatile sig_atomic_t child_gone = 0;
87 static volatile sig_atomic_t pid = -1;
88 static volatile sig_atomic_t do_log_reopen = 0;
89
90 static const char shortopts[] = "+cfHSp:P:ru:o:s:l:t:m:R:T:h";
91
92 static const struct option longopts[] = {
93         { "change-dir",         no_argument,            NULL,           'c' },
94         { "close-fds",          no_argument,            NULL,           'f' },
95         { "sighup",             no_argument,            NULL,           'H' },
96         { "syslog",             no_argument,            NULL,           'S' },
97         { "output-file",        required_argument,      NULL,           'o' },
98         { "output-mask",        required_argument,      NULL,           'm' },
99         { "child-pidfile",      required_argument,      NULL,           'p' },
100         { "supervisor-pidfile", required_argument,      NULL,           'P' },
101         { "restart",            no_argument,            NULL,           'r' },
102         { "restart-delay",      required_argument,      NULL,           'R' },
103         { "title",              required_argument,      NULL,           't' },
104         { "user",               required_argument,      NULL,           'u' },
105         { "syslog-priority",    required_argument,      NULL,           's' },
106         { "syslog-facility",    required_argument,      NULL,           'l' },
107         { "syslog-tag",         required_argument,      NULL,           'T' },
108         { "help",               no_argument,            NULL,           'h' },
109         { NULL,                 0,                      NULL,            0  }
110 };
111
112 static _Noreturn void
113 usage(int exitcode)
114 {
115         (void)fprintf(stderr,
116             "usage: daemon [-cfHrS] [-p child_pidfile] [-P supervisor_pidfile]\n"
117             "              [-u user] [-o output_file] [-t title]\n"
118             "              [-l syslog_facility] [-s syslog_priority]\n"
119             "              [-T syslog_tag] [-m output_mask] [-R restart_delay_secs]\n"
120             "command arguments ...\n");
121
122         (void)fprintf(stderr,
123             "  --change-dir         -c         Change the current working directory to root\n"
124             "  --close-fds          -f         Set stdin, stdout, stderr to /dev/null\n"
125             "  --sighup             -H         Close and re-open output file on SIGHUP\n"
126             "  --syslog             -S         Send output to syslog\n"
127             "  --output-file        -o <file>  Append output of the child process to file\n"
128             "  --output-mask        -m <mask>  What to send to syslog/file\n"
129             "                                  1=stdout, 2=stderr, 3=both\n"
130             "  --child-pidfile      -p <file>  Write PID of the child process to file\n"
131             "  --supervisor-pidfile -P <file>  Write PID of the supervisor process to file\n"
132             "  --restart            -r         Restart child if it terminates (1 sec delay)\n"
133             "  --restart-delay      -R <N>     Restart child if it terminates after N sec\n"
134             "  --title              -t <title> Set the title of the supervisor process\n"
135             "  --user               -u <user>  Drop privileges, run as given user\n"
136             "  --syslog-priority    -s <prio>  Set syslog priority\n"
137             "  --syslog-facility    -l <flty>  Set syslog facility\n"
138             "  --syslog-tag         -T <tag>   Set syslog tag\n"
139             "  --help               -h         Show this help\n");
140
141         exit(exitcode);
142 }
143
144 int
145 main(int argc, char *argv[])
146 {
147         bool supervision_enabled = false;
148         bool log_reopen = false;
149         char *p = NULL;
150         const char *child_pidfile = NULL;
151         const char *parent_pidfile = NULL;
152         const char *title = NULL;
153         const char *user = NULL;
154         int ch = 0;
155         int child_eof = 0;
156         int keep_cur_workdir = 1;
157         int pfd[2] = { -1, -1 };
158         int restart = 0;
159         int stdmask = STDOUT_FILENO | STDERR_FILENO;
160         struct log_params logparams = {
161                 .syslog_enabled = false,
162                 .syslog_priority = LOG_NOTICE,
163                 .syslog_tag = "daemon",
164                 .syslog_facility = LOG_DAEMON,
165                 .keep_fds_open = 1,
166                 .output_fd = -1,
167                 .output_filename = NULL
168         };
169         struct pidfh *parent_pidfh = NULL;
170         struct pidfh *child_pidfh = NULL;
171         sigset_t mask_orig;
172         sigset_t mask_read;
173         sigset_t mask_term;
174         sigset_t mask_susp;
175
176         sigemptyset(&mask_susp);
177         sigemptyset(&mask_read);
178         sigemptyset(&mask_term);
179         sigemptyset(&mask_orig);
180
181         while ((ch = getopt_long(argc, argv, shortopts, longopts, NULL)) != -1) {
182                 switch (ch) {
183                 case 'c':
184                         keep_cur_workdir = 0;
185                         break;
186                 case 'f':
187                         logparams.keep_fds_open = 0;
188                         break;
189                 case 'H':
190                         log_reopen = true;
191                         break;
192                 case 'l':
193                         logparams.syslog_facility = get_log_mapping(optarg,
194                             facilitynames);
195                         if (logparams.syslog_facility == -1) {
196                                 errx(5, "unrecognized syslog facility");
197                         }
198                         logparams.syslog_enabled = true;
199                         break;
200                 case 'm':
201                         stdmask = strtol(optarg, &p, 10);
202                         if (p == optarg || stdmask < 0 || stdmask > 3) {
203                                 errx(6, "unrecognized listening mask");
204                         }
205                         break;
206                 case 'o':
207                         logparams.output_filename = optarg;
208                         break;
209                 case 'p':
210                         child_pidfile = optarg;
211                         break;
212                 case 'P':
213                         parent_pidfile = optarg;
214                         break;
215                 case 'r':
216                         restart = 1;
217                         break;
218                 case 'R':
219                         restart = strtol(optarg, &p, 0);
220                         if (p == optarg || restart < 1) {
221                                 errx(6, "invalid restart delay");
222                         }
223                         break;
224                 case 's':
225                         logparams.syslog_priority = get_log_mapping(optarg,
226                             prioritynames);
227                         if (logparams.syslog_priority == -1) {
228                                 errx(4, "unrecognized syslog priority");
229                         }
230                         logparams.syslog_enabled = true;
231                         break;
232                 case 'S':
233                         logparams.syslog_enabled = true;
234                         break;
235                 case 't':
236                         title = optarg;
237                         break;
238                 case 'T':
239                         logparams.syslog_tag = optarg;
240                         logparams.syslog_enabled = true;
241                         break;
242                 case 'u':
243                         user = optarg;
244                         break;
245                 case 'h':
246                         usage(0);
247                         __builtin_unreachable();
248                 default:
249                         usage(1);
250                 }
251         }
252         argc -= optind;
253         argv += optind;
254
255         if (argc == 0) {
256                 usage(1);
257         }
258
259         if (!title) {
260                 title = argv[0];
261         }
262
263         if (logparams.output_filename) {
264                 logparams.output_fd = open_log(logparams.output_filename);
265                 if (logparams.output_fd == -1) {
266                         err(7, "open");
267                 }
268         }
269
270         if (logparams.syslog_enabled) {
271                 openlog(logparams.syslog_tag, LOG_PID | LOG_NDELAY,
272                     logparams.syslog_facility);
273         }
274
275         /*
276          * Try to open the pidfile before calling daemon(3),
277          * to be able to report the error intelligently
278          */
279         open_pid_files(child_pidfile, parent_pidfile, &child_pidfh, &parent_pidfh);
280         if (daemon(keep_cur_workdir, logparams.keep_fds_open) == -1) {
281                 warn("daemon");
282                 goto exit;
283         }
284         /* Write out parent pidfile if needed. */
285         pidfile_write(parent_pidfh);
286
287         /*
288          * Supervision mode is enabled if one of the following options are used:
289          * --child-pidfile -p
290          * --supervisor-pidfile -P
291          * --restart -r / --restart-delay -R
292          * --syslog -S
293          * --syslog-facility -l
294          * --syslog-priority -s
295          * --syslog-tag -T
296          *
297          * In supervision mode daemon executes the command in a forked process
298          * and observes the child by waiting for SIGCHILD. In supervision mode
299          * daemon must never exit before the child, this is necessary  to prevent
300          * orphaning the child and leaving a stale pid file.
301          * To achieve this daemon catches SIGTERM and
302          * forwards it to the child, expecting to get SIGCHLD eventually.
303          */
304         supervision_enabled = child_pidfile != NULL ||
305                 parent_pidfile != NULL ||
306                 restart != 0 ||
307                 logparams.output_fd != -1   ||
308                 logparams.syslog_enabled == true;
309
310         if (supervision_enabled) {
311                 struct sigaction act_term = { 0 };
312                 struct sigaction act_chld = { 0 };
313                 struct sigaction act_hup = { 0 };
314
315                 /* Avoid PID racing with SIGCHLD and SIGTERM. */
316                 act_term.sa_handler = handle_term;
317                 sigemptyset(&act_term.sa_mask);
318                 sigaddset(&act_term.sa_mask, SIGCHLD);
319
320                 act_chld.sa_handler = handle_chld;
321                 sigemptyset(&act_chld.sa_mask);
322                 sigaddset(&act_chld.sa_mask, SIGTERM);
323
324                 act_hup.sa_handler = handle_hup;
325                 sigemptyset(&act_hup.sa_mask);
326
327                 /* Block these when avoiding racing before sigsuspend(). */
328                 sigaddset(&mask_susp, SIGTERM);
329                 sigaddset(&mask_susp, SIGCHLD);
330                 /* Block SIGTERM when we lack a valid child PID. */
331                 sigaddset(&mask_term, SIGTERM);
332                 /*
333                  * When reading, we wish to avoid SIGCHLD. SIGTERM
334                  * has to be caught, otherwise we'll be stuck until
335                  * the read() returns - if it returns.
336                  */
337                 sigaddset(&mask_read, SIGCHLD);
338                 /* Block SIGTERM to avoid racing until we have forked. */
339                 if (sigprocmask(SIG_BLOCK, &mask_term, &mask_orig)) {
340                         warn("sigprocmask");
341                         goto exit;
342                 }
343                 if (sigaction(SIGTERM, &act_term, NULL) == -1) {
344                         warn("sigaction");
345                         goto exit;
346                 }
347                 if (sigaction(SIGCHLD, &act_chld, NULL) == -1) {
348                         warn("sigaction");
349                         goto exit;
350                 }
351                 /*
352                  * Try to protect against pageout kill. Ignore the
353                  * error, madvise(2) will fail only if a process does
354                  * not have superuser privileges.
355                  */
356                 (void)madvise(NULL, 0, MADV_PROTECT);
357                 if (log_reopen && logparams.output_fd >= 0 &&
358                     sigaction(SIGHUP, &act_hup, NULL) == -1) {
359                         warn("sigaction");
360                         goto exit;
361                 }
362 restart:
363                 if (pipe(pfd)) {
364                         err(1, "pipe");
365                 }
366                 /*
367                  * Spawn a child to exec the command.
368                  */
369                 child_gone = 0;
370                 pid = fork();
371                 if (pid == -1) {
372                         warn("fork");
373                         goto exit;
374                 } else if (pid > 0) {
375                         /*
376                          * Unblock SIGTERM after we know we have a valid
377                          * child PID to signal.
378                          */
379                         if (sigprocmask(SIG_UNBLOCK, &mask_term, NULL)) {
380                                 warn("sigprocmask");
381                                 goto exit;
382                         }
383                         close(pfd[1]);
384                         pfd[1] = -1;
385                 }
386         }
387         if (pid <= 0) {
388                 /* Now that we are the child, write out the pid. */
389                 pidfile_write(child_pidfh);
390
391                 if (user != NULL) {
392                         restrict_process(user);
393                 }
394                 /*
395                  * When forking, the child gets the original sigmask,
396                  * and dup'd pipes.
397                  */
398                 if (pid == 0) {
399                         close(pfd[0]);
400                         if (sigprocmask(SIG_SETMASK, &mask_orig, NULL)) {
401                                 err(1, "sigprogmask");
402                         }
403                         if (stdmask & STDERR_FILENO) {
404                                 if (dup2(pfd[1], STDERR_FILENO) == -1) {
405                                         err(1, "dup2");
406                                 }
407                         }
408                         if (stdmask & STDOUT_FILENO) {
409                                 if (dup2(pfd[1], STDOUT_FILENO) == -1) {
410                                         err(1, "dup2");
411                                 }
412                         }
413                         if (pfd[1] != STDERR_FILENO &&
414                             pfd[1] != STDOUT_FILENO) {
415                                 close(pfd[1]);
416                         }
417                 }
418                 execvp(argv[0], argv);
419                 /*
420                  * execvp() failed -- report the error. The child is
421                  * now running, so the exit status doesn't matter.
422                  */
423                 err(1, "%s", argv[0]);
424         }
425         setproctitle("%s[%d]", title, (int)pid);
426         /*
427          * As we have closed the write end of pipe for parent process,
428          * we might detect the child's exit by reading EOF. The child
429          * might have closed its stdout and stderr, so we must wait for
430          * the SIGCHLD to ensure that the process is actually gone.
431          */
432         for (;;) {
433                 /*
434                  * We block SIGCHLD when listening, but SIGTERM we accept
435                  * so the read() won't block if we wish to depart.
436                  *
437                  * Upon receiving SIGTERM, we have several options after
438                  * sending the SIGTERM to our child:
439                  * - read until EOF
440                  * - read until EOF but only for a while
441                  * - bail immediately
442                  *
443                  * We go for the third, as otherwise we have no guarantee
444                  * that we won't block indefinitely if the child refuses
445                  * to depart. To handle the second option, a different
446                  * approach would be needed (procctl()?)
447                  */
448                 if (child_gone && child_eof) {
449                         break;
450                 } else if (terminate) {
451                         goto exit;
452                 } else if (!child_eof) {
453                         if (sigprocmask(SIG_BLOCK, &mask_read, NULL)) {
454                                 warn("sigprocmask");
455                                 goto exit;
456                         }
457                         child_eof = !listen_child(pfd[0], &logparams);
458                         if (sigprocmask(SIG_UNBLOCK, &mask_read, NULL)) {
459                                 warn("sigprocmask");
460                                 goto exit;
461                         }
462                 } else {
463                         if (sigprocmask(SIG_BLOCK, &mask_susp, NULL)) {
464                                 warn("sigprocmask");
465                                 goto exit;
466                         }
467                         while (!terminate && !child_gone)
468                                 sigsuspend(&mask_orig);
469                         if (sigprocmask(SIG_UNBLOCK, &mask_susp, NULL)) {
470                                 warn("sigprocmask");
471                                 goto exit;
472                         }
473                 }
474         }
475         if (restart && !terminate) {
476                 daemon_sleep(restart, 0);
477         }
478         if (sigprocmask(SIG_BLOCK, &mask_term, NULL)) {
479                 warn("sigprocmask");
480                 goto exit;
481         }
482         if (restart && !terminate) {
483                 close(pfd[0]);
484                 pfd[0] = -1;
485                 goto restart;
486         }
487 exit:
488         close(logparams.output_fd);
489         close(pfd[0]);
490         close(pfd[1]);
491         if (logparams.syslog_enabled) {
492                 closelog();
493         }
494         pidfile_remove(child_pidfh);
495         pidfile_remove(parent_pidfh);
496         exit(1); /* If daemon(3) succeeded exit status does not matter. */
497 }
498
499 static void
500 daemon_sleep(time_t secs, long nsecs)
501 {
502         struct timespec ts = { secs, nsecs };
503
504         while (!terminate && nanosleep(&ts, &ts) == -1) {
505                 if (errno != EINTR) {
506                         err(1, "nanosleep");
507                 }
508         }
509 }
510
511 static void
512 open_pid_files(const char *pidfile, const char *ppidfile,
513                struct pidfh **pfh, struct pidfh **ppfh)
514 {
515         pid_t fpid;
516         int serrno;
517
518         if (pidfile) {
519                 *pfh = pidfile_open(pidfile, 0600, &fpid);
520                 if (*pfh == NULL) {
521                         if (errno == EEXIST) {
522                                 errx(3, "process already running, pid: %d",
523                                     fpid);
524                         }
525                         err(2, "pidfile ``%s''", pidfile);
526                 }
527         }
528         /* Do the same for the actual daemon process. */
529         if (ppidfile) {
530                 *ppfh = pidfile_open(ppidfile, 0600, &fpid);
531                 if (*ppfh == NULL) {
532                         serrno = errno;
533                         pidfile_remove(*pfh);
534                         errno = serrno;
535                         if (errno == EEXIST) {
536                                 errx(3, "process already running, pid: %d",
537                                      fpid);
538                         }
539                         err(2, "ppidfile ``%s''", ppidfile);
540                 }
541         }
542 }
543
544 static int
545 get_log_mapping(const char *str, const CODE *c)
546 {
547         const CODE *cp;
548         for (cp = c; cp->c_name; cp++)
549                 if (strcmp(cp->c_name, str) == 0) {
550                         return cp->c_val;
551                 }
552         return -1;
553 }
554
555 static void
556 restrict_process(const char *user)
557 {
558         struct passwd *pw = NULL;
559
560         pw = getpwnam(user);
561         if (pw == NULL) {
562                 errx(1, "unknown user: %s", user);
563         }
564
565         if (setusercontext(NULL, pw, pw->pw_uid, LOGIN_SETALL) != 0) {
566                 errx(1, "failed to set user environment");
567         }
568
569         setenv("USER", pw->pw_name, 1);
570         setenv("HOME", pw->pw_dir, 1);
571         setenv("SHELL", *pw->pw_shell ? pw->pw_shell : _PATH_BSHELL, 1);
572 }
573
574 /*
575  * We try to collect whole lines terminated by '\n'. Otherwise we collect a
576  * full buffer, and then output it.
577  *
578  * Return value of 0 is assumed to mean EOF or error, and 1 indicates to
579  * continue reading.
580  */
581 static int
582 listen_child(int fd, struct log_params *logpar)
583 {
584         static unsigned char buf[LBUF_SIZE];
585         static size_t bytes_read = 0;
586         int rv;
587
588         assert(logpar);
589         assert(bytes_read < LBUF_SIZE - 1);
590
591         if (do_log_reopen) {
592                 reopen_log(logpar);
593         }
594         rv = read(fd, buf + bytes_read, LBUF_SIZE - bytes_read - 1);
595         if (rv > 0) {
596                 unsigned char *cp;
597
598                 bytes_read += rv;
599                 assert(bytes_read <= LBUF_SIZE - 1);
600                 /* Always NUL-terminate just in case. */
601                 buf[LBUF_SIZE - 1] = '\0';
602                 /*
603                  * Chomp line by line until we run out of buffer.
604                  * This does not take NUL characters into account.
605                  */
606                 while ((cp = memchr(buf, '\n', bytes_read)) != NULL) {
607                         size_t bytes_line = cp - buf + 1;
608                         assert(bytes_line <= bytes_read);
609                         do_output(buf, bytes_line, logpar);
610                         bytes_read -= bytes_line;
611                         memmove(buf, cp + 1, bytes_read);
612                 }
613                 /* Wait until the buffer is full. */
614                 if (bytes_read < LBUF_SIZE - 1) {
615                         return 1;
616                 }
617                 do_output(buf, bytes_read, logpar);
618                 bytes_read = 0;
619                 return 1;
620         } else if (rv == -1) {
621                 /* EINTR should trigger another read. */
622                 if (errno == EINTR) {
623                         return 1;
624                 } else {
625                         warn("read");
626                         return 0;
627                 }
628         }
629         /* Upon EOF, we have to flush what's left of the buffer. */
630         if (bytes_read > 0) {
631                 do_output(buf, bytes_read, logpar);
632                 bytes_read = 0;
633         }
634         return 0;
635 }
636
637 /*
638  * The default behavior is to stay silent if the user wants to redirect
639  * output to a file and/or syslog. If neither are provided, then we bounce
640  * everything back to parent's stdout.
641  */
642 static void
643 do_output(const unsigned char *buf, size_t len, struct log_params *logpar)
644 {
645         assert(len <= LBUF_SIZE);
646         assert(logpar);
647
648         if (len < 1) {
649                 return;
650         }
651         if (logpar->syslog_enabled) {
652                 syslog(logpar->syslog_priority, "%.*s", (int)len, buf);
653         }
654         if (logpar->output_fd != -1) {
655                 if (write(logpar->output_fd, buf, len) == -1)
656                         warn("write");
657         }
658         if (logpar->keep_fds_open &&
659             !logpar->syslog_enabled &&
660             logpar->output_fd == -1) {
661                 printf("%.*s", (int)len, buf);
662         }
663 }
664
665 /*
666  * We use the global PID acquired directly from fork. If there is no valid
667  * child pid, the handler should be blocked and/or child_gone == 1.
668  */
669 static void
670 handle_term(int signo)
671 {
672         if (pid > 0 && !child_gone) {
673                 kill(pid, signo);
674         }
675         terminate = 1;
676 }
677
678 static void
679 handle_chld(int signo __unused)
680 {
681
682         for (;;) {
683                 int rv = waitpid(-1, NULL, WNOHANG);
684                 if (pid == rv) {
685                         child_gone = 1;
686                         break;
687                 } else if (rv == -1 && errno != EINTR) {
688                         warn("waitpid");
689                         return;
690                 }
691         }
692 }
693
694 static void
695 handle_hup(int signo __unused)
696 {
697
698         do_log_reopen = 1;
699 }
700
701 static int
702 open_log(const char *outfn)
703 {
704
705         return open(outfn, O_CREAT | O_WRONLY | O_APPEND | O_CLOEXEC, 0600);
706 }
707
708 static void
709 reopen_log(struct log_params *logparams)
710 {
711         int outfd;
712
713         do_log_reopen = 0;
714         outfd = open_log(logparams->output_filename);
715         if (logparams->output_fd >= 0) {
716                 close(logparams->output_fd);
717         }
718         logparams->output_fd = outfd;
719 }
720