]> git.ipfire.org Git - thirdparty/samba.git/commitdiff
r3702: This is a getpwnam-cache. It is mainly to speed up Samba with slow nss
authorVolker Lendecke <vlendec@samba.org>
Fri, 12 Nov 2004 15:01:40 +0000 (15:01 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 15:53:14 +0000 (10:53 -0500)
backends such as nss_ldap.

Volker

source/lib/util_pw.c

index 9d075a05e885bfc080f7257c3298a2cbcdbd8d5c..e102f2ef81844246c3ea8416f64f9d034b69e82d 100644 (file)
@@ -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);
 }