Skip to content

Commit 5d3ff7c

Browse files
committed
Iterative unshadow_pipe_fd
1 parent e8b810f commit 5d3ff7c

File tree

1 file changed

+17
-18
lines changed

1 file changed

+17
-18
lines changed

cbits/posix/fork_exec.c

Lines changed: 17 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -107,24 +107,23 @@ setup_std_handle_fork(int fd,
107107
* errors. See #266.
108108
*/
109109
int unshadow_pipe_fd(int fd, char **failed_doing) {
110-
if (fd <= 2) {
111-
int fd2 = dup(fd);
112-
if (fd2 == -1) {
113-
*failed_doing = "dup(unshadow)";
114-
return -1;
115-
}
116-
117-
// This should recurse at most three times
118-
int fd3 = unshadow_pipe_fd(fd2, failed_doing);
119-
if (close(fd2) == -1) {
120-
*failed_doing = "close(unshadow)";
121-
return -1;
122-
}
123-
124-
return fd3;
125-
} else {
126-
return fd;
127-
}
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;
128127
}
129128

130129
/* Try spawning with fork. */

0 commit comments

Comments
 (0)