From fa430d0a02b066a6822771c3d62643337d9e8e82 Mon Sep 17 00:00:00 2001 From: Joachim Wiberg Date: Sat, 21 Nov 2026 19:07:45 +0100 Subject: [PATCH] tmpfiles: read a file named - from standard input A package script that has its entries at hand, and not yet in a file, gives them to systemd-tmpfiles on standard input: echo 'd /var/lib/app 0750 app app' | tmpfiles --create - Finit's tmpfiles took - as the name of a file and failed to open it. Signed-off-by: Joachim Wiberg --- doc/ChangeLog.md | 2 ++ src/tmpfiles.c | 22 ++++++++++++---------- test/tmpfiles-files.sh | 5 +++++ 3 files changed, 19 insertions(+), 10 deletions(-) diff --git a/doc/ChangeLog.md b/doc/ChangeLog.md index 29dae51d..68d141f3 100644 --- a/doc/ChangeLog.md +++ b/doc/ChangeLog.md @@ -59,6 +59,8 @@ All relevant changes are documented in this file. 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: a file named `-` on the command line is read from standard + input - 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 a568c706..3d681edf 100644 --- a/src/tmpfiles.c +++ b/src/tmpfiles.c @@ -2351,12 +2351,14 @@ static void run_entries(int create) static void process_file(const char *fn) { - FILE *fp; + FILE *fp = stdin; - fp = fopen(fn, "r"); - if (!fp) { - warn("Failed to open %s", fn); - return; + if (strcmp(fn, "-")) { + fp = fopen(fn, "r"); + if (!fp) { + warn("Failed to open %s", fn); + return; + } } conf = fn; @@ -2364,11 +2366,9 @@ static void process_file(const char *fn) printf("# %s\n", fn); fcopyfile(fp, stdout); puts(""); - fclose(fp); - return; } - while (!feof(fp)) { + while (!cat_config && !feof(fp)) { /* No comment character, a # is only a comment at the start of a line */ const char delim[3] = { '\\', '\\', 0 }; char *line; @@ -2381,7 +2381,8 @@ static void process_file(const char *fn) free(line); } - fclose(fp); + if (fp != stdin) + fclose(fp); } static int by_name(const void *a, const void *b) @@ -2466,7 +2467,8 @@ static int usage(int rc) " -h, --help This help text\n" "\n" "If no CONFIGFILE is specified, all *.conf files in the standard\n" - "tmpfiles.d directories are processed.\n" + "tmpfiles.d directories are processed. A CONFIGFILE of - is read\n" + "from standard input.\n" "\n"); return rc; diff --git a/test/tmpfiles-files.sh b/test/tmpfiles-files.sh index 73e9bf61..1c81024c 100755 --- a/test/tmpfiles-files.sh +++ b/test/tmpfiles-files.sh @@ -45,3 +45,8 @@ say 'Files are read by name, and the first entry for a path counts' assert "First by file name" "$(texec cat $DIR/first)" = "one+three+four" assert "Told about in a dry run" \ "$(texec sh -c "/libexec/finit/tmpfiles -n --create --prefix=$DIR 2>&1 | grep -c 'already has an entry'")" = "1" + +sep +say 'A file named - is read from standard input' +run "echo 'f $DIR/stdin' | /libexec/finit/tmpfiles --create -" +assert "Entry on stdin" "$(texec sh -c "ls $DIR | grep -c stdin")" = "1"