=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/ssh/misc.c,v retrieving revision 1.102 retrieving revision 1.103 diff -u -r1.102 -r1.103 --- src/usr.bin/ssh/misc.c 2016/03/02 22:42:40 1.102 +++ src/usr.bin/ssh/misc.c 2016/04/02 14:37:42 1.103 @@ -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. @@ -75,9 +75,9 @@ { 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) { @@ -99,9 +99,9 @@ { 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)) { @@ -705,16 +705,16 @@ 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); }