]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
exec-invoke: rework apply_working_directory() around chase()
authorLennart Poettering <lennart@poettering.net>
Mon, 5 Feb 2024 14:36:29 +0000 (15:36 +0100)
committerLennart Poettering <lennart@poettering.net>
Tue, 6 Feb 2024 09:28:30 +0000 (10:28 +0100)
let's be more careful and get rid of one more prefix_roota() use, in
favour of the safe chase().

src/core/exec-invoke.c

index 3f64cc78279365bbdf15c1846e454f9f7ccd12e3..1a6f606448f0fb429672fd4a23cfcb6c74444a50 100644 (file)
@@ -3337,31 +3337,39 @@ static int apply_working_directory(
                 const char *home,
                 int *exit_status) {
 
-        const char *d, *wd;
+        const char *wd;
+        int r;
 
         assert(context);
         assert(exit_status);
 
         if (context->working_directory_home) {
-
                 if (!home) {
                         *exit_status = EXIT_CHDIR;
                         return -ENXIO;
                 }
 
                 wd = home;
-
         } else
                 wd = empty_to_root(context->working_directory);
 
         if (params->flags & EXEC_APPLY_CHROOT)
-                d = wd;
-        else
-                d = prefix_roota((runtime ? runtime->ephemeral_copy : NULL) ?: context->root_directory, wd);
+                r = RET_NERRNO(chdir(wd));
+        else {
+                _cleanup_close_ int dfd = -EBADF;
 
-        if (chdir(d) < 0 && !context->working_directory_missing_ok) {
+                r = chase(wd,
+                          (runtime ? runtime->ephemeral_copy : NULL) ?: context->root_directory,
+                          CHASE_PREFIX_ROOT|CHASE_AT_RESOLVE_IN_ROOT,
+                          /* ret_path= */ NULL,
+                          &dfd);
+                if (r >= 0)
+                        r = RET_NERRNO(fchdir(dfd));
+        }
+
+        if (r < 0 && !context->working_directory_missing_ok) {
                 *exit_status = EXIT_CHDIR;
-                return -errno;
+                return r;
         }
 
         return 0;