anitazha / rpms / systemd

Forked from rpms/systemd 3 years ago
Clone

Blame SOURCES/0522-Allow-systemd-tmpfiles-to-set-the-file-directory-att.patch

923a60
From 037b80886a6c3acad294aee139d28d1f574d82cc Mon Sep 17 00:00:00 2001
923a60
From: Goffredo Baroncelli <kreijack@inwind.it>
923a60
Date: Mon, 16 Mar 2015 20:33:50 +0100
923a60
Subject: [PATCH] Allow systemd-tmpfiles to set the file/directory attributes
923a60
923a60
Allow systemd-tmpfiles to set the file/directory attributes, like
923a60
chattr(1) does. Two more commands are added: 'H' and 'h' to set the
923a60
attributes, recursively and not.
923a60
923a60
(cherry picked from commit 22c3a6cadbc99ad623501db9a928f52f6f84c0c3)
923a60
923a60
Related: #1299714
923a60
---
923a60
 src/tmpfiles/tmpfiles.c | 150 ++++++++++++++++++++++++++++++++++++++++
923a60
 1 file changed, 150 insertions(+)
923a60
923a60
diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c
923a60
index ed35b8cf0d..c8c56c722d 100644
923a60
--- a/src/tmpfiles/tmpfiles.c
923a60
+++ b/src/tmpfiles/tmpfiles.c
923a60
@@ -40,6 +40,7 @@
923a60
 #include <sys/types.h>
923a60
 #include <sys/param.h>
923a60
 #include <sys/xattr.h>
923a60
+#include <linux/fs.h>
923a60
 
923a60
 #include "log.h"
923a60
 #include "util.h"
923a60
@@ -91,6 +92,8 @@ typedef enum ItemType {
923a60
         RELABEL_PATH = 'z',
923a60
         RECURSIVE_RELABEL_PATH = 'Z',
923a60
         ADJUST_MODE = 'm', /* legacy, 'z' is identical to this */
923a60
+        SET_ATTRIB = 'h',
923a60
+        RECURSIVE_SET_ATTRIB = 'H',
923a60
 } ItemType;
923a60
 
923a60
 typedef struct Item {
923a60
@@ -109,12 +112,15 @@ typedef struct Item {
923a60
         usec_t age;
923a60
 
923a60
         dev_t major_minor;
923a60
+        unsigned long attrib_value;
923a60
+        unsigned long attrib_mask;
923a60
 
923a60
         bool uid_set:1;
923a60
         bool gid_set:1;
923a60
         bool mode_set:1;
923a60
         bool age_set:1;
923a60
         bool mask_perms:1;
923a60
+        bool attrib_set:1;
923a60
 
923a60
         bool keep_first_level:1;
923a60
 
923a60
@@ -817,6 +823,127 @@ static int path_set_acls(Item *item, const char *path) {
923a60
         return r;
923a60
 }
923a60
 
923a60
+#define ALL_ATTRIBS          \
923a60
+        FS_NOATIME_FL      | \
923a60
+        FS_SYNC_FL         | \
923a60
+        FS_DIRSYNC_FL      | \
923a60
+        FS_APPEND_FL       | \
923a60
+        FS_COMPR_FL        | \
923a60
+        FS_NODUMP_FL       | \
923a60
+        FS_EXTENT_FL       | \
923a60
+        FS_IMMUTABLE_FL    | \
923a60
+        FS_JOURNAL_DATA_FL | \
923a60
+        FS_SECRM_FL        | \
923a60
+        FS_UNRM_FL         | \
923a60
+        FS_NOTAIL_FL       | \
923a60
+        FS_TOPDIR_FL       | \
923a60
+        FS_NOCOW_FL
923a60
+
923a60
+static int get_attrib_from_arg(Item *item) {
923a60
+        static const unsigned attributes[] = {
923a60
+                [(uint8_t)'A'] = FS_NOATIME_FL,      /* do not update atime */
923a60
+                [(uint8_t)'S'] = FS_SYNC_FL,         /* Synchronous updates */
923a60
+                [(uint8_t)'D'] = FS_DIRSYNC_FL,      /* dirsync behaviour (directories only) */
923a60
+                [(uint8_t)'a'] = FS_APPEND_FL,       /* writes to file may only append */
923a60
+                [(uint8_t)'c'] = FS_COMPR_FL,        /* Compress file */
923a60
+                [(uint8_t)'d'] = FS_NODUMP_FL,       /* do not dump file */
923a60
+                [(uint8_t)'e'] = FS_EXTENT_FL,       /* Top of directory hierarchies*/
923a60
+                [(uint8_t)'i'] = FS_IMMUTABLE_FL,    /* Immutable file */
923a60
+                [(uint8_t)'j'] = FS_JOURNAL_DATA_FL, /* Reserved for ext3 */
923a60
+                [(uint8_t)'s'] = FS_SECRM_FL,        /* Secure deletion */
923a60
+                [(uint8_t)'u'] = FS_UNRM_FL,         /* Undelete */
923a60
+                [(uint8_t)'t'] = FS_NOTAIL_FL,       /* file tail should not be merged */
923a60
+                [(uint8_t)'T'] = FS_TOPDIR_FL,       /* Top of directory hierarchies*/
923a60
+                [(uint8_t)'C'] = FS_NOCOW_FL,        /* Do not cow file */
923a60
+        };
923a60
+        char *p = item->argument;
923a60
+        enum {
923a60
+                MODE_ADD,
923a60
+                MODE_DEL,
923a60
+                MODE_SET
923a60
+        } mode = MODE_ADD;
923a60
+        unsigned long value = 0, mask = 0;
923a60
+
923a60
+        if (!p) {
923a60
+                log_error("\"%s\": setting ATTR need an argument", item->path);
923a60
+                return -EINVAL;
923a60
+        }
923a60
+
923a60
+        if (*p == '+') {
923a60
+                mode = MODE_ADD;
923a60
+                p++;
923a60
+        } else if (*p == '-') {
923a60
+                mode = MODE_DEL;
923a60
+                p++;
923a60
+        } else  if (*p == '=') {
923a60
+                mode = MODE_SET;
923a60
+                p++;
923a60
+        }
923a60
+
923a60
+        if (!*p && mode != MODE_SET) {
923a60
+                log_error("\"%s\": setting ATTR: argument is empty", item->path);
923a60
+                return -EINVAL;
923a60
+        }
923a60
+        for (; *p ; p++) {
923a60
+                if ((uint8_t)*p > ELEMENTSOF(attributes) || attributes[(uint8_t)*p] == 0) {
923a60
+                        log_error("\"%s\": setting ATTR: unknown attr '%c'", item->path, *p);
923a60
+                        return -EINVAL;
923a60
+                }
923a60
+                if (mode == MODE_ADD || mode == MODE_SET)
923a60
+                        value |= attributes[(uint8_t)*p];
923a60
+                else
923a60
+                        value &= ~attributes[(uint8_t)*p];
923a60
+                mask |= attributes[(uint8_t)*p];
923a60
+        }
923a60
+
923a60
+        if (mode == MODE_SET)
923a60
+                mask |= ALL_ATTRIBS;
923a60
+
923a60
+        assert(mask);
923a60
+
923a60
+        item->attrib_mask = mask;
923a60
+        item->attrib_value = value;
923a60
+        item->attrib_set = true;
923a60
+
923a60
+        return 0;
923a60
+
923a60
+}
923a60
+
923a60
+static int path_set_attrib(Item *item, const char *path) {
923a60
+        _cleanup_close_ int fd = -1;
923a60
+        int r;
923a60
+        unsigned f;
923a60
+        struct stat st;
923a60
+
923a60
+        /* do nothing */
923a60
+        if (item->attrib_mask == 0 || !item->attrib_set)
923a60
+                return 0;
923a60
+        /*
923a60
+         * It is OK to ignore an lstat() error, because the error
923a60
+         * will be catch by the open() below anyway
923a60
+         */
923a60
+        if (lstat(path, &st) == 0 &&
923a60
+            !S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode)) {
923a60
+                return 0;
923a60
+        }
923a60
+
923a60
+        fd = open(path, O_RDONLY|O_NONBLOCK|O_CLOEXEC);
923a60
+
923a60
+        if (fd < 0)
923a60
+                return log_error_errno(errno, "Cannot open \"%s\": %m", path);
923a60
+
923a60
+        f = item->attrib_value & item->attrib_mask;
923a60
+        if (!S_ISDIR(st.st_mode))
923a60
+                f &= ~FS_DIRSYNC_FL;
923a60
+        r = change_attr_fd(fd, f, item->attrib_mask);
923a60
+        if (r < 0)
923a60
+                return log_error_errno(errno,
923a60
+                        "Cannot set attrib for \"%s\", value=0x%08lx, mask=0x%08lx: %m",
923a60
+                        path, item->attrib_value, item->attrib_mask);
923a60
+
923a60
+        return 0;
923a60
+}
923a60
+
923a60
 static int write_one_file(Item *i, const char *path) {
923a60
         _cleanup_close_ int fd = -1;
923a60
         int flags, r = 0;
923a60
@@ -1266,6 +1393,18 @@ static int create_item(Item *i) {
923a60
                 if (r < 0)
923a60
                         return r;
923a60
                 break;
923a60
+
923a60
+        case SET_ATTRIB:
923a60
+                r = glob_item(i, path_set_attrib, false);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+                break;
923a60
+
923a60
+        case RECURSIVE_SET_ATTRIB:
923a60
+                r = glob_item(i, path_set_attrib, true);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+                break;
923a60
         }
923a60
 
923a60
         return 0;
923a60
@@ -1712,6 +1851,17 @@ static int parse_line(const char *fname, unsigned line, const char *buffer) {
923a60
                         return r;
923a60
                 break;
923a60
 
923a60
+        case SET_ATTRIB:
923a60
+        case RECURSIVE_SET_ATTRIB:
923a60
+                if (!i.argument) {
923a60
+                        log_error("[%s:%u] Set attrib requires argument.", fname, line);
923a60
+                        return -EBADMSG;
923a60
+                }
923a60
+                r = get_attrib_from_arg(&i);
923a60
+                if (r < 0)
923a60
+                        return r;
923a60
+                break;
923a60
+
923a60
         default:
923a60
                 log_error("[%s:%u] Unknown command type '%c'.", fname, line, (char) i.type);
923a60
                 return -EBADMSG;