]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
tmpfiles,chown-recursive: port to xsetxattr()/xremovexattr()
authorMike Yuan <me@yhndnzj.com>
Thu, 30 Jan 2025 16:30:45 +0000 (17:30 +0100)
committerMike Yuan <me@yhndnzj.com>
Sun, 9 Feb 2025 13:51:04 +0000 (14:51 +0100)
src/shared/chown-recursive.c
src/tmpfiles/tmpfiles.c

index 6aa5f6723ec0b050ab7448cec63fe31b5e7d363c..06c5adb1e500f7fee76699a24baaa3a643f16fb3 100644 (file)
@@ -3,7 +3,6 @@
 #include <fcntl.h>
 #include <sys/stat.h>
 #include <sys/types.h>
-#include <sys/xattr.h>
 
 #include "chown-recursive.h"
 #include "dirent-util.h"
@@ -13,6 +12,7 @@
 #include "stdio-util.h"
 #include "strv.h"
 #include "user-util.h"
+#include "xattr-util.h"
 
 static int chown_one(
                 int fd,
@@ -26,14 +26,12 @@ static int chown_one(
         assert(fd >= 0);
         assert(st);
 
-        /* We change ACLs through the /proc/self/fd/%i path, so that we have a stable reference that works
-         * with O_PATH. */
-
         /* 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 (!ERRNO_IS_XATTR_ABSENT(errno))
-                                return -errno;
+        FOREACH_STRING(n, "system.posix_acl_access", "system.posix_acl_default") {
+                r = xremovexattr(fd, /* path = */ NULL, AT_EMPTY_PATH, n);
+                if (r < 0 && !ERRNO_IS_NEG_XATTR_ABSENT(r))
+                        return r;
+        }
 
         r = fchmod_and_chown(fd, st->st_mode & mask, uid, gid);
         if (r < 0)
index 6ce4a78adc1d3cd3e11a5cf31742eb5e40c2b7a3..e401eaa88feadca38c4dd16fa2011699c03d59c5 100644 (file)
@@ -10,7 +10,6 @@
 #include <stddef.h>
 #include <stdlib.h>
 #include <sys/file.h>
-#include <sys/xattr.h>
 #include <sysexits.h>
 #include <time.h>
 #include <unistd.h>
@@ -73,6 +72,7 @@
 #include "umask-util.h"
 #include "user-util.h"
 #include "virt.h"
+#include "xattr-util.h"
 
 /* This reads all files listed in /etc/tmpfiles.d/?*.conf and creates
  * them in the file system. This is intended to be used to create
@@ -1189,6 +1189,8 @@ static int fd_set_xattrs(
                 const struct stat *st,
                 CreationMode creation) {
 
+        int r;
+
         assert(c);
         assert(i);
         assert(fd >= 0);
@@ -1198,10 +1200,12 @@ static int fd_set_xattrs(
                 log_action("Would set", "Setting",
                            "%s extended attribute '%s=%s' on %s", *name, *value, path);
 
-                if (!arg_dry_run &&
-                    setxattr(FORMAT_PROC_FD_PATH(fd), *name, *value, strlen(*value), 0) < 0)
-                        return log_error_errno(errno, "Setting extended attribute %s=%s on %s failed: %m",
-                                               *name, *value, path);
+                if (!arg_dry_run) {
+                        r = xsetxattr(fd, /* path = */ NULL, AT_EMPTY_PATH, *name, *value);
+                        if (r < 0)
+                                return log_error_errno(r, "Failed to set extended attribute %s=%s on '%s': %m",
+                                                       *name, *value, path);
+                }
         }
         return 0;
 }