r8625: move the ldb_wrap logic into the ldb code. This logic is meant to
authorAndrew Tridgell <tridge@samba.org>
Wed, 20 Jul 2005 00:59:38 +0000 (00:59 +0000)
committerGerald (Jerry) Carter <jerry@samba.org>
Wed, 10 Oct 2007 18:29:45 +0000 (13:29 -0500)
avoid the horrors of posix locking, but it was preventing us having an
ldb open twice with different options. Now each ldb open of the same
file shares the same underlying tdb, but uses a different ldb
structure

source/cldap_server/netlogon.c
source/lib/db_wrap.c
source/lib/ldb/Makefile.in
source/lib/ldb/config.mk
source/lib/ldb/ldb_tdb/ldb_tdb.c
source/lib/ldb/ldb_tdb/ldb_tdb.h
source/lib/ldb/ldb_tdb/ldb_tdb_wrap.c [new file with mode: 0644]

index 3f6c8d49727a36adcb6d2e1e5337f2d3b9420b30..3e99fe80b86e67b8c278b9c582db3bf21cfe87e0 100644 (file)
@@ -55,7 +55,7 @@ static NTSTATUS cldapd_netlogon_fill(struct cldapd_server *cldapd,
        const char *pdc_ip;
 
        if (cldapd->samctx == NULL) {
-               cldapd->samctx = samdb_connect(mem_ctx);
+               cldapd->samctx = samdb_connect(cldapd);
                if (cldapd->samctx == NULL) {
                        DEBUG(2,("Unable to open sam in cldap netlogon reply\n"));
                        return NT_STATUS_INTERNAL_DB_CORRUPTION;
index 57055462ff16913c8c4410a4cd4a02f1771c55cb..8698e9affdd5b1c7de3f519ac11b157c3721fe95 100644 (file)
 #include "lib/ldb/include/ldb.h"
 #include "db_wrap.h"
 
-struct ldb_wrap {
-       struct ldb_context *ldb;
-
-       const char *url;
-       struct ldb_wrap *next, *prev;
-};
-
-static struct ldb_wrap *ldb_list;
 static struct tdb_wrap *tdb_list;
 
 /*
@@ -62,14 +54,6 @@ static void ldb_wrap_debug(void *context, enum ldb_debug_level level,
        free(s);
 }
 
-/* destroy the last connection to a ldb */
-static int ldb_wrap_destructor(void *ctx)
-{
-       struct ldb_wrap *w = ctx;
-       DLIST_REMOVE(ldb_list, w);
-       return 0;
-}                               
-
 /*
   wrapped connection to a ldb database
   to close just talloc_free() the returned ldb_context
@@ -80,18 +64,11 @@ struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
                                     const char *options[])
 {
        struct ldb_context *ldb;
-       struct ldb_wrap *w;
        int ret;
        struct event_context *ev;
        char *real_url = NULL;
 
-       for (w = ldb_list; w; w = w->next) {
-               if (strcmp(url, w->url) == 0) {
-                       return talloc_reference(mem_ctx, w->ldb);
-               }
-       }
-
-       ldb = ldb_init(talloc_autofree_context());
+       ldb = ldb_init(mem_ctx);
        if (ldb == NULL) {
                return NULL;
        }
@@ -126,20 +103,8 @@ struct ldb_context *ldb_wrap_connect(TALLOC_CTX *mem_ctx,
 
        talloc_free(real_url);
 
-       w = talloc(ldb, struct ldb_wrap);
-       if (w == NULL) {
-               talloc_free(ldb);
-               return NULL;
-       }
-
-       w->ldb = ldb;
-       w->url = talloc_strdup(w, url);
-
-       talloc_set_destructor(w, ldb_wrap_destructor);
        ldb_set_debug(ldb, ldb_wrap_debug, NULL);
 
-       DLIST_ADD(ldb_list, w);
-
        return ldb;
 }
 
index 2f82f63714c0a5202967e90ce8ed171225d51e62..81e2b147d584111e1690af645a3541250747efaa 100644 (file)
@@ -46,7 +46,7 @@ TALLOC_OBJ=$(TALLOCDIR)/talloc.o
 
 LDB_TDB_OBJ=ldb_tdb/ldb_tdb.o \
        ldb_tdb/ldb_pack.o ldb_tdb/ldb_search.o ldb_tdb/ldb_index.o \
-       ldb_tdb/ldb_cache.o
+       ldb_tdb/ldb_cache.o ldb_tdb/ldb_tdb_wrap.o
 
 
 COMMON_OBJ=common/ldb.o common/ldb_ldif.o \
index 83de5730d4acf00ae7d941b2e5e640515e24d7f0..0844fd056e63bf23284eb179679b49e0cfea6c90 100644 (file)
@@ -50,7 +50,8 @@ ADD_OBJ_FILES = \
                lib/ldb/ldb_tdb/ldb_search.o \
                lib/ldb/ldb_tdb/ldb_pack.o \
                lib/ldb/ldb_tdb/ldb_index.o \
-               lib/ldb/ldb_tdb/ldb_cache.o
+               lib/ldb/ldb_tdb/ldb_cache.o \
+               lib/ldb/ldb_tdb/ldb_tdb_wrap.o
 REQUIRED_SUBSYSTEMS = \
                LIBTDB
 NOPROTO = YES
index 0c8d2cea3e0f54221a0c9cf7ebbc17a408461d39..40cfe97c297c295af09d53c0ae7a5be8a7f78433 100644 (file)
@@ -818,16 +818,6 @@ static const struct ldb_module_ops ltdb_ops = {
 };
 
 
-/*
-  destroy the ltdb context
-*/
-static int ltdb_destructor(void *p)
-{
-       struct ltdb_private *ltdb = p;
-       tdb_close(ltdb->tdb);
-       return 0;
-}
-
 /*
   connect to the database
 */
@@ -837,7 +827,6 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
        const char *path;
        int tdb_flags, open_flags;
        struct ltdb_private *ltdb;
-       TDB_CONTEXT *tdb;
 
        /* parse the url */
        if (strchr(url, ':')) {
@@ -858,24 +847,21 @@ int ltdb_connect(struct ldb_context *ldb, const char *url,
                open_flags = O_CREAT | O_RDWR;
        }
 
-       /* note that we use quite a large default hash size */
-       tdb = tdb_open(path, 10000, tdb_flags, open_flags, 0666);
-       if (!tdb) {
-               ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'\n", path);
-               return -1;
-       }
-
        ltdb = talloc_zero(ldb, struct ltdb_private);
        if (!ltdb) {
-               tdb_close(tdb);
                ldb_oom(ldb);
                return -1;
        }
 
-       ltdb->tdb = tdb;
-       ltdb->sequence_number = 0;
+       /* note that we use quite a large default hash size */
+       ltdb->tdb = ltdb_wrap_open(ltdb, path, 10000, tdb_flags, open_flags, 0666);
+       if (!ltdb->tdb) {
+               ldb_debug(ldb, LDB_DEBUG_ERROR, "Unable to open tdb '%s'\n", path);
+               talloc_free(ltdb);
+               return -1;
+       }
 
-       talloc_set_destructor(ltdb, ltdb_destructor);
+       ltdb->sequence_number = 0;
 
        ldb->modules = talloc(ldb, struct ldb_module);
        if (!ldb->modules) {
index 46c5843d60bbffa8febac5e8a7dba5c9f268c42a..9ee3bfb70f80ff3da7539a9e7321ff001dab5cbb 100644 (file)
@@ -105,3 +105,7 @@ int ltdb_unlock_read(struct ldb_module *module);
 int ltdb_index_del_value(struct ldb_module *module, const char *dn, 
                         struct ldb_message_element *el, int v_idx);
 
+struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
+                                  const char *path, int hash_size, int tdb_flags,
+                                  int open_flags, mode_t mode);
+
diff --git a/source/lib/ldb/ldb_tdb/ldb_tdb_wrap.c b/source/lib/ldb/ldb_tdb/ldb_tdb_wrap.c
new file mode 100644 (file)
index 0000000..f58ec7f
--- /dev/null
@@ -0,0 +1,115 @@
+/* 
+   ldb database library
+
+   Copyright (C) Andrew Tridgell  2005
+
+     ** NOTE! The following LGPL license applies to the ldb
+     ** library. This does NOT imply that all of Samba is released
+     ** under the LGPL
+   
+   This library is free software; you can redistribute it and/or
+   modify it under the terms of the GNU Lesser General Public
+   License as published by the Free Software Foundation; either
+   version 2 of the License, or (at your option) any later version.
+
+   This library is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+   Lesser General Public License for more details.
+
+   You should have received a copy of the GNU Lesser General Public
+   License along with this library; if not, write to the Free Software
+   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+*/
+
+#include "includes.h"
+#include "ldb/include/ldb.h"
+#include "ldb/include/ldb_private.h"
+#include "ldb/ldb_tdb/ldb_tdb.h"
+
+/*
+  the purpose of this code is to work around the braindead posix locking
+  rules, to allow us to have a ldb open more than once while allowing 
+  locking to work
+*/
+
+struct ltdb_wrap {
+       struct ltdb_wrap *next, *prev;
+       struct tdb_context *tdb;
+       dev_t device;
+       ino_t inode;
+};
+
+static struct ltdb_wrap *tdb_list;
+
+/* destroy the last connection to a tdb */
+static int ltdb_wrap_destructor(void *ctx)
+{
+       struct ltdb_wrap *w = talloc_get_type(ctx, struct ltdb_wrap);
+       tdb_close(w->tdb);
+       if (w->next) {
+               w->next->prev = w->prev;
+       }
+       if (w->prev) {
+               w->prev->next = w->next;
+       }
+       if (w == tdb_list) {
+               tdb_list = w->next;
+       }
+       return 0;
+}                               
+
+/*
+  wrapped connection to a tdb database. The caller should _not_ free
+  this as it is not a talloc structure (as tdb does not use talloc
+  yet). It will auto-close when the caller frees the mem_ctx that is
+  passed to this call
+ */
+struct tdb_context *ltdb_wrap_open(TALLOC_CTX *mem_ctx,
+                                  const char *path, int hash_size, int tdb_flags,
+                                  int open_flags, mode_t mode)
+{
+       struct ltdb_wrap *w;
+       struct stat st;
+
+       if (stat(path, &st) == 0) {
+               for (w=tdb_list;w;w=w->next) {
+                       if (st.st_dev == w->device && st.st_ino == w->inode) {
+                               talloc_reference(mem_ctx, w);
+                               return w->tdb;
+                       }
+               }
+       }
+
+       w = talloc(mem_ctx, struct ltdb_wrap);
+       if (w == NULL) {
+               return NULL;
+       }
+
+       w->tdb = tdb_open(path, hash_size, tdb_flags, open_flags, mode);
+       if (w->tdb == NULL) {
+               talloc_free(w);
+               return NULL;
+       }
+
+       if (fstat(w->tdb->fd, &st) != 0) {
+               tdb_close(w->tdb);
+               talloc_free(w);
+               return NULL;
+       }
+
+       w->device = st.st_dev;
+       w->inode  = st.st_ino;
+
+       talloc_set_destructor(w, ltdb_wrap_destructor);
+
+       w->next = tdb_list;
+       w->prev = NULL;
+       if (tdb_list) {
+               tdb_list->prev = w;
+       }
+       tdb_list = w;
+       
+       return w->tdb;
+}
+