summaryrefslogtreecommitdiff
path: root/misc.c
diff options
context:
space:
mode:
Diffstat (limited to 'misc.c')
-rw-r--r--misc.c14
1 files changed, 8 insertions, 6 deletions
diff --git a/misc.c b/misc.c
index 0339cede4..b876c0030 100644
--- a/misc.c
+++ b/misc.c
@@ -24,7 +24,7 @@
24 */ 24 */
25 25
26#include "includes.h" 26#include "includes.h"
27RCSID("$OpenBSD: misc.c,v 1.40 2006/01/02 07:53:44 reyk Exp $"); 27RCSID("$OpenBSD: misc.c,v 1.41 2006/01/05 23:43:53 djm Exp $");
28 28
29#ifdef SSH_TUN_OPENBSD 29#ifdef SSH_TUN_OPENBSD
30#include <net/if.h> 30#include <net/if.h>
@@ -616,18 +616,20 @@ tun_open(int tun, int mode)
616void 616void
617sanitise_stdfd(void) 617sanitise_stdfd(void)
618{ 618{
619 int nullfd; 619 int nullfd, dupfd;
620 620
621 if ((nullfd = open(_PATH_DEVNULL, O_RDWR)) == -1) { 621 if ((nullfd = dupfd = open(_PATH_DEVNULL, O_RDWR)) == -1) {
622 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno)); 622 fprintf(stderr, "Couldn't open /dev/null: %s", strerror(errno));
623 exit(1); 623 exit(1);
624 } 624 }
625 while (nullfd < 2) { 625 while (++dupfd <= 2) {
626 if (dup2(nullfd, nullfd + 1) == -1) { 626 /* Only clobber closed fds */
627 if (fcntl(dupfd, F_GETFL, 0) >= 0)
628 continue;
629 if (dup2(nullfd, dupfd) == -1) {
627 fprintf(stderr, "dup2: %s", strerror(errno)); 630 fprintf(stderr, "dup2: %s", strerror(errno));
628 exit(1); 631 exit(1);
629 } 632 }
630 nullfd++;
631 } 633 }
632 if (nullfd > 2) 634 if (nullfd > 2)
633 close(nullfd); 635 close(nullfd);