diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index f14ea515..37524834 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -24,6 +24,8 @@ All relevant changes are documented in this file. e.g. in a `w` entry for `core_pattern`, must now be written `%%` - tmpfiles: add the `t` and `T` entries, to set extended attributes on a path, `T` also on everything below it +- tmpfiles: add the `h` and `H` entries, to set file attributes as with + chattr(1), `H` also on everything below the path - tmpfiles: add the `L?` entry, a link that is only made when what it points to exists, the same as Finit's own `l` entry - tmpfiles: add `--prefix=PATH`, to only run the entries for paths below diff --git a/doc/index.md b/doc/index.md index d41dbb8d..27d9f0a5 100644 --- a/doc/index.md +++ b/doc/index.md @@ -28,7 +28,7 @@ Features * [Conditions](conditions.md) for network/process/custom dependencies * [Readiness notification](config/service-sync.md); PID files (native) for synchronizing system startup, support for systemd [sd_notify()][], or [s6 style][] too - * Limited support for [tmpfiles.d(5)][] (no attributes or subvolumes) + * Limited support for [tmpfiles.d(5)][] (no subvolumes) * [Pre/Post script actions](config/service-wrappers.md) * Rudimentary [templating support](config/templating.md) * [Tooling](initctl.md) to enable/disable services diff --git a/doc/plugins.md b/doc/plugins.md index d30c67ca..bd3e12b4 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -23,7 +23,7 @@ For your convenience a set of *optional* plugins are available: file systems must be writable for this plugin to work. This plugin is a wrapper for the [tmpfiles.d(5)][] implementation that - Finit has. Capable but limited: no attributes or subvolumes. It + Finit has. Capable but limited: no btrfs subvolumes. It runs `tmpfiles --create --remove --boot`, so entries marked boot-only with a `!` are run here and at no other time. diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 3c2f5abb..397784fb 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -13,7 +13,6 @@ files or common packages already rely on, last what few will miss. * `C+`, which is read as `C` * `z` and `Z` only set the SELinux label when `restorecon` is installed -* `h`, `H` for file attributes * Modifiers `-` (do not fail), `=` (remove what is of another type), `~` (base64 argument), and `^` (credential as argument) * Quoted paths, a path with a space in it cannot be written diff --git a/man/finit.8 b/man/finit.8 index 173acf16..00dad42d 100644 --- a/man/finit.8 +++ b/man/finit.8 @@ -94,7 +94,7 @@ and s6 notification is also supported. .It Limited support for .Xr tmpfiles.d 5 , -no attributes or subvolumes +no subvolumes .It Pre/Post script actions .It diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 9cc9f4b4..92de730d 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -33,6 +33,7 @@ #include #include #include +#include #include #include @@ -41,6 +42,7 @@ #include #include #include +#include #include #include #ifdef _LIBITE_LITE @@ -1076,6 +1078,92 @@ static int xattr_path(const char *path) return 0; } +/* File attributes of an h or H entry, as in chattr(1) */ +static const struct { + char c; + unsigned flag; +} attrs[] = { + { 'A', FS_NOATIME_FL }, { 'a', FS_APPEND_FL }, { 'c', FS_COMPR_FL }, + { 'C', FS_NOCOW_FL }, { 'd', FS_NODUMP_FL }, { 'D', FS_DIRSYNC_FL }, + { 'e', FS_EXTENT_FL }, { 'i', FS_IMMUTABLE_FL }, { 'j', FS_JOURNAL_DATA_FL }, + { 'P', FS_PROJINHERIT_FL }, + { 's', FS_SECRM_FL }, { 'S', FS_SYNC_FL }, { 't', FS_NOTAIL_FL }, + { 'T', FS_TOPDIR_FL }, { 'u', FS_UNRM_FL }, +}; +static unsigned attr_flags; +static char attr_op; + +/* [+-=]LETTERS, to add, which is the default, remove, or set just these */ +static int attr_parse(const char *arg) +{ + attr_flags = 0; + attr_op = '+'; + + if (arg && strchr("+-=", *arg)) + attr_op = *arg++; + + for (; arg && *arg; arg++) { + size_t i; + + for (i = 0; i < NELEMS(attrs); i++) { + if (attrs[i].c == *arg) + break; + } + if (i == NELEMS(attrs)) + break; + attr_flags |= attrs[i].flag; + } + + if (arg && !*arg && (attr_flags || attr_op == '=')) + return 0; + + errno = EINVAL; + return -1; +} + +/* + * Only files and directories have them, anything else is left alone, + * and never opened: that starts a watchdog and rewinds a tape. + */ +static int attr_path(const char *path) +{ + unsigned all = 0, flags, old; + struct stat st; + int fd, rc; + + if (lstat(path, &st) || (!S_ISREG(st.st_mode) && !S_ISDIR(st.st_mode))) + return 0; + + fd = open(path, O_RDONLY | O_NONBLOCK | O_NOCTTY | O_NOFOLLOW | O_CLOEXEC); + if (fd < 0) + return -1; + + /* = leaves what is not in the table, and extents, which cannot be taken away */ + for (size_t i = 0; i < NELEMS(attrs); i++) + all |= attrs[i].flag; + all &= ~FS_EXTENT_FL; + + rc = ioctl(fd, FS_IOC_GETFLAGS, &old); + if (!rc) { + if (attr_op == '+') + flags = old | attr_flags; + else if (attr_op == '-') + flags = old & ~attr_flags; + else + flags = (old & ~all) | attr_flags; + + if (flags != old) + rc = ioctl(fd, FS_IOC_SETFLAGS, &flags); + } + close(fd); + + /* Not every file system has file attributes */ + if (rc && (errno == ENOTTY || errno == EOPNOTSUPP || errno == ENOSYS)) + rc = 0; + + return rc; +} + /* First line of @file, without @strip characters, empty when it cannot be read */ static char *specifier_file(const char *file, const char *strip, char *buf, size_t len) { @@ -1392,6 +1480,8 @@ static void tmpfiles(char *line) case 'c': case 'C': case 'd': + case 'h': + case 'H': case 't': case 'T': break; @@ -1569,6 +1659,12 @@ static void tmpfiles(char *line) rc = fclose(fp); break; } + case 'h': + case 'H': + rc = attr_parse(arg); + if (!rc) + rc = tree_do(path, attr_path, "file attributes", type[0] == 'H'); + break; case 't': case 'T': rc = xattr_parse(arg); diff --git a/test/tmpfiles-attr.sh b/test/tmpfiles-attr.sh index ffe60446..4a2b500e 100755 --- a/test/tmpfiles-attr.sh +++ b/test/tmpfiles-attr.sh @@ -1,6 +1,7 @@ #!/bin/sh -# Verify the t and T entries of tmpfiles, extended attributes. On the -# file system of the sysroot, not all of this works on tmpfs. +# Verify the t and T entries of tmpfiles, extended attributes, and the +# h and H entries, file attributes. On the file system of the sysroot, +# not all attributes work on tmpfs. set -eu TEST_DIR=$(dirname "$0") @@ -21,6 +22,12 @@ assert_xattr() assert "$1 has $2" "$(texec sh -c "setfattr -x $2 $1 && echo yes || echo no")" = "yes" } +assert_attr() +{ + assert "Attributes of $1 have $2: $3" \ + "$(texec sh -c "lsattr -d $1 | cut -d' ' -f1 | grep -q $2 && echo yes || echo no")" = "$3" +} + test_teardown() { say "Running test teardown." @@ -42,3 +49,23 @@ assert_xattr $DIR/file user.one assert_xattr $DIR/file user.two assert_xattr $DIR/tree user.tree assert_xattr $DIR/tree/sub/file user.tree + +sep +say 'h sets file attributes, H on everything below the path too' +tmpfiles < $CONF" <