summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--source3/lib/system.c16
1 files changed, 14 insertions, 2 deletions
diff --git a/source3/lib/system.c b/source3/lib/system.c
index 41877e51df..4481aa2689 100644
--- a/source3/lib/system.c
+++ b/source3/lib/system.c
@@ -612,6 +612,10 @@ static struct passwd *setup_pwret(struct passwd *pass)
<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.
@@ -623,9 +627,11 @@ struct passwd *sys_getpwnam(const char *name)
return NULL;
/* check for a cache hit first */
- if (sv_pw_ret && !strcmp(name, sv_pw_ret->pw_name))
+ 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);
}
@@ -634,6 +640,8 @@ struct passwd *sys_getpwnam(const char *name)
if (!(sv_pw_ret = getpwnam(name)))
return NULL;
+ num_lookups = 1;
+
return setup_pwret(sv_pw_ret);
}
@@ -643,9 +651,11 @@ struct passwd *sys_getpwnam(const char *name)
struct passwd *sys_getpwuid(uid_t uid)
{
- if (sv_pw_ret && (uid == sv_pw_ret->pw_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);
}
@@ -653,6 +663,8 @@ struct passwd *sys_getpwuid(uid_t uid)
if (!(sv_pw_ret = getpwuid(uid)))
return NULL;
+ num_lookups = 1;
+
return setup_pwret(sv_pw_ret);
}