sh: Use O_CLOEXEC and F_DUPFD_CLOEXEC instead of separate fcntl() call.

This commit is contained in:
Jilles Tjoelker 2013-05-05 10:51:40 +00:00
parent 14a75019c2
commit 5aa6dfda1c
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=250267
4 changed files with 11 additions and 17 deletions

View File

@ -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);

View File

@ -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) {

View File

@ -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)

View File

@ -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;
}