Merge pull request #36 from chriskuehl/printerr

Prefix output with [dumb-init]
This commit is contained in:
Buck Evan 2015-10-02 12:44:45 -07:00
commit bac1bf99b0
2 changed files with 12 additions and 14 deletions

View file

@ -18,9 +18,13 @@
#include <sys/wait.h> #include <sys/wait.h>
#include <unistd.h> #include <unistd.h>
#define PRINTERR(...) do { \
fprintf(stderr, "[dumb-init] " __VA_ARGS__); \
} while (0)
#define DEBUG(...) do { \ #define DEBUG(...) do { \
if (debug) { \ if (debug) { \
fprintf(stderr, __VA_ARGS__); \ PRINTERR(__VA_ARGS__); \
} \ } \
} while (0) } while (0)
@ -31,9 +35,9 @@ char use_setsid = 1;
void forward_signal(int signum) { void forward_signal(int signum) {
if (child_pid > 0) { if (child_pid > 0) {
kill(use_setsid ? -child_pid : child_pid, signum); kill(use_setsid ? -child_pid : child_pid, signum);
DEBUG("Forwarded signal %d to child.\n", signum); DEBUG("Forwarded signal %d to children.\n", signum);
} else { } else {
DEBUG("Didn't forward signal %d, no child exists yet.\n", signum); DEBUG("Didn't forward signal %d, no children exists yet.\n", signum);
} }
} }
@ -150,7 +154,7 @@ int main(int argc, char *argv[]) {
continue; continue;
if (signal(signum, handle_signal) == SIG_ERR) { if (signal(signum, handle_signal) == SIG_ERR) {
fprintf(stderr, "Error: Couldn't register signal handler for signal `%d`. Exiting.\n", signum); PRINTERR("Couldn't register signal handler for signal `%d`. Exiting.\n", signum);
return 1; return 1;
} }
} }
@ -159,7 +163,7 @@ int main(int argc, char *argv[]) {
child_pid = fork(); child_pid = fork();
if (child_pid < 0) { if (child_pid < 0) {
fprintf(stderr, "Unable to fork. Exiting.\n"); PRINTERR("Unable to fork. Exiting.\n");
return 1; return 1;
} }
@ -167,8 +171,7 @@ int main(int argc, char *argv[]) {
if (use_setsid) { if (use_setsid) {
pid_t result = setsid(); pid_t result = setsid();
if (result == -1) { if (result == -1) {
fprintf( PRINTERR(
stderr,
"Unable to setsid (errno=%d %s). Exiting.\n", "Unable to setsid (errno=%d %s). Exiting.\n",
errno, errno,
strerror(errno) strerror(errno)
@ -181,12 +184,7 @@ int main(int argc, char *argv[]) {
execvp(argv[1], &argv[1]); execvp(argv[1], &argv[1]);
// if this point is reached, exec failed, so we should exit nonzero // if this point is reached, exec failed, so we should exit nonzero
fprintf( PRINTERR("%s: %s\n", argv[1], strerror(errno));
stderr,
"dumb-init: %s: %s\n",
argv[1],
strerror(errno)
);
exit(2); exit(2);
} else { } else {
pid_t killed_pid; pid_t killed_pid;

View file

@ -131,6 +131,6 @@ def test_fails_nonzero_with_bad_exec(both_debug_modes, both_setsid_modes):
proc.wait() proc.wait()
assert proc.returncode != 0 assert proc.returncode != 0
assert ( assert (
b'dumb-init: /doesnotexist: No such file or directory\n' b'[dumb-init] /doesnotexist: No such file or directory\n'
in proc.stderr in proc.stderr
) )