summaryrefslogtreecommitdiffstats
path: root/src/db/sysdb.c
diff options
context:
space:
mode:
authorSimo Sorce <simo@redhat.com>2013-01-06 03:16:08 -0500
committerJakub Hrozek <jhrozek@redhat.com>2013-01-15 10:49:20 +0100
commit72aa8e7b1d234b6b68446d42efa1cff22b70c81b (patch)
treeb712144660ce3eb931a173fc2d98f00031ca6a52 /src/db/sysdb.c
parentf2ce4a4a45bfc0c9ba6d1a13348494dd4c49d4fb (diff)
downloadsssd-72aa8e7b1d234b6b68446d42efa1cff22b70c81b.tar.gz
sssd-72aa8e7b1d234b6b68446d42efa1cff22b70c81b.tar.xz
sssd-72aa8e7b1d234b6b68446d42efa1cff22b70c81b.zip
Refactor sysdb initialization
Change the way sysdbs are initialized. Make callers responsible for providing the list of domains. Remove the returned array of sysdb contexts, it was used only by sss_cache and not really necessary there either as that tool can easily iterate the domains. Make sysdb ctx children of their respective domains. Neither sysdb context nor domains are ever freed until a program is done so there shouldn't be any memory hierarchy issue. As plus we simplify the code by removing a destructor and a setter function.
Diffstat (limited to 'src/db/sysdb.c')
-rw-r--r--src/db/sysdb.c139
1 files changed, 11 insertions, 128 deletions
diff --git a/src/db/sysdb.c b/src/db/sysdb.c
index b0bea9a73..8b200b01b 100644
--- a/src/db/sysdb.c
+++ b/src/db/sysdb.c
@@ -917,37 +917,6 @@ done:
return ret;
}
-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;
-}
-
/* Compare versions of sysdb, returns ERRNO accordingly */
static errno_t
sysdb_version_check(const char *expected,
@@ -1226,82 +1195,41 @@ done:
}
int sysdb_init(TALLOC_CTX *mem_ctx,
- struct confdb_ctx *cdb,
+ struct sss_domain_info *domains,
const char *alt_db_path,
- bool allow_upgrade,
- struct sysdb_ctx_list **_ctx_list)
+ bool allow_upgrade)
{
- struct sysdb_ctx_list *ctx_list;
- struct sss_domain_info *domains, *dom;
+ struct sss_domain_info *dom;
struct sysdb_ctx *sysdb;
+ const char *db_path;
int ret;
- ctx_list = talloc_zero(mem_ctx, struct sysdb_ctx_list);
- if (!ctx_list) {
- return ENOMEM;
- }
-
if (alt_db_path) {
- ctx_list->db_path = talloc_strdup(ctx_list, alt_db_path);
+ db_path = alt_db_path;
} else {
- ctx_list->db_path = talloc_strdup(ctx_list, DB_PATH);
- }
- if (!ctx_list->db_path) {
- talloc_zfree(ctx_list);
- return ENOMEM;
- }
-
- /* open a db for each backend */
- ret = confdb_get_domains(cdb, &domains);
- if (ret != EOK) {
- talloc_zfree(ctx_list);
- return ret;
+ db_path = DB_PATH;
}
if (allow_upgrade) {
/* check if we have an old sssd.ldb to upgrade */
- ret = sysdb_check_upgrade_02(domains, ctx_list->db_path);
+ ret = sysdb_check_upgrade_02(domains, db_path);
if (ret != EOK) {
- talloc_zfree(ctx_list);
return ret;
}
}
+ /* open a db for each domain */
for (dom = domains; dom; dom = dom->next) {
- ctx_list->dbs = talloc_realloc(ctx_list, ctx_list->dbs,
- struct sysdb_ctx *,
- ctx_list->num_dbs + 1);
- if (!ctx_list->dbs) {
- talloc_zfree(ctx_list);
- return ENOMEM;
- }
-
- ret = sysdb_domain_init_internal(ctx_list, dom,
- ctx_list->db_path,
+ ret = sysdb_domain_init_internal(mem_ctx, dom, db_path,
allow_upgrade, &sysdb);
if (ret != EOK) {
- talloc_zfree(ctx_list);
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++;
- }
- if (ctx_list->num_dbs == 0) {
- /* what? .. */
- talloc_zfree(ctx_list);
- return ENOENT;
+ dom->sysdb = talloc_move(dom, &sysdb);
}
- *_ctx_list = ctx_list;
-
return EOK;
}
@@ -1337,11 +1265,7 @@ errno_t sysdb_init_domain_and_sysdb(TALLOC_CTX *mem_ctx,
return ret;
}
- ret = sysdb_add_to_domain(dom, ctx);
- if (ret != EOK) {
- DEBUG(SSSDBG_OP_FAILURE, ("Error storing cache database context.\n"));
- return ret;
- }
+ dom->sysdb = talloc_steal(dom, ctx);
*_domain = dom;
*_ctx = ctx;
@@ -1349,47 +1273,6 @@ errno_t sysdb_init_domain_and_sysdb(TALLOC_CTX *mem_ctx,
return EOK;
}
-int sysdb_list_init(TALLOC_CTX *mem_ctx,
- const char *path,
- struct sysdb_ctx *sysdb,
- struct sysdb_ctx_list **_list)
-{
- struct sysdb_ctx_list *list;
- int ret;
-
- list = talloc_zero(mem_ctx, struct sysdb_ctx_list);
- if (!list) {
- DEBUG(1, ("talloc_zero failed\n"));
- return ENOMEM;
- }
-
- list->db_path = talloc_strdup(list, path);
- if (!list->db_path) {
- DEBUG(1, ("talloc_strdup failed\n"));
- ret = ENOMEM;
- goto fail;
- }
-
- if (sysdb) {
- list->num_dbs = 1;
- list->dbs = talloc_array(list, struct sysdb_ctx *, list->num_dbs);
- if (!list->dbs) {
- DEBUG(1, ("talloc_array failed\n"));
- ret = ENOMEM;
- goto fail;
- }
-
- list->dbs[0] = talloc_steal(list, sysdb);
- }
-
- *_list = list;
- return EOK;
-
-fail:
- talloc_free(list);
- return ret;
-}
-
int compare_ldb_dn_comp_num(const void *m1, const void *m2)
{
struct ldb_message *msg1 = talloc_get_type(*(void **) discard_const(m1),