]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tree-wide: add ERRNO_IS_XATTR_ABSENT() helper
authorLennart Poettering <lennart@poettering.net>
Mon, 10 Oct 2022 09:06:56 +0000 (11:06 +0200)
committerLennart Poettering <lennart@poettering.net>
Mon, 10 Oct 2022 14:00:15 +0000 (16:00 +0200)
We check the same list of error codes on various xattr operations, and
we should on some more. Add a common helper for this purpose.

15 files changed:
src/basic/errno-util.h
src/basic/os-util.c
src/core/cgroup.c
src/core/execute.c
src/home/homework-luks.c
src/home/user-record-util.c
src/oom/oomd-util.c
src/portable/portable.c
src/random-seed/random-seed.c
src/shared/cgroup-show.c
src/shared/chown-recursive.c
src/shared/dissect-image.c
src/test/test-chown-rec.c
src/test/test-xattr-util.c
src/udev/udevd.c

index 648de50eb497aeddbadc046d476e4214ac2c4e5a..a14167f5267456fa471300e29fa94808080e9c93 100644 (file)
@@ -153,3 +153,10 @@ static inline bool ERRNO_IS_DEVICE_ABSENT(int r) {
                       ENXIO,
                       ENOENT);
 }
+
+/* Quite often we want to handle cases where the backing FS doesn't support extended attributes at all and
+ * where it simply doesn't have the requested xattr the same way */
+static inline bool ERRNO_IS_XATTR_ABSENT(int r) {
+        return abs(r) == ENODATA ||
+                ERRNO_IS_NOT_SUPPORTED(r);
+}
index 96ba683276824b4d211dff4a2c968277dcfd1957..04e146f648f57e24e9dacc3e1d47fba8db90b437 100644 (file)
@@ -126,7 +126,7 @@ int open_extension_release(const char *root, const char *extension, char **ret_p
                                 /* No xattr or cannot parse it? Then skip this. */
                                 _cleanup_free_ char *extension_release_xattr = NULL;
                                 k = fgetxattr_malloc(extension_release_fd, "user.extension-release.strict", &extension_release_xattr);
-                                if (k < 0 && !ERRNO_IS_NOT_SUPPORTED(k) && k != -ENODATA)
+                                if (k < 0 && !ERRNO_IS_XATTR_ABSENT(k))
                                         log_debug_errno(k,
                                                         "%s/%s: Failed to read 'user.extension-release.strict' extended attribute from file: %m",
                                                         extension_release_dir_path, de->d_name);
index 6d40faa48b07ddeb3f521c154e47cea9aa4b23b7..cc18da1f22320142174bc1ff964aff9039191240 100644 (file)
@@ -752,7 +752,7 @@ static void unit_remove_xattr_graceful(Unit *u, const char *cgroup_path, const c
         }
 
         r = cg_remove_xattr(SYSTEMD_CGROUP_CONTROLLER, cgroup_path, name);
-        if (r < 0 && r != -ENODATA)
+        if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                 log_unit_debug_errno(u, r, "Failed to remove '%s' xattr flag on control group %s, ignoring: %m", name, empty_to_root(cgroup_path));
 }
 
@@ -3089,7 +3089,7 @@ int unit_check_oomd_kill(Unit *u) {
                 return 0;
 
         r = cg_get_xattr_malloc(SYSTEMD_CGROUP_CONTROLLER, u->cgroup_path, "user.oomd_ooms", &value);
-        if (r < 0 && r != -ENODATA)
+        if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                 return r;
 
         if (!isempty(value)) {
index 6a4e1e09547444ff9d743324c6b766bb76e75f3b..734111bc75941a1f0383a140983489ec04477d73 100644 (file)
@@ -3254,7 +3254,7 @@ static int setup_smack(
                 _cleanup_free_ char *exec_label = NULL;
 
                 r = mac_smack_read_fd(executable_fd, SMACK_ATTR_EXEC, &exec_label);
-                if (r < 0 && !IN_SET(r, -ENODATA, -EOPNOTSUPP))
+                if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                         return r;
 
                 r = mac_smack_apply_pid(0, exec_label ? : manager->default_smack_process_label);
index 858d9dda194a6620a67430e1f6fe94996edfa13a..d0fb2d91af62af0dbfe88d4c2f19da4c031062ac 100644 (file)
@@ -96,7 +96,7 @@ int run_mark_dirty(int fd, bool b) {
                         return log_debug_errno(r, "Failed to synchronize image before marking it clean: %m");
 
                 ret = fremovexattr(fd, "user.home-dirty");
-                if (ret < 0 && errno != ENODATA)
+                if (ret < 0 && !ERRNO_IS_XATTR_ABSENT(errno))
                         return log_debug_errno(errno, "Could not mark home directory as clean: %m");
         }
 
index 9d85f2abfa55a4e83d143a5ee76542e524890d47..08f1764d1182534815a48d329cdb6310e1f3063e 100644 (file)
@@ -501,7 +501,7 @@ int user_record_test_image_path(UserRecord *h) {
 
                         n = getxattr(ip, "user.home-dirty", x, sizeof(x));
                         if (n < 0) {
-                                if (errno != ENODATA)
+                                if (!ERRNO_IS_XATTR_ABSENT(errno))
                                         log_debug_errno(errno, "Unable to read dirty xattr off image file, ignoring: %m");
 
                         } else if (n == 1 && x[0] == '1')
index d31d5ca62353dc9999716b65b35708b79f6cd44b..1fc81d184341723d3729a1f4fb15b524e15571e8 100644 (file)
@@ -3,6 +3,7 @@
 #include <sys/xattr.h>
 #include <unistd.h>
 
+#include "errno-util.h"
 #include "fd-util.h"
 #include "fileio.h"
 #include "format-util.h"
@@ -39,7 +40,7 @@ static int increment_oomd_xattr(const char *path, const char *xattr, uint64_t nu
         assert(xattr);
 
         r = cg_get_xattr_malloc(SYSTEMD_CGROUP_CONTROLLER, path, xattr, &value);
-        if (r < 0 && r != -ENODATA)
+        if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                 return r;
 
         if (!isempty(value)) {
@@ -169,14 +170,14 @@ int oomd_fetch_cgroup_oom_preference(OomdCGroupContext *ctx, const char *prefix)
                 r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_avoid");
                 if (r == -ENOMEM)
                         return log_oom_debug();
-                if (r < 0 && r != -ENODATA)
+                if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                         log_debug_errno(r, "Failed to get xattr user.oomd_avoid, ignoring: %m");
                 ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_AVOID : ctx->preference;
 
                 r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, ctx->path, "user.oomd_omit");
                 if (r == -ENOMEM)
                         return log_oom_debug();
-                if (r < 0 && r != -ENODATA)
+                if (r < 0 && !ERRNO_IS_XATTR_ABSENT(r))
                         log_debug_errno(r, "Failed to get xattr user.oomd_omit, ignoring: %m");
                 ctx->preference = r > 0 ? MANAGED_OOM_PREFERENCE_OMIT : ctx->preference;
         } else
index 202442903f33b47ba85ea83f61deea838ce081c6..7172701ca29a4aa1095702b1a0bd15be149a089e 100644 (file)
@@ -278,7 +278,7 @@ static int extract_now(
                          * we have to preserve it. Copy it out so that it can be applied later. */
 
                         r = fgetfilecon_raw(fd, &con);
-                        if (r < 0 && errno != ENODATA)
+                        if (r < 0 && !ERRNO_IS_XATTR_ABSENT(errno))
                                 log_debug_errno(errno, "Failed to get SELinux file context from '%s', ignoring: %m", de->d_name);
 #endif
 
index dedbdb2667bc1742a0b342d38c1cdfc6cec5ae06..82c29d0d7ffbe10c4d4e48c89c0e83fc56674479 100644 (file)
@@ -66,7 +66,7 @@ static CreditEntropy may_credit(int seed_fd) {
         /* Determine if the file is marked as creditable */
         r = fgetxattr_malloc(seed_fd, "user.random-seed-creditable", &creditable);
         if (r < 0) {
-                if (IN_SET(r, -ENODATA, -ENOSYS, -EOPNOTSUPP))
+                if (ERRNO_IS_XATTR_ABSENT(r))
                         log_debug_errno(r, "Seed file is not marked as creditable, not crediting.");
                 else
                         log_warning_errno(r, "Failed to read extended attribute, ignoring: %m");
@@ -235,7 +235,7 @@ static int run(int argc, char *argv[]) {
                          * it. */
 
                         if (fremovexattr(seed_fd, "user.random-seed-creditable") < 0) {
-                                if (!IN_SET(errno, ENODATA, ENOSYS, EOPNOTSUPP))
+                                if (!ERRNO_IS_XATTR_ABSENT(errno))
                                         log_warning_errno(errno, "Failed to remove extended attribute, ignoring: %m");
 
                                 /* Otherwise, there was no creditable flag set, which is OK. */
index ca96e198f316951dcd1b5ac5dc6512fafdcfb3bf..e34a68ef8696ae1dc112029aabd6d3b506f9958f 100644 (file)
@@ -135,12 +135,12 @@ static int is_delegated(int cgfd, const char *path) {
         assert(cgfd >= 0 || path);
 
         r = getxattr_malloc(cgfd < 0 ? path : FORMAT_PROC_FD_PATH(cgfd), "trusted.delegate", &b);
-        if (r == -ENODATA) {
+        if (r < 0 && ERRNO_IS_XATTR_ABSENT(r)) {
                 /* If the trusted xattr isn't set (preferred), then check the untrusted one. Under the
                  * assumption that whoever is trusted enough to own the cgroup, is also trusted enough to
                  * decide if it is delegated or not this should be safe. */
                 r = getxattr_malloc(cgfd < 0 ? path : FORMAT_PROC_FD_PATH(cgfd), "user.delegate", &b);
-                if (r == -ENODATA)
+                if (r < 0 && ERRNO_IS_XATTR_ABSENT(r))
                         return false;
         }
         if (r < 0)
index 05a7a10ce411367e61f150f20d99cb62d4010daf..bbc270d34b2ced165b1f6b05837f6c6b2aa5d301 100644 (file)
@@ -32,7 +32,7 @@ static int chown_one(
         /* Drop any ACL if there is one */
         FOREACH_STRING(n, "system.posix_acl_access", "system.posix_acl_default")
                 if (removexattr(FORMAT_PROC_FD_PATH(fd), n) < 0)
-                        if (!IN_SET(errno, ENODATA, EOPNOTSUPP, ENOSYS, ENOTTY))
+                        if (!ERRNO_IS_XATTR_ABSENT(errno))
                                 return -errno;
 
         r = fchmod_and_chown(fd, st->st_mode & mask, uid, gid);
index ed58b9d0ab33ff1ce9896914548413c12865455d..bea29b8ccff5f39804ee4bfd5e05d0fdd2286267 100644 (file)
@@ -2379,7 +2379,7 @@ int verity_settings_load(
                                 if (r < 0) {
                                         _cleanup_free_ char *p = NULL;
 
-                                        if (!IN_SET(r, -ENODATA, -ENOENT) && !ERRNO_IS_NOT_SUPPORTED(r))
+                                        if (r != -ENOENT && !ERRNO_IS_XATTR_ABSENT(r))
                                                 return r;
 
                                         p = build_auxiliary_path(image, ".roothash");
@@ -2408,7 +2408,7 @@ int verity_settings_load(
                                 if (r < 0) {
                                         _cleanup_free_ char *p = NULL;
 
-                                        if (!IN_SET(r, -ENODATA, -ENOENT) && !ERRNO_IS_NOT_SUPPORTED(r))
+                                        if (r != -ENOENT && !ERRNO_IS_XATTR_ABSENT(r))
                                                 return r;
 
                                         p = build_auxiliary_path(image, ".usrhash");
index 97711f58b0dbdf6f204b828d87d825bb8025023f..801b49f7b73fa130e5b6ba601455102b67a44d95 100644 (file)
@@ -32,10 +32,8 @@ static const uint8_t default_acl[] = {
 static bool has_xattr(const char *p) {
         char buffer[sizeof(acl) * 4];
 
-        if (lgetxattr(p, "system.posix_acl_access", buffer, sizeof(buffer)) < 0) {
-                if (IN_SET(errno, EOPNOTSUPP, ENOTTY, ENODATA, ENOSYS))
-                        return false;
-        }
+        if (lgetxattr(p, "system.posix_acl_access", buffer, sizeof(buffer)) < 0)
+                return !ERRNO_IS_XATTR_ABSENT(errno);
 
         return true;
 }
index 2ddb4b4b8438305fc70f774caca41d1aa8fde2a9..02fba3d6ff3165f7ceff4d04aa7d4d79556a2c0e 100644 (file)
@@ -46,7 +46,7 @@ TEST(getxattr_at_malloc) {
         fd = open("/", O_RDONLY|O_DIRECTORY|O_CLOEXEC|O_NOCTTY);
         assert_se(fd >= 0);
         r = getxattr_at_malloc(fd, "usr", "user.idontexist", 0, &value);
-        assert_se(r == -ENODATA || ERRNO_IS_NOT_SUPPORTED(r));
+        assert_se(r < 0 && ERRNO_IS_XATTR_ABSENT(r));
 
         safe_close(fd);
         fd = open(x, O_PATH|O_CLOEXEC);
index 3e0c02893e48a12f90c41e6a8579ed8743fb8838..2c9de63da6ec0acf8adc656a6ef9743664f75d37 100644 (file)
@@ -1808,7 +1808,7 @@ static int create_subcgroup(char **ret) {
         }
 
         r = cg_get_xattr_bool(SYSTEMD_CGROUP_CONTROLLER, cgroup, "trusted.delegate");
-        if (IN_SET(r, 0, -ENODATA))
+        if (r == 0 || (r < 0 && ERRNO_IS_XATTR_ABSENT(r)))
                 return log_debug_errno(SYNTHETIC_ERRNO(EOPNOTSUPP), "The cgroup %s is not delegated to us.", cgroup);
         if (r < 0)
                 return log_debug_errno(r, "Failed to read trusted.delegate attribute: %m");