Remove more function-based inits.
authorJelmer Vernooij <jelmer@samba.org>
Wed, 20 Feb 2008 01:57:07 +0000 (02:57 +0100)
committerJelmer Vernooij <jelmer@samba.org>
Wed, 20 Feb 2008 01:57:07 +0000 (02:57 +0100)
(This used to be commit b1a7810f3e70f9a831d9b8e85d531e448072adaf)

source4/lib/ldb/common/ldb.c
source4/lib/ldb/common/ldb_modules.c
source4/lib/ldb/include/ldb_private.h
source4/lib/ldb/ldb_ildap/ldb_ildap.c
source4/lib/ldb/ldb_sqlite3/ldb_sqlite3.c
source4/lib/ldb/ldb_tdb/ldb_tdb.c

index ffda705a0ba6e65d4290fbef3f25180827eb6fe3..ce69256c69154e2f818e471a1e7cbe7d4600315d 100644 (file)
@@ -56,20 +56,51 @@ struct ldb_context *ldb_init(void *mem_ctx)
        return ldb;
 }
 
-struct ldb_backend {
-       const char *name;
-       ldb_connect_fn connect_fn;
-       struct ldb_backend *prev, *next;
+static struct backends_list_entry {
+       struct ldb_backend_ops *ops;
+       struct backends_list_entry *prev, *next;
 } *ldb_backends = NULL;
 
+#ifndef STATIC_LIBLDB_BACKENDS 
+
+#ifdef HAVE_LDB_LDAP
+#define LDAP_INIT &ldb_ldap_backend_ops, \
+                                 &ldb_ildap_backend_ops, \
+                                 &ldb_ldaps_backend_ops,
+#else
+#define LDAP_INIT
+#endif
+
+#ifdef HAVE_LDB_SQLITE3
+#define SQLITE3_INIT &ldb_sqlite3_backend_ops,
+#else
+#define SQLITE3_INIT
+#endif
+
+#define STATIC_LIBLDB_BACKENDS \
+       LDAP_INIT \
+       SQLITE3_INIT \
+       &ldb_tdb_backend_ops,   \
+       NULL
+#endif
+
+const static struct ldb_backend_ops *builtin_backends[] = {
+       STATIC_LIBLDB_BACKENDS
+};
 
 static ldb_connect_fn ldb_find_backend(const char *url)
 {
-       struct ldb_backend *backend;
+       struct backends_list_entry *backend;
+       int i;
+
+       for (i = 0; builtin_backends[i]; i++) {
+               if (strncmp(builtin_backends[i]->name, url, strlen(builtin_backends[i]->name)) == 0)
+                       return builtin_backends[i]->connect_fn;
+       }
 
        for (backend = ldb_backends; backend; backend = backend->next) {
-               if (strncmp(backend->name, url, strlen(backend->name)) == 0) {
-                       return backend->connect_fn;
+               if (strncmp(backend->ops->name, url, strlen(backend->ops->name)) == 0) {
+                       return backend->ops->connect_fn;
                }
        }
 
@@ -81,7 +112,8 @@ static ldb_connect_fn ldb_find_backend(const char *url)
 */
 int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
 {
-       struct ldb_backend *backend = talloc(talloc_autofree_context(), struct ldb_backend);
+       struct ldb_backend_ops *backend = talloc(talloc_autofree_context(), struct ldb_backend_ops);
+       struct backends_list_entry *entry = talloc(talloc_autofree_context(), struct backends_list_entry);
 
        if (ldb_find_backend(url_prefix)) {
                return LDB_SUCCESS;
@@ -91,7 +123,8 @@ int ldb_register_backend(const char *url_prefix, ldb_connect_fn connectfn)
 
        backend->name = talloc_strdup(backend, url_prefix);
        backend->connect_fn = connectfn;
-       DLIST_ADD(ldb_backends, backend);
+       entry->ops = backend;
+       DLIST_ADD(ldb_backends, entry);
 
        return LDB_SUCCESS;
 }
@@ -136,11 +169,15 @@ int ldb_connect_backend(struct ldb_context *ldb, const char *url, const char *op
        }
 
        if (fn == NULL) {
-               char *symbol_name = talloc_asprintf(ldb, "ldb_%s_connect", backend);
+               struct ldb_backend_ops *ops;
+               char *symbol_name = talloc_asprintf(ldb, "ldb_%s_backend_ops", backend);
                if (symbol_name == NULL) {
                        return LDB_ERR_OPERATIONS_ERROR;
                }
-               fn = ldb_dso_load_symbol(ldb, backend, symbol_name);
+               ops = ldb_dso_load_symbol(ldb, backend, symbol_name);
+               if (ops != NULL) {
+                       fn = ops->connect_fn;
+               }
                talloc_free(symbol_name);
        }
 
index 2dae40ddb00d3f3807244646ce1f4f0e4a761811..7da7b9ba34d073534947978d749d45cbe8817a9d 100644 (file)
@@ -126,9 +126,30 @@ static struct ops_list_entry {
        struct ops_list_entry *next;    
 } *registered_modules = NULL;
 
+#ifndef STATIC_LIBLDB_MODULES
+
+#define STATIC_LIBLDB_MODULES \
+       ldb_operational_module_ops,     \
+       ldb_rdn_name_module_ops,        \
+       ldb_paged_results_module_ops,   \
+       ldb_sort_module_ops,            \
+       ldb_asq_module_ops, \
+       NULL
+#endif
+
+const static struct ldb_module_ops *builtin_modules[] = {
+       STATIC_LIBLDB_MODULES
+};
+
 static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
 {
        struct ops_list_entry *e;
+       int i;
+
+       for (i = 0; builtin_modules[i]; i++) {
+               if (strcmp(builtin_modules[i]->name, name) == 0)
+                       return builtin_modules[i];
+       }
  
        for (e = registered_modules; e; e = e->next) {
                if (strcmp(e->ops->name, name) == 0) 
@@ -138,51 +159,6 @@ static const struct ldb_module_ops *ldb_find_module_ops(const char *name)
        return NULL;
 }
 
-#ifndef STATIC_LIBLDB_MODULES
-
-#ifdef HAVE_LDB_LDAP
-#define LDAP_INIT ldb_ldap_init,
-#else
-#define LDAP_INIT
-#endif
-
-#ifdef HAVE_LDB_SQLITE3
-#define SQLITE3_INIT ldb_sqlite3_init,
-#else
-#define SQLITE3_INIT
-#endif
-
-#define STATIC_LIBLDB_MODULES \
-       LDAP_INIT \
-       SQLITE3_INIT \
-       ldb_tdb_init,   \
-       ldb_operational_init,   \
-       ldb_rdn_name_init,      \
-       ldb_paged_results_init, \
-       ldb_sort_init,          \
-       ldb_asq_init, \
-       NULL
-#endif
-
-int ldb_global_init(void)
-{
-       int (*static_init_fns[])(void) = { STATIC_LIBLDB_MODULES };
-
-       static int initialized = 0;
-       int ret = 0, i;
-
-       if (initialized) 
-               return 0;
-
-       initialized = 1;
-       
-       for (i = 0; static_init_fns[i]; i++) {
-               if (static_init_fns[i]() == -1)
-                       ret = -1;
-       }
-
-       return ret;
-}
 
 int ldb_register_module(const struct ldb_module_ops *ops)
 {
index d9f2defdc914931e3d12e0643ef7ec5b6faea8dc..d2dcc675a58a6b380992c2f3ab26feef7feabe86 100644 (file)
@@ -41,6 +41,8 @@ struct ldb_context;
 
 struct ldb_module_ops;
 
+struct ldb_backend_ops;
+
 /* basic module structure */
 struct ldb_module {
        struct ldb_module *prev, *next;
@@ -70,9 +72,16 @@ struct ldb_module_ops {
        int (*sequence_number)(struct ldb_module *, struct ldb_request *);
 };
 
+
 typedef int (*ldb_connect_fn) (struct ldb_context *ldb, const char *url, unsigned int flags, const char *options[],
                               struct ldb_module **module);
 
+
+struct ldb_backend_ops {
+       const char *name;
+       ldb_connect_fn connect_fn;
+};
+
 const char *ldb_default_modules_dir(void);
 
 /*
@@ -170,17 +179,23 @@ void ldb_debug_set(struct ldb_context *ldb, enum ldb_debug_level level,
 /* The following definitions come from lib/ldb/common/ldb_ldif.c  */
 int ldb_should_b64_encode(const struct ldb_val *val);
 
-int ldb_objectclass_init(void);
-int ldb_operational_init(void);
-int ldb_paged_results_init(void);
-int ldb_rdn_name_init(void);
-int ldb_schema_init(void);
-int ldb_asq_init(void);
-int ldb_sort_init(void);
-int ldb_ldap_init(void);
-int ldb_ildap_init(void);
-int ldb_tdb_init(void);
-int ldb_sqlite3_init(void);
+extern const struct ldb_module_ops ldb_objectclass_module_ops;
+extern const struct ldb_module_ops ldb_operational_module_ops;
+extern const struct ldb_module_ops ldb_paged_results_module_ops;
+extern const struct ldb_module_ops ldb_rdn_name_module_ops;
+extern const struct ldb_module_ops ldb_schema_module_ops;
+extern const struct ldb_module_ops ldb_asq_module_ops;
+extern const struct ldb_module_ops ldb_sort_module_ops;
+extern const struct ldb_module_ops ldb_ldap_module_ops;
+extern const struct ldb_module_ops ldb_ildap_module_ops;
+extern const struct ldb_module_ops ldb_tdb_module_ops;
+extern const struct ldb_module_ops ldb_sqlite3_module_ops;
+
+extern const struct ldb_backend_ops ldb_tdb_backend_ops;
+extern const struct ldb_backend_ops ldb_sqlite3_backend_ops;
+extern const struct ldb_backend_ops ldb_ldap_backend_ops;
+extern const struct ldb_backend_ops ldb_ildap_backend_ops;
+extern const struct ldb_backend_ops ldb_ldaps_backend_ops;
 
 int ldb_match_msg(struct ldb_context *ldb,
                  const struct ldb_message *msg,
index 8ad6830c4354f2b5c821d45b3fc40a8694c79208..fb7100e3c10bf0ce07dea4d18c3a095ccd06fbe2 100644 (file)
@@ -811,6 +811,18 @@ failed:
        return -1;
 }
 
-ldb_connect_fn ldb_ldap_connect = ildb_connect;
-ldb_connect_fn ldb_ldapi_connect = ildb_connect;
-ldb_connect_fn ldb_ldaps_connect = ildb_connect;
+const struct ldb_backend_ops ldb_ldap_backend_ops {
+       .name = "ldap",
+       .connect_fn = ildb_connect
+};
+
+const struct ldb_backend_ops ldb_ildap_backend_ops {
+       .name = "ildap",
+       .connect_fn = ildb_connect
+};
+
+const struct ldb_backend_ops ldb_ldaps_backend_ops {
+       .name = "ldaps",
+       .connect_fn = ildb_connect
+};
+
index 1ec3b1aabdf9355deaad024891da4c3ad7c11b67..8742e257f3058abb96a321b81f90e7a93d5c0f70 100644 (file)
@@ -1903,7 +1903,7 @@ failed:
        return -1;
 }
 
-int ldb_sqlite3_init(void)
-{
-       return ldb_register_backend("sqlite3", lsqlite3_connect);
-}
+const struct ldb_backend_ops ldb_sqlite3_backend_ops = {
+       .name = "sqlite3",
+       .connect_fn = lsqlite3_connect
+};
index 45a810958459d9e3e2e74be6a5ea79f84f4d4ea0..11d6c30710d7d91b6984ad12af20a93e8d4ab4a8 100644 (file)
@@ -1107,7 +1107,7 @@ static int ltdb_connect(struct ldb_context *ldb, const char *url,
        return 0;
 }
 
-int ldb_tdb_init(void)
-{
-       return ldb_register_backend("tdb", ltdb_connect);
-}
+const struct ldb_backend_ops ldb_tdb_backend_ops = {
+       .name = "tdb",
+       .connect_fn = ltdb_connect
+};