tmpfiles: add the % specifiers for path and argument

An entry with a % in the path was skipped, and files from
distributions have them:

    d /run/log/journal/%m 2755 root systemd-journal - -

Expand the specifiers that do not need the OS release or the
architecture: %b, %m, %H, %l, %v, the directories %t, %T, %V, %S, %C,
%L, %h, and the user ones %u, %U, %g, %G.  tmpfiles runs as root, so
the last ones are fixed.  An entry with any other specifier is still
skipped.

The argument of C, f, F, L, and w entries is expanded too, as in
tmpfiles.d(5).  A literal % in such an argument must from now on be
written %%.

Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
Joachim Wiberg committed 2026-11-20 18:03:33 +01:00
1 parent 83f4dceb1b
commit b87d235cf7
9 files changed
+176 -15

No files matched your search

+114 -4
View File
@@ -34,6 +34,7 @@
#include <grp.h>
#include <pwd.h>
#include <sys/sysmacros.h>
#include <sys/utsname.h>
#include <stdlib.h>
#include <string.h>
@@ -847,6 +848,102 @@ static int acl_entry(const char *pattern, char *arg, int recursive, int modify)
return rc;
}
/* First line of @file, without @strip characters, empty when it cannot be read */
static char *specifier_file(const char *file, const char *strip, char *buf, size_t len)
{
size_t i = 0;
FILE *fp;
int c;
fp = fopen(file, "r");
if (fp) {
while ((c = fgetc(fp)) != EOF && c != '\n' && i < len - 1) {
if (!strchr(strip, c))
buf[i++] = c;
}
fclose(fp);
}
buf[i] = 0;
return buf;
}
/*
* The % specifiers of tmpfiles.d(5). tmpfiles runs as root, so the
* user ones are fixed. Not here are those for the OS release and the
* architecture. Returns NULL for one that is unknown or has no value.
*/
static const char *specifier(char c, char *buf, size_t len)
{
struct utsname uts;
switch (c) {
case '%': return "%";
case 'C': return "/var/cache";
case 'g': return "root";
case 'G': return "0";
case 'h': return "/root";
case 'L': return "/var/log";
case 'S': return "/var/lib";
case 't': return "/run";
case 'T': return "/tmp";
case 'u': return "root";
case 'U': return "0";
case 'V': return "/var/tmp";
case 'b':
specifier_file("/proc/sys/kernel/random/boot_id", "-", buf, len);
break;
case 'm':
specifier_file("/etc/machine-id", "", buf, len);
break;
case 'H':
case 'l':
if (gethostname(buf, len))
return NULL;
buf[len - 1] = 0;
if (c == 'l')
buf[strcspn(buf, ".")] = 0;
break;
case 'v':
if (uname(&uts))
return NULL;
strlcpy(buf, uts.release, len);
break;
default:
return NULL;
}
return buf[0] ? buf : NULL;
}
/* Expand the specifiers in @str to @buf, NULL when one fails or @buf is too small */
static char *expand(const char *str, char *buf, size_t len)
{
char val[256];
size_t n = 0;
for (; *str; str++) {
const char *ptr = str;
size_t num = 1;
if (*str == '%') {
/* Also for a % that ends the string, do not step past it */
ptr = specifier(*++str, val, sizeof(val));
if (!ptr)
return NULL;
num = strlen(ptr);
}
if (n + num >= len)
return NULL;
memcpy(&buf[n], ptr, num);
n += num;
}
buf[n] = 0;
return buf;
}
/*
* The configuration format is one line per path, containing type, path,
* mode, ownership, age, and argument fields. The lines are separated by
@@ -868,7 +965,7 @@ static void tmpfiles(char *line)
struct perm perm = { 0 };
int plus = 0, boot = 0;
FILE *fp = NULL;
char buf[1024];
char buf[1024], pathbuf[PATH_MAX], argbuf[1024];
glob_t gl;
type = strtok(line, "\t ");
@@ -892,9 +989,14 @@ static void tmpfiles(char *line)
if (!path)
return;
if (strchr(path, '%')) {
if (!quiet)
warnx("Path name specifiers unsupported, skipping %s", path);
return;
char *ptr = expand(path, pathbuf, sizeof(pathbuf));
if (!ptr) {
if (!quiet)
warnx("Unsupported specifier or too long, skipping %s", path);
return;
}
path = ptr;
}
if (excluded(path))
return;
@@ -955,6 +1057,14 @@ static void tmpfiles(char *line)
if (!arg[0] || !strcmp(arg, "-"))
arg = NULL;
}
if (arg && strchr(arg, '%') && strchr("CfFlLw", type[0])) {
arg = expand(arg, argbuf, sizeof(argbuf));
if (!arg) {
if (!quiet)
warnx("Unsupported specifier in argument or too long, skipping %s", path);
return;
}
}
strc = stat(path, &st);