summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorVolker Lendecke <vlendec@samba.org>2004-11-12 15:01:40 +0000
committerGerald (Jerry) Carter <jerry@samba.org>2007-10-10 10:53:14 -0500
commitc34ec6180a011f23de16cc5e19902470dc7feb6f (patch)
tree141481341f9638f6a8edb295f7daf8e18737ec1e /source3/lib
parent8a48be9cd65d24f3746006a7652ebba910b96361 (diff)
downloadsamba-c34ec6180a011f23de16cc5e19902470dc7feb6f.tar.gz
samba-c34ec6180a011f23de16cc5e19902470dc7feb6f.tar.bz2
samba-c34ec6180a011f23de16cc5e19902470dc7feb6f.zip
r3702: This is a getpwnam-cache. It is mainly to speed up Samba with slow nss
backends such as nss_ldap. Volker (This used to be commit a8bd0b75042f73b753fc1cb8a52e6e90372fd1fe)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/util_pw.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/source3/lib/util_pw.c b/source3/lib/util_pw.c
index 9d075a05e8..e102f2ef81 100644
--- a/source3/lib/util_pw.c
+++ b/source3/lib/util_pw.c
@@ -52,10 +52,40 @@ void passwd_free (struct passwd **buf)
SAFE_FREE(*buf);
}
+#define PWNAMCACHE_SIZE 4
+static struct passwd *pwnam_cache[PWNAMCACHE_SIZE];
+static BOOL pwnam_cache_initialized = False;
+
+static void init_pwnam_cache(void)
+{
+ int i;
+
+ if (pwnam_cache_initialized)
+ return;
+
+ for (i=0; i<PWNAMCACHE_SIZE; i++)
+ pwnam_cache[i] = NULL;
+
+ pwnam_cache_initialized = True;
+ return;
+}
+
struct passwd *getpwnam_alloc(const char *name)
{
+ int i;
+
struct passwd *temp;
+ init_pwnam_cache();
+
+ for (i=0; i<PWNAMCACHE_SIZE; i++) {
+ if ((pwnam_cache[i] != NULL) &&
+ (strcmp(name, pwnam_cache[i]->pw_name) == 0)) {
+ DEBUG(10, ("Got %s from pwnam_cache\n", name));
+ return alloc_copy_passwd(pwnam_cache[i]);
+ }
+ }
+
temp = sys_getpwnam(name);
if (!temp) {
@@ -67,6 +97,19 @@ struct passwd *getpwnam_alloc(const char *name)
return NULL;
}
+ for (i=0; i<PWNAMCACHE_SIZE; i++) {
+ if (pwnam_cache[i] == NULL)
+ break;
+ }
+
+ if (i == PWNAMCACHE_SIZE)
+ i = rand() % PWNAMCACHE_SIZE;
+
+ if (pwnam_cache[i] != NULL)
+ passwd_free(&pwnam_cache[i]);
+
+ pwnam_cache[i] = alloc_copy_passwd(temp);
+
return alloc_copy_passwd(temp);
}