• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1'use strict';
2const common = require('../../common');
3if (common.isWindows)
4  common.skip('No RegisterSignalHandler() on Windows');
5
6const assert = require('assert');
7const fs = require('fs');
8const path = require('path');
9const { signals } = require('os').constants;
10const { spawnSync } = require('child_process');
11
12const bindingPath = path.resolve(
13  __dirname, 'build', common.buildType, 'binding.node');
14
15if (!fs.existsSync(bindingPath))
16  common.skip('binding not built yet');
17
18const binding = require(bindingPath);
19
20if (process.argv[2] === 'child') {
21  const signo = +process.argv[3];
22  const reset = process.argv[4] === 'reset';
23  const count = +process.argv[5];
24
25  binding.registerSignalHandler(signo, reset);
26  for (let i = 0; i < count; i++)
27    process.kill(process.pid, signo);
28  return;
29}
30
31for (const raiseSignal of [ 'SIGABRT', 'SIGSEGV' ]) {
32  const signo = signals[raiseSignal];
33  for (const { reset, count, stderr, code, signal } of [
34    { reset: true, count: 1, stderr: [signo], code: 0, signal: null },
35    { reset: true, count: 2, stderr: [signo], code: null, signal: raiseSignal },
36    { reset: false, count: 1, stderr: [signo], code: 0, signal: null },
37    { reset: false, count: 2, stderr: [signo, signo], code: 0, signal: null },
38  ]) {
39    // We do not want to generate core files when running this test as an
40    // addon test. We require this file as an abort test as well, though,
41    // with ALLOW_CRASHES set.
42    if (signal !== null && !process.env.ALLOW_CRASHES)
43      continue;
44    // reset_handler does not work with SIGSEGV.
45    if (reset && signo === signals.SIGSEGV)
46      continue;
47
48    const args = [__filename, 'child', signo, reset ? 'reset' : '', count];
49    console.log(`Running: node ${args.join(' ')}`);
50    const result = spawnSync(
51      process.execPath, args, { stdio: ['inherit', 'pipe', 'inherit'] });
52    assert.strictEqual(result.status, code);
53    assert.strictEqual(result.signal, signal);
54    assert.deepStrictEqual([...result.stdout], stderr);
55  }
56}
57