tmpfiles: add the h and H entries, file attributes

tmpfiles.d(5) has h to set file attributes, as chattr(1) does, and H
for the path and everything below it:

    h /var/log/journal - - - - +C

tmpfiles skipped them as unsupported.

Add both.  The argument is the letters chattr(1) has, after a + to
add them, which is the default, a - to remove them, or a = to have
just these set.  Only files and directories have such attributes,
anything else is left alone.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 19:10:02 +01:00
1 parent 1ef0da9c50
commit 3ff5c33217
8 files changed
+131 -7

No files matched your search

+2
View File
@@ -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 `%%` 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 - tmpfiles: add the `t` and `T` entries, to set extended attributes on
a path, `T` also on everything below it 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 - 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 points to exists, the same as Finit's own `l` entry
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below - tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
+1 -1
View File
@@ -28,7 +28,7 @@ Features
* [Conditions](conditions.md) for network/process/custom dependencies * [Conditions](conditions.md) for network/process/custom dependencies
* [Readiness notification](config/service-sync.md); PID files (native) for synchronizing system * [Readiness notification](config/service-sync.md); PID files (native) for synchronizing system
startup, support for systemd [sd_notify()][], or [s6 style][] too 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) * [Pre/Post script actions](config/service-wrappers.md)
* Rudimentary [templating support](config/templating.md) * Rudimentary [templating support](config/templating.md)
* [Tooling](initctl.md) to enable/disable services * [Tooling](initctl.md) to enable/disable services
+1 -1
View File
@@ -23,7 +23,7 @@ For your convenience a set of *optional* plugins are available:
file systems must be writable for this plugin to work. file systems must be writable for this plugin to work.
This plugin is a wrapper for the [tmpfiles.d(5)][] implementation that 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 runs `tmpfiles --create --remove --boot`, so entries marked boot-only
with a `!` are run here and at no other time. with a `!` are run here and at no other time.
-1
View File
@@ -13,7 +13,6 @@ files or common packages already rely on, last what few will miss.
* `C+`, which is read as `C` * `C+`, which is read as `C`
* `z` and `Z` only set the SELinux label when `restorecon` is installed * `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), * Modifiers `-` (do not fail), `=` (remove what is of another type),
`~` (base64 argument), and `^` (credential as argument) `~` (base64 argument), and `^` (credential as argument)
* Quoted paths, a path with a space in it cannot be written * Quoted paths, a path with a space in it cannot be written
+1 -1
View File
@@ -94,7 +94,7 @@ and s6 notification is also supported.
.It .It
Limited support for Limited support for
.Xr tmpfiles.d 5 , .Xr tmpfiles.d 5 ,
no attributes or subvolumes no subvolumes
.It .It
Pre/Post script actions Pre/Post script actions
.It .It
+96
View File
@@ -33,6 +33,7 @@
#include <libgen.h> #include <libgen.h>
#include <grp.h> #include <grp.h>
#include <pwd.h> #include <pwd.h>
#include <sys/ioctl.h>
#include <sys/sysmacros.h> #include <sys/sysmacros.h>
#include <sys/utsname.h> #include <sys/utsname.h>
@@ -41,6 +42,7 @@
#include <time.h> #include <time.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <sys/xattr.h> #include <sys/xattr.h>
#include <linux/fs.h>
#include <linux/posix_acl.h> #include <linux/posix_acl.h>
#include <linux/posix_acl_xattr.h> #include <linux/posix_acl_xattr.h>
#ifdef _LIBITE_LITE #ifdef _LIBITE_LITE
@@ -1076,6 +1078,92 @@ static int xattr_path(const char *path)
return 0; 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 */ /* 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) 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 'C': case 'C':
case 'd': case 'd':
case 'h':
case 'H':
case 't': case 't':
case 'T': case 'T':
break; break;
@@ -1569,6 +1659,12 @@ static void tmpfiles(char *line)
rc = fclose(fp); rc = fclose(fp);
break; 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':
case 'T': case 'T':
rc = xattr_parse(arg); rc = xattr_parse(arg);
+29 -2
View File
@@ -1,6 +1,7 @@
#!/bin/sh #!/bin/sh
# Verify the t and T entries of tmpfiles, extended attributes. On the # Verify the t and T entries of tmpfiles, extended attributes, and the
# file system of the sysroot, not all of this works on tmpfs. # h and H entries, file attributes. On the file system of the sysroot,
# not all attributes work on tmpfs.
set -eu set -eu
TEST_DIR=$(dirname "$0") 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 "$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() test_teardown()
{ {
say "Running 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/file user.two
assert_xattr $DIR/tree user.tree assert_xattr $DIR/tree user.tree
assert_xattr $DIR/tree/sub/file 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
h $DIR/file - - - - +dA
H $DIR/tree - - - - d
CONF
assert_attr $DIR/file d yes
assert_attr $DIR/file A yes
assert_attr $DIR/tree/sub/file d yes
say 'With - they are taken away, with = only those named are left'
tmpfiles <<CONF
h $DIR/file - - - - -d
H $DIR/tree - - - - =A
CONF
assert_attr $DIR/file d no
assert_attr $DIR/file A yes
assert_attr $DIR/tree/sub/file d no
assert_attr $DIR/tree/sub/file A yes
+1 -1
View File
@@ -19,7 +19,7 @@ test_teardown()
run "cat > $CONF" <<CONF run "cat > $CONF" <<CONF
d $DIR d $DIR
h $DIR - - - - +i y $DIR - - - - +i
f $DIR/%! f $DIR/%!
f $DIR/after f $DIR/after
CONF CONF