Skip to content

Commit

Permalink
Add --log-stderr option
Browse files Browse the repository at this point in the history
This option was inherited before, but now we can set it explicitly to
allow/disallow logging to stderr when selecting a certain log driver.

For example on exec, we would need to log log anything to stdio even
when the debug logs are enabled.

Signed-off-by: Sascha Grunert <[email protected]>
  • Loading branch information
saschagrunert committed Sep 3, 2024
1 parent 9bc06d0 commit 54d181f
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 5 deletions.
5 changes: 5 additions & 0 deletions crun.1
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,11 @@ Define the format of the log messages. It can either be \fBtext\fP, or
Define the log level. It can either be \fBdebug\fP, \fBwarning\fP or \fBerror\fP\&.
The default is \fBerror\fP\&.

.PP
\fB--log-stderr\fP
Aditionally log to stderr. Especially helpful when \fB--log\fR is set to a certain
destination.

.PP
\fB--no-pivot\fP
Use \fBchroot(2)\fR instead of \fBpivot_root(2)\fR when creating the container.
Expand Down
4 changes: 4 additions & 0 deletions crun.1.md
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ Define the format of the log messages. It can either be **text**, or
Define the log level. It can either be **debug**, **warning** or **error**.
The default is **error**.

**--log-stderr**
Aditionally log to stderr. Especially helpful when `--log` is set to a certain
destination.

**--no-pivot**
Use `chroot(2)` instead of `pivot_root(2)` when creating the container.
This option is not safe, and should be avoided.
Expand Down
10 changes: 8 additions & 2 deletions src/crun.c
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ init_libcrun_context (libcrun_context_t *con, const char *id, struct crun_global
/* Check if global handler is configured and pass it down to crun context */
con->handler = glob->handler;

ret = libcrun_init_logging (&con->output_handler, &con->output_handler_arg, id, glob->log, err);
ret = libcrun_init_logging (&con->output_handler, &con->output_handler_arg, id, glob->log, err, glob->log_to_stderr);
if (UNLIKELY (ret < 0))
return ret;

Expand All @@ -116,7 +116,7 @@ init_libcrun_context (libcrun_context_t *con, const char *id, struct crun_global
return ret;
}

libcrun_set_verbosity (arguments.verbosity);
libcrun_set_verbosity (glob->verbosity);
libcrun_debug ("Using debug verbosity");

if (con->bundle == NULL)
Expand Down Expand Up @@ -213,6 +213,7 @@ enum
OPTION_LOG,
OPTION_LOG_FORMAT,
OPTION_LOG_LEVEL,
OPTION_LOG_STDERR,
OPTION_ROOT,
OPTION_ROOTLESS
};
Expand All @@ -225,6 +226,7 @@ static struct argp_option options[] = { { "debug", OPTION_DEBUG, 0, 0, "produce
{ "log", OPTION_LOG, "FILE", 0, "log destination: 'file:PATH' (default), 'journald:ID' or 'syslog:ID'", 0 },
{ "log-format", OPTION_LOG_FORMAT, "FORMAT", 0, "log format: 'text' (default) or 'json'", 0 },
{ "log-level", OPTION_LOG_LEVEL, "LEVEL", 0, "log level to use: 'error' (default), 'warning' or 'debug'", 0 },
{ "log-stderr", OPTION_LOG_STDERR, 0, 0, "additionally log to stderr", 0 },
{ "root", OPTION_ROOT, "DIR", 0, NULL, 0 },
{ "rootless", OPTION_ROOT, "VALUE", 0, NULL, 0 },
{ "version", OPTION_VERSION, 0, 0, NULL, 0 },
Expand Down Expand Up @@ -332,6 +334,10 @@ parse_opt (int key, char *arg, struct argp_state *state)
}
break;

case OPTION_LOG_STDERR:
arguments.log_to_stderr = true;
break;

case OPTION_ROOT:
arguments.root = argp_mandatory_argument (arg, state);
break;
Expand Down
1 change: 1 addition & 0 deletions src/crun.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ struct crun_global_arguments
bool command;
bool option_systemd_cgroup;
bool option_force_no_cgroup;
bool log_to_stderr;
};

char *argp_mandatory_argument (char *arg, struct argp_state *state);
Expand Down
5 changes: 3 additions & 2 deletions src/libcrun/error.c
Original file line number Diff line number Diff line change
Expand Up @@ -203,12 +203,13 @@ get_log_type (const char *log, const char **data)

int
libcrun_init_logging (crun_output_handler *new_output_handler, void **new_output_handler_arg, const char *id,
const char *log, libcrun_error_t *err)
const char *log, libcrun_error_t *err, bool log_to_stderr)
{
if (log == NULL)
{
*new_output_handler = log_write_to_stderr;
*new_output_handler_arg = NULL;
log_to_stderr = false; // Don't log twice to stderr
}
else
{
Expand Down Expand Up @@ -240,7 +241,7 @@ libcrun_init_logging (crun_output_handler *new_output_handler, void **new_output
break;
}
}
crun_set_output_handler (*new_output_handler, *new_output_handler_arg, log != NULL);
crun_set_output_handler (*new_output_handler, *new_output_handler_arg, log_to_stderr);
return 0;
}

Expand Down
2 changes: 1 addition & 1 deletion src/libcrun/error.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ LIBCRUN_PUBLIC void libcrun_fail_with_error (int errno_, const char *msg, ...) _
LIBCRUN_PUBLIC int libcrun_set_log_format (const char *format, libcrun_error_t *err);

LIBCRUN_PUBLIC int libcrun_init_logging (crun_output_handler *output_handler, void **output_handler_arg, const char *id,
const char *log, libcrun_error_t *err);
const char *log, libcrun_error_t *err, bool log_to_stderr);

LIBCRUN_PUBLIC int libcrun_error_release (libcrun_error_t *err);

Expand Down

0 comments on commit 54d181f

Please sign in to comment.