From 7859e95434bd6892d8f183175fcac3359d769550 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Wed, 30 Nov 2011 09:06:11 +1030 Subject: [PATCH] failtest: report errors in children directly to original stderr. This is useful for debugging failtest itself, as well as for things like tracing. --- ccan/failtest/failtest.c | 37 +++++++++++++++++++++++++++++++++++-- 1 file changed, 35 insertions(+), 2 deletions(-) diff --git a/ccan/failtest/failtest.c b/ccan/failtest/failtest.c index 2ff265f1..0d52a796 100644 --- a/ccan/failtest/failtest.c +++ b/ccan/failtest/failtest.c @@ -24,6 +24,7 @@ enum failtest_result (*failtest_hook)(struct tlist_calls *); static int tracefd = -1; +static int warnfd; unsigned int failtest_timeout_ms = 20000; @@ -150,6 +151,37 @@ static char *failpath_string(void) return ret; } +static void warn_via_fd(int e, const char *fmt, va_list ap) +{ + char *p = failpath_string(); + + vdprintf(warnfd, fmt, ap); + if (e != -1) + dprintf(warnfd, ": %s", strerror(e)); + dprintf(warnfd, " [%s]\n", p); + free(p); +} + +static void fwarn(const char *fmt, ...) +{ + va_list ap; + int e = errno; + + va_start(ap, fmt); + warn_via_fd(e, fmt, ap); + va_end(ap); +} + + +static void fwarnx(const char *fmt, ...) +{ + va_list ap; + + va_start(ap, fmt); + warn_via_fd(-1, fmt, ap); + va_end(ap); +} + static void tell_parent(enum info_type type) { if (control_fd != -1) @@ -1085,12 +1117,13 @@ void failtest_init(int argc, char *argv[]) unsigned int i; orig_pid = getpid(); - + + warnfd = move_fd_to_high(dup(STDERR_FILENO)); for (i = 1; i < argc; i++) { if (!strncmp(argv[i], "--failpath=", strlen("--failpath="))) { failpath = argv[i] + strlen("--failpath="); } else if (strcmp(argv[i], "--tracepath") == 0) { - tracefd = move_fd_to_high(STDERR_FILENO); + tracefd = warnfd; failtest_timeout_ms = -1; } else if (!strncmp(argv[i], "--debugpath=", strlen("--debugpath="))) { -- 2.39.2