teknoraver / rpms / systemd

Forked from rpms/systemd 3 months ago
Clone

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

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