From 9cf6035d14cf292e8e94b25ecacf16a6fbc69f97 Mon Sep 17 00:00:00 2001
From: Mike Yuan <me@yhndnzj.com>
Date: Fri, 4 Oct 2024 21:05:21 +0200
Subject: [PATCH 0913/1160] various: correct laccess() error check

laccess is our own macro that uses RET_NERRNO.

(cherry picked from commit 7c1dd9e288047a69d4a6a6dd6585725410cfdadd)
(cherry picked from commit 4296a567d48ee43917b4f338fa1e927ffd53b36b)
---
 src/basic/os-util.c                           |  5 ++--
 src/basic/path-lookup.c                       |  9 ++++---
 src/coredump/coredump.c                       |  5 ++--
 src/home/homework-luks.c                      |  8 +++---
 src/kernel-install/kernel-install.c           |  8 +++---
 src/libsystemd/sd-daemon/sd-daemon.c          | 15 ++++++-----
 src/shared/condition.c                        |  7 ++---
 src/shared/mount-util.c                       |  5 ++--
 src/sysext/sysext.c                           | 10 +++----
 .../system-update-generator.c                 | 26 +++++++++++--------
 10 files changed, 52 insertions(+), 46 deletions(-)

diff --git a/src/basic/os-util.c b/src/basic/os-util.c
index 3cd6134f72..aaae994060 100644
--- a/src/basic/os-util.c
+++ b/src/basic/os-util.c
@@ -102,8 +102,9 @@ int path_is_extension_tree(ImageClass image_class, const char *path, const char
         /* Does the path exist at all? If not, generate an error immediately. This is useful so that a missing root dir
          * always results in -ENOENT, and we can properly distinguish the case where the whole root doesn't exist from
          * the case where just the os-release file is missing. */
-        if (laccess(path, F_OK) < 0)
-                return -errno;
+        r = laccess(path, F_OK);
+        if (r < 0)
+                return r;
 
         /* We use /usr/lib/extension-release.d/extension-release[.NAME] as flag for something being a system extension,
          * /etc/extension-release.d/extension-release[.NAME] as flag for something being a system configuration, and finally,
diff --git a/src/basic/path-lookup.c b/src/basic/path-lookup.c
index 4e3d59fc56..d76705bd4b 100644
--- a/src/basic/path-lookup.c
+++ b/src/basic/path-lookup.c
@@ -884,6 +884,7 @@ char **env_generator_binary_paths(RuntimeScope runtime_scope) {
 
 int find_portable_profile(const char *name, const char *unit, char **ret_path) {
         const char *dot;
+        int r;
 
         assert(name);
         assert(ret_path);
@@ -897,13 +898,13 @@ int find_portable_profile(const char *name, const char *unit, char **ret_path) {
                 if (!joined)
                         return -ENOMEM;
 
-                if (laccess(joined, F_OK) >= 0) {
+                r = laccess(joined, F_OK);
+                if (r >= 0) {
                         *ret_path = TAKE_PTR(joined);
                         return 0;
                 }
-
-                if (errno != ENOENT)
-                        return -errno;
+                if (r != -ENOENT)
+                        return r;
         }
 
         return -ENOENT;
diff --git a/src/coredump/coredump.c b/src/coredump/coredump.c
index f4adb32588..b6ca6f03b0 100644
--- a/src/coredump/coredump.c
+++ b/src/coredump/coredump.c
@@ -1457,8 +1457,9 @@ static int forward_coredump_to_container(Context *context) {
 
                 pair[0] = safe_close(pair[0]);
 
-                if (laccess("/run/systemd/coredump", W_OK) < 0) {
-                        log_debug_errno(errno, "Cannot find coredump socket, exiting: %m");
+                r = laccess("/run/systemd/coredump", W_OK);
+                if (r < 0) {
+                        log_debug_errno(r, "Cannot find coredump socket, exiting: %m");
                         _exit(EXIT_FAILURE);
                 }
 
diff --git a/src/home/homework-luks.c b/src/home/homework-luks.c
index 5bd78a03ed..30a4df78db 100644
--- a/src/home/homework-luks.c
+++ b/src/home/homework-luks.c
@@ -1983,11 +1983,11 @@ static int wait_for_devlink(const char *path) {
                 _cleanup_free_ char *dn = NULL;
                 usec_t w;
 
-                if (laccess(path, F_OK) < 0) {
-                        if (errno != ENOENT)
-                                return log_error_errno(errno, "Failed to determine whether %s exists: %m", path);
-                } else
+                r = laccess(path, F_OK);
+                if (r >= 0)
                         return 0; /* Found it */
+                if (r != -ENOENT)
+                        return log_error_errno(r, "Failed to determine whether %s exists: %m", path);
 
                 if (inotify_fd < 0) {
                         /* We need to wait for the device symlink to show up, let's create an inotify watch for it */
diff --git a/src/kernel-install/kernel-install.c b/src/kernel-install/kernel-install.c
index 07e5c31116..eef9ef78ba 100644
--- a/src/kernel-install/kernel-install.c
+++ b/src/kernel-install/kernel-install.c
@@ -1162,12 +1162,10 @@ static int kernel_from_version(const char *version, char **ret_kernel) {
                 return log_oom();
 
         r = laccess(vmlinuz, F_OK);
-        if (r < 0) {
-                if (r == -ENOENT)
-                        return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
-
+        if (r == -ENOENT)
+                return log_error_errno(r, "Kernel image not installed to '%s', requiring manual kernel image path specification.", vmlinuz);
+        if (r < 0)
                 return log_error_errno(r, "Failed to determine if kernel image is installed to '%s': %m", vmlinuz);
-        }
 
         *ret_kernel = TAKE_PTR(vmlinuz);
         return 0;
diff --git a/src/libsystemd/sd-daemon/sd-daemon.c b/src/libsystemd/sd-daemon/sd-daemon.c
index 6a60cde4bb..d1a650fd43 100644
--- a/src/libsystemd/sd-daemon/sd-daemon.c
+++ b/src/libsystemd/sd-daemon/sd-daemon.c
@@ -715,17 +715,18 @@ _public_ int sd_pid_notifyf_with_fds(
 }
 
 _public_ int sd_booted(void) {
-        /* We test whether the runtime unit file directory has been
-         * created. This takes place in mount-setup.c, so is
-         * guaranteed to happen very early during boot. */
+        int r;
 
-        if (laccess("/run/systemd/system/", F_OK) >= 0)
-                return true;
+        /* We test whether the runtime unit file directory has been created. This takes place in mount-setup.c,
+         * so is guaranteed to happen very early during boot. */
 
-        if (errno == ENOENT)
+        r = laccess("/run/systemd/system/", F_OK);
+        if (r >= 0)
+                return true;
+        if (r == -ENOENT)
                 return false;
 
-        return -errno;
+        return r;
 }
 
 _public_ int sd_watchdog_enabled(int unset_environment, uint64_t *usec) {
diff --git a/src/shared/condition.c b/src/shared/condition.c
index 3b7436c1d7..20fa1ae9ac 100644
--- a/src/shared/condition.c
+++ b/src/shared/condition.c
@@ -169,10 +169,11 @@ static int condition_test_credential(Condition *c, char **env) {
                 if (!j)
                         return -ENOMEM;
 
-                if (laccess(j, F_OK) >= 0)
+                r = laccess(j, F_OK);
+                if (r >= 0)
                         return true; /* yay! */
-                if (errno != ENOENT)
-                        return -errno;
+                if (r != -ENOENT)
+                        return r;
 
                 /* not found in this dir */
         }
diff --git a/src/shared/mount-util.c b/src/shared/mount-util.c
index 4f2acce513..1c7b727a5e 100644
--- a/src/shared/mount-util.c
+++ b/src/shared/mount-util.c
@@ -425,8 +425,9 @@ int bind_remount_one_with_mountinfo(
 
         fs = mnt_table_find_target(table, path, MNT_ITER_FORWARD);
         if (!fs) {
-                if (laccess(path, F_OK) < 0) /* Hmm, it's not in the mount table, but does it exist at all? */
-                        return -errno;
+                r = laccess(path, F_OK); /* Hmm, it's not in the mount table, but does it exist at all? */
+                if (r < 0)
+                        return r;
 
                 return -EINVAL; /* Not a mount point we recognize */
         }
diff --git a/src/sysext/sysext.c b/src/sysext/sysext.c
index 8dc515e4d5..784b79dc33 100644
--- a/src/sysext/sysext.c
+++ b/src/sysext/sysext.c
@@ -938,13 +938,11 @@ static int merge_subprocess(
                 if (!p)
                         return log_oom();
 
-                if (laccess(p, F_OK) < 0) {
-                        if (errno != ENOENT)
-                                return log_error_errno(errno, "Failed to check if '%s' exists: %m", p);
-
-                        /* Hierarchy apparently was empty in all extensions, and wasn't mounted, ignoring. */
+                r = laccess(p, F_OK);
+                if (r == -ENOENT) /* Hierarchy apparently was empty in all extensions, and wasn't mounted, ignoring. */
                         continue;
-                }
+                if (r < 0)
+                        return log_error_errno(r, "Failed to check if '%s' exists: %m", p);
 
                 r = chase(*h, arg_root, CHASE_PREFIX_ROOT|CHASE_NONEXISTENT, &resolved, NULL);
                 if (r < 0)
diff --git a/src/system-update-generator/system-update-generator.c b/src/system-update-generator/system-update-generator.c
index a1782d5c05..d884530674 100644
--- a/src/system-update-generator/system-update-generator.c
+++ b/src/system-update-generator/system-update-generator.c
@@ -20,22 +20,26 @@
 static const char *arg_dest = NULL;
 
 static int generate_symlink(void) {
+        int r;
+
         FOREACH_STRING(p, "/system-update", "/etc/system-update") {
-                if (laccess(p, F_OK) >= 0) {
-                        _cleanup_free_ char *j = NULL;
+                r = laccess(p, F_OK);
+                if (r < 0) {
+                        if (r != -ENOENT)
+                                log_warning_errno(r, "Failed to check if %s symlink exists, ignoring: %m", p);
+                        continue;
+                }
 
-                        j = path_join(arg_dest, SPECIAL_DEFAULT_TARGET);
-                        if (!j)
-                                return log_oom();
+                _cleanup_free_ char *j = NULL;
 
-                        if (symlink(SYSTEM_DATA_UNIT_DIR "/system-update.target", j) < 0)
-                                return log_error_errno(errno, "Failed to create symlink %s: %m", j);
+                j = path_join(arg_dest, SPECIAL_DEFAULT_TARGET);
+                if (!j)
+                        return log_oom();
 
-                        return 1;
-                }
+                if (symlink(SYSTEM_DATA_UNIT_DIR "/system-update.target", j) < 0)
+                        return log_error_errno(errno, "Failed to create symlink %s: %m", j);
 
-                if (errno != ENOENT)
-                        log_warning_errno(errno, "Failed to check if %s symlink exists, ignoring: %m", p);
+                return 1;
         }
 
         return 0;
-- 
2.33.0