bt_common_abort(): optionally execute a custom command before aborting
authorPhilippe Proulx <eeppeliteloop@gmail.com>
Sat, 2 Nov 2019 02:49:59 +0000 (22:49 -0400)
committerPhilippe Proulx <eeppeliteloop@gmail.com>
Sun, 3 Nov 2019 15:34:34 +0000 (10:34 -0500)
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 <eeppeliteloop@gmail.com>
Change-Id: I78801460b316f04d805162af320ee30028dc90de
Reviewed-on: https://review.lttng.org/c/babeltrace/+/2318
Tested-by: jenkins <jenkins@lttng.org>
Reviewed-by: Simon Marchi <simon.marchi@efficios.com>
doc/man/common-common-env.txt
src/common/common.c

index 099635b7e2351cd4fcb892c3eb9719c8a5168ba6..fe1c2aa0ec8e4a1d67a818cfae791f6c54940e93 100644 (file)
@@ -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.
index 641f16fddf4f76338757ca307e9eef41b079b947..3acad47cfd88a2671293442f0f3f89894be5a967 100644 (file)
@@ -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();
 }
This page took 0.02591 seconds and 4 git commands to generate.