Accept string representations of signals in node.kill and child.kill
authorRyan Dahl <ry@tinyclouds.org>
Thu, 15 Oct 2009 13:45:04 +0000 (15:45 +0200)
committerRyan Dahl <ry@tinyclouds.org>
Thu, 15 Oct 2009 13:45:04 +0000 (15:45 +0200)
doc/api.txt
src/child_process.cc
src/node.cc
test/mjsunit/test-signal-handler.js

index 396635e..4bb146f 100644 (file)
@@ -53,9 +53,10 @@ These objects are available to all programs.
 +node.cwd()+::
 Returns the current working directory of the process.
 
-+node.kill(pid, signal)+ ::
-See kill(2).  The standard POSIX signals are defined under the +node+
-namespace (+node.SIGINT+, +node.SIGUSR1+, ...).
++node.kill(pid, signal="SIGTERM")+ ::
+Send a signal to a process. +pid+ is the process id and +signal+ is the
+signal to send; for example, "SIGINT" or "SIGUSR1".  See kill(2) for more
+information.
 
 +node.compile(source, scriptOrigin)+::
 Just like +eval()+ except that you can specify a +scriptOrigin+ for better
@@ -471,10 +472,9 @@ specifies the encoding: possible values are +"utf8"+, +"ascii"+, and
 Closes the process's +stdin+ stream.
 
 
-+child.kill(signal=node.SIGTERM)+ ::
++child.kill(signal="SIGTERM")+ ::
 Send a signal to the child process.  If no argument is given, the process
-will be sent +node.SIGTERM+.  The standard POSIX signals are defined under
-the +node+ namespace (+node.SIGINT+, +node.SIGUSR1+, ...).
+will be sent +"SIGTERM"+. See signal(7) for a list of available signals.
 
 
 
index 23cfdd9..a70d529 100644 (file)
@@ -133,7 +133,22 @@ Handle<Value> ChildProcess::Kill(const Arguments& args) {
   assert(child);
 
   int sig = SIGTERM;
-  if (args[0]->IsInt32()) sig = args[0]->Int32Value();
+
+  if (args.Length() > 0) {
+    if (args[0]->IsNumber()) {
+      sig = args[0]->Int32Value();
+    } else if (args[0]->IsString()) {
+      Local<String> signame = args[0]->ToString();
+      Local<Object> process = Context::GetCurrent()->Global();
+      Local<Object> node_obj = process->Get(String::NewSymbol("node"))->ToObject();
+
+      Local<Value> sig_v = node_obj->Get(signame);
+      if (!sig_v->IsNumber()) {
+        return ThrowException(Exception::Error(String::New("Unknown signal")));
+      }
+      sig = sig_v->Int32Value();
+    }
+  }
 
   if (child->Kill(sig) != 0) {
     return ThrowException(Exception::Error(String::New(strerror(errno))));
index bccb532..9f2ad43 100644 (file)
@@ -253,13 +253,30 @@ v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
 v8::Handle<v8::Value> Kill(const v8::Arguments& args) {
   HandleScope scope;
   
-  if (args.Length() != 2 || !args[0]->IsNumber() || !args[1]->IsInt32()) {
+  if (args.Length() < 1 || !args[0]->IsNumber()) {
     return ThrowException(Exception::Error(String::New("Bad argument.")));
   }
   
   pid_t pid = args[0]->IntegerValue();
-  int sig = args[1]->Int32Value();
-  
+
+  int sig = SIGTERM;
+
+  if (args.Length() >= 2) {
+    if (args[1]->IsNumber()) {
+      sig = args[1]->Int32Value();
+    } else if (args[1]->IsString()) {
+      Local<String> signame = args[1]->ToString();
+      Local<Object> process = Context::GetCurrent()->Global();
+      Local<Object> node_obj = process->Get(String::NewSymbol("node"))->ToObject();
+
+      Local<Value> sig_v = node_obj->Get(signame);
+      if (!sig_v->IsNumber()) {
+        return ThrowException(Exception::Error(String::New("Unknown signal")));
+      }
+      sig = sig_v->Int32Value();
+    }
+  }
+
   int r = kill(pid, sig);
 
   if (r != 0) {
index e701fe4..4fd8cd0 100644 (file)
@@ -23,7 +23,7 @@ setInterval(function () {
   puts("running process..." + ++i);
 
   if (i == 5) {
-    node.kill(process.pid, node.SIGUSR1);
+    node.kill(process.pid, "SIGUSR1");
   }
 }, 1);