Skip to content

Commit 59808ec

Browse files
committed
Handle most basic FD redirection (dup2)
1 parent b995576 commit 59808ec

File tree

1 file changed

+33
-2
lines changed

1 file changed

+33
-2
lines changed

process.c

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3556,6 +3556,18 @@ rb_execarg_run_options(const struct rb_execarg *eargp, struct rb_execarg *sargp,
35563556
{
35573557
VALUE obj;
35583558

3559+
if (eargp->fd_dup2 != Qfalse) {
3560+
fprintf(stderr, "eargp->fd_dup2 set\n");
3561+
}
3562+
3563+
if (eargp->fd_dup2_child != Qfalse) {
3564+
fprintf(stderr, "eargp->fd_dup2_child set\n");
3565+
}
3566+
3567+
if (eargp->fd_close != Qfalse) {
3568+
fprintf(stderr, "eargp->fd_close set\n");
3569+
}
3570+
35593571
if (sargp) {
35603572
/* assume that sargp is always NULL on fork-able environments */
35613573
MEMZERO(sargp, struct rb_execarg, 1);
@@ -4674,8 +4686,26 @@ rb_posix_spawn(struct rb_execarg *eargp)
46744686
}
46754687
}
46764688

4677-
err = posix_spawn(&pid, abspath, NULL, &attr, argv, envp);
4689+
posix_spawn_file_actions_t file_actions;
4690+
posix_spawn_file_actions_init(&file_actions);
4691+
4692+
if (RTEST(eargp->fd_dup2)) {
4693+
for (long index = 0; index < RARRAY_LEN(eargp->fd_dup2); index++) {
4694+
VALUE pair = RARRAY_AREF(eargp->fd_dup2, index);
4695+
VALUE fd = RARRAY_AREF(pair, 0);
4696+
VALUE params = RARRAY_AREF(pair, 1);
4697+
4698+
int new_fd = NUM2INT(params); // TODO: params may not be a FD, may need more massaging.
4699+
fprintf(stderr, "posix_spawn_file_actions_adddup2(fops, %d, %d)\n", new_fd, NUM2INT(fd));
4700+
if ((err = posix_spawn_file_actions_adddup2(&file_actions, new_fd, NUM2INT(fd)))) {
4701+
rb_syserr_fail(err, "posix_spawn_file_actions_adddup2");
4702+
}
4703+
}
4704+
}
4705+
4706+
err = posix_spawn(&pid, abspath, &file_actions, &attr, argv, envp);
46784707
posix_spawnattr_destroy(&attr);
4708+
posix_spawn_file_actions_destroy(&file_actions);
46794709

46804710
if (err) {
46814711
rb_sys_fail(abspath);
@@ -4698,9 +4728,10 @@ rb_spawn_process(struct rb_execarg *eargp, char *errmsg, size_t errmsg_buflen)
46984728
#endif
46994729

47004730
#if HAVE_POSIX_SPAWN
4731+
// fprintf(stderr, "eargp->close_others_maxhint = %d\n", eargp->close_others_maxhint);
47014732
if (//!eargp->use_shell &&
47024733
// !eargp->pgroup_given &&
4703-
eargp->close_others_maxhint == -1 &&
4734+
// eargp->close_others_maxhint == -1 &&
47044735
!eargp->umask_given &&
47054736
!eargp->unsetenv_others_given &&
47064737
!eargp->close_others_given &&

0 commit comments

Comments
 (0)