summaryrefslogtreecommitdiffstats
path: root/bin
diff options
context:
space:
mode:
authorjilles <jilles@FreeBSD.org>2013-05-05 10:51:40 +0000
committerjilles <jilles@FreeBSD.org>2013-05-05 10:51:40 +0000
commit135786413b1346f647980e2c338087f41e2735a2 (patch)
treedaf1ba66bada98a9f310e8dfed5331cec9dd46cd /bin
parent42b8c0012b80efc0b1d1bacb9d5c45afd8a51301 (diff)
downloadFreeBSD-src-135786413b1346f647980e2c338087f41e2735a2.zip
FreeBSD-src-135786413b1346f647980e2c338087f41e2735a2.tar.gz
sh: Use O_CLOEXEC and F_DUPFD_CLOEXEC instead of separate fcntl() call.
Diffstat (limited to 'bin')
-rw-r--r--bin/sh/input.c9
-rw-r--r--bin/sh/jobs.c12
-rw-r--r--bin/sh/main.c2
-rw-r--r--bin/sh/redir.c5
4 files changed, 11 insertions, 17 deletions
diff --git a/bin/sh/input.c b/bin/sh/input.c
index 62e82a0..f574f46 100644
--- a/bin/sh/input.c
+++ b/bin/sh/input.c
@@ -397,10 +397,10 @@ setinputfile(const char *fname, int push)
int fd2;
INTOFF;
- if ((fd = open(fname, O_RDONLY)) < 0)
+ if ((fd = open(fname, O_RDONLY | O_CLOEXEC)) < 0)
error("cannot open %s: %s", fname, strerror(errno));
if (fd < 10) {
- fd2 = fcntl(fd, F_DUPFD, 10);
+ fd2 = fcntl(fd, F_DUPFD_CLOEXEC, 10);
close(fd);
if (fd2 < 0)
error("Out of file descriptors");
@@ -412,14 +412,13 @@ setinputfile(const char *fname, int push)
/*
- * Like setinputfile, but takes an open file descriptor. Call this with
- * interrupts off.
+ * Like setinputfile, but takes an open file descriptor (which should have
+ * its FD_CLOEXEC flag already set). Call this with interrupts off.
*/
void
setinputfd(int fd, int push)
{
- (void)fcntl(fd, F_SETFD, FD_CLOEXEC);
if (push) {
pushfile();
parsefile->buf = ckmalloc(BUFSIZ + 1);
diff --git a/bin/sh/jobs.c b/bin/sh/jobs.c
index 54f72ea..163b7e7 100644
--- a/bin/sh/jobs.c
+++ b/bin/sh/jobs.c
@@ -127,11 +127,12 @@ setjobctl(int on)
if (on) {
if (ttyfd != -1)
close(ttyfd);
- if ((ttyfd = open(_PATH_TTY, O_RDWR)) < 0) {
+ if ((ttyfd = open(_PATH_TTY, O_RDWR | O_CLOEXEC)) < 0) {
i = 0;
while (i <= 2 && !isatty(i))
i++;
- if (i > 2 || (ttyfd = fcntl(i, F_DUPFD, 10)) < 0)
+ if (i > 2 ||
+ (ttyfd = fcntl(i, F_DUPFD_CLOEXEC, 10)) < 0)
goto out;
}
if (ttyfd < 10) {
@@ -139,7 +140,7 @@ setjobctl(int on)
* Keep our TTY file descriptor out of the way of
* the user's redirections.
*/
- if ((i = fcntl(ttyfd, F_DUPFD, 10)) < 0) {
+ if ((i = fcntl(ttyfd, F_DUPFD_CLOEXEC, 10)) < 0) {
close(ttyfd);
ttyfd = -1;
goto out;
@@ -147,11 +148,6 @@ setjobctl(int on)
close(ttyfd);
ttyfd = i;
}
- if (fcntl(ttyfd, F_SETFD, FD_CLOEXEC) < 0) {
- close(ttyfd);
- ttyfd = -1;
- goto out;
- }
do { /* while we are in the background */
initialpgrp = tcgetpgrp(ttyfd);
if (initialpgrp < 0) {
diff --git a/bin/sh/main.c b/bin/sh/main.c
index 84a1ef2..ffe8a61 100644
--- a/bin/sh/main.c
+++ b/bin/sh/main.c
@@ -248,7 +248,7 @@ read_profile(const char *name)
if (expandedname == NULL)
return;
INTOFF;
- if ((fd = open(expandedname, O_RDONLY)) >= 0)
+ if ((fd = open(expandedname, O_RDONLY | O_CLOEXEC)) >= 0)
setinputfd(fd, 1);
INTON;
if (fd < 0)
diff --git a/bin/sh/redir.c b/bin/sh/redir.c
index fda094d2..855f317 100644
--- a/bin/sh/redir.c
+++ b/bin/sh/redir.c
@@ -121,7 +121,7 @@ redirect(union node *redir, int flags)
if ((flags & REDIR_PUSH) && sv->renamed[fd] == EMPTY) {
INTOFF;
- if ((i = fcntl(fd, F_DUPFD, 10)) == -1) {
+ if ((i = fcntl(fd, F_DUPFD_CLOEXEC, 10)) == -1) {
switch (errno) {
case EBADF:
i = CLOSED;
@@ -131,8 +131,7 @@ redirect(union node *redir, int flags)
error("%d: %s", fd, strerror(errno));
break;
}
- } else
- (void)fcntl(i, F_SETFD, FD_CLOEXEC);
+ }
sv->renamed[fd] = i;
INTON;
}
OpenPOWER on IntegriCloud