Skip to content

Commit

Permalink
Add new --level-prefix option
Browse files Browse the repository at this point in the history
This prepends a syslog-style priority level such as <3> to each line of
diagnostic output, so that the diagnostic output can be parsed by
tools like `systemd-cat --level-prefix=1`. A future version of Steam's
pressure-vessel is likely to use this to make warnings and fatal errors
from bubblewrap more visible.

Signed-off-by: Simon McVittie <[email protected]>
  • Loading branch information
smcv committed Jul 24, 2024
1 parent 60feac9 commit db40f0b
Show file tree
Hide file tree
Showing 6 changed files with 27 additions and 0 deletions.
3 changes: 3 additions & 0 deletions bind-mount.c
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,9 @@ die_with_bind_result (bind_mount_result res,
bool want_errno = TRUE;
char *message;

if (bwrap_level_prefix)
fprintf (stderr, "<%d>", LOG_ERR);

fprintf (stderr, "bwrap: ");

va_start (args, format);
Expand Down
5 changes: 5 additions & 0 deletions bubblewrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@ usage (int ecode, FILE *out)
" --version Print version\n"
" --args FD Parse NUL-separated args from FD\n"
" --argv0 VALUE Set argv[0] to the value VALUE before running the program\n"
" --level-prefix Prepend e.g. <3> to diagnostic messages\n"
" --unshare-all Unshare every namespace we support by default\n"
" --share-net Retain the network namespace (can only combine with --unshare-all)\n"
" --unshare-user Create new user namespace (may be automatically implied if not setuid)\n"
Expand Down Expand Up @@ -1752,6 +1753,10 @@ parse_args_recurse (int *argcp,
argv++;
argc--;
}
else if (strcmp (arg, "--level-prefix") == 0)
{
bwrap_level_prefix = TRUE;
}
else if (strcmp (arg, "--unshare-all") == 0)
{
/* Keep this in order with the older (legacy) --unshare arguments,
Expand Down
9 changes: 9 additions & 0 deletions bwrap.xml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@
<term><option>--argv0 <arg choice="plain">VALUE</arg></option></term>
<listitem><para>Set argv[0] to the value <arg choice="plain">VALUE</arg> before running the program</para></listitem>
</varlistentry>
<varlistentry>
<term><option>--level-prefix</option></term>
<listitem>
<para>Add a syslog-style level prefix to each line of diagnostic
output, for example <literal>&lt;4&gt;</literal> for a warning.
These prefixes can be parsed by tools such as
<literal>systemd-cat --level-prefix=1</literal>.</para>
</listitem>
</varlistentry>
</variablelist>
<para>Options related to kernel namespaces:</para>
<variablelist>
Expand Down
3 changes: 3 additions & 0 deletions tests/test-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -568,4 +568,7 @@ ok "argv0 manipulation"
$RUN --chdir / --chdir / true > stdout 2>&1
assert_file_has_content stdout '^bwrap: Only the last --chdir option will take effect$'

$RUN --level-prefix --chdir / --chdir / true > stdout 2>&1
assert_file_has_content stdout '^<4>bwrap: Only the last --chdir option will take effect$'

done_testing
5 changes: 5 additions & 0 deletions utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,17 @@
#define security_check_context(x) security_check_context ((security_context_t) x)
#endif

bool bwrap_level_prefix = FALSE;

__attribute__((format(printf, 2, 0))) static void
bwrap_logv (int level,
const char *format,
va_list args,
const char *detail)
{
if (bwrap_level_prefix)
fprintf (stderr, "<%d>", level);

fprintf (stderr, "bwrap: ");
vfprintf (stderr, format, args);

Expand Down
2 changes: 2 additions & 0 deletions utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ typedef int bool;
#define PR_SET_CHILD_SUBREAPER 36
#endif

extern bool bwrap_level_prefix;

void bwrap_log (int level,
const char *format,
...) __attribute__((format (printf, 2, 3)));
Expand Down

0 comments on commit db40f0b

Please sign in to comment.