From: Zbigniew Jędrzejewski-Szmek Date: Sun, 3 Dec 2023 14:28:52 +0000 (+0100) Subject: tmpfiles: only populate uid and gid caches once X-Git-Tag: v256-rc1~887^2~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=376d0495f5b9170435824cf9fb067c1077b6cc7e;p=thirdparty%2Fsystemd.git tmpfiles: only populate uid and gid caches once a3451c2c4ce7d3c02451f6ace4ee9f873880f78f added offline uid/gid support in a way where the /etc/passwd and /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. --- diff --git a/src/tmpfiles/tmpfiles.c b/src/tmpfiles/tmpfiles.c index e4e113b4026..4e245e36681 100644 --- a/src/tmpfiles/tmpfiles.c +++ b/src/tmpfiles/tmpfiles.c @@ -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 */