tmpfiles: add the t and T entries, extended attributes

tmpfiles.d(5) has t to set extended attributes on a path, and T for
the path and everything below it:

    t /srv/www - - - - user.mime_type=text/html

tmpfiles skipped them as unsupported.

Add both.  The argument is one or more NAME=VALUE with space between,
a value with space in it goes in quotes.  They are set with
lsetxattr(), a link is never followed.

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 e19e37286a
commit 1ef0da9c50
5 files changed
+170 -2

No files matched your search

+2
View File
@@ -22,6 +22,8 @@ All relevant changes are documented in this file.
`%w`, `%W`, `%B`, `%M`, and `%A`. Before, an entry with a `%`
in the path was skipped. **Note:** a literal `%` in such an argument,
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 `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
+1 -1
View File
@@ -13,7 +13,7 @@ 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
* `t`, `T` for extended attributes and `h`, `H` for file attributes
* `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
+121 -1
View File
@@ -964,6 +964,118 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify)
return rc;
}
/*
* For the entries that take a glob and have a recursive form. @fn
* and @what are global, nftw() cannot pass user data.
*/
static int (*tree_fn)(const char *path);
static const char *tree_what;
static int tree_cb(const char *path, const struct stat *st, int tflag, struct FTW *ftw)
{
(void)tflag; (void)ftw;
/* None of this is for a link, and what it points to is not ours to change */
if (!S_ISLNK(st->st_mode) && tree_fn(path))
warn("Failed setting %s on %s", tree_what, path);
return 0;
}
static int tree_do(const char *pattern, int (*fn)(const char *), const char *what, int recursive)
{
glob_t gl;
if (glob(pattern, GLOB_NOESCAPE, NULL, &gl))
return 0;
tree_fn = fn;
tree_what = what;
for (size_t i = 0; i < gl.gl_pathc; i++) {
struct stat st;
if (recursive)
nftw(gl.gl_pathv[i], tree_cb, 20, FTW_PHYS);
else if (!lstat(gl.gl_pathv[i], &st))
tree_cb(gl.gl_pathv[i], &st, 0, NULL);
}
globfree(&gl);
return 0;
}
/* Extended attributes of a t or T entry, see xattr_parse() */
#define XATTR_MAX 16
static struct {
char *name;
char *value;
} xattrs[XATTR_MAX];
static int num_xattrs;
/*
* NAME=VALUE pairs with space between, cut up where they are in @arg.
* A value may be in quotes, and a backslash takes the next character
* as it is.
*/
static int xattr_parse(char *arg)
{
num_xattrs = 0;
while (arg && *arg) {
char quote = 0, *out;
arg += strspn(arg, "\t ");
if (!*arg)
break;
if (num_xattrs == XATTR_MAX)
goto inval;
xattrs[num_xattrs].name = arg;
arg = strchr(arg, '=');
if (!arg || arg == xattrs[num_xattrs].name)
goto inval;
*arg++ = 0;
if (strpbrk(xattrs[num_xattrs].name, "\t "))
goto inval;
xattrs[num_xattrs++].value = out = arg;
for (; *arg; arg++) {
if (*arg == '\\' && arg[1])
*out++ = *++arg;
else if (quote && *arg == quote)
quote = 0;
else if (!quote && (*arg == '"' || *arg == '\''))
quote = *arg;
else if (!quote && (*arg == ' ' || *arg == '\t'))
break;
else
*out++ = *arg;
}
if (*arg)
arg++;
*out = 0;
if (quote)
goto inval;
}
if (num_xattrs)
return 0;
inval:
errno = EINVAL;
return -1;
}
static int xattr_path(const char *path)
{
for (int i = 0; i < num_xattrs; i++) {
if (lsetxattr(path, xattrs[i].name, xattrs[i].value, strlen(xattrs[i].value), 0))
return -1;
}
return 0;
}
/* 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)
{
@@ -1260,7 +1372,7 @@ static void tmpfiles(char *line)
if (!arg[0] || !strcmp(arg, "-"))
arg = NULL;
}
if (arg && strchr(arg, '%') && strchr("CfFlLw", type[0])) {
if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) {
arg = expand(arg, argbuf, sizeof(argbuf));
if (!arg) {
if (!quiet)
@@ -1280,6 +1392,8 @@ static void tmpfiles(char *line)
case 'c':
case 'C':
case 'd':
case 't':
case 'T':
break;
case 'D':
rmcontents(path);
@@ -1455,6 +1569,12 @@ static void tmpfiles(char *line)
rc = fclose(fp);
break;
}
case 't':
case 'T':
rc = xattr_parse(arg);
if (!rc)
rc = tree_do(path, xattr_path, "extended attributes", type[0] == 'T');
break;
case 'l': /* Finit extension, like 'L' but only if target exists */
if (!arg) {
paste(buf, sizeof(buf), "/usr/share/factory", path);
+2
View File
@@ -83,6 +83,7 @@ EXTRA_DIST += signal-service.sh
EXTRA_DIST += testserv.sh
EXTRA_DIST += tmpfiles-acl.sh
EXTRA_DIST += tmpfiles-arg.sh
EXTRA_DIST += tmpfiles-attr.sh
EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += tmpfiles-boot.sh
EXTRA_DIST += tmpfiles-clean.sh
@@ -168,6 +169,7 @@ if TESTSERV
TESTS += testserv.sh
TESTS += tmpfiles-acl.sh
TESTS += tmpfiles-arg.sh
TESTS += tmpfiles-attr.sh
TESTS += tmpfiles-perm.sh
TESTS += tmpfiles-boot.sh
TESTS += tmpfiles-clean.sh
+44
View File
@@ -0,0 +1,44 @@
#!/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.
set -eu
TEST_DIR=$(dirname "$0")
DIR=/attr-test
CONF=/tmp/attr.conf
tmpfiles()
{
run "cat > $CONF"
run "/libexec/finit/tmpfiles --create $CONF"
}
# The sysroot has setfattr but no getfattr, removing one that is not
# set fails
assert_xattr()
{
assert "$1 has $2" "$(texec sh -c "setfattr -x $2 $1 && echo yes || echo no")" = "yes"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "mkdir -p $DIR/tree/sub; touch $DIR/file $DIR/tree/sub/file"
sep
say 't sets extended attributes, T on everything below the path too'
tmpfiles <<CONF
t $DIR/file - - - - user.one=1 user.two="two words"
T $DIR/tree - - - - user.tree=yes
CONF
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