Skip to content

Commit

Permalink
Correctly pass through flags rb_process_status_wait. (#81)
Browse files Browse the repository at this point in the history
  • Loading branch information
ioquatix authored Aug 29, 2023
1 parent 96d3b0c commit 11d8bb2
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 14 deletions.
6 changes: 4 additions & 2 deletions ext/io/event/selector/epoll.c
Original file line number Diff line number Diff line change
Expand Up @@ -446,6 +446,7 @@ struct process_wait_arguments {
struct IO_Event_Selector_EPoll *selector;
struct IO_Event_Selector_EPoll_Waiting *waiting;
int pid;
int flags;
int descriptor;
};

Expand All @@ -456,7 +457,7 @@ VALUE process_wait_transfer(VALUE _arguments) {
IO_Event_Selector_fiber_transfer(arguments->selector->backend.loop, 0, NULL);

if (arguments->waiting->ready) {
return IO_Event_Selector_process_status_wait(arguments->pid);
return IO_Event_Selector_process_status_wait(arguments->pid, arguments->flags);
} else {
return Qfalse;
}
Expand All @@ -480,7 +481,7 @@ VALUE IO_Event_Selector_EPoll_process_wait(VALUE self, VALUE fiber, VALUE _pid,
TypedData_Get_Struct(self, struct IO_Event_Selector_EPoll, &IO_Event_Selector_EPoll_Type, selector);

pid_t pid = NUM2PIDT(_pid);
// int flags = NUM2INT(_flags);
int flags = NUM2INT(_flags);

int descriptor = pidfd_open(pid, 0);

Expand All @@ -506,6 +507,7 @@ VALUE IO_Event_Selector_EPoll_process_wait(VALUE self, VALUE fiber, VALUE _pid,
struct process_wait_arguments process_wait_arguments = {
.selector = selector,
.pid = pid,
.flags = flags,
.descriptor = descriptor,
.waiting = &waiting,
};
Expand Down
7 changes: 5 additions & 2 deletions ext/io/event/selector/kqueue.c
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,7 @@ struct process_wait_arguments {
struct IO_Event_Selector_KQueue *selector;
struct IO_Event_Selector_KQueue_Waiting *waiting;
pid_t pid;
int flags;
};

static
Expand Down Expand Up @@ -461,7 +462,7 @@ VALUE process_wait_transfer(VALUE _arguments) {

if (arguments->waiting->ready) {
process_prewait(arguments->pid);
return IO_Event_Selector_process_status_wait(arguments->pid);
return IO_Event_Selector_process_status_wait(arguments->pid, arguments->flags);
} else {
return Qfalse;
}
Expand All @@ -483,6 +484,7 @@ VALUE IO_Event_Selector_KQueue_process_wait(VALUE self, VALUE fiber, VALUE _pid,
TypedData_Get_Struct(self, struct IO_Event_Selector_KQueue, &IO_Event_Selector_KQueue_Type, selector);

pid_t pid = NUM2PIDT(_pid);
int flags = NUM2INT(_flags);

struct IO_Event_Selector_KQueue_Waiting waiting = {
.list = {.type = &IO_Event_Selector_KQueue_process_wait_list_type},
Expand All @@ -494,6 +496,7 @@ VALUE IO_Event_Selector_KQueue_process_wait(VALUE self, VALUE fiber, VALUE _pid,
.selector = selector,
.waiting = &waiting,
.pid = pid,
.flags = flags,
};

int result = IO_Event_Selector_KQueue_Waiting_register(selector, pid, &waiting);
Expand All @@ -502,7 +505,7 @@ VALUE IO_Event_Selector_KQueue_process_wait(VALUE self, VALUE fiber, VALUE _pid,
if (errno == ESRCH) {
process_prewait(pid);

return IO_Event_Selector_process_status_wait(pid);
return IO_Event_Selector_process_status_wait(pid, flags);
}

rb_sys_fail("IO_Event_Selector_KQueue_process_wait:IO_Event_Selector_KQueue_Waiting_register");
Expand Down
9 changes: 2 additions & 7 deletions ext/io/event/selector/selector.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ static const int DEBUG = 0;

static ID id_transfer, id_alive_p;

#ifndef HAVE_RB_PROCESS_STATUS_WAIT
static VALUE process_wnohang;
#endif

VALUE IO_Event_Selector_fiber_transfer(VALUE fiber, int argc, VALUE *argv) {
// TODO Consider introducing something like `rb_fiber_scheduler_transfer(...)`.
#ifdef HAVE__RB_FIBER_TRANSFER
Expand Down Expand Up @@ -76,9 +72,9 @@ int IO_Event_Selector_io_descriptor(VALUE io) {
static ID id_wait;
static VALUE rb_Process_Status = Qnil;

VALUE IO_Event_Selector_process_status_wait(rb_pid_t pid)
VALUE IO_Event_Selector_process_status_wait(rb_pid_t pid, int flags)
{
return rb_funcall(rb_Process_Status, id_wait, 2, PIDT2NUM(pid), process_wnohang);
return rb_funcall(rb_Process_Status, id_wait, 2, PIDT2NUM(pid), INT2NUM(flags | WNOHANG));
}
#endif

Expand Down Expand Up @@ -157,7 +153,6 @@ void Init_IO_Event_Selector(VALUE IO_Event_Selector) {

#ifndef HAVE_RB_PROCESS_STATUS_WAIT
id_wait = rb_intern("wait");
process_wnohang = rb_const_get(rb_mProcess, rb_intern("WNOHANG"));
rb_Process_Status = rb_const_get_at(rb_mProcess, rb_intern("Status"));
rb_gc_register_mark_object(rb_Process_Status);
#endif
Expand Down
5 changes: 3 additions & 2 deletions ext/io/event/selector/selector.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,10 +66,11 @@ VALUE IO_Event_Selector_fiber_raise(VALUE fiber, int argc, VALUE *argv);
int IO_Event_Selector_io_descriptor(VALUE io);
#endif

// Reap a process without hanging.
#ifdef HAVE_RB_PROCESS_STATUS_WAIT
#define IO_Event_Selector_process_status_wait(pid) rb_process_status_wait(pid)
#define IO_Event_Selector_process_status_wait(pid, flags) rb_process_status_wait(pid, flags | WNOHANG)
#else
VALUE IO_Event_Selector_process_status_wait(rb_pid_t pid);
VALUE IO_Event_Selector_process_status_wait(rb_pid_t pid, int flags);
#endif

int IO_Event_Selector_nonblock_set(int file_descriptor);
Expand Down
5 changes: 4 additions & 1 deletion ext/io/event/selector/uring.c
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ struct process_wait_arguments {
struct IO_Event_Selector_URing_Waiting *waiting;

pid_t pid;
int flags;
int descriptor;
};

Expand All @@ -438,7 +439,7 @@ VALUE process_wait_transfer(VALUE _arguments) {
IO_Event_Selector_fiber_transfer(arguments->selector->backend.loop, 0, NULL);

if (arguments->waiting->result) {
return IO_Event_Selector_process_status_wait(arguments->pid);
return IO_Event_Selector_process_status_wait(arguments->pid, arguments->flags);
} else {
return Qfalse;
}
Expand All @@ -460,6 +461,7 @@ VALUE IO_Event_Selector_URing_process_wait(VALUE self, VALUE fiber, VALUE _pid,
TypedData_Get_Struct(self, struct IO_Event_Selector_URing, &IO_Event_Selector_URing_Type, selector);

pid_t pid = NUM2PIDT(_pid);
int flags = NUM2INT(_flags);

int descriptor = pidfd_open(pid, 0);
if (descriptor < 0) {
Expand All @@ -477,6 +479,7 @@ VALUE IO_Event_Selector_URing_process_wait(VALUE self, VALUE fiber, VALUE _pid,
.selector = selector,
.waiting = &waiting,
.pid = pid,
.flags = flags,
.descriptor = descriptor,
};

Expand Down

0 comments on commit 11d8bb2

Please sign in to comment.