From b98e0ddd2237391df44a92c894b3203aadcce188 Mon Sep 17 00:00:00 2001 From: Sascha Grunert Date: Tue, 3 Sep 2024 10:28:19 +0200 Subject: [PATCH] Add `--log-stderr` option 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 --- crun.1 | 5 +++++ crun.1.md | 4 ++++ src/crun.c | 10 ++++++++-- src/crun.h | 1 + src/libcrun/error.c | 5 +++-- src/libcrun/error.h | 2 +- 6 files changed, 22 insertions(+), 5 deletions(-) diff --git a/crun.1 b/crun.1 index acad3d48e..57ea8e16e 100644 --- a/crun.1 +++ b/crun.1 @@ -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 +Additionally 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. diff --git a/crun.1.md b/crun.1.md index 72f7f2dbe..7bb195e9e 100644 --- a/crun.1.md +++ b/crun.1.md @@ -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** +Additionally 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. diff --git a/src/crun.c b/src/crun.c index 8a98a567e..458a2ed5b 100644 --- a/src/crun.c +++ b/src/crun.c @@ -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; @@ -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) @@ -213,6 +213,7 @@ enum OPTION_LOG, OPTION_LOG_FORMAT, OPTION_LOG_LEVEL, + OPTION_LOG_STDERR, OPTION_ROOT, OPTION_ROOTLESS }; @@ -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 }, @@ -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; diff --git a/src/crun.h b/src/crun.h index 9cbc70809..038f08798 100644 --- a/src/crun.h +++ b/src/crun.h @@ -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); diff --git a/src/libcrun/error.c b/src/libcrun/error.c index 388057166..4263ad46e 100644 --- a/src/libcrun/error.c +++ b/src/libcrun/error.c @@ -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 { @@ -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; } diff --git a/src/libcrun/error.h b/src/libcrun/error.h index e189324aa..d5940bcf9 100644 --- a/src/libcrun/error.h +++ b/src/libcrun/error.h @@ -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);