>From 7e40266c2fd7044ff491c5d0574e69013eaceef2 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Pavel=20B=C5=99ezina?= <pbrezina@redhat.com>
Date: Wed, 10 Dec 2014 19:24:58 +0100
Subject: [PATCH 1/8] sbus: add new iface via sbus_conn_register_iface()

Rename sbus_conn_add_interface() to sbus_conn_register_iface()
and remove sbus_new_interface() calls since it is just one more
unnecessary call outside the sbus code.

The function sbus_new_interface() is made static and used
directly in sbus_conn_register_iface().

The name was chosen to better describe what the function is
doing. That it registers an interface on a given object path.
The same interface can be used with different paths so it is
not really about adding an interface.

Preparation for:
https://fedorahosted.org/sssd/ticket/2339
---
 src/monitor/monitor.c                   |  9 ++-------
 src/monitor/monitor_sbus.c              |  8 +-------
 src/providers/data_provider_be.c        | 10 +---------
 src/providers/proxy/proxy_child.c       |  8 +-------
 src/providers/proxy/proxy_init.c        | 11 ++---------
 src/responder/common/responder_common.c |  8 +-------
 src/responder/ifp/ifpsrv.c              | 15 +++------------
 src/sbus/sssd_dbus.h                    | 31 ++++++++++++++-----------------
 src/sbus/sssd_dbus_connection.c         | 19 ++++++++++++-------
 src/tests/sbus_codegen_tests.c          |  6 ++----
 src/tests/sbus_tests.c                  | 12 ++++--------
 11 files changed, 43 insertions(+), 94 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index afefe7f11c15ad343e8a79dceeff5a89e2787add..fef1a120fd487fd6586d45305f77f07d92905b4e 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -2443,7 +2443,6 @@ static void init_timeout(struct tevent_context *ev,
  */
 static int monitor_service_init(struct sbus_connection *conn, void *data)
 {
-    struct sbus_interface *intf;
     struct mt_ctx *ctx;
     struct mon_init_conn *mini;
     struct timeval tv;
@@ -2474,12 +2473,8 @@ static int monitor_service_init(struct sbus_connection *conn, void *data)
         return ENOMEM;
     }
 
-    intf = sbus_new_interface(conn, MON_SRV_PATH, &monitor_methods.vtable, mini);
-    if (!intf) {
-        return ENOMEM;
-    }
-
-    return sbus_conn_add_interface(conn, intf);
+    return sbus_conn_register_iface(conn, &monitor_methods.vtable,
+                                    MON_SRV_PATH, mini);
 }
 
 /* service_send_ping
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index 3e6fbff9bb079b403af30dc7f34a7bb66b908d1d..426f02997bf6ad9b3dc040426909cb70df2404c1 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -172,7 +172,6 @@ errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
 {
     errno_t ret;
     char *sbus_address;
-    struct sbus_interface *intf;
     struct sbus_connection *conn;
 
     /* Set up SBUS connection to the monitor */
@@ -190,12 +189,7 @@ errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
     }
     talloc_free(sbus_address);
 
-    intf = sbus_new_interface(mem_ctx, MONITOR_PATH, &mon_iface->vtable, pvt);
-    if (!intf) {
-        ret = ENOMEM;
-    } else {
-        ret = sbus_conn_add_interface(conn, intf);
-    }
+    ret = sbus_conn_register_iface(conn, &mon_iface->vtable, MONITOR_PATH, pvt);
     if (ret != EOK) {
         DEBUG(SSSDBG_FATAL_FAILURE, "Failed to export monitor client.\n");
         return ret;
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 95df2d9c645025a47176a9956e6069ec53dc2391..53a0be25877548783d48b1f8938c84540ec7a210 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -2216,7 +2216,6 @@ static void init_timeout(struct tevent_context *ev,
 
 static int be_client_init(struct sbus_connection *conn, void *data)
 {
-    struct sbus_interface *intf;
     struct be_ctx *bectx;
     struct be_client *becli;
     struct timeval tv;
@@ -2252,14 +2251,7 @@ static int be_client_init(struct sbus_connection *conn, void *data)
     DEBUG(SSSDBG_CONF_SETTINGS,
           "Set-up Backend ID timeout [%p]\n", becli->timeout);
 
-    /* Attach the client context to the connection context, so that it is
-     * always available when we need to manage the connection. */
-    intf = sbus_new_interface(conn, DP_PATH, &be_methods.vtable, becli);
-    if (!intf) {
-        return ENOMEM;
-    }
-
-    return sbus_conn_add_interface(conn, intf);
+    return sbus_conn_register_iface(conn, &be_methods.vtable, DP_PATH, becli);
 }
 
 /* be_srv_init
diff --git a/src/providers/proxy/proxy_child.c b/src/providers/proxy/proxy_child.c
index e261b2f588a89a27ed6fe78f7fc5cfa053b49833..9876b9328bd800a75d042db631a1b54f69a01f29 100644
--- a/src/providers/proxy/proxy_child.c
+++ b/src/providers/proxy/proxy_child.c
@@ -381,7 +381,6 @@ int proxy_child_send_id(struct sbus_connection *conn,
                         uint32_t id);
 static int proxy_cli_init(struct pc_ctx *ctx)
 {
-    struct sbus_interface *intf;
     char *sbus_address;
     int ret;
 
@@ -399,12 +398,7 @@ static int proxy_cli_init(struct pc_ctx *ctx)
         return ret;
     }
 
-    intf = sbus_new_interface(ctx, DP_PATH, &pc_methods.vtable, ctx);
-    if (!intf) {
-        ret = ENOMEM;
-    } else {
-        ret = sbus_conn_add_interface(ctx->conn, intf);
-    }
+    ret = sbus_conn_register_iface(ctx->conn, &pc_methods.vtable, DP_PATH, ctx);
     if (ret != EOK) {
         DEBUG(SSSDBG_FATAL_FAILURE, "Failed to export proxy.\n");
         return ret;
diff --git a/src/providers/proxy/proxy_init.c b/src/providers/proxy/proxy_init.c
index 1e734511766f2c0f58cffc7d726a26ecfd6c9a27..f76ad02ae66248ecc5b499e29906bcc537684705 100644
--- a/src/providers/proxy/proxy_init.c
+++ b/src/providers/proxy/proxy_init.c
@@ -331,7 +331,6 @@ static int proxy_client_init(struct sbus_connection *conn, void *data)
 {
     struct proxy_auth_ctx *proxy_auth_ctx;
     struct proxy_client *proxy_cli;
-    struct sbus_interface *intf;
     struct timeval tv;
 
     proxy_auth_ctx = talloc_get_type(data, struct proxy_auth_ctx);
@@ -362,14 +361,8 @@ static int proxy_client_init(struct sbus_connection *conn, void *data)
     DEBUG(SSSDBG_CONF_SETTINGS,
           "Set-up proxy client ID timeout [%p]\n", proxy_cli->timeout);
 
-    /* Attach the client context to the connection context, so that it is
-     * always available when we need to manage the connection. */
-    intf = sbus_new_interface(conn, DP_PATH, &proxy_methods.vtable, proxy_cli);
-    if (!intf) {
-        return ENOMEM;
-    }
-
-    return sbus_conn_add_interface(conn, intf);
+    return sbus_conn_register_iface(conn, &proxy_methods.vtable,
+                                    DP_PATH, proxy_cli);
 }
 
 static void init_timeout(struct tevent_context *ev,
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
index 666abe6101c308d8f05ecea299af9146384042b4..36e7f15948632e9c637886dee259b494e46ceecb 100644
--- a/src/responder/common/responder_common.c
+++ b/src/responder/common/responder_common.c
@@ -514,7 +514,6 @@ static int sss_dp_init(struct resp_ctx *rctx,
                        const char *cli_name,
                        struct sss_domain_info *domain)
 {
-    struct sbus_interface *intf;
     struct be_conn *be_conn;
     int ret;
 
@@ -539,12 +538,7 @@ static int sss_dp_init(struct resp_ctx *rctx,
         return ret;
     }
 
-    intf = sbus_new_interface(rctx, DP_PATH, dp_intf, rctx);
-    if (!intf) {
-        ret = ENOMEM;
-    } else {
-        ret = sbus_conn_add_interface(be_conn->conn, intf);
-    }
+    ret = sbus_conn_register_iface(be_conn->conn, dp_intf, DP_PATH, rctx);
     if (ret != EOK) {
         DEBUG(SSSDBG_FATAL_FAILURE, "Failed to export data provider.\n");
         return ret;
diff --git a/src/responder/ifp/ifpsrv.c b/src/responder/ifp/ifpsrv.c
index 0c333bc07b4381dbf086aabc815d4f5e3e572351..5b7f26ca1135f9ee47e45d50e44516bf58cdcccd 100644
--- a/src/responder/ifp/ifpsrv.c
+++ b/src/responder/ifp/ifpsrv.c
@@ -178,7 +178,6 @@ sysbus_init(TALLOC_CTX *mem_ctx,
     DBusError dbus_error;
     DBusConnection *conn = NULL;
     struct sysbus_ctx *system_bus = NULL;
-    struct sbus_interface *sif;
     int i;
     errno_t ret;
 
@@ -226,17 +225,9 @@ sysbus_init(TALLOC_CTX *mem_ctx,
     }
 
     for (i = 0; sysbus_ifaces[i].path != NULL; i++) {
-        sif = sbus_new_interface(system_bus->conn,
-                                 sysbus_ifaces[i].path,
-                                 sysbus_ifaces[i].iface_vtable,
-                                 pvt);
-        if (sif == NULL) {
-            DEBUG(SSSDBG_CRIT_FAILURE,
-                  "Could not add the sbus interface\n");
-            goto fail;
-        }
-
-        ret = sbus_conn_add_interface(system_bus->conn, sif);
+        ret = sbus_conn_register_iface(system_bus->conn,
+                                       sysbus_ifaces[i].iface_vtable,
+                                       sysbus_ifaces[i].path, pvt);
         if (ret != EOK) {
             DEBUG(SSSDBG_CRIT_FAILURE,
                   "Could not add the interface\n");
diff --git a/src/sbus/sssd_dbus.h b/src/sbus/sssd_dbus.h
index 5b128eaedb320cb745c1b635867e1b53ca556ec9..7552fbcf1a4fd630970eec378f2f725c6d00eaa4 100644
--- a/src/sbus/sssd_dbus.h
+++ b/src/sbus/sssd_dbus.h
@@ -113,21 +113,6 @@ struct sbus_interface {
     void *instance_data;
 };
 
-/*
- * Creates a new struct sbus_interface instance to be exported by a DBus
- * service.
- *
- * Pass the result to sbus_conn_add_interface(). The interface
- * will be exported at @object_path. The method handlers are represented by
- * @iface_vtable. @instance_data contains additional caller specific data
- * which is made available to handlers.
- */
-struct sbus_interface *
-sbus_new_interface(TALLOC_CTX *mem_ctx,
-                   const char *object_path,
-                   struct sbus_vtable *iface_vtable,
-                   void *instance_data);
-
 /* Server Functions */
 int sbus_new_server(TALLOC_CTX *mem_ctx,
                     struct tevent_context *ev,
@@ -170,9 +155,21 @@ int sbus_init_connection(TALLOC_CTX *ctx,
                          struct sbus_connection **_conn);
 
 DBusConnection *sbus_get_connection(struct sbus_connection *conn);
+
 void sbus_disconnect(struct sbus_connection *conn);
-int sbus_conn_add_interface(struct sbus_connection *conn,
-                            struct sbus_interface *intf);
+
+/*
+ * Register a new interface to be available at given object path.
+ *
+ * The interface will be exported at @object_path. The method handlers are
+ * represented by @iface_vtable. @pvt contains additional caller specific data
+ * which is made available to handlers.
+ */
+int sbus_conn_register_iface(struct sbus_connection *conn,
+                             struct sbus_vtable *iface_vtable,
+                             const char *object_path,
+                             void *pvt);
+
 bool sbus_conn_disconnecting(struct sbus_connection *conn);
 
 /* max_retries < 0: retry forever
diff --git a/src/sbus/sssd_dbus_connection.c b/src/sbus/sssd_dbus_connection.c
index 6102ef9ae4715d36a623b802b9095ec1c99c1a39..7fded56bc10f0f83c50fc7f0b1fe94ad35b9a512 100644
--- a/src/sbus/sssd_dbus_connection.c
+++ b/src/sbus/sssd_dbus_connection.c
@@ -565,7 +565,7 @@ fail:
     sbus_request_finish(dbus_req, reply);
 }
 
-struct sbus_interface *
+static struct sbus_interface *
 sbus_new_interface(TALLOC_CTX *mem_ctx,
                    const char *object_path,
                    struct sbus_vtable *iface_vtable,
@@ -606,18 +606,23 @@ static char *sbus_iface_get_reg_path(TALLOC_CTX *mem_ctx,
     return reg_path;
 }
 
-/* Adds a new D-BUS path message handler to the connection
- * Note: this must be a unique path.
- */
-int sbus_conn_add_interface(struct sbus_connection *conn,
-                            struct sbus_interface *intf)
+int sbus_conn_register_iface(struct sbus_connection *conn,
+                             struct sbus_vtable *iface_vtable,
+                             const char *object_path,
+                             void *pvt)
 {
     struct sbus_interface_p *intf_p;
+    struct sbus_interface *intf;
     dbus_bool_t dbret;
     const char *path;
     bool fallback;
 
-    if (!conn || !intf || !intf->vtable || !intf->vtable->meta) {
+    intf = sbus_new_interface(conn, object_path, iface_vtable, pvt);
+    if (intf == NULL) {
+        return ENOMEM;
+    }
+
+    if (!conn || !intf->vtable || !intf->vtable->meta) {
         return EINVAL;
     }
 
diff --git a/src/tests/sbus_codegen_tests.c b/src/tests/sbus_codegen_tests.c
index d3d5d46809d242bef97ba655309eb4e6055e3f48..dccb5e3147e61c3d6f40137927f8724804a37b97 100644
--- a/src/tests/sbus_codegen_tests.c
+++ b/src/tests/sbus_codegen_tests.c
@@ -574,10 +574,8 @@ static int pilot_test_server_init(struct sbus_connection *server, void *unused)
 {
     int ret;
 
-    ret = sbus_conn_add_interface(server,
-                                  sbus_new_interface(server, "/test/leela",
-                                                     &pilot_iface.vtable,
-                                                     "Crash into the billboard"));
+    ret = sbus_conn_register_iface(server, &pilot_iface.vtable, "/test/leela",
+                                   "Crash into the billboard");
     ck_assert_int_eq(ret, EOK);
 
     return EOK;
diff --git a/src/tests/sbus_tests.c b/src/tests/sbus_tests.c
index 50c452e42ff0085b554be8991a7b34d1405c3b3c..486e39221c619414c315399b1257d417bea44d77 100644
--- a/src/tests/sbus_tests.c
+++ b/src/tests/sbus_tests.c
@@ -178,17 +178,13 @@ static int pilot_test_server_init(struct sbus_connection *server, void *unused)
 {
     int ret;
 
-    ret = sbus_conn_add_interface(server,
-                                  sbus_new_interface(server, "/test/leela",
-                                                     &pilot_impl.vtable,
-                                                     "Crash into the billboard"));
+    ret = sbus_conn_register_iface(server, &pilot_impl.vtable, "/test/leela",
+                                   "Crash into the billboard");
     ck_assert_int_eq(ret, EOK);
 
 
-    ret = sbus_conn_add_interface(server,
-                                  sbus_new_interface(server, "/test/fry",
-                                                     &pilot_impl.vtable,
-                                                     "Don't crash"));
+    ret = sbus_conn_register_iface(server, &pilot_impl.vtable, "/test/fry",
+                                   "Don't crash");
     ck_assert_int_eq(ret, EOK);
 
     return EOK;
-- 
1.9.3

