Skip to content

Commit c25afa9

Browse files
committed
cbits: Implement unshadow_pipe_fd using fcntl(F_DUPFD)
Closes #284.
1 parent ec45621 commit c25afa9

File tree

1 file changed

+11
-17
lines changed

1 file changed

+11
-17
lines changed

cbits/posix/fork_exec.c

Lines changed: 11 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -107,23 +107,17 @@ setup_std_handle_fork(int fd,
107107
* errors. See #266.
108108
*/
109109
int unshadow_pipe_fd(int fd, char **failed_doing) {
110-
int i = 0;
111-
int fds[3] = {0};
112-
for (i = 0; fd < 3 && i < 3; ++i) {
113-
fds[i] = fd;
114-
fd = dup(fd);
115-
if (fd == -1) {
116-
*failed_doing = "dup(unshadow)";
117-
return -1;
118-
}
119-
}
120-
for (int j = 0; j < i; ++j) {
121-
if (close(fds[j]) == -1) {
122-
*failed_doing = "close(unshadow)";
123-
return -1;
124-
}
125-
}
126-
return fd;
110+
if (fd > 2) {
111+
return fd;
112+
}
113+
114+
int new_fd = fcntl(fd, F_DUPFD, 3);
115+
if (new_fd == -1) {
116+
*failed_doing = "fcntl(F_DUP_FD)";
117+
return -1;
118+
}
119+
close(fd);
120+
return new_fd;
127121
}
128122

129123
/* Try spawning with fork. */

0 commit comments

Comments
 (0)