]> git.ipfire.org Git - thirdparty/apache/httpd.git/commitdiff
mod_auth_ldap: Fix some segfaults in the cache logic.
authorJeff Trawick <trawick@apache.org>
Thu, 6 Nov 2003 20:45:56 +0000 (20:45 +0000)
committerJeff Trawick <trawick@apache.org>
Thu, 6 Nov 2003 20:45:56 +0000 (20:45 +0000)
PR:                18756
Submitted by:      Matthieu Estrade <apache@moresecurity.org>
Reviewed by:       Jeff Trawick

git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@101706 13f79535-47bb-0310-9956-ffa450edef68

CHANGES
include/util_ldap.h
modules/experimental/util_ldap.c
modules/experimental/util_ldap_cache.c
modules/experimental/util_ldap_cache.h
modules/experimental/util_ldap_cache_mgr.c

diff --git a/CHANGES b/CHANGES
index 782c840c19d9a31b3e64e9376cb79740abfd7722..cff834bed4fd6f94bdcd17260fba2b8a68aff06d 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -2,6 +2,9 @@ Changes with Apache 2.1.0-dev
 
   [Remove entries to the current 2.0 section below, when backported]
 
+  *) mod_auth_ldap: Fix some segfaults in the cache logic.  PR 18756.
+     [Matthieu Estrade <apache@moresecurity.org>]
+
   *) mod_autoindex: Restore the ability to add a description for
      directories that don't contain an index file.  [AndrĂ© Malo]
 
index c83236aa79a8bbcf099ce54f01d0ea77c474e693..b0a3b0442cb8a9d8a0b8e3a3a0b74d45ffe66d78 100644 (file)
 #include "http_protocol.h"
 #include "http_request.h"
 
+#if APR_HAS_SHARED_MEMORY
+#include "apr_rmm.h"
+#include "apr_shm.h"
+#endif
 
 /* Create a set of LDAP_DECLARE(type), LDLDAP_DECLARE(type) and 
  * LDAP_DECLARE_DATA with appropriate export and import tags for the platform
 #define LDAP_DECLARE_DATA             __declspec(dllimport)
 #endif
 
-
 /*
  * LDAP Connections
  */
@@ -138,9 +141,11 @@ typedef struct util_ldap_state_t {
     apr_pool_t *pool;           /* pool from which this state is allocated */
 #if APR_HAS_THREADS
     apr_thread_mutex_t *mutex;          /* mutex lock for the connection list */
+    apr_thread_rwlock_t *util_ldap_cache_lock;
 #endif
 
     apr_size_t cache_bytes;     /* Size (in bytes) of shared memory cache */
+    char *cache_file;           /* filename for shm */
     long search_cache_ttl;      /* TTL for search cache */
     long search_cache_size;     /* Size (in entries) of search cache */
     long compare_cache_ttl;     /* TTL for compare cache */
@@ -150,6 +155,15 @@ typedef struct util_ldap_state_t {
     char *cert_auth_file; 
     int   cert_file_type;
     int   ssl_support;
+
+#if APR_HAS_SHARED_MEMORY
+    apr_shm_t *cache_shm;
+    apr_rmm_t *cache_rmm;
+#endif
+
+    /* cache ald */
+    void *util_ldap_cache;
+
 } util_ldap_state_t;
 
 
@@ -286,21 +300,21 @@ LDAP_DECLARE(int) util_ldap_ssl_supported(request_rec *r);
  * @param reqsize The size of the shared memory segement to request. A size
  *                of zero requests the max size possible from
  *                apr_shmem_init()
- * @deffunc void util_ldap_cache_init(apr_pool_t *p)
+ * @deffunc void util_ldap_cache_init(apr_pool_t *p, util_ldap_state_t *st)
  * @return The status code returned is the status code of the
  *         apr_smmem_init() call. Regardless of the status, the cache
  *         will be set up at least for in-process or in-thread operation.
  */
-apr_status_t util_ldap_cache_init(apr_pool_t *pool, apr_size_t reqsize);
+apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st);
 
 /**
  * Display formatted stats for cache
  * @param The pool to allocate the returned string from
  * @tip This function returns a string allocated from the provided pool that describes
  *      various stats about the cache.
- * @deffunc char *util_ald_cache_display(apr_pool_t *pool)
+ * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
  */
-char *util_ald_cache_display(apr_pool_t *pool);
+char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
 
 
 /* from apr_ldap_cache_mgr.c */
@@ -310,9 +324,9 @@ char *util_ald_cache_display(apr_pool_t *pool);
  * @param The pool to allocate the returned string from
  * @tip This function returns a string allocated from the provided pool that describes
  *      various stats about the cache.
- * @deffunc char *util_ald_cache_display(apr_pool_t *pool)
+ * @deffunc char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
  */
-char *util_ald_cache_display(apr_pool_t *pool);
+char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st);
 
 #endif /* APU_HAS_LDAP */
 #endif /* UTIL_LDAP_H */
index 033ee140516ae3be4e1c57eb466a3e469268f492..9ab92c5623ad01851993601693985c6f87213d39 100644 (file)
@@ -141,6 +141,7 @@ void *util_ldap_create_config(apr_pool_t *p, server_rec *s);
  */
 int util_ldap_handler(request_rec *r)
 {
+    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
 
     r->allowed |= (1 << M_GET);
     if (r->method_number != M_GET)
@@ -171,7 +172,7 @@ int util_ldap_handler(request_rec *r)
              "</tr>\n", r
             );
 
-    ap_rputs(util_ald_cache_display(r->pool), r);
+    ap_rputs(util_ald_cache_display(r->pool, st), r);
 
     ap_rputs("</table>\n</p>\n", r);
 
@@ -506,9 +507,7 @@ LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection
     LDAPMessage *res, *entry;
     char *searchdn;
 
-    util_ldap_state_t *st = 
-        (util_ldap_state_t *)ap_get_module_config(r->server->module_config,
-        &ldap_module);
+    util_ldap_state_t *st =  (util_ldap_state_t *)ap_get_module_config(r->server->module_config, &ldap_module);
 
     /* read lock this function */
     LDAP_CACHE_LOCK_CREATE(st->pool);
@@ -517,7 +516,7 @@ LDAP_DECLARE(int) util_ldap_cache_comparedn(request_rec *r, util_ldap_connection
     LDAP_CACHE_WRLOCK();
 
     curnode.url = url;
-    curl = util_ald_cache_fetch(util_ldap_cache, &curnode);
+    curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
     if (curl == NULL) {
         curl = util_ald_create_caches(st, url);
     }
@@ -637,7 +636,7 @@ LDAP_DECLARE(int) util_ldap_cache_compare(request_rec *r, util_ldap_connection_t
     /* get cache entry (or create one) */
     LDAP_CACHE_WRLOCK();
     curnode.url = url;
-    curl = util_ald_cache_fetch(util_ldap_cache, &curnode);
+    curl = util_ald_cache_fetch(st->util_ldap_cache, &curnode);
     if (curl == NULL) {
         curl = util_ald_create_caches(st, url);
     }
@@ -760,7 +759,7 @@ LDAP_DECLARE(int) util_ldap_cache_checkuserid(request_rec *r, util_ldap_connecti
     /* Get the cache node for this url */
     LDAP_CACHE_WRLOCK();
     curnode.url = url;
-    curl = (util_url_node_t *)util_ald_cache_fetch(util_ldap_cache, &curnode);
+    curl = (util_url_node_t *)util_ald_cache_fetch(st->util_ldap_cache, &curnode);
     if (curl == NULL) {
         curl = util_ald_create_caches(st, url);
     }
@@ -962,6 +961,26 @@ static const char *util_ldap_set_cache_bytes(cmd_parms *cmd, void *dummy, const
     return NULL;
 }
 
+static const char *util_ldap_set_cache_file(cmd_parms *cmd, void *dummy, const char *file)
+{
+    util_ldap_state_t *st = 
+        (util_ldap_state_t *)ap_get_module_config(cmd->server->module_config, 
+                                                  &ldap_module);
+
+    if (file) {
+        st->cache_file = ap_server_root_relative(st->pool, file);
+    }
+    else {
+        st->cache_file = NULL;
+    }
+
+    ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0, cmd->server, 
+                 "LDAP cache: Setting shared memory cache file to %s bytes.", 
+                 st->cache_file);
+
+    return NULL;
+}
+
 static const char *util_ldap_set_cache_ttl(cmd_parms *cmd, void *dummy, const char *ttl)
 {
     util_ldap_state_t *st = 
@@ -1091,22 +1110,6 @@ void *util_ldap_create_config(apr_pool_t *p, server_rec *s)
     return st;
 }
 
-static void util_ldap_init_module(apr_pool_t *pool, server_rec *s)
-{
-    util_ldap_state_t *st = 
-        (util_ldap_state_t *)ap_get_module_config(s->module_config, 
-                                                 &ldap_module);
-
-    apr_status_t result = util_ldap_cache_init(pool, st->cache_bytes);
-    char buf[MAX_STRING_LEN];
-
-    apr_strerror(result, buf, sizeof(buf));
-    ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
-                      "[%d] ldap cache init: %s", 
-                      getpid(), buf);
-}
-
-
 static apr_status_t util_ldap_cleanup_module(void *data)
 {
     server_rec *s = data;
@@ -1128,13 +1131,42 @@ static int util_ldap_post_config(apr_pool_t *p, apr_pool_t *plog,
                                  apr_pool_t *ptemp, server_rec *s)
 {
     int rc = LDAP_SUCCESS;
+    apr_status_t result;
+    char buf[MAX_STRING_LEN];
 
-    util_ldap_state_t *st = (util_ldap_state_t *)ap_get_module_config(
-                                                s->module_config, 
-                                                &ldap_module);
-
-        /* log the LDAP SDK used 
-        */
+    server_rec *s_vhost;
+    util_ldap_state_t *st_vhost;
+    
+    util_ldap_state_t *st =
+        (util_ldap_state_t *)ap_get_module_config(s->module_config, &ldap_module);
+
+    /* initializing cache if file is here and we already don't have shm addr*/
+    if (st->cache_file && !st->cache_shm) {
+        result = util_ldap_cache_init(p, st);
+        apr_strerror(result, buf, sizeof(buf));
+        ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s,
+                     "LDAP cache init: %s", buf);
+
+        /* merge config in all vhost */
+        s_vhost = s->next;
+        while (s_vhost) {
+            ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, result, s, 
+                         "LDAP merging Shared Cache conf: shm=0x%x rmm=0x%x for VHOST: %s",
+                         st->cache_shm, st->cache_rmm, s_vhost->server_hostname);
+
+            st_vhost = (util_ldap_state_t *)ap_get_module_config(s_vhost->module_config, &ldap_module);
+            st_vhost->cache_shm = st->cache_shm;
+            st_vhost->cache_rmm = st->cache_rmm;
+            st_vhost->cache_file = st->cache_file;
+            s_vhost = s_vhost->next;
+        }
+    }
+    else {
+        ap_log_error(APLOG_MARK, APLOG_DEBUG|APLOG_NOERRNO, 0 , s, "LDAP cache: Unable to init Shared Cache: no file");
+    }
+    
+    /* log the LDAP SDK used 
+     */
     #if APR_HAS_NETSCAPE_LDAPSDK 
     
         ap_log_error(APLOG_MARK, APLOG_NOTICE, 0, s, 
@@ -1297,6 +1329,10 @@ command_rec util_ldap_cmds[] = {
                   "Sets the size of the shared memory cache in bytes. "
                   "Zero means disable the shared memory cache. Defaults to 100KB."),
 
+    AP_INIT_TAKE1("LDAPSharedCacheFile", util_ldap_set_cache_file, NULL, RSRC_CONF,
+                  "Sets the file of the shared memory cache."
+                  "Nothing means disable the shared memory cache."),
+
     AP_INIT_TAKE1("LDAPCacheEntries", util_ldap_set_cache_entries, NULL, RSRC_CONF,
                   "Sets the maximum number of entries that are possible in the LDAP "
                   "search cache. "
@@ -1331,7 +1367,6 @@ command_rec util_ldap_cmds[] = {
 static void util_ldap_register_hooks(apr_pool_t *p)
 {
     ap_hook_post_config(util_ldap_post_config,NULL,NULL,APR_HOOK_MIDDLE);
-    ap_hook_child_init(util_ldap_init_module, NULL, NULL, APR_HOOK_MIDDLE);
     ap_hook_handler(util_ldap_handler, NULL, NULL, APR_HOOK_MIDDLE);
 }
 
index 66b1862a2b51fa8acb454d24bc66868333e037d6..c172107d11877526fb000d8cfb0046d23215e9df 100644 (file)
@@ -85,14 +85,14 @@ int util_ldap_url_node_compare(void *a, void *b)
     return(strcmp(na->url, nb->url) == 0);
 }
 
-void *util_ldap_url_node_copy(void *c)
+void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_url_node_t *n = (util_url_node_t *)c;
-    util_url_node_t *node = (util_url_node_t *)util_ald_alloc(sizeof(util_url_node_t));
+    util_url_node_t *node = (util_url_node_t *)util_ald_alloc(cache->rmm_addr, sizeof(util_url_node_t));
 
     if (node) {
-        if (!(node->url = util_ald_strdup(n->url))) {
-            util_ald_free(node->url);
+        if (!(node->url = util_ald_strdup(cache->rmm_addr, n->url))) {
+            util_ald_free(cache->rmm_addr, node->url);
             return NULL;
         }
         node->search_cache = n->search_cache;
@@ -105,15 +105,15 @@ void *util_ldap_url_node_copy(void *c)
     }
 }
 
-void util_ldap_url_node_free(void *n)
+void util_ldap_url_node_free(util_ald_cache_t *cache, void *n)
 {
     util_url_node_t *node = (util_url_node_t *)n;
 
-    util_ald_free(node->url);
+    util_ald_free(cache->rmm_addr, node->url);
     util_ald_destroy_cache(node->search_cache);
     util_ald_destroy_cache(node->compare_cache);
     util_ald_destroy_cache(node->dn_compare_cache);
-    util_ald_free(node);
+    util_ald_free(cache->rmm_addr, node);
 }
 
 /* ------------------------------------------------------------------ */
@@ -131,10 +131,10 @@ int util_ldap_search_node_compare(void *a, void *b)
                  ((util_search_node_t *)b)->username) == 0);
 }
 
-void *util_ldap_search_node_copy(void *c)
+void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_search_node_t *node = (util_search_node_t *)c;
-    util_search_node_t *newnode = util_ald_alloc(sizeof(util_search_node_t));
+    util_search_node_t *newnode = util_ald_alloc(cache->rmm_addr, sizeof(util_search_node_t));
 
     /* safety check */
     if (newnode) {
@@ -144,13 +144,13 @@ void *util_ldap_search_node_copy(void *c)
             int k = 0;
             int i = 0;
             while (node->vals[k++]);
-            if (!(newnode->vals = util_ald_alloc(sizeof(char *) * (k+1)))) {
-                util_ldap_search_node_free(newnode);
+            if (!(newnode->vals = util_ald_alloc(cache->rmm_addr, sizeof(char *) * (k+1)))) {
+                util_ldap_search_node_free(cache, newnode);
                 return NULL;
             }
             while (node->vals[i]) {
-                if (!(newnode->vals[i] = util_ald_strdup(node->vals[i]))) {
-                    util_ldap_search_node_free(newnode);
+                if (!(newnode->vals[i] = util_ald_strdup(cache->rmm_addr, node->vals[i]))) {
+                    util_ldap_search_node_free(cache, newnode);
                     return NULL;
                 }
                 i++;
@@ -159,10 +159,10 @@ void *util_ldap_search_node_copy(void *c)
         else {
             newnode->vals = NULL;
         }
-        if (!(newnode->username = util_ald_strdup(node->username)) ||
-            !(newnode->dn = util_ald_strdup(node->dn)) ||
-            !(newnode->bindpw = util_ald_strdup(node->bindpw)) ) {
-            util_ldap_search_node_free(newnode);
+        if (!(newnode->username = util_ald_strdup(cache->rmm_addr, node->username)) ||
+            !(newnode->dn = util_ald_strdup(cache->rmm_addr, node->dn)) ||
+            !(newnode->bindpw = util_ald_strdup(cache->rmm_addr, node->bindpw)) ) {
+            util_ldap_search_node_free(cache, newnode);
             return NULL;
         }
         newnode->lastbind = node->lastbind;
@@ -171,20 +171,20 @@ void *util_ldap_search_node_copy(void *c)
     return (void *)newnode;
 }
 
-void util_ldap_search_node_free(void *n)
+void util_ldap_search_node_free(util_ald_cache_t *cache, void *n)
 {
     int i = 0;
     util_search_node_t *node = (util_search_node_t *)n;
     if (node->vals) {
         while (node->vals[i]) {
-            util_ald_free(node->vals[i++]);
+            util_ald_free(cache->rmm_addr, node->vals[i++]);
         }
-        util_ald_free(node->vals);
+        util_ald_free(cache->rmm_addr, node->vals);
     }
-    util_ald_free(node->username);
-    util_ald_free(node->dn);
-    util_ald_free(node->bindpw);
-    util_ald_free(node);
+    util_ald_free(cache->rmm_addr, node->username);
+    util_ald_free(cache->rmm_addr, node->dn);
+    util_ald_free(cache->rmm_addr, node->bindpw);
+    util_ald_free(cache->rmm_addr, node);
 }
 
 /* ------------------------------------------------------------------ */
@@ -204,16 +204,16 @@ int util_ldap_compare_node_compare(void *a, void *b)
            strcmp(na->value, nb->value) == 0);
 }
 
-void *util_ldap_compare_node_copy(void *c)
+void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_compare_node_t *n = (util_compare_node_t *)c;
-    util_compare_node_t *node = (util_compare_node_t *)util_ald_alloc(sizeof(util_compare_node_t));
+    util_compare_node_t *node = (util_compare_node_t *)util_ald_alloc(cache->rmm_addr, sizeof(util_compare_node_t));
 
     if (node) {
-        if (!(node->dn = util_ald_strdup(n->dn)) ||
-            !(node->attrib = util_ald_strdup(n->attrib)) ||
-            !(node->value = util_ald_strdup(n->value))) {
-            util_ldap_compare_node_free(node);
+        if (!(node->dn = util_ald_strdup(cache->rmm_addr, n->dn)) ||
+            !(node->attrib = util_ald_strdup(cache->rmm_addr, n->attrib)) ||
+            !(node->value = util_ald_strdup(cache->rmm_addr, n->value))) {
+            util_ldap_compare_node_free(cache, node);
             return NULL;
         }
         node->lastcompare = n->lastcompare;
@@ -225,13 +225,13 @@ void *util_ldap_compare_node_copy(void *c)
     }
 }
 
-void util_ldap_compare_node_free(void *n)
+void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n)
 {
     util_compare_node_t *node = (util_compare_node_t *)n;
-    util_ald_free(node->dn);
-    util_ald_free(node->attrib);
-    util_ald_free(node->value);
-    util_ald_free(node);
+    util_ald_free(cache->rmm_addr, node->dn);
+    util_ald_free(cache->rmm_addr, node->attrib);
+    util_ald_free(cache->rmm_addr, node->value);
+    util_ald_free(cache->rmm_addr, node);
 }
 
 /* ------------------------------------------------------------------ */
@@ -247,14 +247,14 @@ int util_ldap_dn_compare_node_compare(void *a, void *b)
                   ((util_dn_compare_node_t *)b)->reqdn) == 0);
 }
 
-void *util_ldap_dn_compare_node_copy(void *c)
+void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c)
 {
     util_dn_compare_node_t *n = (util_dn_compare_node_t *)c;
-    util_dn_compare_node_t *node = (util_dn_compare_node_t *)util_ald_alloc(sizeof(util_dn_compare_node_t));
+    util_dn_compare_node_t *node = (util_dn_compare_node_t *)util_ald_alloc(cache->rmm_addr, sizeof(util_dn_compare_node_t));
     if (node) {
-        if (!(node->reqdn = util_ald_strdup(n->reqdn)) ||
-            !(node->dn = util_ald_strdup(n->dn))) {
-            util_ldap_dn_compare_node_free(node);
+        if (!(node->reqdn = util_ald_strdup(cache->rmm_addr, n->reqdn)) ||
+            !(node->dn = util_ald_strdup(cache->rmm_addr, n->dn))) {
+            util_ldap_dn_compare_node_free(cache, node);
             return NULL;
         }
         return node;
@@ -264,12 +264,12 @@ void *util_ldap_dn_compare_node_copy(void *c)
     }
 }
 
-void util_ldap_dn_compare_node_free(void *n)
+void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n)
 {
     util_dn_compare_node_t *node = (util_dn_compare_node_t *)n;
-    util_ald_free(node->reqdn);
-    util_ald_free(node->dn);
-    util_ald_free(node);
+    util_ald_free(cache->rmm_addr, node->reqdn);
+    util_ald_free(cache->rmm_addr, node->dn);
+    util_ald_free(cache->rmm_addr, node);
 }
 
 
@@ -279,46 +279,53 @@ apr_status_t util_ldap_cache_module_kill(void *data);
 
 apr_status_t util_ldap_cache_module_kill(void *data)
 {
+    util_ldap_state_t *st = (util_ldap_state_t *)data;
+
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm != NULL) {
-        apr_status_t result = apr_shm_destroy(util_ldap_shm);
-        util_ldap_shm = NULL;
+    if (st->cache_shm != NULL) {
+        apr_status_t result = apr_shm_destroy(st->cache_shm);
+        st->cache_shm = NULL;
+        apr_file_remove(st->cache_file, st->pool);
         return result;
     }
 #endif
-    util_ald_destroy_cache(util_ldap_cache);
+    util_ald_destroy_cache(st->util_ldap_cache);
     return APR_SUCCESS;
 }
 
-apr_status_t util_ldap_cache_init(apr_pool_t *pool, apr_size_t reqsize)
+apr_status_t util_ldap_cache_init(apr_pool_t *pool, util_ldap_state_t *st)
 {
 #if APR_HAS_SHARED_MEMORY
     apr_status_t result;
 
-    result = apr_shm_create(&util_ldap_shm, reqsize, MODLDAP_SHMEM_CACHE, pool);
+    if (!st->cache_file) {
+       return -1;
+    }
+
+    result = apr_shm_create(&st->cache_shm, st->cache_bytes, st->cache_file, st->pool);
     if (result == EEXIST) {
         /*
          * The cache could have already been created (i.e. we may be a child process).  See
          * if we can attach to the existing shared memory
          */
-        result = apr_shm_attach(&util_ldap_shm, MODLDAP_SHMEM_CACHE, pool);
+        result = apr_shm_attach(&st->cache_shm, st->cache_file, st->pool);
     } 
     if (result != APR_SUCCESS) {
         return result;
     }
 
     /* This will create a rmm "handler" to get into the shared memory area */
-    apr_rmm_init(&util_ldap_rmm, NULL,
-                       (void *)apr_shm_baseaddr_get(util_ldap_shm), reqsize, pool);
+    apr_rmm_init(&st->cache_rmm, NULL, (void *)apr_shm_baseaddr_get(st->cache_shm), st->cache_bytes, st->pool);
 #endif
 
-    apr_pool_cleanup_register(pool, NULL, util_ldap_cache_module_kill, apr_pool_cleanup_null);
+    apr_pool_cleanup_register(st->pool, st , util_ldap_cache_module_kill, apr_pool_cleanup_null);
 
-    util_ldap_cache = util_ald_create_cache(50,
-                                    util_ldap_url_node_hash,
-                                    util_ldap_url_node_compare,
-                                    util_ldap_url_node_copy,
-                                    util_ldap_url_node_free);
+    st->util_ldap_cache =
+        util_ald_create_cache(st,
+                              util_ldap_url_node_hash,
+                              util_ldap_url_node_compare,
+                              util_ldap_url_node_copy,
+                              util_ldap_url_node_free);
     return APR_SUCCESS;
 }
 
index 32ca1f900356b300e972638efbba653769e183b2..88e1224b2eb2d4873c1abd2452aee6a1b2790cfc 100644 (file)
@@ -77,47 +77,48 @@ typedef struct util_cache_node_t {
     struct util_cache_node_t *next;
 } util_cache_node_t;
 
-typedef struct util_ald_cache_t {
-    unsigned long size;                /* Size of cache array */
-    unsigned long maxentries;  /* Maximum number of cache entries */
-    unsigned long numentries;  /* Current number of cache entries */
-    unsigned long fullmark;    /* Used to keep track of when cache becomes 3/4 full */
-    apr_time_t marktime;       /* Time that the cache became 3/4 full */
-    unsigned long (*hash)(void *);  /* Func to hash the payload */
-    int (*compare)(void *, void *); /* Func to compare two payloads */
-    void * (*copy)(void *);    /* Func to alloc mem and copy payload to new mem */
-    void (*free)(void *);      /* Func to free mem used by the payload */
+typedef struct util_ald_cache util_ald_cache_t;
+
+struct util_ald_cache {
+    unsigned long size;                        /* Size of cache array */
+    unsigned long maxentries;           /* Maximum number of cache entries */
+    unsigned long numentries;           /* Current number of cache entries */
+    unsigned long fullmark;             /* Used to keep track of when cache becomes 3/4 full */
+    apr_time_t marktime;                /* Time that the cache became 3/4 full */
+    unsigned long (*hash)(void *);      /* Func to hash the payload */
+    int (*compare)(void *, void *);     /* Func to compare two payloads */
+    void * (*copy)(util_ald_cache_t *cache, void *); /* Func to alloc mem and copy payload to new mem */
+    void (*free)(util_ald_cache_t *cache, void *); /* Func to free mem used by the payload */
     util_cache_node_t **nodes;
 
-    unsigned long numpurges;   /* No. of times the cache has been purged */
-    double avg_purgetime;      /* Average time to purge the cache */
-    apr_time_t last_purge;     /* Time of the last purge */
-    unsigned long npurged;     /* Number of elements purged in last purge. This is not
-                                  obvious: it won't be 3/4 the size of the cache if 
-                                  there were a lot of expired entries. */
+    unsigned long numpurges;    /* No. of times the cache has been purged */
+    double avg_purgetime;       /* Average time to purge the cache */
+    apr_time_t last_purge;      /* Time of the last purge */
+    unsigned long npurged;      /* Number of elements purged in last purge. This is not
+                                   obvious: it won't be 3/4 the size of the cache if 
+                                   there were a lot of expired entries. */
 
-    unsigned long fetches;     /* Number of fetches */
-    unsigned long hits;                /* Number of cache hits */
-    unsigned long inserts;     /* Number of inserts */
-    unsigned long removes;     /* Number of removes */
-} util_ald_cache_t;
+    unsigned long fetches;      /* Number of fetches */
+    unsigned long hits;         /* Number of cache hits */
+    unsigned long inserts;      /* Number of inserts */
+    unsigned long removes;      /* Number of removes */
 
 #if APR_HAS_SHARED_MEMORY
-apr_shm_t *util_ldap_shm;
-apr_rmm_t *util_ldap_rmm;
+    apr_shm_t *shm_addr;
+    apr_rmm_t *rmm_addr;
 #endif
-util_ald_cache_t *util_ldap_cache;
+
+};
 
 #if APR_HAS_THREADS
-apr_thread_rwlock_t *util_ldap_cache_lock;
 #define LDAP_CACHE_LOCK_CREATE(p) \
-    if (!util_ldap_cache_lock) apr_thread_rwlock_create(&util_ldap_cache_lock, p)
+    if (!st->util_ldap_cache_lock) apr_thread_rwlock_create(&st->util_ldap_cache_lock, st->pool)
 #define LDAP_CACHE_WRLOCK() \
-    apr_thread_rwlock_wrlock(util_ldap_cache_lock)
+    apr_thread_rwlock_wrlock(st->util_ldap_cache_lock)
 #define LDAP_CACHE_UNLOCK() \
-    apr_thread_rwlock_unlock(util_ldap_cache_lock)
+    apr_thread_rwlock_unlock(st->util_ldap_cache_lock)
 #define LDAP_CACHE_RDLOCK() \
-    apr_thread_rwlock_rdlock(util_ldap_cache_lock)
+    apr_thread_rwlock_rdlock(st->util_ldap_cache_lock)
 #else
 #define LDAP_CACHE_LOCK_CREATE(p)
 #define LDAP_CACHE_WRLOCK()
@@ -192,35 +193,39 @@ typedef struct util_dn_compare_node_t {
 /* util_ldap_cache.c */
 unsigned long util_ldap_url_node_hash(void *n);
 int util_ldap_url_node_compare(void *a, void *b);
-void *util_ldap_url_node_copy(void *c);
-void util_ldap_url_node_free(void *n);
+void *util_ldap_url_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_url_node_free(util_ald_cache_t *cache, void *n);
 unsigned long util_ldap_search_node_hash(void *n);
 int util_ldap_search_node_compare(void *a, void *b);
-void *util_ldap_search_node_copy(void *c);
-void util_ldap_search_node_free(void *n);
+void *util_ldap_search_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_search_node_free(util_ald_cache_t *cache, void *n);
 unsigned long util_ldap_compare_node_hash(void *n);
 int util_ldap_compare_node_compare(void *a, void *b);
-void *util_ldap_compare_node_copy(void *c);
-void util_ldap_compare_node_free(void *n);
+void *util_ldap_compare_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_compare_node_free(util_ald_cache_t *cache, void *n);
 unsigned long util_ldap_dn_compare_node_hash(void *n);
 int util_ldap_dn_compare_node_compare(void *a, void *b);
-void *util_ldap_dn_compare_node_copy(void *c);
-void util_ldap_dn_compare_node_free(void *n);
+void *util_ldap_dn_compare_node_copy(util_ald_cache_t *cache, void *c);
+void util_ldap_dn_compare_node_free(util_ald_cache_t *cache, void *n);
 
 
 /* util_ldap_cache_mgr.c */
 
-void util_ald_free(const void *ptr);
-void *util_ald_alloc(unsigned long size);
-const char *util_ald_strdup(const char *s);
+/* Cache alloc and free function, dealing or not with shm */
+void util_ald_free(apr_rmm_t *rmm_addr, const void *ptr);
+void *util_ald_alloc(apr_rmm_t *rmm_addr, unsigned long size);
+const char *util_ald_strdup(apr_rmm_t *rmm_addr, const char *s);
+
+/* Cache managing function */
 unsigned long util_ald_hash_string(int nstr, ...);
 void util_ald_cache_purge(util_ald_cache_t *cache);
 util_url_node_t *util_ald_create_caches(util_ldap_state_t *s, const char *url);
-util_ald_cache_t *util_ald_create_cache(unsigned long maxentries,
+util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
                                 unsigned long (*hashfunc)(void *), 
                                 int (*comparefunc)(void *, void *),
-                                void * (*copyfunc)(void *),
-                                void (*freefunc)(void *));
+                                void * (*copyfunc)(util_ald_cache_t *cache, void *),
+                                void (*freefunc)(util_ald_cache_t *cache, void *));
+                                
 void util_ald_destroy_cache(util_ald_cache_t *cache);
 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload);
 void util_ald_cache_insert(util_ald_cache_t *cache, void *payload);
index 94093ab28e8fc46e4ad4b80008b2498d25da106a..0f37dfa022a8b6d9bc2c28c20e065e123623b762 100644 (file)
@@ -112,14 +112,17 @@ static const unsigned long primes[] =
   0
 };
 
-void util_ald_free(const void *ptr)
+void util_ald_free(apr_rmm_t *rmm_addr, const void *ptr)
 {
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm) {
+    if (rmm_addr) {
         if (ptr)
-            apr_rmm_free(util_ldap_rmm, apr_rmm_offset_get(util_ldap_rmm, (void *)ptr));
-    } else {
+            /* Free in shared memory */
+            apr_rmm_free(rmm_addr, apr_rmm_offset_get(rmm_addr, (void *)ptr));
+    }
+    else {
         if (ptr)
+            /* Cache shm is not used */
             free((void *)ptr);
     }
 #else
@@ -128,14 +131,17 @@ void util_ald_free(const void *ptr)
 #endif
 }
 
-void *util_ald_alloc(unsigned long size)
+void *util_ald_alloc(apr_rmm_t *rmm_addr, unsigned long size)
 {
     if (0 == size)
         return NULL;
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm) {
-        return (void *)apr_rmm_addr_get(util_ldap_rmm, apr_rmm_calloc(util_ldap_rmm, size));
-    } else {
+    if (rmm_addr) {
+        /* allocate from shared memory */
+        return (void *)apr_rmm_addr_get(rmm_addr, apr_rmm_calloc(rmm_addr, size));
+    }
+    else {
+        /* Cache shm is not used */
         return (void *)calloc(sizeof(char), size);
     }
 #else
@@ -143,11 +149,12 @@ void *util_ald_alloc(unsigned long size)
 #endif
 }
 
-const char *util_ald_strdup(const char *s)
+const char *util_ald_strdup(apr_rmm_t *rmm_addr, const char *s)
 {
 #if APR_HAS_SHARED_MEMORY
-    if (util_ldap_shm) {
-        char *buf = (char *)apr_rmm_addr_get(util_ldap_rmm, apr_rmm_calloc(util_ldap_rmm, strlen(s)+1));
+    if (rmm_addr) {
+        /* allocate from shared memory */
+        char *buf = (char *)apr_rmm_addr_get(rmm_addr, apr_rmm_calloc(rmm_addr, strlen(s)+1));
         if (buf) {
             strcpy(buf, s);
             return buf;
@@ -156,6 +163,7 @@ const char *util_ald_strdup(const char *s)
             return NULL;
         }
     } else {
+        /* Cache shm is not used */
         return strdup(s);
     }
 #else
@@ -217,8 +225,8 @@ void util_ald_cache_purge(util_ald_cache_t *cache)
         while (p != NULL) {
             if (p->add_time < cache->marktime) {
                q = p->next;
-               (*cache->free)(p->payload);
-               util_ald_free(p);
+               (*cache->free)(cache, p->payload);
+               util_ald_free(cache->rmm_addr, p);
                cache->numentries--;
                cache->npurged++;
                p = q;
@@ -247,17 +255,17 @@ util_url_node_t *util_ald_create_caches(util_ldap_state_t *st, const char *url)
     util_ald_cache_t *dn_compare_cache;
 
     /* create the three caches */
-    search_cache = util_ald_create_cache(st->search_cache_size,
+    search_cache = util_ald_create_cache(st,
                                          util_ldap_search_node_hash,
                                          util_ldap_search_node_compare,
                                          util_ldap_search_node_copy,
                                          util_ldap_search_node_free);
-    compare_cache = util_ald_create_cache(st->compare_cache_size,
+    compare_cache = util_ald_create_cache(st,
                                           util_ldap_compare_node_hash,
                                           util_ldap_compare_node_compare,
                                           util_ldap_compare_node_copy,
                                           util_ldap_compare_node_free);
-    dn_compare_cache = util_ald_create_cache(st->compare_cache_size,
+    dn_compare_cache = util_ald_create_cache(st,
                                              util_ldap_dn_compare_node_hash,
                                              util_ldap_dn_compare_node_compare,
                                              util_ldap_dn_compare_node_copy,
@@ -272,7 +280,7 @@ util_url_node_t *util_ald_create_caches(util_ldap_state_t *st, const char *url)
         curl->compare_cache = compare_cache;
         curl->dn_compare_cache = dn_compare_cache;
 
-        util_ald_cache_insert(util_ldap_cache, curl);
+        util_ald_cache_insert(st->util_ldap_cache, curl);
 
     }
 
@@ -280,32 +288,32 @@ util_url_node_t *util_ald_create_caches(util_ldap_state_t *st, const char *url)
 }
 
 
-util_ald_cache_t *util_ald_create_cache(unsigned long maxentries,
+util_ald_cache_t *util_ald_create_cache(util_ldap_state_t *st,
                                 unsigned long (*hashfunc)(void *), 
                                 int (*comparefunc)(void *, void *),
-                                void * (*copyfunc)(void *),
-                                void (*freefunc)(void *))
+                                void * (*copyfunc)(util_ald_cache_t *cache, void *),
+                                void (*freefunc)(util_ald_cache_t *cache, void *))
 {
     util_ald_cache_t *cache;
     unsigned long i;
 
-    if (maxentries <= 0)
+    if (st->search_cache_size <= 0)
         return NULL;
 
-    cache = (util_ald_cache_t *)util_ald_alloc(sizeof(util_ald_cache_t));
+    cache = (util_ald_cache_t *)util_ald_alloc(st->cache_rmm, sizeof(util_ald_cache_t));
     if (!cache)
         return NULL;
 
-    cache->maxentries = maxentries;
+    cache->maxentries = st->search_cache_size;
     cache->numentries = 0;
-    cache->size = maxentries / 3;
+    cache->size = st->search_cache_size / 3;
     if (cache->size < 64) cache->size = 64;
         for (i = 0; primes[i] && primes[i] < cache->size; ++i) ;
             cache->size = primes[i]? primes[i] : primes[i-1];
 
-    cache->nodes = (util_cache_node_t **)util_ald_alloc(cache->size * sizeof(util_cache_node_t *));
+    cache->nodes = (util_cache_node_t **)util_ald_alloc(cache->rmm_addr, cache->size * sizeof(util_cache_node_t *));
     if (!cache->nodes) {
-        util_ald_free(cache);
+        util_ald_free(cache->rmm_addr, cache);
         return NULL;
     }
 
@@ -345,13 +353,13 @@ void util_ald_destroy_cache(util_ald_cache_t *cache)
         q = NULL;
         while (p != NULL) {
             q = p->next;
-           (*cache->free)(p->payload);
-           util_ald_free(p);
+           (*cache->free)(cache, p->payload);
+           util_ald_free(cache->rmm_addr, p);
            p = q;
         }
     }
-    util_ald_free(cache->nodes);
-    util_ald_free(cache);
+    util_ald_free(cache->rmm_addr, cache->nodes);
+    util_ald_free(cache->rmm_addr, cache);
 }
 
 void *util_ald_cache_fetch(util_ald_cache_t *cache, void *payload)
@@ -392,9 +400,9 @@ void util_ald_cache_insert(util_ald_cache_t *cache, void *payload)
 
     cache->inserts++;
     hashval = (*cache->hash)(payload) % cache->size;
-    node = (util_cache_node_t *)util_ald_alloc(sizeof(util_cache_node_t));
+    node = (util_cache_node_t *)util_ald_alloc(cache->rmm_addr, sizeof(util_cache_node_t));
     node->add_time = apr_time_now();
-    node->payload = (*cache->copy)(payload);
+    node->payload = (*cache->copy)(cache, payload);
     node->next = cache->nodes[hashval];
     cache->nodes[hashval] = node;
     if (++cache->numentries == cache->fullmark) 
@@ -431,8 +439,8 @@ void util_ald_cache_remove(util_ald_cache_t *cache, void *payload)
         /* We found the node and it's not the first in the list */
         q->next = p->next;
     }
-    (*cache->free)(p->payload);
-    util_ald_free(p);
+    (*cache->free)(cache, p->payload);
+    util_ald_free(cache->rmm_addr, p);
     cache->numentries--;
 }
 
@@ -499,16 +507,19 @@ char *util_ald_cache_display_stats(apr_pool_t *p, util_ald_cache_t *cache, char
     return buf;
 }
 
-char *util_ald_cache_display(apr_pool_t *pool)
+char *util_ald_cache_display(apr_pool_t *pool, util_ldap_state_t *st)
 {
     unsigned long i;
     char *buf, *t1, *t2, *t3;
 
+    util_ald_cache_t *util_ldap_cache = st->util_ldap_cache;
+
+
     if (!util_ldap_cache) {
         return "<tr valign='top'><td nowrap colspan=7>Cache has not been enabled/initialised.</td></tr>";
     }
 
-    buf = util_ald_cache_display_stats(pool, util_ldap_cache, "LDAP URL Cache");
+    buf = util_ald_cache_display_stats(pool, st->util_ldap_cache, "LDAP URL Cache");
 
     for (i=0; i < util_ldap_cache->size; ++i) {
         util_cache_node_t *p;