From 5d9628475e81b15f3196a69ab4b3288466cd168f Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Thu, 25 Jul 2013 14:32:09 +0200 Subject: [PATCH] libdm: add DM_ABORT_ON_INTERNAL_ERRORS Support tests with abort when libdm encounters internal error - i.e. for dmsetup tool. Code execution will be aborted when env var DM_ABORT_ON_INTERNAL_ERRORS is set to 1 --- WHATS_NEW_DM | 1 + libdm/libdevmapper.h | 1 + libdm/libdm-common.c | 26 +++++++++++++++----------- test/lib/test.sh | 5 ++++- 4 files changed, 21 insertions(+), 12 deletions(-) diff --git a/WHATS_NEW_DM b/WHATS_NEW_DM index cd77bc672..ffee9a30d 100644 --- a/WHATS_NEW_DM +++ b/WHATS_NEW_DM @@ -1,5 +1,6 @@ Version 1.02.79 - ================================ + Add DM_ABORT_ON_INTERNAL_ERRORS env var support to abort on internal errors. Version 1.02.78 - 24th July 2013 ================================ diff --git a/libdm/libdevmapper.h b/libdm/libdevmapper.h index 0c415c674..0b03eafb4 100644 --- a/libdm/libdevmapper.h +++ b/libdm/libdevmapper.h @@ -57,6 +57,7 @@ extern "C" { * Use dm_log_with_errno_init(NULL) to restore the default log fn. * Error messages may have a non-zero errno. * Debug messages may have a non-zero class. + * Aborts on internal error when env DM_ABORT_ON_INTERNAL_ERRORS is 1 */ typedef void (*dm_log_with_errno_fn) (int level, const char *file, int line, diff --git a/libdm/libdm-common.c b/libdm/libdm-common.c index 8d5eca884..de4b747ee 100644 --- a/libdm/libdm-common.c +++ b/libdm/libdm-common.c @@ -114,22 +114,26 @@ static void _default_log_line(int level, int line __attribute__((unused)), int dm_errno_or_class, const char *f, va_list ap) { - int use_stderr = level & _LOG_STDERR; + static int _abort_on_internal_errors = -1; + FILE *out = (level & _LOG_STDERR) ? stderr : stdout; level &= ~_LOG_STDERR; - if (level > _LOG_WARN && !_verbose) - return; + if (level <= _LOG_WARN || _verbose) { + if (level < _LOG_WARN) + out = stderr; + vfprintf(out, f, ap); + fputc('\n', out); + } - if (level < _LOG_WARN) - vfprintf(stderr, f, ap); - else - vfprintf(use_stderr ? stderr : stdout, f, ap); + if (_abort_on_internal_errors < 0) + /* Set when env DM_ABORT_ON_INTERNAL_ERRORS is 1 */ + _abort_on_internal_errors = + !strcmp(getenv("DM_ABORT_ON_INTERNAL_ERRORS") ? : "0", "1"); - if (level < _LOG_WARN) - fprintf(stderr, "\n"); - else - fprintf(use_stderr ? stderr : stdout, "\n"); + if (_abort_on_internal_errors && + !strncmp(f, INTERNAL_ERROR, sizeof(INTERNAL_ERROR) - 1)) + abort(); } __attribute__((format(printf, 5, 6))) diff --git a/test/lib/test.sh b/test/lib/test.sh index 7e0df73e7..20da059e1 100644 --- a/test/lib/test.sh +++ b/test/lib/test.sh @@ -50,7 +50,10 @@ else mkdir "$DM_DEV_DIR/mapper" fi -export DM_DEV_DIR LVM_SYSTEM_DIR +# abort on the internal dm errors in the tests (allowing test user override) +DM_ABORT_ON_INTERNAL_ERRORS=${DM_ABORT_ON_INTERNAL_ERRORS:-1} + +export DM_DEV_DIR LVM_SYSTEM_DIR DM_ABORT_ON_INTERNAL_ERRORS cd "$TESTDIR"