aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDenys Vlasenko <vda.linux@googlemail.com>2018-03-30 22:15:14 +0200
committerDenys Vlasenko <vda.linux@googlemail.com>2018-03-30 22:15:14 +0200
commit60fb98e51d11ed45bbc836eb28a2539ba3ab76f7 (patch)
treed377201de5c4639b3723141208fb7eacb205b4a6
parentdf65dc89b428c8f66ee2203f4a14eb2592d89ee0 (diff)
downloadbusybox-w32-60fb98e51d11ed45bbc836eb28a2539ba3ab76f7.tar.gz
busybox-w32-60fb98e51d11ed45bbc836eb28a2539ba3ab76f7.tar.bz2
busybox-w32-60fb98e51d11ed45bbc836eb28a2539ba3ab76f7.zip
ash: use F_DUPFD_CLOEXEC and O_CLOEXEC
function old new delta setjobctl 371 367 -4 setinputfile 226 220 -6 ------------------------------------------------------------------------------ (add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-10) Total: -10 bytes Based on patch by Mark Marshall <mark.marshall@omicronenergy.com> Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
-rw-r--r--shell/ash.c25
1 files changed, 16 insertions, 9 deletions
diff --git a/shell/ash.c b/shell/ash.c
index 85690e555..c957b001e 100644
--- a/shell/ash.c
+++ b/shell/ash.c
@@ -258,6 +258,9 @@ typedef long arith_t;
258#ifndef F_DUPFD_CLOEXEC 258#ifndef F_DUPFD_CLOEXEC
259# define F_DUPFD_CLOEXEC F_DUPFD 259# define F_DUPFD_CLOEXEC F_DUPFD
260#endif 260#endif
261#ifndef O_CLOEXEC
262# define O_CLOEXEC 0
263#endif
261#ifndef PIPE_BUF 264#ifndef PIPE_BUF
262# define PIPE_BUF 4096 /* amount of buffering in a pipe */ 265# define PIPE_BUF 4096 /* amount of buffering in a pipe */
263#endif 266#endif
@@ -3972,12 +3975,13 @@ setjobctl(int on)
3972 goto out; 3975 goto out;
3973 } 3976 }
3974 /* fd is a tty at this point */ 3977 /* fd is a tty at this point */
3975 fd = fcntl(fd, F_DUPFD, 10); 3978 fd = fcntl(fd, F_DUPFD_CLOEXEC, 10);
3976 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */ 3979 if (ofd >= 0) /* if it is "/dev/tty", close. If 0/1/2, don't */
3977 close(ofd); 3980 close(ofd);
3978 if (fd < 0) 3981 if (fd < 0)
3979 goto out; /* F_DUPFD failed */ 3982 goto out; /* F_DUPFD failed */
3980 close_on_exec_on(fd); 3983 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
3984 close_on_exec_on(fd);
3981 while (1) { /* while we are in the background */ 3985 while (1) { /* while we are in the background */
3982 pgrp = tcgetpgrp(fd); 3986 pgrp = tcgetpgrp(fd);
3983 if (pgrp < 0) { 3987 if (pgrp < 0) {
@@ -5427,13 +5431,14 @@ savefd(int from)
5427 int newfd; 5431 int newfd;
5428 int err; 5432 int err;
5429 5433
5430 newfd = fcntl(from, F_DUPFD, 10); 5434 newfd = fcntl(from, F_DUPFD_CLOEXEC, 10);
5431 err = newfd < 0 ? errno : 0; 5435 err = newfd < 0 ? errno : 0;
5432 if (err != EBADF) { 5436 if (err != EBADF) {
5433 if (err) 5437 if (err)
5434 ash_msg_and_raise_perror("%d", from); 5438 ash_msg_and_raise_perror("%d", from);
5435 close(from); 5439 close(from);
5436 fcntl(newfd, F_SETFD, FD_CLOEXEC); 5440 if (F_DUPFD_CLOEXEC == F_DUPFD)
5441 close_on_exec_on(newfd);
5437 } 5442 }
5438 5443
5439 return newfd; 5444 return newfd;
@@ -5458,7 +5463,7 @@ dup_CLOEXEC(int fd, int avoid_fd)
5458 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1); 5463 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
5459 if (newfd >= 0) { 5464 if (newfd >= 0) {
5460 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */ 5465 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
5461 fcntl(newfd, F_SETFD, FD_CLOEXEC); 5466 close_on_exec_on(newfd);
5462 } else { /* newfd < 0 */ 5467 } else { /* newfd < 0 */
5463 if (errno == EBUSY) 5468 if (errno == EBUSY)
5464 goto repeat; 5469 goto repeat;
@@ -5472,7 +5477,7 @@ xdup_CLOEXEC_and_close(int fd, int avoid_fd)
5472{ 5477{
5473 int newfd; 5478 int newfd;
5474 repeat: 5479 repeat:
5475 newfd = fcntl(fd, F_DUPFD, avoid_fd + 1); 5480 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
5476 if (newfd < 0) { 5481 if (newfd < 0) {
5477 if (errno == EBUSY) 5482 if (errno == EBUSY)
5478 goto repeat; 5483 goto repeat;
@@ -5483,7 +5488,8 @@ xdup_CLOEXEC_and_close(int fd, int avoid_fd)
5483 return fd; 5488 return fd;
5484 ash_msg_and_raise_perror("%d", newfd); 5489 ash_msg_and_raise_perror("%d", newfd);
5485 } 5490 }
5486 fcntl(newfd, F_SETFD, FD_CLOEXEC); 5491 if (F_DUPFD_CLOEXEC == F_DUPFD)
5492 close_on_exec_on(newfd);
5487 close(fd); 5493 close(fd);
5488 return newfd; 5494 return newfd;
5489} 5495}
@@ -10753,7 +10759,7 @@ setinputfile(const char *fname, int flags)
10753 int fd; 10759 int fd;
10754 10760
10755 INT_OFF; 10761 INT_OFF;
10756 fd = open(fname, O_RDONLY); 10762 fd = open(fname, O_RDONLY | O_CLOEXEC);
10757 if (fd < 0) { 10763 if (fd < 0) {
10758 if (flags & INPUT_NOFILE_OK) 10764 if (flags & INPUT_NOFILE_OK)
10759 goto out; 10765 goto out;
@@ -10762,8 +10768,9 @@ setinputfile(const char *fname, int flags)
10762 } 10768 }
10763 if (fd < 10) 10769 if (fd < 10)
10764 fd = savefd(fd); 10770 fd = savefd(fd);
10765 else 10771 else if (O_CLOEXEC == 0) /* old libc */
10766 close_on_exec_on(fd); 10772 close_on_exec_on(fd);
10773
10767 setinputfd(fd, flags & INPUT_PUSH_FILE); 10774 setinputfd(fd, flags & INPUT_PUSH_FILE);
10768 out: 10775 out:
10769 INT_ON; 10776 INT_ON;