Skip to content

Commit

Permalink
get_status returns a Process::Status object
Browse files Browse the repository at this point in the history
  • Loading branch information
tmm1 committed Feb 11, 2009
1 parent ee1843e commit fb11a41
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 9 deletions.
10 changes: 7 additions & 3 deletions ext/cmain.cpp
Expand Up @@ -310,9 +310,13 @@ evma_get_subprocess_pid
extern "C" int evma_get_subprocess_pid (const char *binding, pid_t *pid) extern "C" int evma_get_subprocess_pid (const char *binding, pid_t *pid)
{ {
ensure_eventmachine("evma_get_subprocess_pid"); ensure_eventmachine("evma_get_subprocess_pid");
EventableDescriptor *ed = dynamic_cast <EventableDescriptor*> (Bindable_t::GetObject (binding)); PipeDescriptor *pd = dynamic_cast <PipeDescriptor*> (Bindable_t::GetObject (binding));
if (ed) { if (pd) {
return ed->GetSubprocessPid (pid) ? 1 : 0; return pd->GetSubprocessPid (pid) ? 1 : 0;
}
else if (pid && EventMachine->SubprocessPid) {
*pid = EventMachine->SubprocessPid;
return 1;
} }
else else
return 0; return 0;
Expand Down
1 change: 1 addition & 0 deletions ext/em.h
Expand Up @@ -89,6 +89,7 @@ class EventMachine_t
static void SetuidString (const char*); static void SetuidString (const char*);
static int SetRlimitNofile (int); static int SetRlimitNofile (int);


pid_t SubprocessPid;
int SubprocessExitStatus; int SubprocessExitStatus;


// Temporary: // Temporary:
Expand Down
3 changes: 3 additions & 0 deletions ext/pipe.cpp
Expand Up @@ -94,6 +94,9 @@ PipeDescriptor::~PipeDescriptor()


assert (MyEventMachine); assert (MyEventMachine);


/* Another hack to make the SubprocessPid available to get_subprocess_status */
MyEventMachine->SubprocessPid = SubprocessPid;

// check if the process is already dead // check if the process is already dead
if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) == 0) { if (waitpid (SubprocessPid, &(MyEventMachine->SubprocessExitStatus), WNOHANG) == 0) {
kill (SubprocessPid, SIGTERM); kill (SubprocessPid, SIGTERM);
Expand Down
22 changes: 16 additions & 6 deletions ext/rubymain.cpp
Expand Up @@ -42,6 +42,8 @@ static VALUE Intern_receive_data;
static VALUE Intern_notify_readable; static VALUE Intern_notify_readable;
static VALUE Intern_notify_writable; static VALUE Intern_notify_writable;


static VALUE rb_cProcStatus;

/**************** /****************
t_event_callback t_event_callback
****************/ ****************/
Expand Down Expand Up @@ -242,16 +244,20 @@ t_get_subprocess_status


static VALUE t_get_subprocess_status (VALUE self, VALUE signature) static VALUE t_get_subprocess_status (VALUE self, VALUE signature)
{ {
VALUE proc_status = Qnil;

int status; int status;
pid_t pid;

if (evma_get_subprocess_status (StringValuePtr (signature), &status)) { if (evma_get_subprocess_status (StringValuePtr (signature), &status)) {
#ifdef WEXITSTATUS if (evma_get_subprocess_pid (StringValuePtr (signature), &pid)) {
return INT2NUM (WEXITSTATUS(status)); proc_status = rb_obj_alloc(rb_cProcStatus);
#else rb_iv_set(proc_status, "status", INT2FIX(status));
return INT2NUM (status); rb_iv_set(proc_status, "pid", INT2FIX(pid));
#endif }
} }


return Qnil; return proc_status;
} }


/***************************** /*****************************
Expand Down Expand Up @@ -644,6 +650,10 @@ Init_rubyeventmachine


extern "C" void Init_rubyeventmachine() extern "C" void Init_rubyeventmachine()
{ {
// Lookup Process::Status for get_subprocess_status
VALUE rb_mProcess = rb_const_get(rb_cObject, rb_intern("Process"));
rb_cProcStatus = rb_const_get(rb_mProcess, rb_intern("Status"));

// Tuck away some symbol values so we don't have to look 'em up every time we need 'em. // Tuck away some symbol values so we don't have to look 'em up every time we need 'em.
Intern_at_signature = rb_intern ("@signature"); Intern_at_signature = rb_intern ("@signature");
Intern_at_timers = rb_intern ("@timers"); Intern_at_timers = rb_intern ("@timers");
Expand Down

0 comments on commit fb11a41

Please sign in to comment.