summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRyan <ry@tinyclouds.org>2009-06-21 13:41:03 +0200
committerRyan <ry@tinyclouds.org>2009-06-21 13:41:03 +0200
commite39923a3d7a87ccd984bfafe3aacefe419a15cdb (patch)
tree415ba2bb4d248dd39caafa4b83fbd7d01546ac31
parent2fd4958698b6efa8b2f924ed28d3520806de1a38 (diff)
downloadnodejs-e39923a3d7a87ccd984bfafe3aacefe419a15cdb.tar.gz
nodejs-e39923a3d7a87ccd984bfafe3aacefe419a15cdb.tar.bz2
nodejs-e39923a3d7a87ccd984bfafe3aacefe419a15cdb.zip
Add process.kill(sig = SIGTERM)
-rw-r--r--src/process.cc37
-rw-r--r--src/process.h2
2 files changed, 28 insertions, 11 deletions
diff --git a/src/process.cc b/src/process.cc
index 1c83c533c..3cdeadc39 100644
--- a/src/process.cc
+++ b/src/process.cc
@@ -29,6 +29,7 @@ Process::Initialize (Handle<Object> 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
@@ -157,6 +151,23 @@ Process::Write (const Arguments& args)
}
Handle<Value>
+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<Value>
Process::Close (const Arguments& args)
{
HandleScope scope;
@@ -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 b48ba9713..38138b87c 100644
--- a/src/process.h
+++ b/src/process.h
@@ -18,6 +18,7 @@ class Process : ObjectWrap {
static v8::Handle<v8::Value> New (const v8::Arguments& args);
static v8::Handle<v8::Value> Write (const v8::Arguments& args);
static v8::Handle<v8::Value> Close (const v8::Arguments& args);
+ static v8::Handle<v8::Value> Kill (const v8::Arguments& args);
static v8::Handle<v8::Value> PIDGetter (v8::Local<v8::String> _, const v8::AccessorInfo& info);
Process(v8::Handle<v8::Object> 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);