valeriyvdovin / rpms / systemd

Forked from rpms/systemd 4 years ago
Clone

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

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