tmpfiles: fix C entries, never copy over what exists

A C entry for a directory does the opposite of tmpfiles.d(5):

    C /etc/pam.d - - - -

When /etc/pam.d does not exist the entry fails with "Not a directory",
the files of the source are copied to a file with the name of the
path, one over the other.  When it is an empty directory nothing is
copied.  When it has files in it, they are overwritten with the ones
from /usr/share/factory, at every boot.  For a file, an existing one
is overwritten as well.

is_dir_empty() returns true for a directory that is not empty, and
rsync() from libite copies over what is there and needs a slash at the
end of a destination directory that is missing.

Copy with a tree walk of our own instead.  It copies to a path that
is missing or an empty directory, overwrites nothing, and does not
follow links.  Mode, owner, and modification time are those of the
source.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-21 17:14:04 +01:00
1 parent 9160ca3ddf
commit 7b3b217e60
4 files changed
+258 -29

No files matched your search

+6
View File
@@ -337,6 +337,12 @@ All relevant changes are documented in this file.
- Fix tmpfiles `w` entries for a regular file, the file was truncated. - Fix tmpfiles `w` entries for a regular file, the file was truncated.
As in tmpfiles.d(5), the argument is now written over the start of what As in tmpfiles.d(5), the argument is now written over the start of what
the file holds the file holds
- Fix tmpfiles `C` entries. A directory was not copied to a path that
did not exist, or to an empty directory, and was copied over a
directory that had files in it, at every boot, as with the `/etc/pam.d`
entry in Finit's own `etc.conf`. A file was copied over an existing
one. As in tmpfiles.d(5), nothing that exists is now overwritten, and
links are copied as links
- Fix logins refused after boot on a system with systemd's tmpfiles.d - Fix logins refused after boot on a system with systemd's tmpfiles.d
files installed. They create `/run/nologin` at boot, and on systemd it files installed. They create `/run/nologin` at boot, and on systemd it
is removed by systemd-user-sessions. Finit now removes it when leaving is removed by systemd-user-sessions. Finit now removes it when leaving
+194 -29
View File
@@ -131,7 +131,7 @@ static int is_dir_empty(const char *path)
free(namelist[i]); free(namelist[i]);
free(namelist); free(namelist);
return num >= 3; return num < 3;
} }
static int glob_do(const char *path, int (*cb)(const char *)) static int glob_do(const char *path, int (*cb)(const char *))
@@ -183,6 +183,195 @@ static void mkparent(char *path, mode_t mode)
mkpath(dirname(strdupa(path)), mode); mkpath(dirname(strdupa(path)), mode);
} }
static int fd_chmod(int fd, mode_t mode)
{
char proc[32];
if (!fchmod(fd, mode))
return 0;
if (errno != EBADF)
return -1;
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
return chmod(proc, mode);
}
static int copy_errno;
static void copy_fail(void)
{
if (!copy_errno)
copy_errno = errno;
}
/* Owner before mode, as in perm_set(). Not root when run by hand */
static void copy_perm(int fd, const struct stat *st)
{
if ((fchownat(fd, "", st->st_uid, st->st_gid, AT_EMPTY_PATH) && errno != EPERM) ||
fd_chmod(fd, st->st_mode & 07777))
copy_fail();
}
static void copy_file(int sdir, const char *sname, int ddir, const char *dname, const struct stat *st)
{
struct timespec tv[2] = { st->st_atim, st->st_mtim };
char buf[BUFSIZ];
ssize_t len = -1;
int in, out;
in = openat(sdir, sname, O_RDONLY | O_NOFOLLOW | O_CLOEXEC);
if (in < 0) {
copy_fail();
return;
}
out = openat(ddir, dname, O_WRONLY | O_CREAT | O_EXCL | O_NOFOLLOW | O_CLOEXEC, 0600);
if (out < 0) {
if (errno != EEXIST)
copy_fail();
close(in);
return;
}
while ((len = read(in, buf, sizeof(buf))) > 0) {
for (ssize_t off = 0, num; off < len; off += num) {
num = write(out, &buf[off], len - off);
if (num < 0) {
len = -1;
goto done;
}
}
}
done:
if (len < 0)
copy_fail();
else
copy_perm(out, st);
futimens(out, tv);
close(in);
close(out);
}
/*
* Copy @sname in the directory @sdir to @dname in @ddir, with all that
* is below it. What exists at the destination is left as it is, and
* only a real directory there is gone into, never a link to one.
*/
static void copy_node(int sdir, const char *sname, int ddir, const char *dname)
{
char target[PATH_MAX];
int sfd, dfd, created;
struct dirent *d;
struct stat st;
ssize_t len;
DIR *dir;
if (fstatat(sdir, sname, &st, AT_SYMLINK_NOFOLLOW)) {
copy_fail();
return;
}
switch (st.st_mode & S_IFMT) {
case S_IFDIR:
break;
case S_IFREG:
copy_file(sdir, sname, ddir, dname, &st);
return;
case S_IFLNK:
len = readlinkat(sdir, sname, target, sizeof(target) - 1);
if (len < 0) {
copy_fail();
return;
}
target[len] = 0;
if (symlinkat(target, ddir, dname)) {
if (errno != EEXIST)
copy_fail();
return;
}
if (fchownat(ddir, dname, st.st_uid, st.st_gid, AT_SYMLINK_NOFOLLOW) && errno != EPERM)
copy_fail();
return;
default:
if (mknodat(ddir, dname, (st.st_mode & S_IFMT) | 0600, st.st_rdev)) {
if (errno != EEXIST)
copy_fail();
return;
}
dfd = openat(ddir, dname, O_PATH | O_NOFOLLOW | O_CLOEXEC);
if (dfd < 0) {
copy_fail();
return;
}
copy_perm(dfd, &st);
close(dfd);
return;
}
created = !mkdirat(ddir, dname, 0700);
if (!created && errno != EEXIST) {
copy_fail();
return;
}
dfd = openat(ddir, dname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
if (dfd < 0) {
if (created)
copy_fail();
return;
}
sfd = openat(sdir, sname, O_RDONLY | O_DIRECTORY | O_NOFOLLOW | O_CLOEXEC);
dir = sfd < 0 ? NULL : fdopendir(sfd);
if (!dir) {
copy_fail();
if (sfd >= 0)
close(sfd);
close(dfd);
return;
}
while ((d = readdir(dir))) {
if (!strcmp(d->d_name, ".") || !strcmp(d->d_name, ".."))
continue;
copy_node(sfd, d->d_name, dfd, d->d_name);
}
closedir(dir);
if (created)
copy_perm(dfd, &st);
close(dfd);
}
/*
* Copy @src, of which @st tells the kind, to @dst. Links are not
* followed. A file is not copied when @dst exists. A directory is
* when @dst is missing or an empty directory, with @merge also when
* @dst has something in it, to add what is missing.
*/
static int copy_tree(const char *src, const struct stat *st, char *dst, int merge)
{
struct stat dt;
if (!lstat(dst, &dt)) {
if (!S_ISDIR(st->st_mode) || !S_ISDIR(dt.st_mode))
return 0;
if (!merge && !is_dir_empty(dst))
return 0;
}
mkparent(dst, 0755);
copy_errno = 0;
copy_node(AT_FDCWD, src, AT_FDCWD, dst);
if (copy_errno) {
errno = copy_errno;
return -1;
}
return 0;
}
static char *write_hex(FILE *fp, char *p) static char *write_hex(FILE *fp, char *p)
{ {
unsigned char val; unsigned char val;
@@ -495,20 +684,6 @@ struct perm {
}; };
/* Also for an fd opened with O_PATH, which fchmod() does not take */ /* Also for an fd opened with O_PATH, which fchmod() does not take */
static int fd_chmod(int fd, mode_t mode)
{
char proc[32];
if (!fchmod(fd, mode))
return 0;
if (errno != EBADF)
return -1;
snprintf(proc, sizeof(proc), "/proc/self/fd/%d", fd);
return chmod(proc, mode);
}
/* /*
* Set mode and owner on an open fd, only what differs is touched, the * Set mode and owner on an open fd, only what differs is touched, the
* path may be immutable. @created tells if the entry made the path. * path may be immutable. @created tells if the entry made the path.
@@ -1739,22 +1914,12 @@ static void tmpfiles(char *line)
paste(buf, sizeof(buf), "/usr/share/factory", path); paste(buf, sizeof(buf), "/usr/share/factory", path);
arg = buf; arg = buf;
} }
if (fisdir(path) && !is_dir_empty(path)) if (lstat(arg, &ast))
break; break;
if (fisdir(arg) && !fisslashdir(arg)) {
size_t len = strlen(arg) + 2;
dst = malloc(len); rc = copy_tree(arg, &ast, path, 0);
if (!dst) if (!rc && !lstat(path, &st) && !S_ISLNK(st.st_mode) && !((st.st_mode ^ ast.st_mode) & S_IFMT) &&
break; perm_path(path, &perm, strc, O_PATH))
snprintf(dst, len, "%s/", arg);
arg = dst;
}
mkparent(path, 0755);
rc = rsync(arg, path, LITE_FOPT_KEEP_MTIME, NULL);
if (rc && errno == ENOENT)
rc = 0;
else if (!rc && (strc || S_ISDIR(st.st_mode)) && perm_path(path, &perm, strc, O_PATH))
warn("Failed setting mode/owner on %s", path); warn("Failed setting mode/owner on %s", path);
break; break;
case 'd': case 'd':
+2
View File
@@ -87,6 +87,7 @@ EXTRA_DIST += tmpfiles-attr.sh
EXTRA_DIST += tmpfiles-perm.sh EXTRA_DIST += tmpfiles-perm.sh
EXTRA_DIST += tmpfiles-boot.sh EXTRA_DIST += tmpfiles-boot.sh
EXTRA_DIST += tmpfiles-clean.sh EXTRA_DIST += tmpfiles-clean.sh
EXTRA_DIST += tmpfiles-copy.sh
EXTRA_DIST += tmpfiles-order.sh EXTRA_DIST += tmpfiles-order.sh
EXTRA_DIST += tmpfiles-specifier.sh EXTRA_DIST += tmpfiles-specifier.sh
EXTRA_DIST += tmpfiles-unsupported.sh EXTRA_DIST += tmpfiles-unsupported.sh
@@ -173,6 +174,7 @@ TESTS += tmpfiles-attr.sh
TESTS += tmpfiles-perm.sh TESTS += tmpfiles-perm.sh
TESTS += tmpfiles-boot.sh TESTS += tmpfiles-boot.sh
TESTS += tmpfiles-clean.sh TESTS += tmpfiles-clean.sh
TESTS += tmpfiles-copy.sh
TESTS += tmpfiles-order.sh TESTS += tmpfiles-order.sh
TESTS += tmpfiles-specifier.sh TESTS += tmpfiles-specifier.sh
TESTS += tmpfiles-unsupported.sh TESTS += tmpfiles-unsupported.sh
+56
View File
@@ -0,0 +1,56 @@
#!/bin/sh
# Verify the C entry of tmpfiles, copy a file or a tree
set -eu
TEST_DIR=$(dirname "$0")
DIR=/tmp/copy
SRC=/tmp/copy-src
CONF=/tmp/copy.conf
content()
{
texec sh -c "cat $1 2>/dev/null || echo none"
}
test_teardown()
{
say "Running test teardown."
run "rm -rf $DIR $SRC $CONF"
}
# shellcheck source=/dev/null
. "$TEST_DIR/lib/setup.sh"
run "mkdir -p $SRC/sub $DIR/empty $DIR/full/sub $DIR/merge/sub"
run "echo new > $SRC/file; echo new > $SRC/sub/file; echo new > $SRC/sub/other; ln -s file $SRC/link; chmod 640 $SRC/file"
run "echo old > $DIR/full/file; echo old > $DIR/merge/sub/file; echo old > $DIR/one-old"
run "cat > $CONF" <<CONF
C $DIR/tree - - - - $SRC
C $DIR/empty - - - - $SRC
C $DIR/full - - - - $SRC
C $DIR/one 0600 - - - $SRC/file
C $DIR/one-old - - - - $SRC/file
C $DIR/missing - - - - /no/such/path
CONF
run "/libexec/finit/tmpfiles --create $CONF"
say 'C copies a tree, links are not followed'
assert "File" "$(content $DIR/tree/file)" = "new"
assert "File in subdir" "$(content $DIR/tree/sub/file)" = "new"
assert "Mode is kept" "$(texec stat -c %a $DIR/tree/file)" = "640"
assert "Link is a link" "$(texec readlink $DIR/tree/link)" = "file"
sep
say 'C fills a directory that is empty, and leaves one that is not'
assert "Empty directory" "$(content $DIR/empty/sub/file)" = "new"
assert "Existing file" "$(content $DIR/full/file)" = "old"
assert "Nothing added" "$(content $DIR/full/sub/file)" = "none"
sep
say 'C copies a file, unless it is there'
assert "New file" "$(content $DIR/one)" = "new"
assert "Mode of entry" "$(texec stat -c %a $DIR/one)" = "600"
assert "Existing file" "$(content $DIR/one-old)" = "old"
assert "No source" "$(texec sh -c "ls $DIR | grep -c missing")" = "0"