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 <tyhicks@canonical.com>
This commit is contained in:
Tyler Hicks 2018-05-31 17:04:20 +00:00
parent c9d341782e
commit 00800cb905

View file

@ -25,6 +25,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <sys/apparmor.h> #include <sys/apparmor.h>
#include <sys/types.h>
#include <unistd.h> #include <unistd.h>
#define _(s) gettext(s) #define _(s) gettext(s)
@ -33,6 +34,7 @@ static const char *opt_namespace = NULL;
static bool opt_debug = false; static bool opt_debug = false;
static bool opt_immediate = false; static bool opt_immediate = false;
static bool opt_verbose = false; static bool opt_verbose = false;
static pid_t pid = 0;
static void usage(const char *name, bool error) static void usage(const char *name, bool error)
{ {
@ -60,7 +62,7 @@ static void usage(const char *name, bool error)
exit(status); 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, ...) static void _error(const char *fmt, ...)
{ {
va_list args; va_list args;
@ -71,7 +73,7 @@ static void _error(const char *fmt, ...)
exit(EXIT_FAILURE); 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, ...) static void _debug(const char *fmt, ...)
{ {
va_list args; va_list args;
@ -84,7 +86,7 @@ static void _debug(const char *fmt, ...)
va_end(args); 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, ...) static void _verbose(const char *fmt, ...)
{ {
va_list args; va_list args;
@ -102,7 +104,7 @@ static void verbose_print_argv(char **argv)
if (!opt_verbose) if (!opt_verbose)
return; return;
fprintf(stderr, _("exec")); fprintf(stderr, _("[%ld] exec"), (long)pid);
for (; *argv; argv++) for (; *argv; argv++)
fprintf(stderr, " %s", *argv); fprintf(stderr, " %s", *argv);
fprintf(stderr, "\n"); fprintf(stderr, "\n");
@ -183,6 +185,11 @@ int main(int argc, char **argv)
char name[PATH_MAX]; char name[PATH_MAX];
int rc = 0; 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); argv = parse_args(argc, argv);
if (opt_namespace || opt_profile) if (opt_namespace || opt_profile)