Browse Source

Accept string representations of signals in node.kill and child.kill

v0.7.4-release
Ryan Dahl 15 years ago
parent
commit
3456a16f71
  1. 12
      doc/api.txt
  2. 17
      src/child_process.cc
  3. 23
      src/node.cc
  4. 2
      test/mjsunit/test-signal-handler.js

12
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.

17
src/child_process.cc

@ -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))));

23
src/node.cc

@ -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) {

2
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);

Loading…
Cancel
Save