From 3456a16f711cdcace89687ebab6416d77f9aff9a Mon Sep 17 00:00:00 2001 From: Ryan Dahl Date: Thu, 15 Oct 2009 15:45:04 +0200 Subject: [PATCH] Accept string representations of signals in node.kill and child.kill --- doc/api.txt | 12 ++++++------ src/child_process.cc | 17 ++++++++++++++++- src/node.cc | 23 ++++++++++++++++++++--- test/mjsunit/test-signal-handler.js | 2 +- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/doc/api.txt b/doc/api.txt index 396635e52f..4bb146feb8 100644 --- a/doc/api.txt +++ b/doc/api.txt @@ -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. diff --git a/src/child_process.cc b/src/child_process.cc index 23cfdd936a..a70d529d1f 100644 --- a/src/child_process.cc +++ b/src/child_process.cc @@ -133,7 +133,22 @@ Handle 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 signame = args[0]->ToString(); + Local process = Context::GetCurrent()->Global(); + Local node_obj = process->Get(String::NewSymbol("node"))->ToObject(); + + Local 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)))); diff --git a/src/node.cc b/src/node.cc index bccb532ac6..9f2ad4342a 100644 --- a/src/node.cc +++ b/src/node.cc @@ -253,13 +253,30 @@ v8::Handle Exit(const v8::Arguments& args) { v8::Handle 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 signame = args[1]->ToString(); + Local process = Context::GetCurrent()->Global(); + Local node_obj = process->Get(String::NewSymbol("node"))->ToObject(); + + Local 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) { diff --git a/test/mjsunit/test-signal-handler.js b/test/mjsunit/test-signal-handler.js index e701fe46b0..4fd8cd0b2b 100644 --- a/test/mjsunit/test-signal-handler.js +++ b/test/mjsunit/test-signal-handler.js @@ -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);