diff options
author | Rusty Russell <rusty@rustcorp.com.au> | 2010-06-21 15:02:05 +0930 |
---|---|---|
committer | Rusty Russell <rusty@rustcorp.com.au> | 2010-06-21 15:02:05 +0930 |
commit | fee11bf13be72de71d83912dfc12e61c5e4ecbbf (patch) | |
tree | d269431ada2f5e176fd3b972589baca602c181ad /ctdb/libctdb | |
parent | 4c0f3dcffefa8a527dad01ba054523c515b03caa (diff) | |
download | samba-fee11bf13be72de71d83912dfc12e61c5e4ecbbf.tar.gz samba-fee11bf13be72de71d83912dfc12e61c5e4ecbbf.tar.xz samba-fee11bf13be72de71d83912dfc12e61c5e4ecbbf.zip |
libctdb: test: logging enhancement
Make children log through a pipe to the parent, which then spits it out
only if the child has a problem.
Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
(This used to be ctdb commit 8ac006cf6c6cbfd3fe1606178eb0f0127d33f632)
Diffstat (limited to 'ctdb/libctdb')
-rw-r--r-- | ctdb/libctdb/test/failtest.c | 39 | ||||
-rw-r--r-- | ctdb/libctdb/test/log.c | 14 | ||||
-rw-r--r-- | ctdb/libctdb/test/log.h | 1 | ||||
-rw-r--r-- | ctdb/libctdb/test/utils.h | 1 |
4 files changed, 35 insertions, 20 deletions
diff --git a/ctdb/libctdb/test/failtest.c b/ctdb/libctdb/test/failtest.c index 2560e3e5eba..d42ab057094 100644 --- a/ctdb/libctdb/test/failtest.c +++ b/ctdb/libctdb/test/failtest.c @@ -22,6 +22,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "tui.h" #include "log.h" #include "failtest.h" +#include "ctdb-test.h" #include "talloc.h" #include "dlinklist.h" #include <stdlib.h> @@ -229,8 +230,10 @@ bool should_i_fail_once(const char *location) bool should_i_fail(const char *func) { pid_t child; - int status; + int status, pfd[2]; struct fail_decision *dec; + size_t log_size; + char *log; if (failpath) return do_failpath(func); @@ -253,6 +256,9 @@ bool should_i_fail(const char *func) DLIST_ADD_END(decisions, dec, struct fail_decision); + if (pipe(pfd) != 0) + err(1, "pipe failed for failtest!"); + fflush(stdout); child = fork(); if (child == -1) @@ -261,6 +267,10 @@ bool should_i_fail(const char *func) /* The child actually fails. The script will screw up at this * point, but should not crash. */ if (child == 0) { + /* Log to parent (including stderr if things go really bad). */ + close(pfd[0]); + dup2(pfd[1], STDOUT_FILENO); + dup2(pfd[1], STDERR_FILENO); dec->failed = true; if (!failtest_no_report || !strstr(func, failtest_no_report)) fails++; @@ -268,6 +278,10 @@ bool should_i_fail(const char *func) } dec->failed = false; + + close(pfd[1]); + log = grab_fd(pfd[0], &log_size); + while (waitpid(child, &status, 0) < 0) { if (errno != EINTR) err(1, "failtest waitpid failed for child %i", @@ -276,19 +290,20 @@ bool should_i_fail(const char *func) /* If child succeeded, or mere script failure, continue. */ if (WIFEXITED(status) && (WEXITSTATUS(status) == EXIT_SUCCESS - || WEXITSTATUS(status) == EXIT_SCRIPTFAIL)) + || WEXITSTATUS(status) == EXIT_SCRIPTFAIL)) { + talloc_free(log); return false; - - /* Report unless child already reported it. */ - if (!WIFEXITED(status) || WEXITSTATUS(status) != EXIT_SILENT) { - /* Reproduce child's path */ - dec->failed = true; - log_line(LOG_ALWAYS, "Child %s %i on failure path: %s", - WIFEXITED(status) ? "exited" : "signalled", - WIFEXITED(status) ? WEXITSTATUS(status) - : WTERMSIG(status), failpath_string()); } - exit(EXIT_SILENT); + + /* Reproduce child's path */ + dec->failed = true; + + log_line(LOG_ALWAYS, "Child %s %i on failure path: %s", + WIFEXITED(status) ? "exited" : "signalled", + WIFEXITED(status) ? WEXITSTATUS(status) + : WTERMSIG(status), failpath_string()); + log_line(LOG_ALWAYS, "Child output:\n%s", log); + exit(EXIT_FAILURE); } void dump_failinfo(void) diff --git a/ctdb/libctdb/test/log.c b/ctdb/libctdb/test/log.c index a4a9be62b85..c8293be8f6f 100644 --- a/ctdb/libctdb/test/log.c +++ b/ctdb/libctdb/test/log.c @@ -26,6 +26,7 @@ Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA #include "expect.h" #include <string.h> #include <talloc.h> +#include <err.h> static struct { enum log_type type; @@ -37,7 +38,6 @@ static struct { { LOG_VERBOSE, "verbose" }, }; -static FILE *logstream; static int typemask = ~LOG_VERBOSE; bool log_line(enum log_type type, const char *format, ...) @@ -50,8 +50,10 @@ bool log_line(enum log_type type, const char *format, ...) line = talloc_vasprintf(NULL, format, ap); va_end(ap); - if (!type || (type & typemask)) - fprintf(logstream ?: stderr, "%s\n", line); + if (!type || (type & typemask)) { + printf("%s\n", line); + fflush(stdout); + } ret = expect_log_hook(line); talloc_free(line); @@ -69,8 +71,9 @@ static void log_partial_v(enum log_type type, /* write to the end of buffer */ if (vsnprintf(buf + len, bufsize - len - 1, format, ap) - > bufsize - len - 1) - log_line(LOG_ALWAYS, "log_line_partial buffer is full!"); + > bufsize - len - 1) { + errx(1, "log_line_partial buffer is full!"); + } ptr = buf; @@ -222,7 +225,6 @@ static void log_admin_help(int agc, char **argv) static void log_init(void) { - logstream = stdout; if (tui_quiet) typemask = 0; tui_register_command("log", log_admin, log_admin_help); diff --git a/ctdb/libctdb/test/log.h b/ctdb/libctdb/test/log.h index d19d6b2f5c0..068dd55543b 100644 --- a/ctdb/libctdb/test/log.h +++ b/ctdb/libctdb/test/log.h @@ -44,5 +44,4 @@ bool log_line(enum log_type type, const char *format, ...); /* Builds up buffer and prints out line at a time. */ void log_partial(enum log_type type, char *buf, unsigned bufsize, const char *format, ...); - #endif /* __HAVE_LOG_H */ diff --git a/ctdb/libctdb/test/utils.h b/ctdb/libctdb/test/utils.h index eb849135895..c8ba7cee739 100644 --- a/ctdb/libctdb/test/utils.h +++ b/ctdb/libctdb/test/utils.h @@ -56,7 +56,6 @@ static inline bool strends(const char *a, const char *b) enum exitcodes { /* EXIT_SUCCESS, EXIT_FAILURE is in stdlib.h */ EXIT_SCRIPTFAIL = EXIT_FAILURE + 1, - EXIT_SILENT, }; /* init code */ |