Skip to content

Commit

Permalink
Merge pull request #60 from chriskuehl/fix-signal-terminated-processe…
Browse files Browse the repository at this point in the history
…s-exit-code

Fix signal terminated processes exit code
  • Loading branch information
chriskuehl committed Mar 11, 2016
2 parents a9eadb5 + b579371 commit b0ee633
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 11 deletions.
10 changes: 8 additions & 2 deletions dumb-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -217,8 +217,14 @@ int main(int argc, char *argv[]) {
DEBUG("Child spawned with PID %d.\n", child_pid);

while ((killed_pid = waitpid(-1, &status, 0))) {
exit_status = WEXITSTATUS(status);
DEBUG("A child with PID %d exited with exit status %d.\n", killed_pid, exit_status);
if (WIFEXITED(status)) {
exit_status = WEXITSTATUS(status);
DEBUG("A child with PID %d exited with exit status %d.\n", killed_pid, exit_status);
} else {
assert(WIFSIGNALED(status));
exit_status = 128 + WTERMSIG(status);
DEBUG("A child with PID %d was terminated by signal %d.\n", killed_pid, exit_status - 128);
}

if (killed_pid == child_pid) {
// send SIGTERM to any remaining children
Expand Down
30 changes: 24 additions & 6 deletions tests/exit_status_test.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
import signal
from subprocess import Popen

import pytest

def test_exit_status(both_debug_modes, both_setsid_modes):

@pytest.mark.parametrize('exit_status', [0, 1, 2, 32, 64, 127, 254, 255])
def test_exit_status_regular_exit(exit_status, both_debug_modes, both_setsid_modes):
"""dumb-init should exit with the same exit status as the process that it
supervises.
supervises when that process exits normally.
"""
proc = Popen(('dumb-init', 'sh', '-c', 'exit {0}'.format(exit_status)))
proc.wait()
assert proc.returncode == exit_status


@pytest.mark.parametrize('signal', [
signal.SIGTERM,
signal.SIGINT,
signal.SIGQUIT,
signal.SIGKILL,
])
def test_exit_status_terminated_by_signal(signal, both_debug_modes, both_setsid_modes):
"""dumb-init should exit with status 128 + signal when the child process is
terminated by a signal.
"""
for status in [0, 1, 2, 32, 64, 127, 254, 255]:
proc = Popen(('dumb-init', 'sh', '-c', 'exit {0}'.format(status)))
proc.wait()
assert proc.returncode == status
proc = Popen(('dumb-init', 'sh', '-c', 'kill -{0} $$'.format(signal)))
proc.wait()
assert proc.returncode == 128 + signal
6 changes: 3 additions & 3 deletions tests/lib/testing.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@
])

NORMAL_SIGNALS = frozenset(
set(range(1, 32))
- set([signal.SIGKILL, signal.SIGSTOP, signal.SIGCHLD])
- SUSPEND_SIGNALS
set(range(1, 32)) -
set([signal.SIGKILL, signal.SIGSTOP, signal.SIGCHLD]) -
SUSPEND_SIGNALS
)


Expand Down

0 comments on commit b0ee633

Please sign in to comment.