diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 392b3045..29dae51d 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -52,6 +52,13 @@ All relevant changes are documented in this file. tmpfiles.d(5), a path is created after the paths above it, and removed after those below it, wherever in the files the entries are. Entries that take a glob, like `w` and `z`, are run after the others +- tmpfiles: the files are now read in the order of their names, whatever + directory they are in, and the first entry that makes a path is the one + that counts, as in tmpfiles.d(5). Before, they were read one directory + at a time and every entry for a path was run, so the last one won. To + change an entry from a file in `/usr/lib/tmpfiles.d`, use a file of the + same name in `/etc/tmpfiles.d`, or one with a name that sorts before it. + `tmpfiles --dry-run` tells which entries are skipped - 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` diff --git a/doc/plugins.md b/doc/plugins.md index 083f8766..6b35e47b 100644 --- a/doc/plugins.md +++ b/doc/plugins.md @@ -27,15 +27,22 @@ For your convenience a set of *optional* plugins are available: runs `tmpfiles --create --remove --boot`, so entries marked boot-only with a `!` are run here and at no other time. - By default, `/lib/finit/tmpfiles.d` carries all the default .conf - files distributed with Finit. It is read first but can be overridden - by any of the standard tmpfiles.d directories, e.g. `/etc/tmpfiles.d`. + The .conf files distributed with Finit are in `/lib/finit/tmpfiles.d` + and `/usr/lib/tmpfiles.d`. A file of the same name in `/run/tmpfiles.d` + or `/etc/tmpfiles.d` is read in its place. The files that remain are + read in the order of their names, whatever directory they are in, and + when two entries make the same path, the first one counts. So to + change an entry, copy its file to `/etc/tmpfiles.d` and edit it there, + or put the entry in a file with a name that sorts before it. To see what the files on a system amount to, without anything being done, add `--dry-run`: /libexec/finit/tmpfiles --dry-run --create --remove --boot + It also tells which entries are skipped because a path already has + one. + The age of an entry, e.g. `10d` below, is used only by `tmpfiles --clean`, which Finit never runs on its own: diff --git a/doc/tmpfiles-todo.md b/doc/tmpfiles-todo.md index 9600414b..6345e4b3 100644 --- a/doc/tmpfiles-todo.md +++ b/doc/tmpfiles-todo.md @@ -18,15 +18,3 @@ files or common packages already rely on, last what few will miss. --------------- Missing options: `--root`, `--replace`, and `--purge`. - - -3. Smaller things ------------------- - -* Files are processed one directory at a time. tmpfiles.d(5) sorts - all of them by name, whatever directory they are in, so `00-x.conf` - in `/etc` should run before `50-y.conf` in `/usr/lib`. Sorting is - not enough: with two entries for the same path systemd goes by the - first and Finit runs both, so the last one wins. Today that is the - one in `/etc`, which is what an admin expects. Sorted by name it - would be the one in `/usr/lib`. Both changes have to go in together diff --git a/src/tmpfiles.c b/src/tmpfiles.c index 4b7ba777..a568c706 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -1724,11 +1724,13 @@ struct entry { char *age, *arg; ssize_t arglen; /* Of an argument that was in Base64, else -1 */ int plus, kind; + unsigned sum; /* Of the line, to tell if two are the same */ int done; }; static struct entry *entries; static size_t num_entries; +static const char *conf; /* File being read */ static char *path_key(const char *path) { @@ -1749,6 +1751,49 @@ static char *path_key(const char *path) return key; } +/* Types that take a glob. They change what the others make, and are run after them */ +static int globs(const struct entry *e) +{ + return !!strchr("aAehHrRtTwxXzZ", e->type[0]); +} + +static unsigned line_sum(const char *str, unsigned sum) +{ + for (; *str; str++) { + if ((*str == ' ' || *str == '\t') && (str[1] == ' ' || str[1] == '\t' || !str[1])) + continue; + sum = sum * 33 + (*str == '\t' ? ' ' : *str); + } + + return sum; +} + +/* + * As in tmpfiles.d(5), the first entry for a path is the one that + * counts. Among those that make the path, and among those that take + * a glob and are not only there to set something on it. A w+ entry + * always counts, it adds to the file. + */ +static int duplicate(const struct entry *e) +{ + const char *types = globs(e) ? "erRwxX" : "bcCdDfFlLp"; + + if (!strchr(types, e->type[0]) || (e->type[0] == 'w' && e->plus)) + return 0; + + for (size_t i = 0; i < num_entries; i++) { + const struct entry *o = &entries[i]; + + if (!strchr(types, o->type[0]) || strcmp(o->key, e->key)) + continue; + if (o->sum != e->sum && (debug || dry_run)) + warnx("%s: %s already has an entry, skipping this %s one", conf, e->path, e->type); + return 1; + } + + return 0; +} + /* * The configuration format is one line per path, containing type, path, * mode, ownership, age, and argument fields. The lines are separated by @@ -1769,6 +1814,7 @@ static int parse(char *line) struct entry e = { .arglen = -1 }; struct entry *tmp; char *token, *user, *group, *end; + char *xpath = NULL, *xarg = NULL; int boot = 0, b64 = 0; e.type = field(&line); @@ -1818,6 +1864,7 @@ static int parse(char *line) } if (excluded(e.path)) return -1; + e.sum = line_sum(line, line_sum(e.type, 0)); token = field(&line); if (token) { @@ -1883,14 +1930,22 @@ static int parse(char *line) } if (e.path == pathbuf) - e.path = strdup(pathbuf); - if (e.arg == argbuf && !(e.arg = strdup(argbuf))) + e.path = xpath = strdup(pathbuf); + if (e.arg == argbuf && !(e.arg = xarg = strdup(argbuf))) e.path = NULL; e.key = e.path ? path_key(e.path) : NULL; tmp = reallocarray(entries, num_entries + 1, sizeof(e)); if (!e.key || !tmp) err(1, "Failed reading entry"); entries = tmp; + if (duplicate(&e)) { + free(e.key); + if (e.path == xpath) + free(xpath); + if (e.arg == xarg) + free(xarg); + return -1; + } entries[num_entries++] = e; /* Cleaning needs every x and X entry before the first one is run */ @@ -2259,12 +2314,6 @@ static void tmpfiles(struct entry *e) warn("Failed %s operation on path %s", type, path); } -/* Types that take a glob. They change what the others make, and are run after them */ -static int globs(const struct entry *e) -{ - return !!strchr("aAehHrRtTwxXzZ", e->type[0]); -} - /* * A path is created after those above it, and removed or cleaned * after those below it, wherever in the files they are. @@ -2309,6 +2358,7 @@ static void process_file(const char *fn) warn("Failed to open %s", fn); return; } + conf = fn; if (cat_config) { printf("# %s\n", fn); @@ -2334,6 +2384,11 @@ static void process_file(const char *fn) fclose(fp); } +static int by_name(const void *a, const void *b) +{ + return strcmp(basenm(*(char **)a), basenm(*(char **)b)); +} + static void process(int num, char *file[]) { /* If config files specified on command line, process only those */ @@ -2348,11 +2403,11 @@ static void process(int num, char *file[]) * * Only the three last tmpfiles.d/ directories are defined in * tmpfiles.d(5) as system search paths. Finit adds two more - * before that to have Finit specific ones sorted first, and - * a configure prefix specific one after that for user needs. + * before that, for its own files and for the configure prefix. + * A file overrides one of the same name in a directory before + * its own, and what is left is read in order of the file names, + * whatever directory they are in. */ - - /* in priority order */ char *dir[] = { FINIT_TMPFILES "/*.conf", TMPFILES_PATH_ "/*.conf", @@ -2361,32 +2416,33 @@ static void process(int num, char *file[]) "/etc/tmpfiles.d/*.conf", /* local admin overrides */ }; int flags = GLOB_NOESCAPE; + size_t i, j, cnt = 0; + char **list; glob_t gl; - size_t i; for (i = 0; i < NELEMS(dir); i++) { glob(dir[i], flags, NULL, &gl); flags |= GLOB_APPEND; } + list = calloc(gl.gl_pathc + 1, sizeof(char *)); + if (!list) + err(1, "Failed listing files"); + for (i = 0; i < gl.gl_pathc; i++) { - char *fn = gl.gl_pathv[i]; - size_t j; - - /* check for overrides */ for (j = i + 1; j < gl.gl_pathc; j++) { - if (strcmp(basenm(fn), basenm(gl.gl_pathv[j]))) - continue; - fn = NULL; - break; + if (!strcmp(basenm(gl.gl_pathv[i]), basenm(gl.gl_pathv[j]))) + break; } - - if (!fn) - continue; /* skip, override exists */ - - process_file(fn); + if (j == gl.gl_pathc) + list[cnt++] = gl.gl_pathv[i]; } + qsort(list, cnt, sizeof(char *), by_name); + for (i = 0; i < cnt; i++) + process_file(list[i]); + + free(list); globfree(&gl); } diff --git a/test/Makefile.am b/test/Makefile.am index 5a660d16..f58d7e49 100644 --- a/test/Makefile.am +++ b/test/Makefile.am @@ -88,6 +88,7 @@ EXTRA_DIST += tmpfiles-perm.sh EXTRA_DIST += tmpfiles-boot.sh EXTRA_DIST += tmpfiles-clean.sh EXTRA_DIST += tmpfiles-copy.sh +EXTRA_DIST += tmpfiles-files.sh EXTRA_DIST += tmpfiles-match.sh EXTRA_DIST += tmpfiles-order.sh EXTRA_DIST += tmpfiles-specifier.sh @@ -176,6 +177,7 @@ TESTS += tmpfiles-perm.sh TESTS += tmpfiles-boot.sh TESTS += tmpfiles-clean.sh TESTS += tmpfiles-copy.sh +TESTS += tmpfiles-files.sh TESTS += tmpfiles-match.sh TESTS += tmpfiles-order.sh TESTS += tmpfiles-specifier.sh diff --git a/test/tmpfiles-arg.sh b/test/tmpfiles-arg.sh index 95003cbb..d54e73d2 100755 --- a/test/tmpfiles-arg.sh +++ b/test/tmpfiles-arg.sh @@ -71,9 +71,10 @@ run "printf abcdef > $DIR/w1" run "cat > $CONF" <<'CONF' w /tmp/arg/w1 - - - - XY w+ /tmp/arg/w1 - - - - Z +w+ /tmp/arg/w1 - - - - Z CONF run "/libexec/finit/tmpfiles --create $CONF" -assert "Written in place" "$(texec cat $DIR/w1)" = "XYcdefZ" +assert "Written in place, added to by each w+" "$(texec cat $DIR/w1)" = "XYcdefZZ" sep say 'Escapes, and a # that is not at the start of the line' diff --git a/test/tmpfiles-files.sh b/test/tmpfiles-files.sh new file mode 100755 index 00000000..73e9bf61 --- /dev/null +++ b/test/tmpfiles-files.sh @@ -0,0 +1,47 @@ +#!/bin/sh +# Verify which tmpfiles.d files are read, in what order, and that the +# first entry for a path is the one that counts +set -eu + +TEST_DIR=$(dirname "$0") + +DIR=/tmp/files +LIB=/usr/lib/tmpfiles.d +ETC=/etc/tmpfiles.d + +test_teardown() +{ + say "Running test teardown." + run "rm -rf $DIR $LIB/[0-9]0-test.conf $ETC/[0-9]0-test.conf" +} + +# shellcheck source=/dev/null +. "$TEST_DIR/lib/setup.sh" + +run "mkdir -p $LIB $ETC" +run "cat > $LIB/50-test.conf" < $ETC/50-test.conf" < $LIB/10-test.conf" < $ETC/20-test.conf" <&1 | grep -c 'already has an entry'")" = "1" diff --git a/tmpfiles.d/legacy.conf b/tmpfiles.d/legacy.conf index 1f8040fe..26ed1c27 100644 --- a/tmpfiles.d/legacy.conf +++ b/tmpfiles.d/legacy.conf @@ -1,8 +1,4 @@ -d /run/lock 0755 root root - -L /var/lock - - - - ../run/lock - -d /run/lock/subsys 0755 root root - - +# For /run/lock and /var/lock, see var.conf L /run/shm - - - - ../dev/shm # These are not supported by Finit, see command line options diff --git a/tmpfiles.d/var.conf b/tmpfiles.d/var.conf index a7cce202..abf55f0d 100644 --- a/tmpfiles.d/var.conf +++ b/tmpfiles.d/var.conf @@ -19,8 +19,7 @@ f /run/console/console.lock 0644 - - - nobody d /run/lock 0777 - - - d /run/lock/subsys 0755 - - - -# Compat symlink to new /run ramdisk -L /var/run - - - - ../run +# Compat symlink to new /run ramdisk, for /var/run see finit.conf L /var/lock - - - - ../run/lock # Needed by Debian/Ubuntu ifupdown @@ -35,7 +34,6 @@ d /var/empty 0755 - - - d /var/log 0755 - - - d /var/mail 0755 - - - d /var/opt 0755 - - - -d /var/spool 0755 - - - d /var/spool/cron 0755 - - - # UTMP actually needs multiple db files