From a1c8005bbdbadedd6a9106a201368e4a40f7d07b Mon Sep 17 00:00:00 2001 From: Mateusz Majewski Date: Fri, 16 Apr 2021 11:52:01 +0200 Subject: [PATCH] Allow no fallback mode in dlog_redirect_stdout Change-Id: Iaeb8a4bda937e3ec696269604de45d65e4702a28 --- src/log-redirect-stdout/main.c | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/src/log-redirect-stdout/main.c b/src/log-redirect-stdout/main.c index 6dec438..197ac3d 100644 --- a/src/log-redirect-stdout/main.c +++ b/src/log-redirect-stdout/main.c @@ -40,6 +40,7 @@ struct parse_redirect_info { struct parse_info { char *cmd; char **argv; + bool fallback; struct parse_redirect_info out; struct parse_redirect_info err; }; @@ -75,6 +76,7 @@ struct parse_result parse_command_line(int argc, char **argv) .tag = {}, .prio = DLOG_DEFAULT, }; + bool fallback = true; for (;;) { int opt = getopt_long(argc, argv, ":h", (struct option[]) { @@ -84,6 +86,7 @@ struct parse_result parse_command_line(int argc, char **argv) { "errbuffer" , required_argument, NULL, 4 }, { "outtag" , required_argument, NULL, 5 }, { "errtag" , required_argument, NULL, 6 }, + { "nofallback" , no_argument, NULL, 7 }, { "help" , no_argument, NULL, 'h' }, { NULL, 0, NULL, 0 }, }, NULL); @@ -127,6 +130,10 @@ struct parse_result parse_command_line(int argc, char **argv) snprintf(err.tag, sizeof(err.tag), "%s", optarg); break; + case 7: // --nofallback + fallback = false; + break; + case 'h': return (struct parse_result) { .result = PARSE_HELP }; @@ -171,6 +178,7 @@ struct parse_result parse_command_line(int argc, char **argv) .info = { .cmd = argv[optind], .argv = argv + optind, + .fallback = fallback, .out = out, .err = err, } @@ -220,6 +228,7 @@ void print_usage(char *argv0) fprintf(stderr, "Usage: dlog_redirect_stdout [--outtag TAG [--outpriority P] [--outbuffer BUFFER]]\n" " [--errtag TAG [--errpriority P] [--errbuffer BUFFER]]\n" + " [--nofallback]\n" " -- /full/path/to/some/executable --with --args\n" "\n" "--outtag is any string identifier for any logs produced via stdout,\n" @@ -237,6 +246,8 @@ void print_usage(char *argv0) "The stderr family of options is similar, except the default\n" "priority is E and the unspecified tag is STDERR_/full/path.\n" "\n" + "--nofallback makes sure the application isn't run if redirection fails\n" + "\n" "Don't forget to put '--' between redirector args and the redirectee or\n" "the redirectee's arguments will get interpreted as the redirector's.\n" ); @@ -280,8 +291,12 @@ int main(int argc, char **argv) int r = try_redirect(&result.info); if (r < 0) { errno = -r; - fprintf(stderr, "%s: failed to redirect (%m); attempting to run the inner application anyway…\n", argv[0]); - /* We do NOT quit here; we want to redirect anyway. */ + if (result.info.fallback) + fprintf(stderr, "%s: failed to redirect (%m); attempting to run the inner application anyway…\n", argv[0]); + else { + fprintf(stderr, "%s: failed to redirect (%m)\n", argv[0]); + return EXIT_FAILURE; + } } /* We use execv instead of execvp. We first intended to use execvp because -- 2.7.4