From 2b0cf44080c59c7bc05f876b963d4779e3618eec Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Fri, 2 Oct 2026 14:55:43 -0500 Subject: [PATCH] Clear buffered pwd/grp entries if timestamps on /etc/{password,group} change --- lib/lib.c | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/lib/lib.c b/lib/lib.c index 4b69f05a..1bfed028 100644 --- a/lib/lib.c +++ b/lib/lib.c @@ -1284,8 +1284,16 @@ struct passwd *bufgetpwnamuid(char *name, uid_t uid) } *list = 0; struct passwd *temp; static struct pwuidbuf_list *pwuidbuf; + static time_t passtime; + struct stat *st = (void *)libbuf; unsigned size = 256; + // Free cache list if time changes. (stat() cheap due to dentry cache) + if (!stat("/etc/passwd", st) && passtime!=st->st_mtime) { + passtime = st->st_mtime; + while (pwuidbuf) free(llist_pop(&pwuidbuf)); + } + // If we already have this one, return it. for (list = pwuidbuf; list; list = list->next) if (name ? !strcmp(name, list->pw.pw_name) : list->pw.pw_uid==uid) @@ -1325,8 +1333,16 @@ struct group *bufgetgrnamgid(char *name, gid_t gid) } *list = 0; struct group *temp; static struct grgidbuf_list *grgidbuf; + static time_t grptime; + struct stat *st = (void *)libbuf; unsigned size = 256; + // Free cache list if time changes. (stat() cheap due to dentry cache) + if (!stat("/etc/group", st) && grptime!=st->st_mtime) { + grptime = st->st_mtime; + while (grgidbuf) free(llist_pop(&grgidbuf)); + } + for (list = grgidbuf; list; list = list->next) if (name ? !strcmp(name, list->gr.gr_name) : list->gr.gr_gid==gid) return &(list->gr); -- 2.39.5