util: add convertProcessSignalToExitCode utility · nodejs/node@5051d90 · GitHub
Skip to content

Commit 5051d90

Browse files
ErickWendelRafaelGSS
authored andcommitted
util: add convertProcessSignalToExitCode utility
Add convertProcessSignalToExitCode() to convert signal names to POSIX exit codes (128 + signal number). Exposed in public util API. Refs: #60720 PR-URL: #60963 Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rafael Gonzaga <rafael.nunu@hotmail.com> Reviewed-By: Moshe Atlow <moshe@atlow.co.il>
1 parent d2e6dff commit 5051d90

5 files changed

Lines changed: 125 additions & 0 deletions

File tree

doc/api/child_process.md

Lines changed: 16 additions & 0 deletions

doc/api/util.md

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,38 @@ callbackFunction((err, ret) => {
8989
});
9090
```
9191

92+
## `util.convertProcessSignalToExitCode(signalCode)`
93+
94+
<!-- YAML
95+
added: REPLACEME
96+
-->
97+
98+
* `signalCode` {string} A signal name (e.g., `'SIGTERM'`, `'SIGKILL'`).
99+
* Returns: {number|null} The exit code, or `null` if the signal is invalid.
100+
101+
The `util.convertProcessSignalToExitCode()` method converts a signal name to its
102+
corresponding POSIX exit code. Following the POSIX standard, the exit code
103+
for a process terminated by a signal is calculated as `128 + signal number`.
104+
105+
```mjs
106+
import { convertProcessSignalToExitCode } from 'node:util';
107+
108+
console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
109+
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)
110+
console.log(convertProcessSignalToExitCode('INVALID')); // null
111+
```
112+
113+
```cjs
114+
const { convertProcessSignalToExitCode } = require('node:util');
115+
116+
console.log(convertProcessSignalToExitCode('SIGTERM')); // 143 (128 + 15)
117+
console.log(convertProcessSignalToExitCode('SIGKILL')); // 137 (128 + 9)
118+
console.log(convertProcessSignalToExitCode('INVALID')); // null
119+
```
120+
121+
This is particularly useful when working with processes to determine
122+
the exit code based on the signal that terminated the process.
123+
92124
## `util.debuglog(section[, callback])`
93125

94126
<!-- YAML

lib/internal/util.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ const {
1616
ObjectGetOwnPropertyDescriptor,
1717
ObjectGetOwnPropertyDescriptors,
1818
ObjectGetPrototypeOf,
19+
ObjectKeys,
1920
ObjectPrototypeHasOwnProperty,
2021
ObjectSetPrototypeOf,
2122
ObjectValues,
@@ -106,6 +107,7 @@ function isError(e) {
106107
const codesWarned = new SafeSet();
107108

108109
let validateString;
110+
let validateOneOf;
109111

110112
function getDeprecationWarningEmitter(
111113
code, msg, deprecated, useEmitSync,
@@ -393,6 +395,17 @@ function convertToValidSignal(signal) {
393395
throw new ERR_UNKNOWN_SIGNAL(signal);
394396
}
395397

398+
function convertProcessSignalToExitCode(signalCode) {
399+
// Lazy-load to avoid a circular dependency.
400+
if (validateOneOf === undefined)
401+
({ validateOneOf } = require('internal/validators'));
402+
403+
validateOneOf(signalCode, 'signalCode', ObjectKeys(signals));
404+
405+
// POSIX standard: exit code for signal termination is 128 + signal number.
406+
return 128 + signals[signalCode];
407+
}
408+
396409
function getConstructorOf(obj) {
397410
while (obj) {
398411
const descriptor = ObjectGetOwnPropertyDescriptor(obj, 'constructor');
@@ -956,6 +969,7 @@ module.exports = {
956969
assignFunctionName,
957970
cachedResult,
958971
constructSharedArrayBuffer,
972+
convertProcessSignalToExitCode,
959973
convertToValidSignal,
960974
createClassWrapper,
961975
decorateErrorStack,

lib/util.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,7 @@ const { getOptionValue } = require('internal/options');
8484
const binding = internalBinding('util');
8585

8686
const {
87+
convertProcessSignalToExitCode,
8788
deprecate: internalDeprecate,
8889
getLazy,
8990
getSystemErrorMap,
@@ -472,6 +473,7 @@ module.exports = {
472473
'The `util._extend` API is deprecated. Please use Object.assign() instead.',
473474
'DEP0060'),
474475
callbackify,
476+
convertProcessSignalToExitCode,
475477
debug: debuglog,
476478
debuglog,
477479
deprecate,
Lines changed: 61 additions & 0 deletions

0 commit comments

Comments
 (0)