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