Skip to content

Commit 3456a16

Browse files
committed
Accept string representations of signals in node.kill and child.kill
1 parent 334d56d commit 3456a16

File tree

4 files changed

+43
-11
lines changed

4 files changed

+43
-11
lines changed

doc/api.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -53,9 +53,10 @@ These objects are available to all programs.
5353
+node.cwd()+::
5454
Returns the current working directory of the process.
5555

56-
+node.kill(pid, signal)+ ::
57-
See kill(2). The standard POSIX signals are defined under the +node+
58-
namespace (+node.SIGINT+, +node.SIGUSR1+, ...).
56+
+node.kill(pid, signal="SIGTERM")+ ::
57+
Send a signal to a process. +pid+ is the process id and +signal+ is the
58+
signal to send; for example, "SIGINT" or "SIGUSR1". See kill(2) for more
59+
information.
5960

6061
+node.compile(source, scriptOrigin)+::
6162
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
471472
Closes the process's +stdin+ stream.
472473

473474

474-
+child.kill(signal=node.SIGTERM)+ ::
475+
+child.kill(signal="SIGTERM")+ ::
475476
Send a signal to the child process. If no argument is given, the process
476-
will be sent +node.SIGTERM+. The standard POSIX signals are defined under
477-
the +node+ namespace (+node.SIGINT+, +node.SIGUSR1+, ...).
477+
will be sent +"SIGTERM"+. See signal(7) for a list of available signals.
478478

479479

480480

src/child_process.cc

+16-1
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,22 @@ Handle<Value> ChildProcess::Kill(const Arguments& args) {
133133
assert(child);
134134

135135
int sig = SIGTERM;
136-
if (args[0]->IsInt32()) sig = args[0]->Int32Value();
136+
137+
if (args.Length() > 0) {
138+
if (args[0]->IsNumber()) {
139+
sig = args[0]->Int32Value();
140+
} else if (args[0]->IsString()) {
141+
Local<String> signame = args[0]->ToString();
142+
Local<Object> process = Context::GetCurrent()->Global();
143+
Local<Object> node_obj = process->Get(String::NewSymbol("node"))->ToObject();
144+
145+
Local<Value> sig_v = node_obj->Get(signame);
146+
if (!sig_v->IsNumber()) {
147+
return ThrowException(Exception::Error(String::New("Unknown signal")));
148+
}
149+
sig = sig_v->Int32Value();
150+
}
151+
}
137152

138153
if (child->Kill(sig) != 0) {
139154
return ThrowException(Exception::Error(String::New(strerror(errno))));

src/node.cc

+20-3
Original file line numberDiff line numberDiff line change
@@ -253,13 +253,30 @@ v8::Handle<v8::Value> Exit(const v8::Arguments& args) {
253253
v8::Handle<v8::Value> Kill(const v8::Arguments& args) {
254254
HandleScope scope;
255255

256-
if (args.Length() != 2 || !args[0]->IsNumber() || !args[1]->IsInt32()) {
256+
if (args.Length() < 1 || !args[0]->IsNumber()) {
257257
return ThrowException(Exception::Error(String::New("Bad argument.")));
258258
}
259259

260260
pid_t pid = args[0]->IntegerValue();
261-
int sig = args[1]->Int32Value();
262-
261+
262+
int sig = SIGTERM;
263+
264+
if (args.Length() >= 2) {
265+
if (args[1]->IsNumber()) {
266+
sig = args[1]->Int32Value();
267+
} else if (args[1]->IsString()) {
268+
Local<String> signame = args[1]->ToString();
269+
Local<Object> process = Context::GetCurrent()->Global();
270+
Local<Object> node_obj = process->Get(String::NewSymbol("node"))->ToObject();
271+
272+
Local<Value> sig_v = node_obj->Get(signame);
273+
if (!sig_v->IsNumber()) {
274+
return ThrowException(Exception::Error(String::New("Unknown signal")));
275+
}
276+
sig = sig_v->Int32Value();
277+
}
278+
}
279+
263280
int r = kill(pid, sig);
264281

265282
if (r != 0) {

test/mjsunit/test-signal-handler.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ setInterval(function () {
2323
puts("running process..." + ++i);
2424

2525
if (i == 5) {
26-
node.kill(process.pid, node.SIGUSR1);
26+
node.kill(process.pid, "SIGUSR1");
2727
}
2828
}, 1);
2929

0 commit comments

Comments
 (0)