[SSSD] [PATCH] Keep sysdb context in domain info struct

Sumit Bose sbose at redhat.com
Wed Jan 25 17:12:17 UTC 2012


Hi,

a few days ago I send a draft patch where the sysdb context is stored
in the domain info struct. I created a patch which is a bot more
conservative than the last one and included the comments by Simo (added
a destructor and don't unconditionally add the context all the time).

This patch will make the handling of sub-domains much easier, because
now only the domain info struct for the sub-domains needs to be
up-to-date.

bye,
Sumit
-------------- next part --------------
From e540f31d1d513ba0765310df37fdf04d4917a6dd Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose at redhat.com>
Date: Mon, 23 Jan 2012 12:57:33 +0100
Subject: [PATCH] Keep sysdb context in domain info struct

---
 src/confdb/confdb.h              |    2 +
 src/db/sysdb.c                   |   80 ++++++++++++++++++++++++++++++++++++++
 src/db/sysdb.h                   |    9 ++++
 src/providers/data_provider_be.c |    9 +---
 src/python/pysss.c               |   11 +----
 src/tests/auth-tests.c           |   12 +----
 src/tests/sysdb-tests.c          |   12 +----
 src/tools/sss_cache.c            |   10 +----
 src/tools/tools_util.c           |   10 +----
 9 files changed, 105 insertions(+), 50 deletions(-)

diff --git a/src/confdb/confdb.h b/src/confdb/confdb.h
index 7cfc73d..88cb751 100644
--- a/src/confdb/confdb.h
+++ b/src/confdb/confdb.h
@@ -163,6 +163,8 @@ struct sss_domain_info {
 
     uint32_t entry_cache_timeout;
 
+    struct sysdb_ctx *sysdb;
+
     struct sss_domain_info *next;
 };
 
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index d872bc9..aaeb2c1 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -796,6 +796,37 @@ int sysdb_get_db_file(TALLOC_CTX *mem_ctx,
     return EOK;
 }
 
+static int remove_sysdb_from_domain(void *mem)
+{
+    struct sysdb_ctx *ctx = talloc_get_type(mem, struct sysdb_ctx);
+
+    if (ctx->domain != NULL && ctx->domain->sysdb == ctx) {
+        ctx->domain->sysdb = NULL;
+    }
+
+    return 0;
+}
+
+errno_t sysdb_add_to_domain(struct sss_domain_info *domain,
+                            struct sysdb_ctx *ctx)
+{
+    if (domain == NULL || ctx == NULL) {
+        DEBUG(SSSDBG_OP_FAILURE, ("Missing domain or sysdb context.\n"));
+        return EINVAL;
+    }
+
+    if (domain->sysdb != NULL) {
+        DEBUG(SSSDBG_OP_FAILURE, ("Sysdb context already set.\n"));
+        return EINVAL;
+    }
+
+    domain->sysdb = ctx;
+
+    talloc_set_destructor((TALLOC_CTX *) ctx, remove_sysdb_from_domain);
+
+    return EOK;
+}
+
 int sysdb_domain_init_internal(TALLOC_CTX *mem_ctx,
                                struct sss_domain_info *domain,
                                const char *db_path,
@@ -1134,6 +1165,12 @@ int sysdb_init(TALLOC_CTX *mem_ctx,
             return ret;
         }
 
+        ret = sysdb_add_to_domain(dom, sysdb);
+        if (ret != EOK) {
+            talloc_zfree(ctx_list);
+            return ret;
+        }
+
         ctx_list->dbs[ctx_list->num_dbs] = sysdb;
         ctx_list->num_dbs++;
     }
@@ -1157,6 +1194,41 @@ int sysdb_domain_init(TALLOC_CTX *mem_ctx,
                                       db_path, false, _ctx);
 }
 
+errno_t sysdb_init_domain_and_sysdb(TALLOC_CTX *mem_ctx,
+                                    struct confdb_ctx *cdb,
+                                    const char *domain_name,
+                                    const char *db_path,
+                                    struct sss_domain_info **_domain,
+                                    struct sysdb_ctx **_ctx)
+{
+    int ret;
+    struct sss_domain_info *dom;
+    struct sysdb_ctx *ctx;
+
+    ret = confdb_get_domain(cdb, domain_name, &dom);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, ("Error retrieving domain configuration.\n"));
+        return ret;
+    }
+
+    ret = sysdb_domain_init(mem_ctx, dom, db_path, &ctx);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, ("Error opening cache database.\n"));
+        return ret;
+    }
+
+    ret = sysdb_add_to_domain(dom, ctx);
+    if (ret != EOK) {
+        DEBUG(SSSDBG_OP_FAILURE, ("Error storing cache database context.\n"));
+        return ret;
+    }
+
+    *_domain = dom;
+    *_ctx = ctx;
+
+    return EOK;
+}
+
 int sysdb_list_init(TALLOC_CTX *mem_ctx,
                     const char *path,
                     struct sysdb_ctx *sysdb,
@@ -1204,6 +1276,14 @@ int sysdb_get_ctx_from_list(struct sysdb_ctx_list *ctx_list,
 {
     int i;
 
+    if (domain->sysdb != NULL) {
+        *sysdb = domain->sysdb;
+        return EOK;
+    }
+
+    DEBUG(SSSDBG_TRACE_FUNC, ("sysdb context not stored in domain, "
+                              "trying to find by name.\n"));
+
     for (i = 0; i < ctx_list->num_dbs; i++) {
         if (ctx_list->dbs[i]->domain == domain) {
             *sysdb = ctx_list->dbs[i];
diff --git a/src/db/sysdb.h b/src/db/sysdb.h
index c4d64be..8566d14 100644
--- a/src/db/sysdb.h
+++ b/src/db/sysdb.h
@@ -315,6 +315,13 @@ int sysdb_domain_init(TALLOC_CTX *mem_ctx,
                       const char *db_path,
                       struct sysdb_ctx **_ctx);
 
+errno_t sysdb_init_domain_and_sysdb(TALLOC_CTX *mem_ctx,
+                                    struct confdb_ctx *cdb,
+                                    const char *domain_name,
+                                    const char *db_path,
+                                    struct sss_domain_info **_domain,
+                                    struct sysdb_ctx **_ctx);
+
 int sysdb_list_init(TALLOC_CTX *mem_ctx,
                     const char *path,
                     struct sysdb_ctx *sysdb,
@@ -324,6 +331,8 @@ int sysdb_get_ctx_from_list(struct sysdb_ctx_list *ctx_list,
                             struct sss_domain_info *domain,
                             struct sysdb_ctx **_ctx);
 
+errno_t sysdb_add_to_domain(struct sss_domain_info *domain,
+                            struct sysdb_ctx *ctx);
 /* functions to retrieve information from sysdb
  * These functions automatically starts an operation
  * therefore they cannot be called within a transaction */
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index e30395d..865c09d 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -1238,13 +1238,8 @@ int be_process_init(TALLOC_CTX *mem_ctx,
         return ret;
     }
 
-    ret = confdb_get_domain(cdb, be_domain, &ctx->domain);
-    if (ret != EOK) {
-        DEBUG(0, ("fatal error retrieving domain configuration\n"));
-        return ret;
-    }
-
-    ret = sysdb_domain_init(ctx, ctx->domain, DB_PATH, &ctx->sysdb);
+    ret = sysdb_init_domain_and_sysdb(ctx, cdb, be_domain, DB_PATH,
+                                      &ctx->domain, &ctx->sysdb);
     if (ret != EOK) {
         DEBUG(0, ("fatal error opening cache database\n"));
         return ret;
diff --git a/src/python/pysss.c b/src/python/pysss.c
index 948fd16..45725c0 100644
--- a/src/python/pysss.c
+++ b/src/python/pysss.c
@@ -772,15 +772,8 @@ static PyObject *PySssLocalObject_new(PyTypeObject *type,
         return NULL;
     }
 
-    ret = confdb_get_domain(self->confdb, "local", &self->local);
-    if (ret != EOK) {
-        talloc_free(mem_ctx);
-        PyErr_SetSssErrorWithMessage(ret, "Cannot get local domain");
-        return NULL;
-    }
-
-    /* open 'local' sysdb at default path */
-    ret = sysdb_domain_init(self->mem_ctx, self->local, DB_PATH, &self->sysdb);
+    ret = sysdb_init_domain_and_sysdb(self->mem_ctx, self->confdb, "local",
+                                      DB_PATH, &self->local, &self->sysdb);
     if (ret != EOK) {
         talloc_free(mem_ctx);
         PyErr_SetSssErrorWithMessage(ret,
diff --git a/src/tests/auth-tests.c b/src/tests/auth-tests.c
index 96bae98..4d25e2e 100644
--- a/src/tests/auth-tests.c
+++ b/src/tests/auth-tests.c
@@ -134,15 +134,9 @@ static int setup_sysdb_tests(struct sysdb_test_ctx **ctx)
         return ret;
     }
 
-    ret = confdb_get_domain(test_ctx->confdb, "local", &test_ctx->domain);
-    if (ret != EOK) {
-        fail("Could not retrieve LOCAL domain");
-        talloc_free(test_ctx);
-        return ret;
-    }
-
-    ret = sysdb_domain_init(test_ctx,
-                            test_ctx->domain, TESTS_PATH, &test_ctx->sysdb);
+    ret = sysdb_init_domain_and_sysdb(test_ctx, test_ctx->confdb, "local",
+                                      TESTS_PATH,
+                                      &test_ctx->domain,  &test_ctx->sysdb);
     if (ret != EOK) {
         fail("Could not initialize connection to the sysdb (%d)", ret);
         talloc_free(test_ctx);
diff --git a/src/tests/sysdb-tests.c b/src/tests/sysdb-tests.c
index 9c080c9..2216963 100644
--- a/src/tests/sysdb-tests.c
+++ b/src/tests/sysdb-tests.c
@@ -140,15 +140,9 @@ static int setup_sysdb_tests(struct sysdb_test_ctx **ctx)
         return ret;
     }
 
-    ret = confdb_get_domain(test_ctx->confdb, "local", &test_ctx->domain);
-    if (ret != EOK) {
-        fail("Could not retrieve LOCAL domain");
-        talloc_free(test_ctx);
-        return ret;
-    }
-
-    ret = sysdb_domain_init(test_ctx,
-                            test_ctx->domain, TESTS_PATH, &test_ctx->sysdb);
+    ret = sysdb_init_domain_and_sysdb(test_ctx, test_ctx->confdb, "local",
+                                      TESTS_PATH,
+                                      &test_ctx->domain, &test_ctx->sysdb);
     if (ret != EOK) {
         fail("Could not initialize connection to the sysdb (%d)", ret);
         talloc_free(test_ctx);
diff --git a/src/tools/sss_cache.c b/src/tools/sss_cache.c
index e8b1926..a9885b0 100644
--- a/src/tools/sss_cache.c
+++ b/src/tools/sss_cache.c
@@ -204,14 +204,8 @@ errno_t init_domains(struct cache_tool_ctx *ctx, const char *domain) {
     }
 
     if (domain) {
-        ret = confdb_get_domain(ctx->confdb, domain, &ctx->domains);
-        if (ret != EOK) {
-            DEBUG(1, ("Could not get '%s' domain: [%d] [%s]\n",
-                      domain, ret, strerror(ret)));
-            goto fail;
-        }
-
-        ret = sysdb_domain_init(ctx, ctx->domains, DB_PATH, &db_ctx);
+        ret = sysdb_init_domain_and_sysdb(ctx, ctx->confdb, domain, DB_PATH,
+                                          &ctx->domains, &db_ctx);
         if (ret != EOK) {
             DEBUG(1, ("Could not initialize connection to the sysdb\n"));
             goto fail;
diff --git a/src/tools/tools_util.c b/src/tools/tools_util.c
index 404aef2..a9314d4 100644
--- a/src/tools/tools_util.c
+++ b/src/tools/tools_util.c
@@ -56,14 +56,8 @@ static int setup_db(struct tools_ctx *ctx)
         return ret;
     }
 
-    ret = confdb_get_domain(ctx->confdb, "local", &ctx->local);
-    if (ret != EOK) {
-        DEBUG(1, ("Could not get 'local' domain: [%d] [%s]\n", ret, strerror(ret)));
-        return ret;
-    }
-
-    /* open 'local' sysdb at default path */
-    ret = sysdb_domain_init(ctx, ctx->local, DB_PATH, &ctx->sysdb);
+    ret = sysdb_init_domain_and_sysdb(ctx, ctx->confdb, "local", DB_PATH,
+                                      &ctx->local, &ctx->sysdb);
     if (ret != EOK) {
         DEBUG(1, ("Could not initialize connection to the sysdb\n"));
         return ret;
-- 
1.7.6



More information about the sssd-devel mailing list