Skip to content

Commit 5a4ede0

Browse files
committed
Handle most basic FD redirection (dup2)
1 parent 1bfd82f commit 5a4ede0

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
@@ -3558,6 +3558,18 @@ rb_execarg_run_options(const struct rb_execarg *eargp, struct rb_execarg *sargp,
35583558
{
35593559
VALUE obj;
35603560

3561+
if (eargp->fd_dup2 != Qfalse) {
3562+
fprintf(stderr, "eargp->fd_dup2 set\n");
3563+
}
3564+
3565+
if (eargp->fd_dup2_child != Qfalse) {
3566+
fprintf(stderr, "eargp->fd_dup2_child set\n");
3567+
}
3568+
3569+
if (eargp->fd_close != Qfalse) {
3570+
fprintf(stderr, "eargp->fd_close set\n");
3571+
}
3572+
35613573
if (sargp) {
35623574
/* assume that sargp is always NULL on fork-able environments */
35633575
MEMZERO(sargp, struct rb_execarg, 1);
@@ -4676,8 +4688,26 @@ rb_posix_spawn(struct rb_execarg *eargp)
46764688
}
46774689
}
46784690

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

46824712
if (err) {
46834713
rb_sys_fail(abspath);
@@ -4700,9 +4730,10 @@ rb_spawn_process(struct rb_execarg *eargp, char *errmsg, size_t errmsg_buflen)
47004730
#endif
47014731

47024732
#if HAVE_POSIX_SPAWN
4733+
// fprintf(stderr, "eargp->close_others_maxhint = %d\n", eargp->close_others_maxhint);
47034734
if (//!eargp->use_shell &&
47044735
// !eargp->pgroup_given &&
4705-
eargp->close_others_maxhint == -1 &&
4736+
// eargp->close_others_maxhint == -1 &&
47064737
!eargp->umask_given &&
47074738
!eargp->unsetenv_others_given &&
47084739
!eargp->close_others_given &&

0 commit comments

Comments
 (0)