Skip site navigation (1)Skip section navigation (2)
Date:      Sun, 5 May 2013 10:51:40 +0000 (UTC)
From:      Jilles Tjoelker <jilles@FreeBSD.org>
To:        src-committers@freebsd.org, svn-src-all@freebsd.org, svn-src-head@freebsd.org
Subject:   svn commit: r250267 - head/bin/sh
Message-ID:  <201305051051.r45Apewp037524@svn.freebsd.org>

next in thread | raw e-mail | index | archive | help
Author: jilles
Date: Sun May  5 10:51:40 2013
New Revision: 250267
URL: http://svnweb.freebsd.org/changeset/base/250267

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

Modified:
  head/bin/sh/input.c
  head/bin/sh/jobs.c
  head/bin/sh/main.c
  head/bin/sh/redir.c

Modified: head/bin/sh/input.c
==============================================================================
--- head/bin/sh/input.c	Sun May  5 09:38:25 2013	(r250266)
+++ head/bin/sh/input.c	Sun May  5 10:51:40 2013	(r250267)
@@ -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);

Modified: head/bin/sh/jobs.c
==============================================================================
--- head/bin/sh/jobs.c	Sun May  5 09:38:25 2013	(r250266)
+++ head/bin/sh/jobs.c	Sun May  5 10:51:40 2013	(r250267)
@@ -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) {

Modified: head/bin/sh/main.c
==============================================================================
--- head/bin/sh/main.c	Sun May  5 09:38:25 2013	(r250266)
+++ head/bin/sh/main.c	Sun May  5 10:51:40 2013	(r250267)
@@ -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)

Modified: head/bin/sh/redir.c
==============================================================================
--- head/bin/sh/redir.c	Sun May  5 09:38:25 2013	(r250266)
+++ head/bin/sh/redir.c	Sun May  5 10:51:40 2013	(r250267)
@@ -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;
 		}



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?201305051051.r45Apewp037524>