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.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-05-12 15:29:51 +02:00
parent 1dbd734048
commit 1955c18a18
3 changed files with 32 additions and 27 deletions
+1 -1
View File
@@ -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),
+30 -26
View File
@@ -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();
+1
View File
@@ -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,