]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
run0: swap the order of setting default wd and user
authorMike Yuan <me@yhndnzj.com>
Tue, 25 Nov 2025 17:52:05 +0000 (18:52 +0100)
committerLuca Boccassi <bluca@debian.org>
Wed, 26 Nov 2025 01:10:55 +0000 (01:10 +0000)
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

index 088c30edcd292168b65aca026accb49c395f328a..b0cb2e527986a958fce761329c8e9449c52a57e2 100644 (file)
@@ -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;