From 4d0f6aeb9611900d7ee34075f921f3a707e8659d Mon Sep 17 00:00:00 2001 From: Trent Mick Date: Fri, 2 Nov 2012 12:15:29 -0700 Subject: [PATCH] ensure termination of child dtrace on CLI term by a signal --- CHANGES.md | 4 +++- bin/bunyan | 17 +++++++++++++++++ 2 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index abc83db..b915820 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -2,7 +2,9 @@ ## bunyan 0.16.1 (not yet released) -(nothing yet) +- Ensure that a possible dtrace child process (with using `bunyan -p PID`) is + terminated on signal termination of the bunyan CLI (at least for SIGINT, + SIGQUIT, SIGTERM, SIGHUP). ## bunyan 0.16.0 diff --git a/bin/bunyan b/bin/bunyan index dfd58ba..61cf143 100755 --- a/bin/bunyan +++ b/bin/bunyan @@ -65,6 +65,9 @@ Object.keys(levelFromName).forEach(function (name) { // The current raw input line being processed. Used for `uncaughtException`. var currLine = null; +// Child dtrace process, if any. Used for signal-handling. +var child = null; + //---- support functions @@ -905,6 +908,7 @@ function processPid(opts, stylize, callback) { var argv = ['dtrace', '-Z', '-x', 'strsize=4k', '-qn', format('bunyan%d:::log-*{printf("%s", copyinstr(arg0))}', opts.pid)]; var dtrace = spawn(argv[0], argv.slice(1)); + child = dtrace; // intentionall global dtrace.stderr.pipe(process.stderr); @@ -1023,6 +1027,19 @@ function asyncForEach(arr, iterator, callback) { //---- mainline +// Try to ensure we close a child 'dtrace' process on signalled exit. +function signalCleanupAndExit(signal) { + if (child) { + child.kill(signal); + } + process.exit(); +} +process.on('SIGINT', function () { signalCleanupAndExit('SIGINT'); }); +process.on('SIGQUIT', function () { signalCleanupAndExit('SIGQUIT'); }); +process.on('SIGTERM', function () { signalCleanupAndExit('SIGTERM'); }); +process.on('SIGHUP', function () { signalCleanupAndExit('SIGHUP'); }); + + process.on('uncaughtException', function (err) { function indent(s) { var lines = s.split(/\r?\n/);