1 | var assert = require('assert')
|
2 |
|
3 | module.exports = function (cb) {
|
4 | assert.equal(typeof cb, 'function', 'a callback must be provided for exit handler')
|
5 |
|
6 | var emittedExit = false
|
7 |
|
8 | Object.keys(signals).forEach(function (sig) {
|
9 | var listener = function () {
|
10 |
|
11 | if (process.listeners(sig).length === 1) {
|
12 | process.removeListener(sig, listener)
|
13 | cb(process.exitCode || signals[sig], sig)
|
14 | process.kill(process.pid, sig)
|
15 | }
|
16 | }
|
17 |
|
18 | try {
|
19 | process.on(sig, listener)
|
20 | } catch (er) {}
|
21 | })
|
22 |
|
23 | process.on('exit', function (code) {
|
24 | if (emittedExit) return
|
25 | emittedExit = true
|
26 | return cb(code, undefined)
|
27 | })
|
28 | }
|
29 |
|
30 | var signals = {
|
31 | 'SIGALRM': 142,
|
32 | 'SIGBUS': 138,
|
33 | 'SIGCLD': undefined,
|
34 | 'SIGEMT': undefined,
|
35 | 'SIGFPE': 136,
|
36 | 'SIGHUP': 129,
|
37 | 'SIGILL': 132,
|
38 | 'SIGINFO': undefined,
|
39 | 'SIGINT': 130,
|
40 | 'SIGIOT': 134,
|
41 | 'SIGKILL': 137,
|
42 | 'SIGLOST': undefined,
|
43 | 'SIGPIPE': 141,
|
44 | 'SIGPOLL': undefined,
|
45 | 'SIGPROF': 155,
|
46 | 'SIGPWR': undefined,
|
47 | 'SIGQUIT': 131,
|
48 | 'SIGSEGV': 139,
|
49 | 'SIGSTKFLT': undefined,
|
50 | 'SIGSYS': 140,
|
51 | 'SIGTERM': 143,
|
52 | 'SIGTRAP': 133,
|
53 | 'SIGTSTP': 146,
|
54 | 'SIGTTIN': 149,
|
55 | 'SIGTTOU': 150,
|
56 | 'SIGUNUSED': undefined,
|
57 | 'SIGUSR2': 159,
|
58 | 'SIGVTALRM': 154,
|
59 | 'SIGXCPU': 152,
|
60 | 'SIGXFSZ': 153
|
61 | }
|
62 |
|
63 | var nonFatalSignals = [
|
64 | 'SIGWINCH',
|
65 | 'SIGUSR1',
|
66 | 'SIGCHLD',
|
67 | 'SIGSTOP',
|
68 | 'SIGCONT',
|
69 | 'SIGIO',
|
70 | 'SIGURG',
|
71 | 'SIGABRT',
|
72 | 'SIGURG'
|
73 | ]
|