about summary refs log tree commit diff
path: root/src/process
diff options
context:
space:
mode:
authorRich Felker <dalias@aerifal.cx>2017-11-05 17:26:48 -0500
committerRich Felker <dalias@aerifal.cx>2017-11-05 17:26:48 -0500
commit6fc6ca1a323bc0b6b9e9cdc8fa72221ae18fe206 (patch)
tree2b523366bae1478eb28c9922331c127421e44e5f /src/process
parenteb03bde2f24582874cb72b56c7811bf51da0c817 (diff)
downloadmusl-6fc6ca1a323bc0b6b9e9cdc8fa72221ae18fe206.tar.gz
musl-6fc6ca1a323bc0b6b9e9cdc8fa72221ae18fe206.tar.xz
musl-6fc6ca1a323bc0b6b9e9cdc8fa72221ae18fe206.zip
adjust posix_spawn dup2 action behavior to match future requirements
the resolution to Austin Group issue #411 defined new semantics for
the posix_spawn dup2 file action in the (previously useless) case
where src and dest fd are equal. future issues will require the dup2
file action to remove the close-on-exec flag. without this change,
passing fds to a child with posix_spawn while avoiding fd-leak races
in a multithreaded parent required a complex dance with temporary fds.

based on patch by Petr Skocik. changes were made to preserve the
80-column formatting of the function and to remove code that became
unreachable as a result of the new functionality.
Diffstat (limited to 'src/process')
-rw-r--r--src/process/posix_spawn.c20
1 files changed, 12 insertions, 8 deletions
diff --git a/src/process/posix_spawn.c b/src/process/posix_spawn.c
index 0849c71f..93fb1555 100644
--- a/src/process/posix_spawn.c
+++ b/src/process/posix_spawn.c
@@ -27,12 +27,7 @@ static int __sys_dup2(int old, int new)
 #ifdef SYS_dup2
 	return __syscall(SYS_dup2, old, new);
 #else
-	if (old==new) {
-		int r = __syscall(SYS_fcntl, old, F_GETFD);
-		return r<0 ? r : old;
-	} else {
-		return __syscall(SYS_dup3, old, new, 0);
-	}
+	return __syscall(SYS_dup3, old, new, 0);
 #endif
 }
 
@@ -109,8 +104,17 @@ static int child(void *args_vp)
 				__syscall(SYS_close, op->fd);
 				break;
 			case FDOP_DUP2:
-				if ((ret=__sys_dup2(op->srcfd, op->fd))<0)
-					goto fail;
+				fd = op->srcfd;
+				if (fd != op->fd) {
+					if ((ret=__sys_dup2(fd, op->fd))<0)
+						goto fail;
+				} else {
+					ret = __syscall(SYS_fcntl, fd, F_GETFD);
+					ret = __syscall(SYS_fcntl, fd, F_SETFD,
+					                ret & ~FD_CLOEXEC);
+					if (ret<0)
+						goto fail;
+				}
 				break;
 			case FDOP_OPEN:
 				fd = __sys_open(op->path, op->oflag, op->mode);