Skip to content

Commit

Permalink
Follow POSIX exit codes for processes killed by signals
Browse files Browse the repository at this point in the history
  • Loading branch information
fhunleth committed Aug 22, 2019
1 parent 22275a2 commit dd043d0
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 6 deletions.
17 changes: 15 additions & 2 deletions src/muontrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -497,9 +497,22 @@ static int child_wait_loop(pid_t child_pid, int *still_running)
int status;
pid_t dying_pid = wait(&status);
if (dying_pid == child_pid) {
int exit_status = WIFEXITED(status) ? WEXITSTATUS(status) : EXIT_FAILURE;
INFO("main child exited. status=%d, our exit code: %d", status, exit_status);
// Let the caller know that the child isn't running and has been cleaned up
*still_running = 0;

int exit_status;
if (WIFSIGNALED(status)) {
// Crash on signal, return the signal in the exit status. See POSIX:
// http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_08_02
exit_status = 128 + WTERMSIG(status);
INFO("child terminated via signal %d. our exit status: %d", status, exit_status);
} else if (WIFEXITED(status)) {
exit_status = WEXITSTATUS(status);
INFO("child exited with exit status: %d", exit_status);
} else {
INFO("child terminated with unexpected status: %d", status);
exit_status = EXIT_FAILURE;
}
return exit_status;
} else {
INFO("something else caused sigchild: pid=%d, status=%d. our child=%d", dying_pid, status, child_pid);
Expand Down
4 changes: 0 additions & 4 deletions test/daemon_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,6 @@ defmodule DaemonTest do

alias MuonTrap.Daemon

defp test_path(cmd) do
Path.join([File.cwd!(), "test", cmd])
end

defp daemon_spec(cmd, args) do
Supervisor.child_spec({Daemon, [cmd, args]}, id: :test_daemon)
end
Expand Down
17 changes: 17 additions & 0 deletions test/kill_self_with_signal.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include <err.h>
#include <signal.h>
#include <stdlib.h>
#include <unistd.h>

int main(int argc, char **argv)
{
// This test kills itself with a SIGTERM to see if
// muontrap reports the expected exit code.
if (kill(getpid(), SIGTERM) < 0)
err(EXIT_FAILURE, "kill");

// Give the OS up to a second to deliver the signal.
sleep(1);

errx(EXIT_FAILURE, "expected a signal");
}
5 changes: 5 additions & 0 deletions test/muontrap_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,11 @@ defmodule MuonTrapTest do
File.rm_rf!(@tmp_path)
end

test "signals return an exit code of 128 + signal" do
# SIGTERM == 15
assert {"", 128 + 15} == MuonTrap.cmd(test_path("kill_self_with_signal.test"), [])
end

test "README.md version is up to date" do
app = :muontrap
app_version = Application.spec(app, :vsn) |> to_string()
Expand Down
5 changes: 5 additions & 0 deletions test/support/test_case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ defmodule MuonTrapTest.Case do

@timeout_before_close_check 20

@spec test_path(Path.t()) :: Path.t()
def test_path(cmd) do
Path.join([File.cwd!(), "test", cmd])
end

@spec cpu_cgroup_exists(String.t()) :: boolean
def cpu_cgroup_exists(path) do
{rc, 0} = System.cmd("cgget", ["-g", "cpu", path], stderr_to_stdout: true)
Expand Down

0 comments on commit dd043d0

Please sign in to comment.