dhclient already uses the same pattern as rdaemon(), let's sync the
implementation.  No functional change intended, the extra safety check
for devnull > 2 is a bonus.


Index: dhclient.c
===================================================================
RCS file: /cvs/src/sbin/dhclient/dhclient.c,v
retrieving revision 1.387
diff -u -p -p -u -r1.387 dhclient.c
--- dhclient.c  4 Sep 2016 11:21:24 -0000       1.387
+++ dhclient.c  4 Sep 2016 13:12:00 -0000
@@ -167,6 +167,7 @@ char *lease_as_string(struct interface_i
 struct client_lease *packet_to_lease(struct interface_info *, struct in_addr,
     struct option_data *);
 void go_daemon(void);
+int rdaemon(int);
 
 #define        ROUNDUP(a) \
            ((a) > 0 ? (1 + (((a) - 1) | (sizeof(long) - 1))) : sizeof(long))
@@ -2057,17 +2058,8 @@ go_daemon(void)
        /* Stop logging to stderr. */
        log_perror = 0;
 
-       if (daemon(1, 0) == -1)
-               error("daemon");
-
-       /* we are chrooted, daemon(3) fails to open /dev/null */
-       if (nullfd != -1) {
-               dup2(nullfd, STDIN_FILENO);
-               dup2(nullfd, STDOUT_FILENO);
-               dup2(nullfd, STDERR_FILENO);
-               close(nullfd);
-               nullfd = -1;
-       }
+       if (rdaemon(nullfd) == -1)
+               error("rdaemon");
 
        /* Catch stuff that might be trying to terminate the program. */
        signal(SIGHUP, sighdlr);
@@ -2077,6 +2069,31 @@ go_daemon(void)
        signal(SIGUSR2, sighdlr);
 
        signal(SIGPIPE, SIG_IGN);
+}
+
+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);
 }
 
 int

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

Reply via email to