diff options
author | Gerald Carter <jerry@samba.org> | 2001-01-24 15:47:31 +0000 |
---|---|---|
committer | Gerald Carter <jerry@samba.org> | 2001-01-24 15:47:31 +0000 |
commit | b1be311add2bd56875f3ffb9fcde56f6289ade22 (patch) | |
tree | 3e6181860e2d2481468ce6ec069af09abee92534 /source3/lib | |
parent | ba3ee7fdaa44eb18de0346ab2d4f88c24c6a461b (diff) | |
download | samba-b1be311add2bd56875f3ffb9fcde56f6289ade22.tar.gz samba-b1be311add2bd56875f3ffb9fcde56f6289ade22.tar.bz2 samba-b1be311add2bd56875f3ffb9fcde56f6289ade22.zip |
getpw[nam|uid] caching patch from "Richard Bollinger"
<rabollinger@home.com>
jerry
(This used to be commit 158430ba6a030061bc7d7b84126c6f7ea0041c91)
Diffstat (limited to 'source3/lib')
-rw-r--r-- | source3/lib/system.c | 44 |
1 files changed, 37 insertions, 7 deletions
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" + <rabollinger@home.com> */ + +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); } /************************************************************************** |