tmpfiles: only populate uid and gid caches once

a3451c2c4c added offline uid/gid support in a way
where the <root>/etc/passwd and <root>/etc/group would be read anew for each
configuration file that was parsed. The result would always be the same, so I
assume that this was an oversight. Let's use a global cache and and read the
file just once.
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2024-02-09 17:57:41 +01:00
parent 5ac41a1672
commit 376d0495f5
+11 -8
View File
@@ -214,8 +214,11 @@ static ImagePolicy *arg_image_policy = NULL;
#define MAX_DEPTH 256
typedef struct Context {
OrderedHashmap *items, *globs;
OrderedHashmap *items;
OrderedHashmap *globs;
Set *unix_sockets;
Hashmap *uid_cache;
Hashmap *gid_cache;
} Context;
STATIC_DESTRUCTOR_REGISTER(arg_include_prefixes, strv_freep);
@@ -239,6 +242,9 @@ static void context_done(Context *c) {
ordered_hashmap_free(c->globs);
set_free(c->unix_sockets);
hashmap_free(c->uid_cache);
hashmap_free(c->gid_cache);
}
/* Different kinds of errors that mean that information is not available in the environment. */
@@ -3461,9 +3467,7 @@ static int parse_line(
const char *fname,
unsigned line,
const char *buffer,
bool *invalid_config,
Hashmap **uid_cache,
Hashmap **gid_cache) {
bool *invalid_config) {
_cleanup_free_ char *action = NULL, *mode = NULL, *user = NULL, *group = NULL, *age = NULL, *path = NULL;
_cleanup_(item_free_contents) Item i = {
@@ -3823,7 +3827,7 @@ static int parse_line(
else
u = user;
r = find_uid(u, &i.uid, uid_cache);
r = find_uid(u, &i.uid, &c->uid_cache);
if (r == -ESRCH && arg_graceful) {
log_syntax(NULL, LOG_DEBUG, fname, line, r,
"%s: user '%s' not found, not adjusting ownership.", i.path, u);
@@ -3844,7 +3848,7 @@ static int parse_line(
else
g = group;
r = find_gid(g, &i.gid, gid_cache);
r = find_gid(g, &i.gid, &c->gid_cache);
if (r == -ESRCH && arg_graceful) {
log_syntax(NULL, LOG_DEBUG, fname, line, r,
"%s: group '%s' not found, not adjusting ownership.", i.path, g);
@@ -4221,7 +4225,6 @@ static int read_config_file(
bool ignore_enoent,
bool *invalid_config) {
_cleanup_hashmap_free_ Hashmap *uid_cache = NULL, *gid_cache = NULL;
_cleanup_fclose_ FILE *_f = NULL;
_cleanup_free_ char *pp = NULL;
unsigned v = 0;
@@ -4268,7 +4271,7 @@ static int read_config_file(
if (IN_SET(line[0], 0, '#'))
continue;
k = parse_line(c, fn, v, line, &invalid_line, &uid_cache, &gid_cache);
k = parse_line(c, fn, v, line, &invalid_line);
if (k < 0) {
if (invalid_line)
/* Allow reporting with a special code if the caller requested this */