From 2207b7f9a4360421485ecea5c527d4a914bb74b2 Mon Sep 17 00:00:00 2001 From: Mike Yuan Date: Tue, 25 Nov 2025 18:52:05 +0100 Subject: [PATCH] run0: swap the order of setting default wd and user Follow-up for 4f6ef13f43aed654cbadb2785afee1ce567d710d Special casing --area= rather than --empower makes the code self-explanatory, as --area= is about alternative home dir after all. On top of that this ensures when --area= and --empower are specified in combination we honor the home dir switch, too. --- src/run/run.c | 42 ++++++++++++++++++------------------------ 1 file changed, 18 insertions(+), 24 deletions(-) diff --git a/src/run/run.c b/src/run/run.c index 088c30edcd..b0cb2e5279 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1129,6 +1129,24 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) { assert_not_reached(); } + if (!arg_working_directory) { + if (arg_exec_user || arg_area) { + /* When switching to a specific user or an area, also switch to its home directory. */ + arg_working_directory = strdup("~"); + if (!arg_working_directory) + return log_oom(); + } else { + /* When elevating privileges without this being specified, then stay in the current directory */ + r = safe_getcwd(&arg_working_directory); + if (r < 0) + return log_error_errno(r, "Failed to get current working directory: %m"); + } + } else { + /* Root was not suppressed earlier, to allow the above check to work properly. */ + if (empty_or_root(arg_working_directory)) + arg_working_directory = mfree(arg_working_directory); + } + if (!arg_exec_user && (arg_area || arg_empower)) { /* If the user specifies --area= but not --user= then consider this an area switch request, * and default to logging into our own account. @@ -1139,30 +1157,6 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) { arg_exec_user = getusername_malloc(); if (!arg_exec_user) return log_oom(); - - if (arg_empower && !arg_working_directory) { - r = safe_getcwd(&arg_working_directory); - if (r < 0) - return log_error_errno(r, "Failed to get current working directory: %m"); - } - } - - if (!arg_working_directory) { - if (arg_exec_user) { - /* When switching to a specific user, also switch to its home directory. */ - arg_working_directory = strdup("~"); - if (!arg_working_directory) - return log_oom(); - } else { - /* When switching to root without this being specified, then stay in the current directory */ - r = safe_getcwd(&arg_working_directory); - if (r < 0) - return log_error_errno(r, "Failed to get current working directory: %m"); - } - } else { - /* Root was not suppressed earlier, to allow the above check to work properly. */ - if (empty_or_root(arg_working_directory)) - arg_working_directory = mfree(arg_working_directory); } arg_service_type = "exec";