From 9bbd8e06acdcc06e6a6b22670c79a4da3e793f70 Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Wed, 18 May 2016 14:04:17 -0400 Subject: [PATCH] Add environment variable to allow abort on error MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit The new environment variable LTTNG_ABORT_ON_ERROR allows each lttng-tools program to call abort() on PERROR() and ERR() after the error message has been printed to stderr. Signed-off-by: Mathieu Desnoyers Signed-off-by: Jérémie Galarneau --- doc/man/common-cmd-footer.txt | 3 +++ doc/man/lttng-relayd.8.txt | 3 +++ doc/man/lttng-sessiond.8.txt | 3 +++ src/common/error.c | 28 ++++++++++++++++++++++++++++ src/common/error.h | 5 +++++ 5 files changed, 42 insertions(+) diff --git a/doc/man/common-cmd-footer.txt b/doc/man/common-cmd-footer.txt index d776cc4a4..6d16c0351 100644 --- a/doc/man/common-cmd-footer.txt +++ b/doc/man/common-cmd-footer.txt @@ -1,5 +1,8 @@ ENVIRONMENT VARIABLES --------------------- +`LTTNG_ABORT_ON_ERROR`:: + Set to 1 to abort the process after the first error is encountered. + `LTTNG_HOME`:: Overrides the `$HOME` environment variable. Useful when the user running the commands has a non-writable home directory. diff --git a/doc/man/lttng-relayd.8.txt b/doc/man/lttng-relayd.8.txt index d667be178..4326daba3 100644 --- a/doc/man/lttng-relayd.8.txt +++ b/doc/man/lttng-relayd.8.txt @@ -152,6 +152,9 @@ option:-V, option:--version:: ENVIRONMENT VARIABLES --------------------- +`LTTNG_ABORT_ON_ERROR`:: + Set to 1 to abort the process after the first error is encountered. + `LTTNG_NETWORK_SOCKET_TIMEOUT`:: Socket connection, receive and send timeout (milliseconds). A value of 0 or -1 uses the timeout of the operating system (default). diff --git a/doc/man/lttng-sessiond.8.txt b/doc/man/lttng-sessiond.8.txt index f4348d85b..d57f5fe1c 100644 --- a/doc/man/lttng-sessiond.8.txt +++ b/doc/man/lttng-sessiond.8.txt @@ -224,6 +224,9 @@ ENVIRONMENT VARIABLES Note that command-line options override their equivalent environment variable. +`LTTNG_ABORT_ON_ERROR`:: + Set to 1 to abort the process after the first error is encountered. + `LTTNG_APP_SOCKET_TIMEOUT`:: Application socket's timeout (seconds) when sending/receiving commands. After this period of time, the application is unregistered diff --git a/src/common/error.c b/src/common/error.c index f7e11e163..84bc04fac 100644 --- a/src/common/error.c +++ b/src/common/error.c @@ -18,14 +18,23 @@ #define _LGPL_SOURCE #include #include +#include +#include #include #include +#include #include "error.h" #define ERROR_INDEX(code) (code - LTTNG_OK) +/* + * lttng_opt_abort_on_error: unset: -1, disabled: 0, enabled: 1. + * Controlled by the LTTNG_ABORT_ON_ERROR environment variable. + */ +static int lttng_opt_abort_on_error = -1; + /* TLS variable that contains the time of one single log entry. */ DEFINE_URCU_TLS(struct log_time, error_log_time); @@ -196,3 +205,22 @@ const char *error_get_str(int32_t code) return error_string_array[ERROR_INDEX(code)]; } + +LTTNG_HIDDEN +void lttng_abort_on_error(void) +{ + if (lttng_opt_abort_on_error < 0) { + /* Use lttng_secure_getenv() to query its state. */ + const char *value; + + value = lttng_secure_getenv("LTTNG_ABORT_ON_ERROR"); + if (value && !strcmp(value, "1")) { + lttng_opt_abort_on_error = 1; + } else { + lttng_opt_abort_on_error = 0; + } + } + if (lttng_opt_abort_on_error > 0) { + abort(); + } +} diff --git a/src/common/error.h b/src/common/error.h index 9c9d2a7b1..6c239fe56 100644 --- a/src/common/error.h +++ b/src/common/error.h @@ -89,6 +89,9 @@ extern int lttng_opt_mi; ((type) & (PRINT_WARN | PRINT_ERR | PRINT_BUG))) { \ fprintf(stderr, fmt, ## args); \ } \ + if ((type) & (PRINT_ERR | PRINT_BUG)) { \ + lttng_abort_on_error(); \ + } \ } while (0); /* Three level of debug. Use -v, -vv or -vvv for the levels */ @@ -174,4 +177,6 @@ const char *error_get_str(int32_t code); */ const char *log_add_time(); +void lttng_abort_on_error(void); + #endif /* _ERROR_H */ -- 2.34.1