diff options
author | Jeremy Allison <jra@samba.org> | 2001-01-24 21:46:20 +0000 |
---|---|---|
committer | Jeremy Allison <jra@samba.org> | 2001-01-24 21:46:20 +0000 |
commit | 955f1e0f5d6c079c00da110a249b127a9dcaa953 (patch) | |
tree | e5ffde12b6460e855d474b427d383329db05fd74 /source/lib/system.c | |
parent | 0f8d0eb3cee51199afc4496501d19c0ee34b4192 (diff) | |
download | samba-955f1e0f5d6c079c00da110a249b127a9dcaa953.tar.gz samba-955f1e0f5d6c079c00da110a249b127a9dcaa953.tar.xz samba-955f1e0f5d6c079c00da110a249b127a9dcaa953.zip |
Added modification of Richard Bollinger getpw[nam|uid] cache patch. Only
uses cache max 100 times.
Jeremy.
Diffstat (limited to 'source/lib/system.c')
-rw-r--r-- | source/lib/system.c | 48 |
1 files changed, 46 insertions, 2 deletions
diff --git a/source/lib/system.c b/source/lib/system.c index 0c8e7ddcfc8..85ccee03221 100644 --- a/source/lib/system.c +++ b/source/lib/system.c @@ -608,13 +608,43 @@ 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" + <rabollinger@home.com> */ + +static struct passwd *sv_pw_ret; /* implicitly initialized to NULL */ +static int num_lookups; /* Counter so we don't always use cache. */ +#ifndef PW_RET_CACHE_MAX_LOOKUPS +#define PW_RET_CACHE_MAX_LOOKUPS 100 +#endif + /************************************************************************** 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 (num_lookups && sv_pw_ret && !strcmp(name, sv_pw_ret->pw_name)) + { + DEBUG(2,("getpwnam(%s) avoided - using cached results\n",name)); + num_lookups++; + num_lookups = (num_lookups % PW_RET_CACHE_MAX_LOOKUPS); + 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; + + num_lookups = 1; + + return setup_pwret(sv_pw_ret); } /************************************************************************** @@ -623,7 +653,21 @@ struct passwd *sys_getpwnam(const char *name) struct passwd *sys_getpwuid(uid_t uid) { - return setup_pwret(getpwuid(uid)); + if (num_lookups && sv_pw_ret && (uid == sv_pw_ret->pw_uid)) + { + DEBUG(2,("getpwuid(%d) avoided - using cached results\n",uid)); + num_lookups++; + num_lookups = (num_lookups % PW_RET_CACHE_MAX_LOOKUPS); + return setup_pwret(sv_pw_ret); + } + + DEBUG(2,("getpwuid(%d) called\n",uid)); + if (!(sv_pw_ret = getpwuid(uid))) + return NULL; + + num_lookups = 1; + + return setup_pwret(sv_pw_ret); } /************************************************************************** |