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

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