Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use posix spawn #551

Draft
wants to merge 15 commits into
base: master
Choose a base branch
from
Prev Previous commit
Next Next commit
posix_spawn: support eargp->fd_close
  • Loading branch information
byroot committed Jan 24, 2024
commit 0c313a44d2d8ac9ba641912da82c6a9cb53b361a
32 changes: 32 additions & 0 deletions process.c
Original file line number Diff line number Diff line change
@@ -4691,6 +4691,26 @@ rb_posix_spawn(struct rb_execarg *eargp)
posix_spawn_file_actions_t file_actions;
posix_spawn_file_actions_init(&file_actions);

// TODO: do we need it? Currently the `open` seem to be done in the parent.
// if (RTEST(eargp->fd_open)) {
// for (long index = 0; index < RARRAY_LEN(eargp->fd_open); index++) {
// VALUE pair = RARRAY_AREF(eargp->fd_open, index);
// VALUE fd = RARRAY_AREF(pair, 0);
// VALUE params = RARRAY_AREF(pair, 1);
//
// VALUE path = RARRAY_AREF(param, 0);
// VALUE flags = RARRAY_AREF(param, 1);
// // param = rb_ary_new3(4, path, flags, perm, Qnil);
//
//
// int new_fd = NUM2INT(params); // TODO: params may not be a FD, may need more massaging.
// fprintf(stderr, "posix_spawn_file_actions_addopen(fops, %d, %d)\n", new_fd, NUM2INT(fd));
// if ((err = posix_spawn_file_actions_addopen(&file_actions, fd, RSTRING_PTR(path), NUM2INT(flags)))) {
// rb_syserr_fail(err, "posix_spawn_file_actions_addopen");
// }
// }
// }

if (RTEST(eargp->fd_dup2)) {
for (long index = 0; index < RARRAY_LEN(eargp->fd_dup2); index++) {
VALUE pair = RARRAY_AREF(eargp->fd_dup2, index);
@@ -4705,6 +4725,18 @@ rb_posix_spawn(struct rb_execarg *eargp)
}
}

if (RTEST(eargp->fd_close)) {
for (long index = 0; index < RARRAY_LEN(eargp->fd_close); index++) {
VALUE pair = RARRAY_AREF(eargp->fd_close, index);
VALUE fd = RARRAY_AREF(pair, 0);

fprintf(stderr, "posix_spawn_file_actions_addclose(fops, %d)\n", NUM2INT(fd));
if ((err = posix_spawn_file_actions_addclose(&file_actions, NUM2INT(fd)))) {
rb_syserr_fail(err, "posix_spawn_file_actions_addclose");
}
}
}

err = posix_spawn(&pid, abspath, &file_actions, &attr, argv, envp);
posix_spawnattr_destroy(&attr);
posix_spawn_file_actions_destroy(&file_actions);