diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index af5468ec..392b3045 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -48,6 +48,10 @@ All relevant changes are documented in this file. - tmpfiles: add the `=` modifier, as in `d=`, to first remove what is in place of the path when it is of another kind than the entry makes, also what is in place of a directory above it +- tmpfiles: every entry is now read before the first one is run. As in + 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: 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/src/tmpfiles.c b/src/tmpfiles.c index dce4ceb2..4b7ba777 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -65,9 +65,6 @@ int boot_flag = 0; int dry_run = 0; int cat_config = 0; -/* Set for the first of two passes, the second one does the complaining */ -static int quiet; - #define MAX_EXCLUDES 32 static const char *exclude_prefixes[MAX_EXCLUDES]; static int num_excludes; @@ -770,7 +767,6 @@ struct keep { static struct keep *keeps; static int num_keeps; -static int collect; /* Pass that only gathers the above */ static const char *keep_own; /* Entry that cleans what it keeps, by its own age */ static void keep_add(const char *pattern, int self) @@ -1712,14 +1708,47 @@ static char *field(char **line) *out = 0; *line = in; if (quote || nul || out == start) { - if (!quiet) - warnx("Missing end quote, or an empty field, skipping %s", start); + warnx("Missing end quote, or an empty field, skipping %s", start); return NULL; } return start; } +/* One line of a file, read by parse() and run by tmpfiles() */ +struct entry { + char *type; /* As written, the modifiers after the letter */ + char *path; + char *key; /* The path with no // and no / at the end */ + struct perm perm; + char *age, *arg; + ssize_t arglen; /* Of an argument that was in Base64, else -1 */ + int plus, kind; + int done; +}; + +static struct entry *entries; +static size_t num_entries; + +static char *path_key(const char *path) +{ + char *key, *out; + + key = out = strdup(path); + if (!key) + return NULL; + + for (; *path; path++) { + if (path[0] != '/' || path[1] != '/') + *out++ = *path; + } + while (out > &key[1] && out[-1] == '/') + out--; + *out = 0; + + return key; +} + /* * The configuration format is one line per path, containing type, path, * mode, ownership, age, and argument fields. The lines are separated by @@ -1730,98 +1759,91 @@ static char *field(char **line) * L /tmp/foobar - - - - /dev/null * * https://www.freedesktop.org/software/systemd/man/tmpfiles.d.html + * + * Returns -1 for a line that is not kept as an entry. The entry of one + * that is points into @line. */ -static void tmpfiles(char *line) +static int parse(char *line) { - char *type, *path, *token, *user, *group, *age, *arg; - char *dst = NULL, *opts = ""; - struct stat st, ast; - int major, minor; - int strc, rc = 0; - struct perm perm = { 0 }; - int plus = 0, boot = 0, b64 = 0; - mode_t kind = 0; - ssize_t arglen = -1; - FILE *fp = NULL; - char buf[1024], pathbuf[PATH_MAX], argbuf[1024]; - glob_t gl; + char pathbuf[PATH_MAX], argbuf[1024]; + struct entry e = { .arglen = -1 }; + struct entry *tmp; + char *token, *user, *group, *end; + int boot = 0, b64 = 0; - type = field(&line); - if (!type || type[0] == '#') - return; + e.type = field(&line); + if (!e.type || e.type[0] == '#') + return -1; - for (char *mod = &type[1]; *mod; mod++) { + for (char *mod = &e.type[1]; *mod; mod++) { if (*mod == '+') - plus = 1; + e.plus = 1; else if (*mod == '!') boot = 1; else if (*mod == '~') b64 = 1; else if (*mod == '=') - kind = 1; - else if (*mod == '?' && type[0] == 'L') - type[0] = 'l'; /* What Finit has had as l */ + e.kind = 1; + else if (*mod == '?' && e.type[0] == 'L') + e.type[0] = 'l'; /* What Finit has had as l */ else if (*mod == '^') - return; /* Argument is a credential, there are none */ + return -1; /* Argument is a credential, there are none */ } if (boot && !boot_flag) - return; + return -1; /* No btrfs subvolumes, a plain directory as tmpfiles.d(5) has for other file systems */ - if (strchr("vqQ", type[0])) - type[0] = 'd'; + if (strchr("vqQ", e.type[0])) + e.type[0] = 'd'; - path = field(&line); - if (!path) - return; - if (strchr(path, '%')) { - char *ptr = expand(path, pathbuf, sizeof(pathbuf)); + e.path = field(&line); + if (!e.path) + return -1; + if (!strchr("aAbcCdDefFhHlLprRtTwxXzZ", e.type[0])) { + warnx("Unsupported tmpfiles command '%s', skipping %s", e.type, e.path); + return -1; + } + if (strchr(e.path, '%')) { + char *ptr = expand(e.path, pathbuf, sizeof(pathbuf)); if (!ptr) { - if (!quiet) - warnx("Unsupported specifier or too long, skipping %s", path); - return; + warnx("Unsupported specifier or too long, skipping %s", e.path); + return -1; } - path = ptr; + e.path = pathbuf; } - if (path[0] != '/') { - if (!quiet) - warnx("Path is not absolute, skipping %s", path); - return; - } - if (excluded(path)) - return; - if (collect) { - if (type[0] == 'x' || type[0] == 'X') - keep_add(path, type[0] == 'X'); - return; + if (e.path[0] != '/') { + warnx("Path is not absolute, skipping %s", e.path); + return -1; } + if (excluded(e.path)) + return -1; token = field(&line); if (token) { for (; *token == '~' || *token == ':'; token++) { if (*token == '~') - perm.mask = 1; + e.perm.mask = 1; else - perm.new_mode = 1; + e.perm.new_mode = 1; } errno = 0; - perm.mode = strtoul(token, &arg, 8); - if (errno || arg == token) - perm.mode = 0; + e.perm.mode = strtoul(token, &end, 8); + if (errno || end == token) + e.perm.mode = 0; } user = field(&line); if (user && user[0] == ':') { - perm.new_uid = 1; + e.perm.new_uid = 1; user++; } if (user && !strcmp(user, "-")) user = NULL; group = field(&line); if (group && group[0] == ':') { - perm.new_gid = 1; + e.perm.new_gid = 1; group++; } if (group && !strcmp(group, "-")) @@ -1831,50 +1853,75 @@ static void tmpfiles(char *line) * Quiet about it, Finit's own files have entries for users of * services that may not be installed, and for the utmp group */ - perm.uid = parse_uid(user); - if (user && perm.uid < 0) { + e.perm.uid = parse_uid(user); + if (user && e.perm.uid < 0) { if (debug) - warnx("Unknown user %s, skipping %s", user, path); - return; + warnx("Unknown user %s, skipping %s", user, e.path); + return -1; } - perm.gid = parse_gid(group); - if (group && perm.gid < 0 && debug) - warnx("Unknown group %s, not setting the group of %s", group, path); + e.perm.gid = parse_gid(group); + if (group && e.perm.gid < 0 && debug) + warnx("Unknown group %s, not setting the group of %s", group, e.path); - age = field(&line); - arg = line + strspn(line, "\t "); - if (!arg[0] || !strcmp(arg, "-")) - arg = NULL; + e.age = field(&line); + e.arg = line + strspn(line, "\t "); + if (!e.arg[0] || !strcmp(e.arg, "-")) + e.arg = NULL; if (b64) { - if (arg && strchr("fFw", type[0])) - arglen = b64_decode(arg); - if (arg && arglen < 0) { - if (!quiet) - warnx("Argument is not Base64, or not one to decode, skipping %s", path); - return; + if (e.arg && strchr("fFw", e.type[0])) + e.arglen = b64_decode(e.arg); + if (e.arg && e.arglen < 0) { + warnx("Argument is not Base64, or not one to decode, skipping %s", e.path); + return -1; } - } else if (arg && strchr(arg, '%') && strchr("CfFlLtTw", type[0])) { - arg = expand(arg, argbuf, sizeof(argbuf)); - if (!arg) { - if (!quiet) - warnx("Unsupported specifier in argument or too long, skipping %s", path); - return; + } else if (e.arg && strchr(e.arg, '%') && strchr("CfFlLtTw", e.type[0])) { + e.arg = expand(e.arg, argbuf, sizeof(argbuf)); + if (!e.arg) { + warnx("Unsupported specifier in argument or too long, skipping %s", e.path); + return -1; } } + if (e.path == pathbuf) + e.path = strdup(pathbuf); + if (e.arg == argbuf && !(e.arg = 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; + entries[num_entries++] = e; + + /* Cleaning needs every x and X entry before the first one is run */ + if (clean_flag && (e.type[0] == 'x' || e.type[0] == 'X')) + keep_add(e.path, e.type[0] == 'X'); + + return 0; +} + +static void tmpfiles(struct entry *e) +{ + char *type = e->type, *path = e->path, *age = e->age, *arg = e->arg; + char *dst = NULL, *opts = ""; + struct perm perm = e->perm; + ssize_t arglen = e->arglen; + mode_t kind = e->kind; + int plus = e->plus; + struct stat st, ast; + int major, minor; + int strc, rc = 0; + FILE *fp = NULL; + char buf[1024]; + glob_t gl; + if (dry_run) { - if (!strchr("aAbcCdDefFhHlLprRtTwxXzZ", type[0])) { - if (!quiet) - warnx("Unsupported tmpfiles command '%s', skipping %s", type, path); - return; - } - if (remove_flag && strchr("DrR", type[0])) printf("remove %s %s\n", type, path); if (clean_flag && strchr("CdDexX", type[0]) && !parse_age(age, &clean_age)) printf("clean %s %s, older than %s\n", type, path, age); if (create_flag && !strchr("rRxX", type[0])) - printf("create %s %s%s%s\n", type, path, arg && !b64 ? " " : "", arg && !b64 ? arg : ""); + printf("create %s %s%s%s\n", type, path, arg && arglen < 0 ? " " : "", arg && arglen < 0 ? arg : ""); return; } @@ -1937,10 +1984,6 @@ static void tmpfiles(char *line) case 'Z': case 'z': break; - default: - if (!quiet) - warnx("Unsupported tmpfiles command '%s', skipping %s", type, path); - return; } } @@ -2206,10 +2249,6 @@ static void tmpfiles(char *line) } globfree(&gl); break; - default: - if (!quiet) - warnx("Unsupported tmpfiles command '%s', skipping %s", type, path); - return; } } @@ -2220,6 +2259,47 @@ static void tmpfiles(char *line) 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. + */ +static void run_entry(struct entry *e, int create) +{ + if (e->done) + return; + e->done = 1; + + for (size_t i = 0; i < num_entries; i++) { + struct entry *o = &entries[i]; + + if (o->done || !strcmp(o->key, e->key)) + continue; + if (create ? !globs(o) && below(e->key, o->key) : below(o->key, e->key)) + run_entry(o, create); + } + + tmpfiles(e); +} + +static void run_entries(int create) +{ + for (size_t i = 0; i < num_entries; i++) + entries[i].done = 0; + + for (int glob = 0; glob < 2; glob++) { + for (size_t i = 0; i < num_entries; i++) { + if (globs(&entries[i]) == glob) + run_entry(&entries[i], create); + } + } +} + static void process_file(const char *fn) { FILE *fp; @@ -2247,8 +2327,8 @@ static void process_file(const char *fn) if (!line) continue; - tmpfiles(line); - free(line); + if (parse(line)) + free(line); } fclose(fp); @@ -2420,33 +2500,24 @@ int main(int argc, char *argv[]) } } - if (cat_config) { - process(argc - optind, &argv[optind]); - return 0; - } - - if (create_flag + clean_flag + remove_flag == 0) { + if (!cat_config && create_flag + clean_flag + remove_flag == 0) { fprintf(stderr, "You need to specify at least one of --clean, --create, or --remove.\n"); return 1; } - /* Cleaning needs every x and X entry, also those in files not yet read */ - if (clean_flag) { - collect = quiet = 1; - process(argc - optind, &argv[optind]); - collect = quiet = 0; - } + process(argc - optind, &argv[optind]); /* Remove and clean first, or an r entry takes what an f entry above it made */ - if (create_flag && (clean_flag || remove_flag)) { + if (clean_flag || remove_flag) { + int create = create_flag; + create_flag = 0; - quiet = 1; - process(argc - optind, &argv[optind]); - create_flag = 1; - quiet = 0; + run_entries(0); + create_flag = create; clean_flag = remove_flag = 0; } - process(argc - optind, &argv[optind]); + if (create_flag) + run_entries(1); return 0; } diff --git a/test/tmpfiles-order.sh b/test/tmpfiles-order.sh index 4615bf27..0d6d9f9f 100755 --- a/test/tmpfiles-order.sh +++ b/test/tmpfiles-order.sh @@ -1,5 +1,5 @@ #!/bin/sh -# Verify that tmpfiles removes before it creates when asked to do both +# Verify the order tmpfiles runs the entries in set -eu TEST_DIR=$(dirname "$0") @@ -34,5 +34,18 @@ r $DIR/file d $DIR/sub - - - 1d CONF out=$(texec /libexec/finit/tmpfiles --dry-run --create --remove --clean "$CONF") -assert "What it would do" "$(echo "$out" | tr '\n' ';')" = "remove r $DIR/file;clean d $DIR/sub, older than 1d;create f $DIR/dry text;create d $DIR/sub;" +assert "What it would do" "$(echo "$out" | tr '\n' ';')" = "clean d $DIR/sub, older than 1d;remove r $DIR/file;create f $DIR/dry text;create d $DIR/sub;" assert "Nothing is done" "$(texec ls $DIR)" = "file" + +sep +say 'A path is made after the one above it, wherever in the file they are' +run "cat > $CONF" <