start-stop-daemon: do try to close fds > 2
authorDenys Vlasenko <vda.linux@googlemail.com>
Mon, 29 Apr 2019 12:24:07 +0000 (14:24 +0200)
committerDenys Vlasenko <vda.linux@googlemail.com>
Mon, 29 Apr 2019 12:24:07 +0000 (14:24 +0200)
sh -c 'exec 3>&1; exec start-stop-daemon -S -b -x /bin/sleep -- 123'

now closes fd 3.

function                                             old     new   delta
bb_daemonize_or_rexec                                183     192      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/0 up/down: 9/0)                 Total: 9 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
libbb/vfork_daemon_rexec.c

index 1aac27b360adc19ea23e57f695b7c558149cad8e..65271e84f1fbc0158ffa77f28eee566181188f04 100644 (file)
@@ -263,12 +263,6 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
        if (flags & DAEMON_CHDIR_ROOT)
                xchdir("/");
 
-       if (flags & DAEMON_DEVNULL_STDIO) {
-               close(0);
-               close(1);
-               close(2);
-       }
-
        fd = open(bb_dev_null, O_RDWR);
        if (fd < 0) {
                /* NB: we can be called as bb_sanitize_stdio() from init
@@ -278,8 +272,15 @@ void FAST_FUNC bb_daemonize_or_rexec(int flags, char **argv)
                fd = xopen("/", O_RDONLY); /* don't believe this can fail */
        }
 
-       while ((unsigned)fd < 2)
-               fd = dup(fd); /* have 0,1,2 open at least to /dev/null */
+       if (flags & DAEMON_DEVNULL_STDIO) {
+               xdup2(fd, 0);
+               xdup2(fd, 1);
+               xdup2(fd, 2);
+       } else {
+               /* have 0,1,2 open at least to /dev/null */
+               while ((unsigned)fd < 2)
+                       fd = dup(fd);
+       }
 
        if (!(flags & DAEMON_ONLY_SANITIZE)) {