From e39923a3d7a87ccd984bfafe3aacefe419a15cdb Mon Sep 17 00:00:00 2001 From: Ryan Date: Sun, 21 Jun 2009 13:41:03 +0200 Subject: [PATCH] Add process.kill(sig = SIGTERM) --- src/process.cc | 37 ++++++++++++++++++++++++++----------- src/process.h | 2 ++ 2 files changed, 28 insertions(+), 11 deletions(-) diff --git a/src/process.cc b/src/process.cc index 1c83c533c2..3cdeadc39c 100644 --- a/src/process.cc +++ b/src/process.cc @@ -29,6 +29,7 @@ Process::Initialize (Handle target) NODE_SET_PROTOTYPE_METHOD(constructor_template, "write", Process::Write); NODE_SET_PROTOTYPE_METHOD(constructor_template, "close", Process::Close); + NODE_SET_PROTOTYPE_METHOD(constructor_template, "kill", Process::Kill); constructor_template->PrototypeTemplate()->SetAccessor(PID_SYMBOL, PIDGetter); @@ -101,13 +102,6 @@ Process::Write (const Arguments& args) Process *process = NODE_UNWRAP(Process, args.Holder()); assert(process); -#if 0 - if ( connection->ReadyState() != OPEN - && connection->ReadyState() != WRITE_ONLY - ) - return ThrowException(String::New("Socket is not open for writing")); -#endif - // XXX // A lot of improvement can be made here. First of all we're allocating // oi_bufs for every send which is clearly inefficent - it should use a @@ -156,6 +150,23 @@ Process::Write (const Arguments& args) return Undefined(); } +Handle +Process::Kill (const Arguments& args) +{ + HandleScope scope; + Process *process = NODE_UNWRAP(Process, args.Holder()); + assert(process); + + int sig = SIGTERM; + if (args[0]->IsInt32()) sig = args[0]->Int32Value(); + + if (process->Kill(sig) != 0) { + return ThrowException(String::New("Process already dead")); + } + + return Undefined(); +} + Handle Process::Close (const Arguments& args) { @@ -451,8 +462,7 @@ Process::OnExit (EV_P_ ev_child *watcher, int revents) int Process::Write (oi_buf *buf) { - if (stdin_pipe_[1] < 0 || got_close_) - return -1; + if (stdin_pipe_[1] < 0 || got_close_) return -1; oi_queue_insert_head(&out_stream_, &buf->queue); buf->written = 0; ev_io_start(EV_DEFAULT_UC_ &stdin_watcher_); @@ -462,10 +472,15 @@ Process::Write (oi_buf *buf) int Process::Close () { - if (stdin_pipe_[1] < 0 || got_close_) - return -1; + if (stdin_pipe_[1] < 0 || got_close_) return -1; got_close_ = true; ev_io_start(EV_DEFAULT_UC_ &stdin_watcher_); return 0; } +int +Process::Kill (int sig) +{ + if (pid_ == 0) return -1; + return kill(pid_, sig); +} diff --git a/src/process.h b/src/process.h index b48ba9713a..38138b87c1 100644 --- a/src/process.h +++ b/src/process.h @@ -18,6 +18,7 @@ class Process : ObjectWrap { static v8::Handle New (const v8::Arguments& args); static v8::Handle Write (const v8::Arguments& args); static v8::Handle Close (const v8::Arguments& args); + static v8::Handle Kill (const v8::Arguments& args); static v8::Handle PIDGetter (v8::Local _, const v8::AccessorInfo& info); Process(v8::Handle handle); @@ -27,6 +28,7 @@ class Process : ObjectWrap { int Spawn (const char *command); int Write (oi_buf *buf); int Close (); + int Kill (int sig); private: static void OnOutput (EV_P_ ev_io *watcher, int revents);