]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
fs-util: introduce fchmod_opath()
authorFranck Bui <fbui@suse.com>
Wed, 11 Apr 2018 14:58:49 +0000 (16:58 +0200)
committerFranck Bui <fbui@suse.com>
Tue, 24 Apr 2018 09:57:48 +0000 (11:57 +0200)
fchmod(2) still doesn't take file descriptors opened with O_PATH.

src/basic/fs-util.c
src/basic/fs-util.h
src/tmpfiles/tmpfiles.c

index 61aeb1fc8a22060b0ea4666485851442de899b01..13dccfef545307978caedaa392c29f71c85b0ed1 100644 (file)
@@ -241,6 +241,21 @@ int fchmod_umask(int fd, mode_t m) {
         return r;
 }
 
+int fchmod_opath(int fd, mode_t m) {
+        char procfs_path[strlen("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
+
+        /* This function operates also on fd that might have been opened with
+         * O_PATH. Indeed fchmodat() doesn't have the AT_EMPTY_PATH flag like
+         * fchownat() does. */
+
+        xsprintf(procfs_path, "/proc/self/fd/%i", fd);
+
+        if (chmod(procfs_path, m) < 0)
+                return -errno;
+
+        return 0;
+}
+
 int fd_warn_permissions(const char *path, int fd) {
         struct stat st;
 
index 2b1d2097fdb28a8f843b4f08307125ad3e115455..6157fe81bff1932da3715258e141d7b8d28a5bd6 100644 (file)
@@ -33,6 +33,7 @@ int readlink_and_make_absolute(const char *p, char **r);
 int chmod_and_chown(const char *path, mode_t mode, uid_t uid, gid_t gid);
 
 int fchmod_umask(int fd, mode_t mode);
+int fchmod_opath(int fd, mode_t m);
 
 int fd_warn_permissions(const char *path, int fd);
 
index 5755f22160bcd46754b13eb1f5b3e747d47ca2ff..4e437a57fd7f26b09436d02ded95a3866d3bbb7e 100644 (file)
@@ -808,15 +808,9 @@ static int fd_set_perms(Item *i, int fd, const struct stat *st) {
                         if (m == (st->st_mode & 07777))
                                 log_debug("\"%s\" has correct mode %o already.", path, st->st_mode);
                         else {
-                                char procfs_path[STRLEN("/proc/self/fd/") + DECIMAL_STR_MAX(int)];
-
                                 log_debug("Changing \"%s\" to mode %o.", path, m);
-
-                                /* fchmodat() still doesn't have AT_EMPTY_PATH flag. */
-                                xsprintf(procfs_path, "/proc/self/fd/%i", fd);
-
-                                if (chmod(procfs_path, m) < 0)
-                                        return log_error_errno(errno, "chmod() of %s via %s failed: %m", path, procfs_path);
+                                if (fchmod_opath(fd, m) < 0)
+                                        return log_error_errno(errno, "fchmod() of %s failed: %m", path);
                         }
                 }
         }