From 338ee96b31e12459b4d172b6e2d5f0b3c414cc40 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 24 Jan 2017 12:36:04 +0100
Subject: [PATCH 1/4] sbus: remove unused symbols

---
 src/sbus/sssd_dbus.h            | 2 --
 src/sbus/sssd_dbus_connection.c | 9 ---------
 2 files changed, 11 deletions(-)

diff --git a/src/sbus/sssd_dbus.h b/src/sbus/sssd_dbus.h
index 5a66f09..c6cca7d 100644
--- a/src/sbus/sssd_dbus.h
+++ b/src/sbus/sssd_dbus.h
@@ -247,8 +247,6 @@ sbus_opath_get_object_name(TALLOC_CTX *mem_ctx,
                            const char *object_path,
                            const char *base_path);
 
-bool sbus_conn_disconnecting(struct sbus_connection *conn);
-
 /* max_retries < 0: retry forever
  * max_retries = 0: never retry (why are you calling this function?)
  * max_retries > 0: obvious
diff --git a/src/sbus/sssd_dbus_connection.c b/src/sbus/sssd_dbus_connection.c
index 450cee9..9def708 100644
--- a/src/sbus/sssd_dbus_connection.c
+++ b/src/sbus/sssd_dbus_connection.c
@@ -27,9 +27,6 @@
 #include "sbus/sssd_dbus_private.h"
 #include "sbus/sssd_dbus_meta.h"
 
-/* Types */
-struct dbus_ctx_list;
-
 static int sbus_auto_reconnect(struct sbus_connection *conn);
 
 static void sbus_dispatch(struct tevent_context *ev,
@@ -501,12 +498,6 @@ void sbus_reconnect_init(struct sbus_connection *conn,
     conn->reconnect_pvt = pvt;
 }
 
-bool sbus_conn_disconnecting(struct sbus_connection *conn)
-{
-    if (conn->disconnect == 1) return true;
-    return false;
-}
-
 int sss_dbus_conn_send(DBusConnection *dbus_conn,
                        DBusMessage *msg,
                        int timeout_ms,

From 44cf73936c824014040bfda2db6c7e159bfa2a68 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 24 Jan 2017 13:14:47 +0100
Subject: [PATCH 2/4] sbus: use sss_ptr_hash for opath table

---
 src/sbus/sssd_dbus_connection.c |  4 +-
 src/sbus/sssd_dbus_interface.c  | 94 +++++++++++------------------------------
 src/sbus/sssd_dbus_private.h    |  5 +--
 3 files changed, 28 insertions(+), 75 deletions(-)

diff --git a/src/sbus/sssd_dbus_connection.c b/src/sbus/sssd_dbus_connection.c
index 9def708..6ca039e 100644
--- a/src/sbus/sssd_dbus_connection.c
+++ b/src/sbus/sssd_dbus_connection.c
@@ -163,8 +163,8 @@ int sbus_init_connection(TALLOC_CTX *ctx,
     conn->last_request_time = last_request_time;
     conn->client_destructor_data = client_destructor_data;
 
-    ret = sbus_opath_hash_init(conn, conn, &conn->managed_paths);
-    if (ret != EOK) {
+    conn->managed_paths = sbus_opath_hash_init(conn, conn);
+    if (conn->managed_paths == NULL) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create object paths hash table\n");
         talloc_free(conn);
         return EIO;
diff --git a/src/sbus/sssd_dbus_interface.c b/src/sbus/sssd_dbus_interface.c
index 32e5b27..e8c8851 100644
--- a/src/sbus/sssd_dbus_interface.c
+++ b/src/sbus/sssd_dbus_interface.c
@@ -23,6 +23,7 @@
 #include <dhash.h>
 
 #include "util/util.h"
+#include "util/sss_ptr_hash.h"
 #include "sbus/sssd_dbus.h"
 #include "sbus/sssd_dbus_meta.h"
 #include "sbus/sssd_dbus_private.h"
@@ -492,13 +493,11 @@ sbus_opath_hash_delete_cb(hash_entry_t *item,
     dbus_connection_unregister_object_path(conn->dbus.conn, path);
 }
 
-errno_t
+hash_table_t *
 sbus_opath_hash_init(TALLOC_CTX *mem_ctx,
-                     struct sbus_connection *conn,
-                     hash_table_t **_table)
+                     struct sbus_connection *conn)
 {
-    return sss_hash_create_ex(mem_ctx, 10, _table, 0, 0, 0, 0,
-                              sbus_opath_hash_delete_cb, conn);
+    return sss_ptr_hash_create(mem_ctx, sbus_opath_hash_delete_cb, conn);
 }
 
 static errno_t
@@ -511,11 +510,8 @@ sbus_opath_hash_add_iface(hash_table_t *table,
     struct sbus_interface_list *list = NULL;
     struct sbus_interface_list *item = NULL;
     const char *iface_name = iface->vtable->meta->name;
-    hash_key_t key;
-    hash_value_t value;
     bool path_known;
     errno_t ret;
-    int hret;
 
     tmp_ctx = talloc_new(NULL);
     if (tmp_ctx == NULL) {
@@ -536,22 +532,14 @@ sbus_opath_hash_add_iface(hash_table_t *table,
 
     /* first lookup existing list in hash table */
 
-    key.type = HASH_KEY_STRING;
-    key.str = talloc_strdup(tmp_ctx, object_path);
-    if (key.str == NULL) {
-        ret = ENOMEM;
-        goto done;
-    }
-
-    hret = hash_lookup(table, &key, &value);
-    if (hret == HASH_SUCCESS) {
+    list = sss_ptr_hash_lookup(table, object_path, struct sbus_interface_list);
+    if (list != NULL) {
         /* This object path has already some interface registered. We will
          * check for existence of the interface currently being added and
          * add it if missing. */
 
         path_known = true;
 
-        list = talloc_get_type(value.ptr, struct sbus_interface_list);
         if (sbus_iface_list_lookup(list, iface_name) != NULL) {
             DEBUG(SSSDBG_MINOR_FAILURE, "Trying to register the same interface"
                   " twice: iface=%s, opath=%s\n", iface_name, object_path);
@@ -562,9 +550,6 @@ sbus_opath_hash_add_iface(hash_table_t *table,
         DLIST_ADD_END(list, item, struct sbus_interface_list *);
         ret = EOK;
         goto done;
-    } else if (hret != HASH_ERROR_KEY_NOT_FOUND) {
-        ret = EIO;
-        goto done;
     }
 
     /* otherwise create new hash entry and new list */
@@ -572,17 +557,8 @@ sbus_opath_hash_add_iface(hash_table_t *table,
     path_known = false;
     list = item;
 
-    value.type = HASH_VALUE_PTR;
-    value.ptr = list;
-
-    hret = hash_enter(table, &key, &value);
-    if (hret != HASH_SUCCESS) {
-        ret = EIO;
-        goto done;
-    }
-
-    talloc_steal(table, key.str);
-    ret = EOK;
+    ret = sss_ptr_hash_add(table, object_path, list,
+                           struct sbus_interface_list);
 
 done:
     if (ret == EOK) {
@@ -599,12 +575,7 @@ static bool
 sbus_opath_hash_has_path(hash_table_t *table,
                          const char *object_path)
 {
-    hash_key_t key;
-
-    key.type = HASH_KEY_STRING;
-    key.str = discard_const(object_path);
-
-    return hash_has_key(table, &key);
+    return sss_ptr_hash_has_key(table, object_path);
 }
 
 /**
@@ -621,9 +592,6 @@ sbus_opath_hash_lookup_iface(hash_table_t *table,
     struct sbus_interface_list *list = NULL;
     struct sbus_interface *iface = NULL;
     char *lookup_path = NULL;
-    hash_key_t key;
-    hash_value_t value;
-    int hret;
 
     tmp_ctx = talloc_new(NULL);
     if (tmp_ctx == NULL) {
@@ -636,21 +604,13 @@ sbus_opath_hash_lookup_iface(hash_table_t *table,
     }
 
     while (lookup_path != NULL) {
-        key.type = HASH_KEY_STRING;
-        key.str = lookup_path;
-
-        hret = hash_lookup(table, &key, &value);
-        if (hret == HASH_SUCCESS) {
-            list = talloc_get_type(value.ptr, struct sbus_interface_list);
+        list = sss_ptr_hash_lookup(table, lookup_path,
+                                   struct sbus_interface_list);
+        if (list != NULL) {
             iface = sbus_iface_list_lookup(list, iface_name);
             if (iface != NULL) {
                 goto done;
             }
-        } else if (hret != HASH_ERROR_KEY_NOT_FOUND) {
-            DEBUG(SSSDBG_OP_FAILURE,
-                  "Unable to search hash table: hret=%d\n", hret);
-            iface = NULL;
-            goto done;
         }
 
         /* we will not free lookup path since it is freed with tmp_ctx
@@ -674,13 +634,11 @@ sbus_opath_hash_lookup_supported(TALLOC_CTX *mem_ctx,
 {
     TALLOC_CTX *tmp_ctx = NULL;
     TALLOC_CTX *list_ctx = NULL;
-    struct sbus_interface_list *copy = NULL;
-    struct sbus_interface_list *list = NULL;
+    struct sbus_interface_list *copy;
+    struct sbus_interface_list *output_list;
+    struct sbus_interface_list *table_list;
     char *lookup_path = NULL;
-    hash_key_t key;
-    hash_value_t value;
     errno_t ret;
-    int hret;
 
     tmp_ctx = talloc_new(NULL);
     if (tmp_ctx == NULL) {
@@ -699,23 +657,19 @@ sbus_opath_hash_lookup_supported(TALLOC_CTX *mem_ctx,
         goto done;
     }
 
-    while (lookup_path != NULL) {
-        key.type = HASH_KEY_STRING;
-        key.str = lookup_path;
+    /* Initialize output_list. */
+    output_list = NULL;
 
-        hret = hash_lookup(table, &key, &value);
-        if (hret == HASH_SUCCESS) {
-            ret = sbus_iface_list_copy(list_ctx, value.ptr, &copy);
+    while (lookup_path != NULL) {
+        table_list = sss_ptr_hash_lookup(table, lookup_path,
+                                         struct sbus_interface_list);
+        if (table_list != NULL) {
+            ret = sbus_iface_list_copy(list_ctx, table_list, &copy);
             if (ret != EOK) {
                 goto done;
             }
 
-            DLIST_CONCATENATE(list, copy, struct sbus_interface_list *);
-        } else if (hret != HASH_ERROR_KEY_NOT_FOUND) {
-            DEBUG(SSSDBG_OP_FAILURE,
-                  "Unable to search hash table: hret=%d\n", hret);
-            ret = EIO;
-            goto done;
+            DLIST_CONCATENATE(output_list, copy, struct sbus_interface_list *);
         }
 
         /* we will not free lookup path since it is freed with tmp_ctx
@@ -724,7 +678,7 @@ sbus_opath_hash_lookup_supported(TALLOC_CTX *mem_ctx,
     }
 
     talloc_steal(mem_ctx, list_ctx);
-    *_list = list;
+    *_list = output_list;
     ret = EOK;
 
 done:
diff --git a/src/sbus/sssd_dbus_private.h b/src/sbus/sssd_dbus_private.h
index 8abca66..c8913d0 100644
--- a/src/sbus/sssd_dbus_private.h
+++ b/src/sbus/sssd_dbus_private.h
@@ -121,10 +121,9 @@ struct sbus_interface_list {
     struct sbus_interface *interface;
 };
 
-errno_t
+hash_table_t *
 sbus_opath_hash_init(TALLOC_CTX *mem_ctx,
-                     struct sbus_connection *conn,
-                     hash_table_t **_table);
+                     struct sbus_connection *conn);
 
 struct sbus_interface *
 sbus_opath_hash_lookup_iface(hash_table_t *table,

From 1392c11c15340ca61e04778696c4007621addaa4 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 24 Jan 2017 13:47:42 +0100
Subject: [PATCH 3/4] sbus: use sss_ptr_hash for nodes table

---
 src/sbus/sssd_dbus_connection.c |  4 +--
 src/sbus/sssd_dbus_interface.c  | 72 +++++++----------------------------------
 src/sbus/sssd_dbus_private.h    |  6 ++--
 3 files changed, 16 insertions(+), 66 deletions(-)

diff --git a/src/sbus/sssd_dbus_connection.c b/src/sbus/sssd_dbus_connection.c
index 6ca039e..5e493fb 100644
--- a/src/sbus/sssd_dbus_connection.c
+++ b/src/sbus/sssd_dbus_connection.c
@@ -170,8 +170,8 @@ int sbus_init_connection(TALLOC_CTX *ctx,
         return EIO;
     }
 
-    ret = sbus_nodes_hash_init(conn, conn, &conn->nodes_fns);
-    if (ret != EOK) {
+    conn->nodes_fns = sbus_nodes_hash_init(conn);
+    if (conn->nodes_fns == NULL) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create node functions hash table\n");
         talloc_free(conn);
         return EIO;
diff --git a/src/sbus/sssd_dbus_interface.c b/src/sbus/sssd_dbus_interface.c
index e8c8851..1a11c6a 100644
--- a/src/sbus/sssd_dbus_interface.c
+++ b/src/sbus/sssd_dbus_interface.c
@@ -686,13 +686,10 @@ sbus_opath_hash_lookup_supported(TALLOC_CTX *mem_ctx,
     return ret;
 }
 
-errno_t
-sbus_nodes_hash_init(TALLOC_CTX *mem_ctx,
-                     struct sbus_connection *conn,
-                     hash_table_t **_table)
+hash_table_t *
+sbus_nodes_hash_init(TALLOC_CTX *mem_ctx)
 {
-    return sss_hash_create_ex(mem_ctx, 10, _table, 0, 0, 0, 0,
-                              NULL, conn);
+    return sss_ptr_hash_create(mem_ctx, NULL, NULL);
 }
 
 struct sbus_nodes_data {
@@ -706,57 +703,24 @@ sbus_nodes_hash_add(hash_table_t *table,
                     sbus_nodes_fn nodes_fn,
                     void *handler_data)
 {
-    TALLOC_CTX *tmp_ctx;
     struct sbus_nodes_data *data;
-    hash_key_t key;
-    hash_value_t value;
     errno_t ret;
-    bool has_key;
-    int hret;
-
-    tmp_ctx = talloc_new(NULL);
-    if (tmp_ctx == NULL) {
-        return ENOMEM;
-    }
 
-    key.type = HASH_KEY_STRING;
-    key.str = talloc_strdup(tmp_ctx, object_path);
-    if (key.str == NULL) {
-        return ENOMEM;
-    }
-
-    has_key = hash_has_key(table, &key);
-    if (has_key) {
-        ret = EEXIST;
-        goto done;
-    }
-
-    data = talloc_zero(tmp_ctx, struct sbus_nodes_data);
+    data = talloc_zero(table, struct sbus_nodes_data);
     if (data == NULL) {
-        ret = ENOMEM;
-        goto done;
+        return ENOMEM;
     }
 
     data->handler_data = handler_data;
     data->nodes_fn = nodes_fn;
 
-    value.type = HASH_VALUE_PTR;
-    value.ptr = data;
-
-    hret = hash_enter(table, &key, &value);
-    if (hret != HASH_SUCCESS) {
-        ret = EIO;
-        goto done;
+    ret = sss_ptr_hash_add(table, object_path, data, struct sbus_nodes_data);
+    if (ret != EOK) {
+        talloc_free(data);
+        return ret;
     }
 
-    talloc_steal(table, key.str);
-    talloc_steal(table, data);
-
-    ret = EOK;
-
-done:
-    talloc_free(tmp_ctx);
-    return ret;
+    return EOK;
 }
 
 const char **
@@ -765,24 +729,12 @@ sbus_nodes_hash_lookup(TALLOC_CTX *mem_ctx,
                        const char *object_path)
 {
     struct sbus_nodes_data *data;
-    hash_key_t key;
-    hash_value_t value;
-    int hret;
 
-    key.type = HASH_KEY_STRING;
-    key.str = discard_const(object_path);
-
-    hret = hash_lookup(table, &key, &value);
-    if (hret == HASH_ERROR_KEY_NOT_FOUND) {
-        return NULL;
-    } else if (hret != HASH_SUCCESS) {
-        DEBUG(SSSDBG_OP_FAILURE,
-              "Unable to search hash table: hret=%d\n", hret);
+    data = sss_ptr_hash_lookup(table, object_path, struct sbus_nodes_data);
+    if (data == NULL) {
         return NULL;
     }
 
-    data = talloc_get_type(value.ptr, struct sbus_nodes_data);
-
     return data->nodes_fn(mem_ctx, object_path, data->handler_data);
 }
 
diff --git a/src/sbus/sssd_dbus_private.h b/src/sbus/sssd_dbus_private.h
index c8913d0..a5a2d47 100644
--- a/src/sbus/sssd_dbus_private.h
+++ b/src/sbus/sssd_dbus_private.h
@@ -136,10 +136,8 @@ sbus_opath_hash_lookup_supported(TALLOC_CTX *mem_ctx,
                                  const char *object_path,
                                  struct sbus_interface_list **_list);
 
-errno_t
-sbus_nodes_hash_init(TALLOC_CTX *mem_ctx,
-                     struct sbus_connection *conn,
-                     hash_table_t **_table);
+hash_table_t *
+sbus_nodes_hash_init(TALLOC_CTX *mem_ctx);
 
 const char **
 sbus_nodes_hash_lookup(TALLOC_CTX *mem_ctx,

From f259f0a69abbfaf1a1e0d04b9a42ba9fbae2af34 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Tue, 24 Jan 2017 14:02:51 +0100
Subject: [PATCH 4/4] sbus: use sss_ptr_hash for signals table

---
 src/sbus/sssd_dbus_connection.c |  4 +--
 src/sbus/sssd_dbus_private.h    |  5 ++--
 src/sbus/sssd_dbus_signals.c    | 58 ++++++++++-------------------------------
 3 files changed, 18 insertions(+), 49 deletions(-)

diff --git a/src/sbus/sssd_dbus_connection.c b/src/sbus/sssd_dbus_connection.c
index 5e493fb..de134f2 100644
--- a/src/sbus/sssd_dbus_connection.c
+++ b/src/sbus/sssd_dbus_connection.c
@@ -177,8 +177,8 @@ int sbus_init_connection(TALLOC_CTX *ctx,
         return EIO;
     }
 
-    ret = sbus_incoming_signal_hash_init(conn, &conn->incoming_signals);
-    if (ret != EOK) {
+    conn->incoming_signals = sbus_incoming_signal_hash_init(conn);
+    if (conn->incoming_signals == NULL) {
         DEBUG(SSSDBG_CRIT_FAILURE, "Cannot create incoming singals "
               "hash table\n");
         talloc_free(conn);
diff --git a/src/sbus/sssd_dbus_private.h b/src/sbus/sssd_dbus_private.h
index a5a2d47..a3d4bae 100644
--- a/src/sbus/sssd_dbus_private.h
+++ b/src/sbus/sssd_dbus_private.h
@@ -180,9 +180,8 @@ sbus_signal_handler(DBusConnection *conn,
                     DBusMessage *message,
                     void *handler_data);
 
-errno_t
-sbus_incoming_signal_hash_init(TALLOC_CTX *mem_ctx,
-                               hash_table_t **_table);
+hash_table_t *
+sbus_incoming_signal_hash_init(TALLOC_CTX *mem_ctx);
 
 void sbus_register_common_signals(struct sbus_connection *conn, void *pvt);
 
diff --git a/src/sbus/sssd_dbus_signals.c b/src/sbus/sssd_dbus_signals.c
index 3f463e6..be1c852 100644
--- a/src/sbus/sssd_dbus_signals.c
+++ b/src/sbus/sssd_dbus_signals.c
@@ -23,6 +23,7 @@
 #include <dhash.h>
 
 #include "util/util.h"
+#include "util/sss_ptr_hash.h"
 #include "sbus/sssd_dbus.h"
 #include "sbus/sssd_dbus_private.h"
 
@@ -60,11 +61,10 @@ struct sbus_incoming_signal_data {
     void *handler_data;
 };
 
-errno_t
-sbus_incoming_signal_hash_init(TALLOC_CTX *mem_ctx,
-                               hash_table_t **_table)
+hash_table_t *
+sbus_incoming_signal_hash_init(TALLOC_CTX *mem_ctx)
 {
-    return sss_hash_create(mem_ctx, 10, _table);
+    return sss_ptr_hash_create(mem_ctx, NULL, NULL);
 }
 
 static errno_t
@@ -76,30 +76,20 @@ sbus_incoming_signal_hash_add(hash_table_t *table,
 {
     TALLOC_CTX *tmp_ctx;
     struct sbus_incoming_signal_data *data;
-    hash_key_t key;
-    hash_value_t value;
+    char *key;
     errno_t ret;
-    bool has_key;
-    int hret;
 
     tmp_ctx = talloc_new(NULL);
     if (tmp_ctx == NULL) {
         return ENOMEM;
     }
 
-    key.type = HASH_KEY_STRING;
-    key.str = talloc_asprintf(tmp_ctx, "%s.%s", iface, a_signal);
-    if (key.str == NULL) {
+    key = talloc_asprintf(tmp_ctx, "%s.%s", iface, a_signal);
+    if (key == NULL) {
         ret = ENOMEM;
         goto done;
     }
 
-    has_key = hash_has_key(table, &key);
-    if (has_key) {
-        ret = EEXIST;
-        goto done;
-    }
-
     data = talloc_zero(tmp_ctx, struct sbus_incoming_signal_data);
     if (data == NULL) {
         ret = ENOMEM;
@@ -109,16 +99,11 @@ sbus_incoming_signal_hash_add(hash_table_t *table,
     data->handler_data = handler_data;
     data->handler_fn = handler_fn;
 
-    value.type = HASH_VALUE_PTR;
-    value.ptr = data;
-
-    hret = hash_enter(table, &key, &value);
-    if (hret != HASH_SUCCESS) {
-        ret = EIO;
+    ret = sss_ptr_hash_add(table, key, data, struct sbus_incoming_signal_data);
+    if (ret != EOK) {
         goto done;
     }
 
-    talloc_steal(table, key.str);
     talloc_steal(table, data);
 
     ret = EOK;
@@ -134,31 +119,16 @@ sbus_incoming_signal_hash_lookup(hash_table_t *table,
                                  const char *a_signal)
 {
     struct sbus_incoming_signal_data *data;
-    hash_key_t key;
-    hash_value_t value;
-    int hret;
+    char *key;
 
-    key.type = HASH_KEY_STRING;
-    key.str = talloc_asprintf(NULL, "%s.%s", iface, a_signal);
-    if (key.str == NULL) {
+    key = talloc_asprintf(NULL, "%s.%s", iface, a_signal);
+    if (key == NULL) {
         return NULL;
     }
 
-    hret = hash_lookup(table, &key, &value);
-    if (hret == HASH_ERROR_KEY_NOT_FOUND) {
-        data = NULL;
-        goto done;
-    } else if (hret != HASH_SUCCESS) {
-        DEBUG(SSSDBG_OP_FAILURE,
-              "Unable to search hash table: hret=%d\n", hret);
-        data = NULL;
-        goto done;
-    }
-
-    data = talloc_get_type(value.ptr, struct sbus_incoming_signal_data);
+    data = sss_ptr_hash_lookup(table, key, struct sbus_incoming_signal_data);
+    talloc_free(key);
 
-done:
-    talloc_free(key.str);
     return data;
 }
 
