process: Send signal name to signal handlers

Backport-PR-URL: https://github.com/nodejs/node/pull/22380
PR-URL: https://github.com/nodejs/node/pull/15606
Reviewed-By: Colin Ihrig <cjihrig@gmail.com>
Reviewed-By: James M Snell <jasnell@gmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Reviewed-By: Evan Lucas <evanlucas@me.com>
Reviewed-By: Gireesh Punathil <gpunathi@in.ibm.com>
Reviewed-By: Bartosz Sosnowski <bartosz@janeasystems.com>
This commit is contained in:
Robert Rossmann authored and Myles Borins committed 2018-09-06 09:40:48 -07:00
1 parent ec1828c2b6
commit 711098e88c
3 files changed
+36 -1

No files matched your search

+11
View File
@@ -349,6 +349,9 @@ Signal events will be emitted when the Node.js process receives a signal. Please
refer to signal(7) for a listing of standard POSIX signal names such as
`SIGINT`, `SIGHUP`, etc.
The signal handler will receive the signal's name (`'SIGINT'`,
`'SIGTERM'`, etc.) as the first argument.
The name of each event will be the uppercase common name for the signal (e.g.
`'SIGINT'` for `SIGINT` signals).
@@ -361,6 +364,14 @@ process.stdin.resume();
process.on('SIGINT', () => {
console.log('Received SIGINT. Press Control-D to exit.');
});
// Using a single function to handle multiple signals
function handle(signal) {
console.log(`Received ${signal}`);
}
process.on('SIGINT', handle);
process.on('SIGTERM', handle);
```
* `SIGUSR1` is reserved by Node.js to start the [debugger][]. It's possible to
+1 -1
View File
@@ -194,7 +194,7 @@ function setupSignalHandlers() {
wrap.unref();
wrap.onsignal = function() { process.emit(type); };
wrap.onsignal = function() { process.emit(type, type); };
const signum = constants[type];
const err = wrap.start(signum);
+24
View File
@@ -0,0 +1,24 @@
'use strict';
const common = require('../common');
const assert = require('assert');
if (common.isWindows) {
common.skip('Sending signals with process.kill is not supported on Windows');
}
process.once('SIGINT', common.mustCall((signal) => {
assert.strictEqual(signal, 'SIGINT');
}));
process.kill(process.pid, 'SIGINT');
process.once('SIGTERM', common.mustCall((signal) => {
assert.strictEqual(signal, 'SIGTERM');
}));
process.kill(process.pid, 'SIGTERM');
// Prevent Node.js from exiting due to empty event loop before signal handlers
// are fired
setImmediate(() => {});