From 1955c18a18b2822b35b3ab37bb868fff05f33fa6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Zbigniew=20J=C4=99drzejewski-Szmek?= Date: Tue, 12 May 2026 14:37:35 +0200 Subject: [PATCH] journalctl: move handling of --smart-relinquish-var to action logic The help string for --smart-relinquish-var and --relinquish-var were in reversed order because of the _fallthrough_. We would resolve the conditions for "smart relinquish" immediately in parse_argv() and call 'return 0' if the conditions were wrong, terminating option parsing and the program. It seems nicer to delay action until later. This makes the logic flow more standard. This also allows the option parsing cases to be exchanged, fixing the issue with --help. --- src/journal/journalctl-varlink.c | 2 +- src/journal/journalctl.c | 56 +++++++++++++++++--------------- src/journal/journalctl.h | 1 + 3 files changed, 32 insertions(+), 27 deletions(-) diff --git a/src/journal/journalctl-varlink.c b/src/journal/journalctl-varlink.c index c278bc3365..a38c320a4f 100644 --- a/src/journal/journalctl-varlink.c +++ b/src/journal/journalctl-varlink.c @@ -65,7 +65,7 @@ int action_relinquish_var(void) { _cleanup_(sd_varlink_flush_close_unrefp) sd_varlink *link = NULL; int r; - assert(arg_action == ACTION_RELINQUISH_VAR); + assert(IN_SET(arg_action, ACTION_RELINQUISH_VAR, ACTION_SMART_RELINQUISH_VAR)); if (arg_machine || arg_namespace) return log_error_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), diff --git a/src/journal/journalctl.c b/src/journal/journalctl.c index a48e28497d..c5009eb6f0 100644 --- a/src/journal/journalctl.c +++ b/src/journal/journalctl.c @@ -835,36 +835,15 @@ static int parse_argv(int argc, char *argv[], char ***remaining_args) { arg_action = ACTION_SYNC; break; - OPTION_LONG("smart-relinquish-var", NULL, - "Similar, but NOP if log directory is on root mount"): { - int root_mnt_id, log_mnt_id; - - /* Try to be smart about relinquishing access to /var/log/journal/ during shutdown: - * if it's on the same mount as the root file system there's no point in - * relinquishing access and we can leave journald write to it until the very last - * moment. */ - - r = path_get_mnt_id("/", &root_mnt_id); - if (r < 0) - log_debug_errno(r, "Failed to get root mount ID, ignoring: %m"); - else { - r = path_get_mnt_id("/var/log/journal/", &log_mnt_id); - if (r < 0) - log_debug_errno(r, "Failed to get journal directory mount ID, ignoring: %m"); - else if (root_mnt_id == log_mnt_id) { - log_debug("/var/log/journal/ is on root file system, not relinquishing access to /var."); - return 0; - } else - log_debug("/var/log/journal/ is not on the root file system, relinquishing access to it."); - } - - _fallthrough_; - } - OPTION_LONG("relinquish-var", NULL, "Stop logging to disk, log to temporary file system"): arg_action = ACTION_RELINQUISH_VAR; break; + OPTION_LONG("smart-relinquish-var", NULL, + "Similar, but NOP if log directory is on root mount"): + arg_action = ACTION_SMART_RELINQUISH_VAR; + break; + OPTION_LONG("flush", NULL, "Flush all journal data from /run into /var"): arg_action = ACTION_FLUSH; break; @@ -1075,6 +1054,31 @@ static int run(int argc, char *argv[]) { case ACTION_FLUSH: return action_flush_to_var(); + case ACTION_SMART_RELINQUISH_VAR: { + int root_mnt_id, log_mnt_id; + + /* Try to be smart about relinquishing access to /var/log/journal/ during shutdown: + * if it's on the same mount as the root file system there's no point in + * relinquishing access and we can leave journald write to it until the very last + * moment. */ + + r = path_get_mnt_id("/", &root_mnt_id); + if (r < 0) + log_debug_errno(r, "Failed to get root mount ID, ignoring: %m"); + else { + r = path_get_mnt_id("/var/log/journal/", &log_mnt_id); + if (r < 0) + log_debug_errno(r, "Failed to get journal directory mount ID, ignoring: %m"); + else if (root_mnt_id == log_mnt_id) { + log_debug("/var/log/journal/ is on root file system, not relinquishing access to /var."); + return 0; + } else + log_debug("/var/log/journal/ is not on the root file system, relinquishing access to it."); + } + + _fallthrough_; + } + case ACTION_RELINQUISH_VAR: return action_relinquish_var(); diff --git a/src/journal/journalctl.h b/src/journal/journalctl.h index 090e338249..406a16077b 100644 --- a/src/journal/journalctl.h +++ b/src/journal/journalctl.h @@ -21,6 +21,7 @@ typedef enum JournalctlAction { ACTION_LIST_NAMESPACES, ACTION_FLUSH, ACTION_RELINQUISH_VAR, + ACTION_SMART_RELINQUISH_VAR, ACTION_SYNC, ACTION_ROTATE, ACTION_VACUUM,