rdaemon allows us to go in the background, saving stderr error messages
and exit status for a longer interval in the startup step.

Thoughts?


Index: rtadvd.c
===================================================================
RCS file: /cvs/src/usr.sbin/rtadvd/rtadvd.c,v
retrieving revision 1.78
diff -u -p -p -u -r1.78 rtadvd.c
--- rtadvd.c    3 Sep 2016 16:57:29 -0000       1.78
+++ rtadvd.c    3 Sep 2016 22:35:18 -0000
@@ -55,6 +55,8 @@
 #include <string.h>
 #include <pwd.h>
 #include <signal.h>
+#include <fcntl.h>
+#include <paths.h>
 
 #include "rtadvd.h"
 #include "advcap.h"
@@ -139,6 +141,7 @@ static int nd6_options(struct nd_opt_hdr
 static void free_ndopts(union nd_opts *);
 static void ra_output(struct rainfo *);
 static struct rainfo *if_indextorainfo(int);
+static int rdaemon(int);
 
 static void dump_cb(int, short, void *);
 static void die_cb(int, short, void *);
@@ -151,6 +154,7 @@ main(int argc, char *argv[])
 {
        struct passwd *pw;
        int ch;
+       int devnull = -1;
        struct event ev_sock;
        struct event ev_rtsock;
        struct event ev_sigterm;
@@ -182,6 +186,12 @@ main(int argc, char *argv[])
        if (argc == 0)
                usage();
 
+       if (!dflag) {
+               devnull = open(_PATH_DEVNULL, O_RDWR, 0);
+               if (devnull == -1)
+                       fatal("open(\"" _PATH_DEVNULL "\")");
+       }
+
        SLIST_INIT(&ralist);
 
        /* get iflist block from kernel */
@@ -196,12 +206,6 @@ main(int argc, char *argv[])
        if (inet_pton(AF_INET6, ALLNODES, &sin6_allnodes.sin6_addr) != 1)
                fatal("inet_pton failed");
 
-       if (conffile != NULL)
-               log_init(dflag);
-
-       if (!dflag)
-               daemon(1, 0);
-
        sock_open();
 
        if (sflag == 0)
@@ -218,6 +222,14 @@ main(int argc, char *argv[])
            setresuid(pw->pw_uid, pw->pw_uid, pw->pw_uid))
                fatal("cannot drop privileges");
 
+       if (!dflag) {
+               if (rdaemon(devnull) == -1)
+                       fatal("rdaemon");
+       }
+
+       if (conffile != NULL)
+               log_init(dflag);
+
        if (pledge("stdio inet route", NULL) == -1)
                err(1, "pledge");
 
@@ -1296,4 +1308,29 @@ ra_timer_update(struct rainfo *rai)
 
        log_debug("RA timer on %s set to %lld.%lds", rai->ifname,
            (long long)tm->tv_sec, tm->tv_usec);
+}
+
+int
+rdaemon(int devnull)
+{
+
+       switch (fork()) {
+       case -1:
+               return (-1);
+       case 0:
+               break;
+       default:
+               _exit(0);
+       }
+
+       if (setsid() == -1)
+               return (-1);
+
+       (void)dup2(devnull, STDIN_FILENO);
+       (void)dup2(devnull, STDOUT_FILENO);
+       (void)dup2(devnull, STDERR_FILENO);
+       if (devnull > 2)
+               (void)close(devnull);
+
+       return (0);
 }

-- 
jca | PGP : 0x1524E7EE / 5135 92C1 AD36 5293 2BDF  DDCC 0DFA 74AE 1524 E7EE

Reply via email to