From d2f49d4563244f4a34d132829e1a2740a5cb6404 Mon Sep 17 00:00:00 2001 From: Kir Kolyshkin Date: Tue, 27 Jul 2021 17:16:37 -0700 Subject: [PATCH] libct/nsenter/nsexec.c: improve bail This makes it possible to use bail() even if logging is not set up (yet), so we don't have to think whether it's OK to use it or not. In addition, this might help some unit tests that do not set log forwarding. Signed-off-by: Kir Kolyshkin --- libcontainer/nsenter/nsexec.c | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/libcontainer/nsenter/nsexec.c b/libcontainer/nsenter/nsexec.c index 30b6d5e4a..554ffd6c2 100644 --- a/libcontainer/nsenter/nsexec.c +++ b/libcontainer/nsenter/nsexec.c @@ -182,10 +182,14 @@ out: /* XXX: This is ugly. */ static int syncfd = -1; -#define bail(fmt, ...) \ - do { \ - write_log(FATAL, fmt ": %m", ##__VA_ARGS__); \ - exit(1); \ +#define bail(fmt, ...) \ + do { \ + if (logfd < 0) \ + fprintf(stderr, "FATAL: " fmt ": %m\n", \ + ##__VA_ARGS__); \ + else \ + write_log(FATAL, fmt ": %m", ##__VA_ARGS__); \ + exit(1); \ } while(0) static int write_file(char *data, size_t data_len, char *pathfmt, ...) @@ -407,9 +411,7 @@ static void setup_logpipe(void) logfd = strtol(logpipe, &endptr, 10); if (logpipe == endptr || *endptr != '\0') { - fprintf(stderr, "unable to parse _LIBCONTAINER_LOGPIPE, value: %s\n", logpipe); - /* It is too early to use bail */ - exit(1); + bail("unable to parse _LIBCONTAINER_LOGPIPE, value: %s", logpipe); } }