From 59b3e31ded241ad159dbc618cc84a039f34f6e19 Mon Sep 17 00:00:00 2001 From: Philippe Proulx Date: Fri, 1 Nov 2019 22:49:59 -0400 Subject: [PATCH] bt_common_abort(): optionally execute a custom command before aborting This patch makes bt_common_abort() execute the value of the `BABELTRACE_EXEC_ON_ABORT` environment variable, if it's set, as a shell command line if the setuid/setgid flags are NOT set (for security). The function uses g_spawn_command_line_sync() which claims to parse the command line string like a UNIX 98 shell would, so that's what I wrote in the environment variable's description in the manual page. You can use this to execute any command when any part of the Babeltrace project would abort, for example report it to some system. Signed-off-by: Philippe Proulx Change-Id: I78801460b316f04d805162af320ee30028dc90de Reviewed-on: https://review.lttng.org/c/babeltrace/+/2318 Tested-by: jenkins Reviewed-by: Simon Marchi --- doc/man/common-common-env.txt | 10 ++++++++++ src/common/common.c | 15 +++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/doc/man/common-common-env.txt b/doc/man/common-common-env.txt index 099635b7..fe1c2aa0 100644 --- a/doc/man/common-common-env.txt +++ b/doc/man/common-common-env.txt @@ -1,3 +1,13 @@ +`BABELTRACE_EXEC_ON_ABORT`='CMDLINE':: + Execute the command line 'CMDLINE', as parsed like a UNIX~98 shell, + when any part of the Babeltrace~2 project unexpectedly aborts. ++ +The application only aborts when the executed command returns, ignoring +its exit status. ++ +This environment variable is ignored when the application has the +`setuid` or the `setgid` access right flag set. + `BABELTRACE_TERM_COLOR`=(`AUTO` | `NEVER` | `ALWAYS`):: Force the terminal color support for the man:babeltrace2(1) program and the project's plugins. diff --git a/src/common/common.c b/src/common/common.c index 641f16fd..3acad47c 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -1905,5 +1905,20 @@ end: BT_HIDDEN void bt_common_abort(void) { + static const char * const exec_on_abort_env_name = + "BABELTRACE_EXEC_ON_ABORT"; + const char *env_exec_on_abort; + + env_exec_on_abort = getenv(exec_on_abort_env_name); + if (env_exec_on_abort) { + if (bt_common_is_setuid_setgid()) { + goto do_abort; + } + + (void) g_spawn_command_line_sync(env_exec_on_abort, + NULL, NULL, NULL, NULL); + } + +do_abort: abort(); } -- 2.34.1