mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: add --root, run on the system below a directory
There was no way to run the tmpfiles.d files of a root file system
that is not the running one, for example an image that is being put
together.
systemd-tmpfiles has --root=DIR for that. Add the same:
tmpfiles --create --root=/mnt/target
The tmpfiles.d files, users, and groups are those below DIR, and so is
every path, also what an absolute link there points to. A file on the
command line is opened before the root is changed, as systemd does.
It is done with chroot(2), so tmpfiles must be run as root. The new
root most likely has no /proc, which is needed to set the mode, ACL,
or attributes of a path opened with O_PATH. The work directory is
therefore left in the /proc/self/fd of the old root. Users and groups
are read from /etc/passwd and /etc/group, NSS would be that of the old
root.
Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
5 files changed
+205
-41
No files matched your search
@@ -67,6 +67,11 @@ All relevant changes are documented in this file.
|
||||
- tmpfiles: add `--purge` and the `$` modifier, as in systemd-tmpfiles.
|
||||
`tmpfiles --purge FILE` removes what the entries of `FILE` that are
|
||||
marked with a `$`, as in `d$`, make. For when a package is removed
|
||||
- tmpfiles: add `--root=DIR`, to run on the system below `DIR`: its
|
||||
tmpfiles.d files, its `/etc/passwd` and `/etc/group`, and its paths.
|
||||
For a root file system that is being put together. Finit's tmpfiles
|
||||
does this with chroot(2), so unlike systemd-tmpfiles it must be run
|
||||
as root
|
||||
- tmpfiles: add `--prefix=PATH`, to only run the entries for paths below
|
||||
it. `--prefix` and `--exclude-prefix` match whole path components,
|
||||
`--exclude-prefix=/dev` no longer skips `/devices`
|
||||
|
||||
+11
-14
@@ -1,20 +1,17 @@
|
||||
tmpfiles TODO
|
||||
=============
|
||||
|
||||
What `tmpfiles` lacks compared to [tmpfiles.d(5)][], from a read of
|
||||
`src/tmpfiles.c` in November 2026. Ranked: first what Finit's own
|
||||
files or common packages already rely on, last what few will miss.
|
||||
What `tmpfiles` lacks compared to [tmpfiles.d(5)][] and
|
||||
systemd-tmpfiles(8), from a read of `src/tmpfiles.c` in November 2026.
|
||||
|
||||
[tmpfiles.d(5)]: https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html
|
||||
|
||||
|
||||
1. Remaining types and modifiers
|
||||
--------------------------------
|
||||
|
||||
* `z` and `Z` only set the SELinux label when `restorecon` is installed
|
||||
|
||||
|
||||
2. Command line
|
||||
---------------
|
||||
|
||||
Missing option: `--root`.
|
||||
* `z` and `Z` only set the SELinux label when `restorecon` is installed,
|
||||
and no other entry sets a label on what it creates. Doing it without
|
||||
the program needs libselinux
|
||||
* `--root` needs chroot(2), so it cannot be run as a regular user, or
|
||||
under fakeroot when a root file system is built. The `%b` specifier
|
||||
does not work there, the root has no `/proc`
|
||||
* No `--image`, `--user`, or `--tldr`, and no btrfs subvolumes: `v`, `q`,
|
||||
and `Q` make a plain directory
|
||||
* An entry whose argument comes from a credential, `^`, is skipped
|
||||
+124
-27
@@ -69,6 +69,35 @@ int cat_config = 0;
|
||||
/* File the ones on the command line are read in place of, --replace */
|
||||
static char *replace;
|
||||
|
||||
/* Directory all paths are below, --root */
|
||||
static char *root;
|
||||
|
||||
/*
|
||||
* Path of an open @fd, for what cannot be done on an fd opened with
|
||||
* O_PATH. With --root there is no /proc, see change_root().
|
||||
*/
|
||||
static char *fd_path(int fd, char *buf, size_t len)
|
||||
{
|
||||
snprintf(buf, len, "%s%d", root ? "" : "/proc/self/fd/", fd);
|
||||
|
||||
return buf;
|
||||
}
|
||||
|
||||
/*
|
||||
* No /proc below @dir, most likely, so the work dir is left in the
|
||||
* /proc/self/fd of the old root. Nothing else may be relative to it,
|
||||
* and nothing from below @dir may be run.
|
||||
*/
|
||||
static void change_root(const char *dir)
|
||||
{
|
||||
int fd;
|
||||
|
||||
fd = open("/proc/self/fd", O_PATH | O_DIRECTORY | O_CLOEXEC);
|
||||
if (fd < 0 || chroot(dir) || fchdir(fd))
|
||||
err(1, "Failed changing root to %s", dir);
|
||||
close(fd);
|
||||
}
|
||||
|
||||
#define MAX_EXCLUDES 32
|
||||
static const char *exclude_prefixes[MAX_EXCLUDES];
|
||||
static int num_excludes;
|
||||
@@ -193,9 +222,7 @@ static int fd_chmod(int fd, mode_t mode)
|
||||
if (errno != EBADF)
|
||||
return -1;
|
||||
|
||||
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
|
||||
|
||||
return chmod(proc, mode);
|
||||
return chmod(fd_path(fd, proc, sizeof(proc)), mode);
|
||||
}
|
||||
|
||||
static int copy_errno;
|
||||
@@ -636,6 +663,49 @@ static int parse_age(const char *str, struct age *age)
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* With --root the files are read, NSS is that of the old root */
|
||||
static int file_uid(const char *user)
|
||||
{
|
||||
struct passwd *pw;
|
||||
int uid = -1;
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen("/etc/passwd", "r");
|
||||
if (!fp)
|
||||
return -1;
|
||||
|
||||
while ((pw = fgetpwent(fp))) {
|
||||
if (strcmp(pw->pw_name, user))
|
||||
continue;
|
||||
uid = pw->pw_uid;
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return uid;
|
||||
}
|
||||
|
||||
static int file_gid(const char *group)
|
||||
{
|
||||
struct group *gr;
|
||||
int gid = -1;
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen("/etc/group", "r");
|
||||
if (!fp)
|
||||
return -1;
|
||||
|
||||
while ((gr = fgetgrent(fp))) {
|
||||
if (strcmp(gr->gr_name, group))
|
||||
continue;
|
||||
gid = gr->gr_gid;
|
||||
break;
|
||||
}
|
||||
fclose(fp);
|
||||
|
||||
return gid;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse user string - supports both names and numeric UIDs.
|
||||
* Returns UID on success, -1 on failure or when no user is given.
|
||||
@@ -653,7 +723,7 @@ static int parse_uid(const char *user)
|
||||
return val;
|
||||
|
||||
/* Not numeric, look up by name */
|
||||
return getuser(user, NULL);
|
||||
return root ? file_uid(user) : getuser(user, NULL);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -673,7 +743,7 @@ static int parse_gid(const char *group)
|
||||
return val;
|
||||
|
||||
/* Not numeric, look up by name */
|
||||
return getgroup(group);
|
||||
return root ? file_gid(group) : getgroup(group);
|
||||
}
|
||||
|
||||
/* Mode and owner of an entry, mode 0 and id -1 when not given */
|
||||
@@ -989,10 +1059,7 @@ static int acl_parse_entry(char *str, struct acl *access, struct acl *def)
|
||||
if (type != ACL_USER && type != ACL_GROUP)
|
||||
return -1;
|
||||
|
||||
if (type == ACL_USER)
|
||||
val = parse_uid(name);
|
||||
else if ((val = atonum(name)) < 0)
|
||||
val = getgroup(name);
|
||||
val = type == ACL_USER ? parse_uid(name) : parse_gid(name);
|
||||
if (val < 0)
|
||||
return -1;
|
||||
id = val;
|
||||
@@ -1166,7 +1233,7 @@ static int acl_path(const char *path)
|
||||
if (fd < 0)
|
||||
return -1;
|
||||
|
||||
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
|
||||
fd_path(fd, proc, sizeof(proc));
|
||||
if (fstat(fd, &st)) {
|
||||
rc = -1;
|
||||
} else if (!S_ISLNK(st.st_mode)) {
|
||||
@@ -2333,7 +2400,7 @@ static void tmpfiles(struct entry *e)
|
||||
} else if (perm_path(p, &perm, 0, O_PATH)) {
|
||||
warn("Failed setting mode/owner on %s", p);
|
||||
}
|
||||
if (whichp("restorecon"))
|
||||
if (!root && whichp("restorecon"))
|
||||
systemf("restorecon %s %s", opts, p);
|
||||
}
|
||||
globfree(&gl);
|
||||
@@ -2383,26 +2450,20 @@ static void run_entries(int create)
|
||||
}
|
||||
}
|
||||
|
||||
static void process_file(const char *fn)
|
||||
static void read_file(const char *fn, FILE *fp)
|
||||
{
|
||||
FILE *fp = stdin;
|
||||
|
||||
if (strcmp(fn, "-")) {
|
||||
fp = fopen(fn, "r");
|
||||
if (!fp) {
|
||||
warn("Failed to open %s", fn);
|
||||
return;
|
||||
}
|
||||
}
|
||||
if (!fp)
|
||||
return;
|
||||
conf = fn;
|
||||
|
||||
if (cat_config) {
|
||||
printf("# %s\n", fn);
|
||||
fcopyfile(fp, stdout);
|
||||
puts("");
|
||||
return;
|
||||
}
|
||||
|
||||
while (!cat_config && !feof(fp)) {
|
||||
while (!feof(fp)) {
|
||||
/* No comment character, a # is only a comment at the start of a line */
|
||||
const char delim[3] = { '\\', '\\', 0 };
|
||||
char *line;
|
||||
@@ -2414,8 +2475,18 @@ static void process_file(const char *fn)
|
||||
if (parse(line))
|
||||
free(line);
|
||||
}
|
||||
}
|
||||
|
||||
if (fp != stdin)
|
||||
static void process_file(const char *fn)
|
||||
{
|
||||
FILE *fp;
|
||||
|
||||
fp = fopen(fn, "r");
|
||||
if (!fp)
|
||||
warn("Failed to open %s", fn);
|
||||
|
||||
read_file(fn, fp);
|
||||
if (fp)
|
||||
fclose(fp);
|
||||
}
|
||||
|
||||
@@ -2456,7 +2527,7 @@ static int by_name(const void *a, const void *b)
|
||||
* name in a directory before its own, and what is left is read in
|
||||
* order of the file names, whatever directory they are in.
|
||||
*/
|
||||
static void process(int num, char *file[])
|
||||
static void process(int num, char *file[], FILE *fp[])
|
||||
{
|
||||
int flags = GLOB_NOESCAPE;
|
||||
size_t i, j, cnt = 0;
|
||||
@@ -2465,7 +2536,7 @@ static void process(int num, char *file[])
|
||||
|
||||
if (num && !replace) {
|
||||
for (int k = 0; k < num; k++)
|
||||
process_file(file[k]);
|
||||
read_file(file[k], fp[k]);
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -2508,7 +2579,7 @@ static void process(int num, char *file[])
|
||||
continue;
|
||||
}
|
||||
for (int k = 0; k < num; k++)
|
||||
process_file(file[k]);
|
||||
read_file(file[k], fp[k]);
|
||||
}
|
||||
|
||||
free(list);
|
||||
@@ -2533,6 +2604,7 @@ static int usage(int rc)
|
||||
" --prefix=PFX Only apply rules for paths starting with PFX\n"
|
||||
" --purge Remove all that the entries marked with $ in CONFIGFILE make\n"
|
||||
" --replace=PATH Read all files, with CONFIGFILE in place of PATH\n"
|
||||
" --root=DIR Run on the system below DIR, with its tmpfiles.d files\n"
|
||||
" -r, --remove Remove files and directories marked for removal\n"
|
||||
" -h, --help This help text\n"
|
||||
"\n"
|
||||
@@ -2552,6 +2624,7 @@ enum {
|
||||
OPT_GRACEFUL,
|
||||
OPT_REPLACE,
|
||||
OPT_PURGE,
|
||||
OPT_ROOT,
|
||||
};
|
||||
|
||||
int main(int argc, char *argv[])
|
||||
@@ -2569,10 +2642,12 @@ int main(int argc, char *argv[])
|
||||
{ "purge", 0, NULL, OPT_PURGE },
|
||||
{ "remove", 0, NULL, 'r' },
|
||||
{ "replace", 1, NULL, OPT_REPLACE },
|
||||
{ "root", 1, NULL, OPT_ROOT },
|
||||
{ "help", 0, NULL, 'h' },
|
||||
{ NULL, 0, NULL, 0 }
|
||||
};
|
||||
|
||||
FILE **fp;
|
||||
int c;
|
||||
|
||||
while ((c = getopt_long(argc, argv, "CcdnrEh?", long_options, NULL)) != EOF) {
|
||||
@@ -2623,6 +2698,10 @@ int main(int argc, char *argv[])
|
||||
purge_flag = 1;
|
||||
break;
|
||||
|
||||
case OPT_ROOT:
|
||||
root = optarg;
|
||||
break;
|
||||
|
||||
case OPT_EXCLUDE_PREFIX:
|
||||
add_exclude_prefix(optarg);
|
||||
break;
|
||||
@@ -2654,7 +2733,25 @@ int main(int argc, char *argv[])
|
||||
return 1;
|
||||
}
|
||||
|
||||
process(argc - optind, &argv[optind]);
|
||||
/* Those on the command line are of the old root, also with --root */
|
||||
argc -= optind;
|
||||
argv += optind;
|
||||
fp = calloc(argc + 1, sizeof(FILE *));
|
||||
if (!fp)
|
||||
err(1, "Failed opening files");
|
||||
for (c = 0; c < argc; c++) {
|
||||
fp[c] = strcmp(argv[c], "-") ? fopen(argv[c], "r") : stdin;
|
||||
if (!fp[c])
|
||||
warn("Failed to open %s", argv[c]);
|
||||
}
|
||||
|
||||
if (root && strcmp(root, "/"))
|
||||
change_root(root);
|
||||
else
|
||||
root = NULL;
|
||||
|
||||
process(argc, argv, fp);
|
||||
free(fp);
|
||||
|
||||
if (purge_flag) {
|
||||
purging = 1;
|
||||
|
||||
@@ -86,6 +86,7 @@ EXTRA_DIST += tmpfiles-arg.sh
|
||||
EXTRA_DIST += tmpfiles-attr.sh
|
||||
EXTRA_DIST += tmpfiles-perm.sh
|
||||
EXTRA_DIST += tmpfiles-purge.sh
|
||||
EXTRA_DIST += tmpfiles-root.sh
|
||||
EXTRA_DIST += tmpfiles-boot.sh
|
||||
EXTRA_DIST += tmpfiles-clean.sh
|
||||
EXTRA_DIST += tmpfiles-copy.sh
|
||||
@@ -176,6 +177,7 @@ TESTS += tmpfiles-arg.sh
|
||||
TESTS += tmpfiles-attr.sh
|
||||
TESTS += tmpfiles-perm.sh
|
||||
TESTS += tmpfiles-purge.sh
|
||||
TESTS += tmpfiles-root.sh
|
||||
TESTS += tmpfiles-boot.sh
|
||||
TESTS += tmpfiles-clean.sh
|
||||
TESTS += tmpfiles-copy.sh
|
||||
|
||||
Executable
+63
@@ -0,0 +1,63 @@
|
||||
#!/bin/sh
|
||||
# Verify tmpfiles --root, which runs on the system below a directory:
|
||||
# its tmpfiles.d files, its users, and its paths
|
||||
set -eu
|
||||
|
||||
TEST_DIR=$(dirname "$0")
|
||||
|
||||
ROOT=/tmp/root
|
||||
CONF=/tmp/root.conf
|
||||
|
||||
assert_stat()
|
||||
{
|
||||
assert "$1 is $2" "$(texec stat -c '%a %u %g' "$ROOT$1")" = "$2"
|
||||
}
|
||||
|
||||
test_teardown()
|
||||
{
|
||||
say "Running test teardown."
|
||||
run "rm -rf $ROOT $CONF"
|
||||
}
|
||||
|
||||
# shellcheck source=/dev/null
|
||||
. "$TEST_DIR/lib/setup.sh"
|
||||
|
||||
run "mkdir -p $ROOT/etc/tmpfiles.d $ROOT/usr/share/factory/etc"
|
||||
run "echo 'app:x:1:1::/:/bin/sh' > $ROOT/etc/passwd"
|
||||
run "echo 'grp:x:2:' > $ROOT/etc/group"
|
||||
run "echo factory > $ROOT/usr/share/factory/etc/issue"
|
||||
run "cat > $ROOT/etc/tmpfiles.d/root.conf" <<CONF
|
||||
d /var/lib/root-test 0750 app grp
|
||||
f /var/lib/root-test/file 0600 - - - inside
|
||||
f /var/lib/root-test/other 0600
|
||||
z /var/lib/root-test/other 0644
|
||||
L /link - - - - /var/lib/root-test
|
||||
f /link/through 0640
|
||||
C /etc/issue
|
||||
d /skipped 0700 daemon
|
||||
CONF
|
||||
run "echo 'd /from/cmdline 0711' > $CONF"
|
||||
|
||||
# The leak check of a sanitizer build needs /proc, the root has none
|
||||
tmpfiles()
|
||||
{
|
||||
run "ASAN_OPTIONS=detect_leaks=0 /libexec/finit/tmpfiles --create --root=$ROOT $*"
|
||||
}
|
||||
tmpfiles
|
||||
|
||||
say 'The tmpfiles.d files, users, and groups are those below the root'
|
||||
assert_stat /var/lib/root-test "750 1 2"
|
||||
assert "File content" "$(texec cat $ROOT/var/lib/root-test/file)" = "inside"
|
||||
assert "No user daemon there" "$(texec sh -c "ls $ROOT | grep -c skipped")" = "0"
|
||||
|
||||
sep
|
||||
say 'So are the paths, also what a link points to'
|
||||
assert_stat /var/lib/root-test/other "644 0 0"
|
||||
assert_stat /var/lib/root-test/through "640 0 0"
|
||||
assert "Copied from its factory" "$(texec cat $ROOT/etc/issue)" = "factory"
|
||||
assert "Nothing outside it" "$(texec sh -c "ls /var/lib | grep -c root-test")" = "0"
|
||||
|
||||
sep
|
||||
say 'A file on the command line is not below the root'
|
||||
tmpfiles $CONF
|
||||
assert_stat /from/cmdline "711 0 0"
|
||||
Reference in new issue
Block a user