From 00800cb9054bbd60e18827393efa354ed3bcbdbc Mon Sep 17 00:00:00 2001 From: Tyler Hicks Date: Thu, 31 May 2018 17:04:20 +0000 Subject: [PATCH] aa-exec: Prepend [PID] to all error, debug, and verbose messages Add the pid of the process to the error, debug, and verbose output. This is useful for debugging. For example, $ aa-exec -v -- whoami [30389] exec whoami tyhicks $ aa-exec -p dne -- true [30390] aa-exec: ERROR: profile 'dne' does not exist $ aa-exec -d -p unconfined -- true [30409] aa-exec: DEBUG: 0 = aa_change_onexec("unconfined") It can also help when multiple aa-exec invocations are used across a fork and exec. Here's a contrived example: $ aa-exec -v -- \ sh -c 'aa-exec -vp /usr/sbin/tcpdump -- aa-exec -p unconfined -- true' [3424] exec sh -c aa-exec -vp /usr/sbin/tcpdump -- aa-exec -p unconfined -- true [3425] aa_change_onexec("/usr/sbin/tcpdump") [3425] exec aa-exec -p unconfined -- true [3425] aa-exec: ERROR: profile 'unconfined' does not exist Signed-off-by: Tyler Hicks --- binutils/aa_exec.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/binutils/aa_exec.c b/binutils/aa_exec.c index 7e73f45f3..f8fe43a3d 100644 --- a/binutils/aa_exec.c +++ b/binutils/aa_exec.c @@ -25,6 +25,7 @@ #include #include #include +#include #include #define _(s) gettext(s) @@ -33,6 +34,7 @@ static const char *opt_namespace = NULL; static bool opt_debug = false; static bool opt_immediate = false; static bool opt_verbose = false; +static pid_t pid = 0; static void usage(const char *name, bool error) { @@ -60,7 +62,7 @@ static void usage(const char *name, bool error) exit(status); } -#define error(fmt, args...) _error(_("aa-exec: ERROR: " fmt "\n"), ## args) +#define error(fmt, args...) _error(_("[%ld] aa-exec: ERROR: " fmt "\n"), (long)pid, ## args) static void _error(const char *fmt, ...) { va_list args; @@ -71,7 +73,7 @@ static void _error(const char *fmt, ...) exit(EXIT_FAILURE); } -#define debug(fmt, args...) _debug(_("aa-exec: DEBUG: " fmt "\n"), ## args) +#define debug(fmt, args...) _debug(_("[%ld] aa-exec: DEBUG: " fmt "\n"), (long)pid, ## args) static void _debug(const char *fmt, ...) { va_list args; @@ -84,7 +86,7 @@ static void _debug(const char *fmt, ...) va_end(args); } -#define verbose(fmt, args...) _verbose(_(fmt "\n"), ## args) +#define verbose(fmt, args...) _verbose(_("[%ld] " fmt "\n"), (long)pid, ## args) static void _verbose(const char *fmt, ...) { va_list args; @@ -102,7 +104,7 @@ static void verbose_print_argv(char **argv) if (!opt_verbose) return; - fprintf(stderr, _("exec")); + fprintf(stderr, _("[%ld] exec"), (long)pid); for (; *argv; argv++) fprintf(stderr, " %s", *argv); fprintf(stderr, "\n"); @@ -183,6 +185,11 @@ int main(int argc, char **argv) char name[PATH_MAX]; int rc = 0; + /* IMPORTANT: pid must be initialized before doing anything else since + * it is used in a global context when printing messages + */ + pid = getpid(); + argv = parse_args(argc, argv); if (opt_namespace || opt_profile)