mirror of
https://github.com/AuxXxilium/eudev.git
synced 2024-12-19 04:59:35 +07:00
tmpfiles: support globs
This commit is contained in:
parent
72f576bdd6
commit
b8bb3e8f34
4
TODO
4
TODO
@ -9,6 +9,8 @@ Bugs:
|
|||||||
|
|
||||||
Features:
|
Features:
|
||||||
|
|
||||||
|
* perhaps add "systemctl reenable" as combination of "systemctl disable" and "systemctl enable"
|
||||||
|
|
||||||
* tty name lock for password agent is broken, since it will always lock "/dev/tty" since we now reattach the agent process when forking it off systemctl
|
* tty name lock for password agent is broken, since it will always lock "/dev/tty" since we now reattach the agent process when forking it off systemctl
|
||||||
|
|
||||||
* need a way to apply mount options of api vfs from systemd unit files instead of fstab
|
* need a way to apply mount options of api vfs from systemd unit files instead of fstab
|
||||||
@ -50,8 +52,6 @@ Features:
|
|||||||
* load EnvironmentFile= when starting services, not when reloading configuration
|
* load EnvironmentFile= when starting services, not when reloading configuration
|
||||||
https://bugzilla.redhat.com/show_bug.cgi?id=661282
|
https://bugzilla.redhat.com/show_bug.cgi?id=661282
|
||||||
|
|
||||||
* support globs in systemd-tmpfiles
|
|
||||||
|
|
||||||
* drop IN_ATTRIB from inotify watches for .path units where possible to avoid
|
* drop IN_ATTRIB from inotify watches for .path units where possible to avoid
|
||||||
lots of wakeups due to /dev changing when we watch a subdir of /dev.
|
lots of wakeups due to /dev changing when we watch a subdir of /dev.
|
||||||
|
|
||||||
|
@ -54,11 +54,11 @@
|
|||||||
|
|
||||||
<para><command>systemd</command> uses
|
<para><command>systemd</command> uses
|
||||||
<filename>/etc/tmpfiles.d/</filename> to describe the
|
<filename>/etc/tmpfiles.d/</filename> to describe the
|
||||||
creation, cleaning and removal of temporary files and
|
creation, cleaning and removal of volatile files and
|
||||||
directories which usually reside in
|
directories which usually reside in directories such
|
||||||
<filename>/var/run</filename> or
|
as <filename>/var/run</filename> or
|
||||||
<filename>/tmp</filename>). Each configuration file
|
<filename>/tmp</filename>. Each configuration file is
|
||||||
is named in the style of
|
named in the style of
|
||||||
<filename>/etc/tmpfiles.d/<program>.conf</filename>.</para>
|
<filename>/etc/tmpfiles.d/<program>.conf</filename>.</para>
|
||||||
</refsect1>
|
</refsect1>
|
||||||
|
|
||||||
@ -97,17 +97,41 @@ d /var/run/user 0755 root root 10d</programlisting>
|
|||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><varname>x</varname></term>
|
<term><varname>x</varname></term>
|
||||||
<listitem><para>Ignore a path</para></listitem>
|
<listitem><para>Ignore a path
|
||||||
|
during cleaning. Use this type
|
||||||
|
to exclude paths from clean-up
|
||||||
|
as controlled with the Age
|
||||||
|
parameter. Note that lines of
|
||||||
|
this type do not influence the
|
||||||
|
effect of r or R lines. Lines
|
||||||
|
of this type accept
|
||||||
|
shell-style globs in place of
|
||||||
|
of normal path
|
||||||
|
names.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><varname>r</varname></term>
|
<term><varname>r</varname></term>
|
||||||
<listitem><para>Remove a path</para></listitem>
|
<listitem><para>Remove a file
|
||||||
|
or directory if it
|
||||||
|
exists. This may not be used
|
||||||
|
to remove non-empty
|
||||||
|
directories, use R for
|
||||||
|
that. Lines of this type
|
||||||
|
accept shell-style globs in
|
||||||
|
place of normal path
|
||||||
|
names.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
|
|
||||||
<varlistentry>
|
<varlistentry>
|
||||||
<term><varname>R</varname></term>
|
<term><varname>R</varname></term>
|
||||||
<listitem><para>Recursively remove a path</para></listitem>
|
<listitem><para>Recursively
|
||||||
|
remove a path and all its
|
||||||
|
subdirectories (if it is a
|
||||||
|
directory). Lines of this type
|
||||||
|
accept shell-style globs in
|
||||||
|
place of normal path
|
||||||
|
names.</para></listitem>
|
||||||
</varlistentry>
|
</varlistentry>
|
||||||
</variablelist>
|
</variablelist>
|
||||||
</refsect2>
|
</refsect2>
|
||||||
@ -115,10 +139,11 @@ d /var/run/user 0755 root root 10d</programlisting>
|
|||||||
<refsect2>
|
<refsect2>
|
||||||
<title>Mode</title>
|
<title>Mode</title>
|
||||||
|
|
||||||
<para>The file access mode to use for this
|
<para>The file access mode to use when
|
||||||
file or directory. If omitted or when set to -
|
creating this file or directory. If omitted or
|
||||||
the default is used: 0755 for directories,
|
when set to - the default is used: 0755 for
|
||||||
0644 for files.</para>
|
directories, 0644 for files. This parameter is
|
||||||
|
ignored for x, r, R lines.</para>
|
||||||
</refsect2>
|
</refsect2>
|
||||||
|
|
||||||
<refsect2>
|
<refsect2>
|
||||||
@ -127,8 +152,9 @@ d /var/run/user 0755 root root 10d</programlisting>
|
|||||||
<para>The user and group to use for this file
|
<para>The user and group to use for this file
|
||||||
or directory. This may either be a numeric
|
or directory. This may either be a numeric
|
||||||
user/group ID or a user or group name. If
|
user/group ID or a user or group name. If
|
||||||
omitted or when set to - the default 0 is
|
omitted or when set to - the default 0 (root)
|
||||||
used.</para>
|
is used. . These parameters are ignored for x,
|
||||||
|
r, R lines.</para>
|
||||||
</refsect2>
|
</refsect2>
|
||||||
|
|
||||||
<refsect2>
|
<refsect2>
|
||||||
|
@ -36,6 +36,8 @@
|
|||||||
#include <time.h>
|
#include <time.h>
|
||||||
#include <sys/types.h>
|
#include <sys/types.h>
|
||||||
#include <sys/param.h>
|
#include <sys/param.h>
|
||||||
|
#include <glob.h>
|
||||||
|
#include <fnmatch.h>
|
||||||
|
|
||||||
#include "log.h"
|
#include "log.h"
|
||||||
#include "util.h"
|
#include "util.h"
|
||||||
@ -50,10 +52,13 @@
|
|||||||
* bootup. */
|
* bootup. */
|
||||||
|
|
||||||
enum {
|
enum {
|
||||||
|
/* These ones take file names */
|
||||||
CREATE_FILE = 'f',
|
CREATE_FILE = 'f',
|
||||||
TRUNCATE_FILE = 'F',
|
TRUNCATE_FILE = 'F',
|
||||||
CREATE_DIRECTORY = 'd',
|
CREATE_DIRECTORY = 'd',
|
||||||
TRUNCATE_DIRECTORY = 'D',
|
TRUNCATE_DIRECTORY = 'D',
|
||||||
|
|
||||||
|
/* These ones take globs */
|
||||||
IGNORE_PATH = 'x',
|
IGNORE_PATH = 'x',
|
||||||
REMOVE_PATH = 'r',
|
REMOVE_PATH = 'r',
|
||||||
RECURSIVE_REMOVE_PATH = 'R'
|
RECURSIVE_REMOVE_PATH = 'R'
|
||||||
@ -74,7 +79,7 @@ typedef struct Item {
|
|||||||
bool age_set:1;
|
bool age_set:1;
|
||||||
} Item;
|
} Item;
|
||||||
|
|
||||||
static Hashmap *items = NULL;
|
static Hashmap *items = NULL, *globs = NULL;
|
||||||
|
|
||||||
static bool arg_create = false;
|
static bool arg_create = false;
|
||||||
static bool arg_clean = false;
|
static bool arg_clean = false;
|
||||||
@ -82,6 +87,21 @@ static bool arg_remove = false;
|
|||||||
|
|
||||||
#define MAX_DEPTH 256
|
#define MAX_DEPTH 256
|
||||||
|
|
||||||
|
static bool needs_glob(int t) {
|
||||||
|
return t == IGNORE_PATH || t == REMOVE_PATH || t == RECURSIVE_REMOVE_PATH;
|
||||||
|
}
|
||||||
|
|
||||||
|
static struct Item* find_glob(Hashmap *h, const char *match) {
|
||||||
|
Item *j;
|
||||||
|
Iterator i;
|
||||||
|
|
||||||
|
HASHMAP_FOREACH(j, h, i)
|
||||||
|
if (fnmatch(j->path, match, FNM_PATHNAME|FNM_PERIOD) == 0)
|
||||||
|
return j;
|
||||||
|
|
||||||
|
return NULL;
|
||||||
|
}
|
||||||
|
|
||||||
static int dir_cleanup(
|
static int dir_cleanup(
|
||||||
const char *p,
|
const char *p,
|
||||||
DIR *d,
|
DIR *d,
|
||||||
@ -136,6 +156,9 @@ static int dir_cleanup(
|
|||||||
if (hashmap_get(items, sub_path))
|
if (hashmap_get(items, sub_path))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
|
if (find_glob(globs, sub_path))
|
||||||
|
continue;
|
||||||
|
|
||||||
if (S_ISDIR(s.st_mode)) {
|
if (S_ISDIR(s.st_mode)) {
|
||||||
|
|
||||||
if (mountpoint &&
|
if (mountpoint &&
|
||||||
@ -411,7 +434,7 @@ finish:
|
|||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
static int remove_item(Item *i) {
|
static int remove_item(Item *i, const char *instance) {
|
||||||
int r;
|
int r;
|
||||||
|
|
||||||
assert(i);
|
assert(i);
|
||||||
@ -425,8 +448,8 @@ static int remove_item(Item *i) {
|
|||||||
break;
|
break;
|
||||||
|
|
||||||
case REMOVE_PATH:
|
case REMOVE_PATH:
|
||||||
if (remove(i->path) < 0 && errno != ENOENT) {
|
if (remove(instance) < 0 && errno != ENOENT) {
|
||||||
log_error("remove(%s): %m", i->path);
|
log_error("remove(%s): %m", instance);
|
||||||
return -errno;
|
return -errno;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -434,9 +457,9 @@ static int remove_item(Item *i) {
|
|||||||
|
|
||||||
case TRUNCATE_DIRECTORY:
|
case TRUNCATE_DIRECTORY:
|
||||||
case RECURSIVE_REMOVE_PATH:
|
case RECURSIVE_REMOVE_PATH:
|
||||||
if ((r = rm_rf(i->path, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
|
if ((r = rm_rf(instance, false, i->type == RECURSIVE_REMOVE_PATH)) < 0 &&
|
||||||
r != -ENOENT) {
|
r != -ENOENT) {
|
||||||
log_error("rm_rf(%s): %s", i->path, strerror(-r));
|
log_error("rm_rf(%s): %s", instance, strerror(-r));
|
||||||
return r;
|
return r;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -446,13 +469,57 @@ static int remove_item(Item *i) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static int remove_item_glob(Item *i) {
|
||||||
|
assert(i);
|
||||||
|
|
||||||
|
switch (i->type) {
|
||||||
|
|
||||||
|
case CREATE_FILE:
|
||||||
|
case TRUNCATE_FILE:
|
||||||
|
case CREATE_DIRECTORY:
|
||||||
|
case IGNORE_PATH:
|
||||||
|
break;
|
||||||
|
|
||||||
|
case REMOVE_PATH:
|
||||||
|
case TRUNCATE_DIRECTORY:
|
||||||
|
case RECURSIVE_REMOVE_PATH: {
|
||||||
|
int r = 0, k;
|
||||||
|
glob_t g;
|
||||||
|
char **fn;
|
||||||
|
|
||||||
|
zero(g);
|
||||||
|
|
||||||
|
errno = 0;
|
||||||
|
if ((k = glob(i->path, GLOB_NOSORT|GLOB_BRACE, NULL, &g)) != 0) {
|
||||||
|
|
||||||
|
if (k != GLOB_NOMATCH) {
|
||||||
|
if (errno != 0)
|
||||||
|
errno = EIO;
|
||||||
|
|
||||||
|
log_error("glob(%s) failed: %m", i->path);
|
||||||
|
return -errno;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
STRV_FOREACH(fn, g.gl_pathv)
|
||||||
|
if ((k = remove_item(i, *fn)) < 0)
|
||||||
|
r = k;
|
||||||
|
|
||||||
|
globfree(&g);
|
||||||
|
return r;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
static int process_item(Item *i) {
|
static int process_item(Item *i) {
|
||||||
int r, q, p;
|
int r, q, p;
|
||||||
|
|
||||||
assert(i);
|
assert(i);
|
||||||
|
|
||||||
r = arg_create ? create_item(i) : 0;
|
r = arg_create ? create_item(i) : 0;
|
||||||
q = arg_remove ? remove_item(i) : 0;
|
q = arg_remove ? remove_item_glob(i) : 0;
|
||||||
p = arg_clean ? clean_item(i) : 0;
|
p = arg_clean ? clean_item(i) : 0;
|
||||||
|
|
||||||
if (r < 0)
|
if (r < 0)
|
||||||
@ -590,7 +657,7 @@ static int parse_line(const char *fname, unsigned line, const char *buffer, cons
|
|||||||
i->age_set = true;
|
i->age_set = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((r = hashmap_put(items, i->path, i)) < 0) {
|
if ((r = hashmap_put(needs_glob(i->type) ? globs : items, i->path, i)) < 0) {
|
||||||
if (r == -EEXIST) {
|
if (r == -EEXIST) {
|
||||||
log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
|
log_warning("Two or more conflicting lines for %s configured, ignoring.", i->path);
|
||||||
r = 0;
|
r = 0;
|
||||||
@ -721,7 +788,10 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
label_init();
|
label_init();
|
||||||
|
|
||||||
if (!(items = hashmap_new(string_hash_func, string_compare_func))) {
|
items = hashmap_new(string_hash_func, string_compare_func);
|
||||||
|
globs = hashmap_new(string_hash_func, string_compare_func);
|
||||||
|
|
||||||
|
if (!items || !globs) {
|
||||||
log_error("Out of memory");
|
log_error("Out of memory");
|
||||||
r = EXIT_FAILURE;
|
r = EXIT_FAILURE;
|
||||||
goto finish;
|
goto finish;
|
||||||
@ -796,6 +866,10 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
free(de);
|
free(de);
|
||||||
|
|
||||||
|
HASHMAP_FOREACH(i, globs, iterator)
|
||||||
|
if (process_item(i) < 0)
|
||||||
|
r = EXIT_FAILURE;
|
||||||
|
|
||||||
HASHMAP_FOREACH(i, items, iterator)
|
HASHMAP_FOREACH(i, items, iterator)
|
||||||
if (process_item(i) < 0)
|
if (process_item(i) < 0)
|
||||||
r = EXIT_FAILURE;
|
r = EXIT_FAILURE;
|
||||||
@ -805,6 +879,7 @@ finish:
|
|||||||
item_free(i);
|
item_free(i);
|
||||||
|
|
||||||
hashmap_free(items);
|
hashmap_free(items);
|
||||||
|
hashmap_free(globs);
|
||||||
|
|
||||||
label_finish();
|
label_finish();
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user