From 618b9f4b0c9a424ca8364abc797a9e8ccfdded6f Mon Sep 17 00:00:00 2001 From: Sam Roberts Date: Sun, 12 Nov 2017 09:47:14 +0000 Subject: [PATCH] lib: allow process kill by signal number This brings the behaviour in line with the documentation. PR-URL: https://github.com/nodejs/node/pull/16944 Reviewed-By: Colin Ihrig Reviewed-By: Anna Henningsen Reviewed-By: Ruben Bridgewater Reviewed-By: James M Snell --- lib/internal/process.js | 4 ++-- test/parallel/test-process-kill-pid.js | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/lib/internal/process.js b/lib/internal/process.js index 371281dfd44041..3de3ae1603deb5 100644 --- a/lib/internal/process.js +++ b/lib/internal/process.js @@ -162,8 +162,8 @@ function setupKillAndExit() { } // preserve null signal - if (0 === sig) { - err = process._kill(pid, 0); + if (sig === (sig | 0)) { + err = process._kill(pid, sig); } else { sig = sig || 'SIGTERM'; if (constants[sig]) { diff --git a/test/parallel/test-process-kill-pid.js b/test/parallel/test-process-kill-pid.js index 2ff35c6e4f866f..594a2a6a0bd8e6 100644 --- a/test/parallel/test-process-kill-pid.js +++ b/test/parallel/test-process-kill-pid.js @@ -57,16 +57,19 @@ assert.throws(function() { process.kill(1 / 0); }, assert.throws(function() { process.kill(-1 / 0); }, invalidPidArgument); -// Test that kill throws an error for invalid signal -const unknownSignal = common.expectsError({ +// Test that kill throws an error for unknown signal names +common.expectsError(() => process.kill(0, 'test'), { code: 'ERR_UNKNOWN_SIGNAL', type: TypeError, message: 'Unknown signal: test' }); - -assert.throws(function() { process.kill(1, 'test'); }, - unknownSignal); +// Test that kill throws an error for invalid signal numbers +common.expectsError(() => process.kill(0, 987), { + code: 'EINVAL', + type: Error, + message: 'kill EINVAL' +}); // Test kill argument processing in valid cases. // @@ -99,6 +102,11 @@ kill(0, undefined, 0, 15); kill('0', 'SIGHUP', 0, 1); kill('0', undefined, 0, 15); +// Confirm that numeric signal arguments are supported + +kill(0, 1, 0, 1); +kill(0, 15, 0, 15); + // negative numbers are meaningful on unix kill(-1, 'SIGHUP', -1, 1); kill(-1, undefined, -1, 15);