From a78ac8fe237fd0b96bef73e03c874bac42b70ed4 Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Tue, 29 Sep 2015 10:33:26 -0700 Subject: [PATCH 1/3] Exit nonzero if exec fails --- dumb-init.c | 10 ++++++++++ tests/child_processes_test.py | 11 +++++++++++ tox.ini | 7 +++++++ 3 files changed, 28 insertions(+) diff --git a/dumb-init.c b/dumb-init.c index febd4b8..d8093d0 100644 --- a/dumb-init.c +++ b/dumb-init.c @@ -128,6 +128,16 @@ int main(int argc, char *argv[]) { } execvp(argv[1], &argv[1]); + + // if this point is reached, exec failed, so we should exit nonzero + fprintf( + stderr, + "Failed to launch process '%s' (errno %d: %s)\n", + argv[1], + errno, + strerror(errno) + ); + exit(2); } else { pid_t killed_pid; int exit_status, status; diff --git a/tests/child_processes_test.py b/tests/child_processes_test.py index 5e2d686..3d28467 100644 --- a/tests/child_processes_test.py +++ b/tests/child_processes_test.py @@ -123,3 +123,14 @@ def test_processes_dont_receive_term_on_exit_if_no_setsid( assert child_stdout.readline() == str(signum).encode('ascii') + b'\n' os.kill(child_pid, signal.SIGKILL) + + +def test_fails_nonzero_with_bad_exec(both_debug_modes, both_setsid_modes): + """If dumb-init can't exec as requested, it should exit nonzero.""" + proc = Popen(('dumb-init', '/doesnotexist'), stderr=PIPE) + proc.wait() + assert proc.returncode != 0 + assert ( + "Failed to launch process '/doesnotexist' (errno 2: No such file or directory)\n" + in proc.stderr + ) diff --git a/tox.ini b/tox.ini index 8f94ee6..26a65b3 100644 --- a/tox.ini +++ b/tox.ini @@ -9,3 +9,10 @@ commands = [testenv:pre-commit] commands = pre-commit {posargs} + +[flake8] +max-line-length = 119 + +[pep8] +# autopep8 will rewrite lines to be shorter, even though we raised the length +ignore = E501 From d178fbaf525630913646683ee32b618358765e3b Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Tue, 29 Sep 2015 10:49:08 -0700 Subject: [PATCH 2/3] Print better error message when exec fails --- dumb-init.c | 3 +-- tests/child_processes_test.py | 2 +- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/dumb-init.c b/dumb-init.c index d8093d0..ed9de6b 100644 --- a/dumb-init.c +++ b/dumb-init.c @@ -132,9 +132,8 @@ int main(int argc, char *argv[]) { // if this point is reached, exec failed, so we should exit nonzero fprintf( stderr, - "Failed to launch process '%s' (errno %d: %s)\n", + "dumb-init: %s: %s\n", argv[1], - errno, strerror(errno) ); exit(2); diff --git a/tests/child_processes_test.py b/tests/child_processes_test.py index 3d28467..df27625 100644 --- a/tests/child_processes_test.py +++ b/tests/child_processes_test.py @@ -131,6 +131,6 @@ def test_fails_nonzero_with_bad_exec(both_debug_modes, both_setsid_modes): proc.wait() assert proc.returncode != 0 assert ( - "Failed to launch process '/doesnotexist' (errno 2: No such file or directory)\n" + 'dumb-init: /doesnotexist: No such file or directory\n' in proc.stderr ) From dd361496d17910a995f1986a901340d73cfa36af Mon Sep 17 00:00:00 2001 From: Chris Kuehl Date: Tue, 29 Sep 2015 10:50:49 -0700 Subject: [PATCH 3/3] tests/child_processes_test: fix py34 --- tests/child_processes_test.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/child_processes_test.py b/tests/child_processes_test.py index df27625..315795a 100644 --- a/tests/child_processes_test.py +++ b/tests/child_processes_test.py @@ -131,6 +131,6 @@ def test_fails_nonzero_with_bad_exec(both_debug_modes, both_setsid_modes): proc.wait() assert proc.returncode != 0 assert ( - 'dumb-init: /doesnotexist: No such file or directory\n' + b'dumb-init: /doesnotexist: No such file or directory\n' in proc.stderr )