mirror of
https://github.com/troglobit/finit.git
synced 2026-11-23 20:11:44 +00:00
tmpfiles: read the files by name, the first entry for a path counts
The files were read one directory at a time, Finit's own first and /etc/tmpfiles.d last, and every entry for a path was run. With two entries for a path the last one read won, so an entry in /etc won over one in /usr/lib whatever the files were called. tmpfiles.d(5) reads the files in the order of their names, whatever directory they are in, and goes by the first entry that makes a path. A package relies on that when it names its file to sort before or after another. Do the same. As before, a file in a later directory is read in place of one of the same name in an earlier one. A later entry is skipped when it makes a path an earlier one makes, or when both take a glob and remove, write to, or keep the path. Several w+ entries may add to one file. The skip is only told about with --dry-run or --debug, since Finit's files and those of other packages overlap on most systems. Finit's own files had two entries for some paths, and relied on the last one. Drop the ones that lost: /run/lock, /run/lock/subsys, and /var/lock in legacy.conf, and /var/run and a second /var/spool in var.conf. Signed-off-by: Joachim Wiberg <troglobit@gmail.com>
This commit is contained in:
9 files changed
+152
-50
No files matched your search
+82
-26
@@ -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);
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user