From: Mike Yuan Date: Tue, 25 Nov 2025 17:52:05 +0000 (+0100) Subject: run0: swap the order of setting default wd and user X-Git-Tag: v259-rc2~7^2~1 X-Git-Url: http://git.ipfire.org/?a=commitdiff_plain;h=2207b7f9a4360421485ecea5c527d4a914bb74b2;p=thirdparty%2Fsystemd.git 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. --- diff --git a/src/run/run.c b/src/run/run.c index 088c30edcd2..b0cb2e52798 100644 --- a/src/run/run.c +++ b/src/run/run.c @@ -1129,32 +1129,14 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) { assert_not_reached(); } - 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. - * - * If the user specifies --empower but not --user= then consider this a request to empower - * the current user. */ - - 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. */ + 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 switching to root without this being specified, then stay in the current directory */ + /* 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"); @@ -1165,6 +1147,18 @@ static int parse_argv_sudo_mode(int argc, char *argv[]) { 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. + * + * If the user specifies --empower but not --user= then consider this a request to empower + * the current user. */ + + arg_exec_user = getusername_malloc(); + if (!arg_exec_user) + return log_oom(); + } + arg_service_type = "exec"; arg_quiet = true; arg_wait = true;