run0: swap the order of setting default wd and user

Follow-up for 4f6ef13f43

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.
This commit is contained in:
Mike Yuan
2025-11-25 18:52:05 +01:00
committed by Luca Boccassi
parent 2ba910ab06
commit 2207b7f9a4

View File

@@ -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";