From 53c522d9deee8a8cfdf3d95673bae0ee31d63715 Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Mon, 2 May 2011 10:04:44 -0400
Subject: [PATCH 1/2] Create common sss_monitor_init()

This was implemented almost identically for both the responders
and the providers. It is easier to maintain as a single routine.

This patch also adds the ability to provide a private context to
attach to the sbus_connection for later use.
---
 src/monitor/monitor_interfaces.h        |    7 +++++
 src/monitor/monitor_sbus.c              |   42 +++++++++++++++++++++++++++++++
 src/providers/data_provider_be.c        |   38 ++-------------------------
 src/responder/common/responder_common.c |   37 ++-------------------------
 4 files changed, 55 insertions(+), 69 deletions(-)

diff --git a/src/monitor/monitor_interfaces.h b/src/monitor/monitor_interfaces.h
index 4830f678f3709bf3ab023a57cf251b5245ce86fc..8ec6d89bdaa25a7ce312aafc6de80a4bad584378 100644
--- a/src/monitor/monitor_interfaces.h
+++ b/src/monitor/monitor_interfaces.h
@@ -56,3 +56,10 @@ int monitor_common_res_init(DBusMessage *message,
 int monitor_common_rotate_logs(DBusMessage *message,
                                struct sbus_connection *conn);
 
+errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
+                         struct tevent_context *ev,
+                         struct sbus_interface *intf,
+                         const char *svc_name,
+                         uint16_t svc_version,
+                         void *pvt,
+                         struct sbus_connection **mon_conn);
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index c2106e8624c539d11917623d3f1a85149a902d9d..3d0d9d310210a423fda4b6841b42476e08e36fad 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -27,6 +27,7 @@
 #include "util/util.h"
 #include "confdb/confdb.h"
 #include "sbus/sssd_dbus.h"
+#include "sbus/sbus_client.h"
 #include "monitor/monitor_interfaces.h"
 
 int monitor_get_sbus_address(TALLOC_CTX *mem_ctx, char **address)
@@ -191,3 +192,44 @@ int monitor_common_rotate_logs(DBusMessage *message,
 
     return monitor_common_pong(message, conn);
 }
+
+errno_t sss_monitor_init(TALLOC_CTX *mem_ctx,
+                         struct tevent_context *ev,
+                         struct sbus_interface *intf,
+                         const char *svc_name,
+                         uint16_t svc_version,
+                         void *pvt,
+                         struct sbus_connection **mon_conn)
+{
+    errno_t ret;
+    char *sbus_address;
+    struct sbus_connection *conn;
+
+    /* Set up SBUS connection to the monitor */
+    ret = monitor_get_sbus_address(NULL, &sbus_address);
+    if (ret != EOK) {
+        DEBUG(0, ("Could not locate monitor address.\n"));
+        return ret;
+    }
+
+    ret = sbus_client_init(mem_ctx, ev, sbus_address,
+                           intf, &conn,
+                           NULL, pvt);
+    if (ret != EOK) {
+        DEBUG(0, ("Failed to connect to monitor services.\n"));
+        talloc_free(sbus_address);
+        return ret;
+    }
+    talloc_free(sbus_address);
+
+    /* Identify ourselves to the monitor */
+    ret = monitor_common_send_id(conn, svc_name, svc_version);
+    if (ret != EOK) {
+        DEBUG(0, ("Failed to identify to the monitor!\n"));
+        return ret;
+    }
+
+    *mon_conn = conn;
+
+    return EOK;
+}
diff --git a/src/providers/data_provider_be.c b/src/providers/data_provider_be.c
index 01d28558035f6c936dd541f8df34034e6710809f..0fbf809e50784c4601ff4d29d3db84713543a979 100644
--- a/src/providers/data_provider_be.c
+++ b/src/providers/data_provider_be.c
@@ -868,40 +868,6 @@ static int be_srv_init(struct be_ctx *ctx)
     return EOK;
 }
 
-/* mon_cli_init
- * sbus channel to the monitor daemon */
-static int mon_cli_init(struct be_ctx *ctx)
-{
-    char *sbus_address;
-    int ret;
-
-    /* Set up SBUS connection to the monitor */
-    ret = monitor_get_sbus_address(ctx, &sbus_address);
-    if (ret != EOK) {
-        DEBUG(0, ("Could not locate monitor address.\n"));
-        return ret;
-    }
-
-    ret = sbus_client_init(ctx, ctx->ev, sbus_address,
-                           &monitor_be_interface, &ctx->mon_conn,
-                           NULL, ctx);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to connect to monitor services.\n"));
-        return ret;
-    }
-
-    /* Identify ourselves to the monitor */
-    ret = monitor_common_send_id(ctx->mon_conn,
-                                 ctx->identity,
-                                 DATA_PROVIDER_VERSION);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to identify to the monitor!\n"));
-        return ret;
-    }
-
-    return EOK;
-}
-
 static void be_target_access_permit(struct be_req *be_req)
 {
     struct pam_data *pd = talloc_get_type(be_req->req_data, struct pam_data);
@@ -1138,7 +1104,9 @@ int be_process_init(TALLOC_CTX *mem_ctx,
         return ret;
     }
 
-    ret = mon_cli_init(ctx);
+    ret = sss_monitor_init(ctx, ctx->ev, &monitor_be_interface,
+                           ctx->identity, DATA_PROVIDER_VERSION,
+                           ctx, &ctx->mon_conn);
     if (ret != EOK) {
         DEBUG(0, ("fatal error setting up monitor bus\n"));
         return ret;
diff --git a/src/responder/common/responder_common.c b/src/responder/common/responder_common.c
index 2a4a5d20c171ff7c7a8be3dfafaef17844c96c08..4ddb549c16ab96717d0771a8d37b4fb0d83b66ce 100644
--- a/src/responder/common/responder_common.c
+++ b/src/responder/common/responder_common.c
@@ -362,39 +362,6 @@ static void accept_fd_handler(struct tevent_context *ev,
     return;
 }
 
-static int sss_monitor_init(struct resp_ctx *rctx,
-                            struct sbus_interface *intf,
-                            const char *svc_name,
-                            uint16_t svc_version)
-{
-    char *sbus_address;
-    int ret;
-
-    /* Set up SBUS connection to the monitor */
-    ret = monitor_get_sbus_address(rctx, &sbus_address);
-    if (ret != EOK) {
-        DEBUG(0, ("Could not locate monitor address.\n"));
-        return ret;
-    }
-
-    ret = sbus_client_init(rctx, rctx->ev, sbus_address,
-                           intf, &rctx->mon_conn,
-                           NULL, NULL);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to connect to monitor services.\n"));
-        return ret;
-    }
-
-    /* Identify ourselves to the monitor */
-    ret = monitor_common_send_id(rctx->mon_conn, svc_name, svc_version);
-    if (ret != EOK) {
-        DEBUG(0, ("Failed to identify to the monitor!\n"));
-        return ret;
-    }
-
-    return EOK;
-}
-
 static int sss_dp_init(struct resp_ctx *rctx,
                        struct sbus_interface *intf,
                        const char *cli_name,
@@ -620,7 +587,9 @@ int sss_process_init(TALLOC_CTX *mem_ctx,
         return ret;
     }
 
-    ret = sss_monitor_init(rctx, monitor_intf, svc_name, svc_version);
+    ret = sss_monitor_init(rctx, rctx->ev, monitor_intf,
+                           svc_name, svc_version, rctx,
+                           &rctx->mon_conn);
     if (ret != EOK) {
         DEBUG(0, ("fatal error setting up message bus\n"));
         return ret;
-- 
1.7.5

