Skip to content

Commit

Permalink
Merge pull request Yelp#36 from chriskuehl/printerr
Browse files Browse the repository at this point in the history
Prefix output with [dumb-init]
  • Loading branch information
Buck Evan committed Oct 2, 2015
2 parents cd79164 + 316f6b0 commit bac1bf9
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 14 deletions.
24 changes: 11 additions & 13 deletions dumb-init.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@
#include <sys/wait.h>
#include <unistd.h>

#define PRINTERR(...) do { \
fprintf(stderr, "[dumb-init] " __VA_ARGS__); \
} while (0)

#define DEBUG(...) do { \
if (debug) { \
fprintf(stderr, __VA_ARGS__); \
PRINTERR(__VA_ARGS__); \
} \
} while (0)

Expand All @@ -31,9 +35,9 @@ char use_setsid = 1;
void forward_signal(int signum) {
if (child_pid > 0) {
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 {
DEBUG("Didn't forward signal %d, no child exists yet.\n", signum);
DEBUG("Didn't forward signal %d, no children exists yet.\n", signum);
}
}

Expand Down Expand Up @@ -150,7 +154,7 @@ int main(int argc, char *argv[]) {
continue;

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;
}
}
Expand All @@ -159,16 +163,15 @@ int main(int argc, char *argv[]) {
child_pid = fork();

if (child_pid < 0) {
fprintf(stderr, "Unable to fork. Exiting.\n");
PRINTERR("Unable to fork. Exiting.\n");
return 1;
}

if (child_pid == 0) {
if (use_setsid) {
pid_t result = setsid();
if (result == -1) {
fprintf(
stderr,
PRINTERR(
"Unable to setsid (errno=%d %s). Exiting.\n",
errno,
strerror(errno)
Expand All @@ -181,12 +184,7 @@ 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,
"dumb-init: %s: %s\n",
argv[1],
strerror(errno)
);
PRINTERR("%s: %s\n", argv[1], strerror(errno));
exit(2);
} else {
pid_t killed_pid;
Expand Down
2 changes: 1 addition & 1 deletion tests/child_processes_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,6 @@ def test_fails_nonzero_with_bad_exec(both_debug_modes, both_setsid_modes):
proc.wait()
assert proc.returncode != 0
assert (
b'dumb-init: /doesnotexist: No such file or directory\n'
b'[dumb-init] /doesnotexist: No such file or directory\n'
in proc.stderr
)

0 comments on commit bac1bf9

Please sign in to comment.