From b1be311add2bd56875f3ffb9fcde56f6289ade22 Mon Sep 17 00:00:00 2001 From: Gerald Carter Date: Wed, 24 Jan 2001 15:47:31 +0000 Subject: getpw[nam|uid] caching patch from "Richard Bollinger" jerry (This used to be commit 158430ba6a030061bc7d7b84126c6f7ea0041c91) --- source3/lib/system.c | 44 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 37 insertions(+), 7 deletions(-) (limited to 'source3/lib/system.c') diff --git a/source3/lib/system.c b/source3/lib/system.c index 0c8e7ddcfc..41877e51df 100644 --- a/source3/lib/system.c +++ b/source3/lib/system.c @@ -582,15 +582,14 @@ int sys_setgroups(int setlen, gid_t *gidset) static struct passwd *setup_pwret(struct passwd *pass) { - static pstring pw_name; - static pstring pw_passwd; + static fstring pw_name; + static fstring pw_passwd; static struct passwd pw_ret; - + if (pass == NULL) - { return NULL; - } + /* this gets the uid, gid and null pointers */ memcpy((char *)&pw_ret, pass, sizeof(struct passwd)); if (pass->pw_name) @@ -608,13 +607,34 @@ static struct passwd *setup_pwret(struct passwd *pass) return &pw_ret; } +/* static pointer to be used for caching the last + getpw[nam|uid]() call. Patch by "Richard Bollinger" + */ + +static struct passwd *sv_pw_ret; /* implicitly initialized to NULL */ + /************************************************************************** Wrapper for getpwnam(). Always returns a static that can be modified. ****************************************************************************/ struct passwd *sys_getpwnam(const char *name) { - return setup_pwret(getpwnam(name)); + if (!name || !name[0]) + return NULL; + + /* check for a cache hit first */ + if (sv_pw_ret && !strcmp(name, sv_pw_ret->pw_name)) + { + DEBUG(2,("getpwnam(%s) avoided - using cached results\n",name)); + return setup_pwret(sv_pw_ret); + } + + /* no cache hit--use old lookup instead */ + DEBUG(2,("getpwnam(%s) called\n",name)); + if (!(sv_pw_ret = getpwnam(name))) + return NULL; + + return setup_pwret(sv_pw_ret); } /************************************************************************** @@ -623,7 +643,17 @@ struct passwd *sys_getpwnam(const char *name) struct passwd *sys_getpwuid(uid_t uid) { - return setup_pwret(getpwuid(uid)); + if (sv_pw_ret && (uid == sv_pw_ret->pw_uid)) + { + DEBUG(2,("getpwuid(%d) avoided - using cached results\n",uid)); + return setup_pwret(sv_pw_ret); + } + + DEBUG(2,("getpwuid(%d) called\n",uid)); + if (!(sv_pw_ret = getpwuid(uid))) + return NULL; + + return setup_pwret(sv_pw_ret); } /************************************************************************** -- cgit