]> git.ipfire.org Git - thirdparty/openssh-portable.git/commitdiff
upstream commit
authorkrw@openbsd.org <krw@openbsd.org>
Sat, 2 Apr 2016 14:37:42 +0000 (14:37 +0000)
committerDarren Tucker <dtucker@zip.com.au>
Fri, 8 Apr 2016 04:12:18 +0000 (14:12 +1000)
Another use for fcntl() and thus of the superfluous 3rd
 parameter is when sanitising standard fd's before calling daemon().

Use a tweaked version of the ssh(1) function in all three places
found using fcntl() this way.

ok jca@ beck@

Upstream-ID: f16811ffa19a1c5f4ef383c5f0fecb843c84e218

misc.c

diff --git a/misc.c b/misc.c
index f20259777e2e2dff9b30aeca9a3460b429a48e73..db5ff564e3c60bcba58fd362bb6ce4e2df58e0d8 100644 (file)
--- a/misc.c
+++ b/misc.c
@@ -1,4 +1,4 @@
-/* $OpenBSD: misc.c,v 1.102 2016/03/02 22:42:40 dtucker Exp $ */
+/* $OpenBSD: misc.c,v 1.103 2016/04/02 14:37:42 krw Exp $ */
 /*
  * Copyright (c) 2000 Markus Friedl.  All rights reserved.
  * Copyright (c) 2005,2006 Damien Miller.  All rights reserved.
@@ -84,9 +84,9 @@ set_nonblock(int fd)
 {
        int val;
 
-       val = fcntl(fd, F_GETFL, 0);
+       val = fcntl(fd, F_GETFL);
        if (val < 0) {
-               error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
+               error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
                return (-1);
        }
        if (val & O_NONBLOCK) {
@@ -108,9 +108,9 @@ unset_nonblock(int fd)
 {
        int val;
 
-       val = fcntl(fd, F_GETFL, 0);
+       val = fcntl(fd, F_GETFL);
        if (val < 0) {
-               error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno));
+               error("fcntl(%d, F_GETFL): %s", fd, strerror(errno));
                return (-1);
        }
        if (!(val & O_NONBLOCK)) {
@@ -729,16 +729,16 @@ sanitise_stdfd(void)
                    strerror(errno));
                exit(1);
        }
-       while (++dupfd <= 2) {
-               /* Only clobber closed fds */
-               if (fcntl(dupfd, F_GETFL, 0) >= 0)
-                       continue;
-               if (dup2(nullfd, dupfd) == -1) {
-                       fprintf(stderr, "dup2: %s\n", strerror(errno));
-                       exit(1);
+       while (++dupfd <= STDERR_FILENO) {
+               /* Only populate closed fds. */
+               if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) {
+                       if (dup2(nullfd, dupfd) == -1) {
+                               fprintf(stderr, "dup2: %s\n", strerror(errno));
+                               exit(1);
+                       }
                }
        }
-       if (nullfd > 2)
+       if (nullfd > STDERR_FILENO)
                close(nullfd);
 }