diff options
author | krw@openbsd.org <krw@openbsd.org> | 2016-04-02 14:37:42 +0000 |
---|---|---|
committer | Darren Tucker <dtucker@zip.com.au> | 2016-04-08 14:12:18 +1000 |
commit | 574def0eb493cd6efeffd4ff2e9257abcffee0c8 (patch) | |
tree | bbb66ad781b86ca0c02b38e19564fe17e5e23e2d | |
parent | b3413534aa9d71a941005df2760d1eec2c2b0854 (diff) |
upstream commit
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
-rw-r--r-- | misc.c | 26 |
1 files changed, 13 insertions, 13 deletions
@@ -1,4 +1,4 @@ | |||
1 | /* $OpenBSD: misc.c,v 1.102 2016/03/02 22:42:40 dtucker Exp $ */ | 1 | /* $OpenBSD: misc.c,v 1.103 2016/04/02 14:37:42 krw Exp $ */ |
2 | /* | 2 | /* |
3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. | 3 | * Copyright (c) 2000 Markus Friedl. All rights reserved. |
4 | * Copyright (c) 2005,2006 Damien Miller. All rights reserved. | 4 | * Copyright (c) 2005,2006 Damien Miller. All rights reserved. |
@@ -84,9 +84,9 @@ set_nonblock(int fd) | |||
84 | { | 84 | { |
85 | int val; | 85 | int val; |
86 | 86 | ||
87 | val = fcntl(fd, F_GETFL, 0); | 87 | val = fcntl(fd, F_GETFL); |
88 | if (val < 0) { | 88 | if (val < 0) { |
89 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); | 89 | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); |
90 | return (-1); | 90 | return (-1); |
91 | } | 91 | } |
92 | if (val & O_NONBLOCK) { | 92 | if (val & O_NONBLOCK) { |
@@ -108,9 +108,9 @@ unset_nonblock(int fd) | |||
108 | { | 108 | { |
109 | int val; | 109 | int val; |
110 | 110 | ||
111 | val = fcntl(fd, F_GETFL, 0); | 111 | val = fcntl(fd, F_GETFL); |
112 | if (val < 0) { | 112 | if (val < 0) { |
113 | error("fcntl(%d, F_GETFL, 0): %s", fd, strerror(errno)); | 113 | error("fcntl(%d, F_GETFL): %s", fd, strerror(errno)); |
114 | return (-1); | 114 | return (-1); |
115 | } | 115 | } |
116 | if (!(val & O_NONBLOCK)) { | 116 | if (!(val & O_NONBLOCK)) { |
@@ -729,16 +729,16 @@ sanitise_stdfd(void) | |||
729 | strerror(errno)); | 729 | strerror(errno)); |
730 | exit(1); | 730 | exit(1); |
731 | } | 731 | } |
732 | while (++dupfd <= 2) { | 732 | while (++dupfd <= STDERR_FILENO) { |
733 | /* Only clobber closed fds */ | 733 | /* Only populate closed fds. */ |
734 | if (fcntl(dupfd, F_GETFL, 0) >= 0) | 734 | if (fcntl(dupfd, F_GETFL) == -1 && errno == EBADF) { |
735 | continue; | 735 | if (dup2(nullfd, dupfd) == -1) { |
736 | if (dup2(nullfd, dupfd) == -1) { | 736 | fprintf(stderr, "dup2: %s\n", strerror(errno)); |
737 | fprintf(stderr, "dup2: %s\n", strerror(errno)); | 737 | exit(1); |
738 | exit(1); | 738 | } |
739 | } | 739 | } |
740 | } | 740 | } |
741 | if (nullfd > 2) | 741 | if (nullfd > STDERR_FILENO) |
742 | close(nullfd); | 742 | close(nullfd); |
743 | } | 743 | } |
744 | 744 | ||