From 6dccaaead3df8be00649404610c56e52bb37100a Mon Sep 17 00:00:00 2001
From: Stephen Gallagher <sgallagh@redhat.com>
Date: Wed, 10 Mar 2010 15:27:59 -0500
Subject: [PATCH] Automatically retry SBUS requests within the timeout period

This patch adds a new helper function, sbus_conn_send() that will
handle automatically resending a request if the connection is
closed at the time of the initial send.
---
 src/monitor/monitor.c               |   74 ++++++---------
 src/monitor/monitor_sbus.c          |   33 +++-----
 src/providers/dp_auth_util.c        |   33 +++-----
 src/responder/common/responder_dp.c |   16 ++++
 src/responder/pam/pamsrv_dp.c       |   32 ++-----
 src/sbus/sssd_dbus.h                |   24 +++++
 src/sbus/sssd_dbus_connection.c     |  171 +++++++++++++++++++++++++++++++++++
 7 files changed, 272 insertions(+), 111 deletions(-)

diff --git a/src/monitor/monitor.c b/src/monitor/monitor.c
index 56a74fb..f97682b 100644
--- a/src/monitor/monitor.c
+++ b/src/monitor/monitor.c
@@ -642,6 +642,15 @@ static void reload_reply(DBusPendingCall *pending, void *data)
     DBusMessage *reply;
     struct mt_svc *svc = talloc_get_type(data, struct mt_svc);
 
+    if (!pending) {
+        /* We were unable to send the request over the SBUS
+         * during the timeout period. This is an internal
+         * timeout, not a D-BUS timeout.
+         */
+
+        return;
+    }
+
     reply = dbus_pending_call_steal_reply(pending);
     if (!reply) {
         /* reply should never be null. This function shouldn't be called
@@ -650,16 +659,16 @@ static void reload_reply(DBusPendingCall *pending, void *data)
          */
         DEBUG(0, ("A reply callback was called but no reply was received"
                   " and no timeout occurred\n"));
-
         /* Destroy this connection */
         sbus_disconnect(svc->conn);
-        goto done;
+        dbus_pending_call_unref(pending);
+        return;
     }
 
     /* TODO: Handle cases where the call has timed out or returned
      * with an error.
      */
-done:
+
     dbus_pending_call_unref(pending);
     dbus_message_unref(reply);
 }
@@ -687,9 +696,6 @@ static int monitor_update_resolv(struct config_file_ctx *file_ctx,
 static int service_signal(struct mt_svc *svc, const char *svc_signal)
 {
     DBusMessage *msg;
-    dbus_bool_t dbret;
-    DBusConnection *dbus_conn;
-    DBusPendingCall *pending_reply;
 
     if (svc->provider && strcasecmp(svc->provider, "local") == 0) {
         /* The local provider requires no signaling */
@@ -705,7 +711,6 @@ static int service_signal(struct mt_svc *svc, const char *svc_signal)
         return EIO;
     }
 
-    dbus_conn = sbus_get_connection(svc->conn);
     msg = dbus_message_new_method_call(NULL,
                                        MONITOR_PATH,
                                        MONITOR_INTERFACE,
@@ -717,21 +722,9 @@ static int service_signal(struct mt_svc *svc, const char *svc_signal)
         return ENOMEM;
     }
 
-    dbret = dbus_connection_send_with_reply(dbus_conn, msg, &pending_reply,
-                                            svc->mt_ctx->service_id_timeout);
-    if (!dbret || pending_reply == NULL) {
-        DEBUG(0, ("D-BUS send failed.\n"));
-        dbus_message_unref(msg);
-        monitor_kill_service(svc);
-        talloc_free(svc);
-        return EIO;
-    }
-
-    /* Set up the reply handler */
-    dbus_pending_call_set_notify(pending_reply, reload_reply, svc, NULL);
-    dbus_message_unref(msg);
-
-    return EOK;
+    return sbus_conn_send(svc->conn, msg, reload_reply,
+                          svc->mt_ctx->service_id_timeout,
+                          svc, NULL);
 }
 
 static int service_signal_dns_reload(struct mt_svc *svc)
@@ -1859,9 +1852,6 @@ static int monitor_service_init(struct sbus_connection *conn, void *data)
 static int service_send_ping(struct mt_svc *svc)
 {
     DBusMessage *msg;
-    DBusPendingCall *pending_reply;
-    DBusConnection *dbus_conn;
-    dbus_bool_t dbret;
 
     if (!svc->conn) {
         DEBUG(8, ("Service not yet initialized\n"));
@@ -1870,8 +1860,6 @@ static int service_send_ping(struct mt_svc *svc)
 
     DEBUG(4,("Pinging %s\n", svc->name));
 
-    dbus_conn = sbus_get_connection(svc->conn);
-
     /*
      * Set up identity request
      * This should be a well-known path and method
@@ -1887,24 +1875,9 @@ static int service_send_ping(struct mt_svc *svc)
         return ENOMEM;
     }
 
-    dbret = dbus_connection_send_with_reply(dbus_conn, msg, &pending_reply,
-                                            svc->mt_ctx->service_id_timeout);
-    if (!dbret || pending_reply == NULL) {
-        /*
-         * Critical Failure
-         * We can't communicate on this connection
-         * We'll drop it using the default destructor.
-         */
-        DEBUG(0, ("D-BUS send failed.\n"));
-        talloc_zfree(svc->conn);
-        return EIO;
-    }
-
-    /* Set up the reply handler */
-    dbus_pending_call_set_notify(pending_reply, ping_check, svc, NULL);
-    dbus_message_unref(msg);
-
-    return EOK;
+    return sbus_conn_send(svc->conn, msg, ping_check,
+                   svc->mt_ctx->service_id_timeout,
+                   svc, NULL);
 }
 
 static void ping_check(DBusPendingCall *pending, void *data)
@@ -1916,6 +1889,17 @@ static void ping_check(DBusPendingCall *pending, void *data)
 
     svc = talloc_get_type(data, struct mt_svc);
 
+    if (!pending) {
+        /* We were unable to send the request over the SBUS
+         * during the timeout period. This is an internal
+         * timeout, not a D-BUS timeout.
+         */
+
+        DEBUG(0, ("Timeout occurred sending ping. Terminating connection\n"));
+        sbus_disconnect(svc->conn);
+        return;
+    }
+
     reply = dbus_pending_call_steal_reply(pending);
     if (!reply) {
         /* reply should never be null. This function shouldn't be called
diff --git a/src/monitor/monitor_sbus.c b/src/monitor/monitor_sbus.c
index d60a087..9d3435f 100644
--- a/src/monitor/monitor_sbus.c
+++ b/src/monitor/monitor_sbus.c
@@ -54,6 +54,15 @@ static void id_callback(DBusPendingCall *pending, void *ptr)
 
     dbus_error_init(&dbus_error);
 
+    if (!pending) {
+        /* We were unable to send the request over the SBUS
+         * during the timeout period. This is an internal
+         * timeout, not a D-BUS timeout.
+         */
+
+        return;
+    }
+
     reply = dbus_pending_call_steal_reply(pending);
     if (!reply) {
         /* reply should never be null. This function shouldn't be called
@@ -109,13 +118,9 @@ done:
 int monitor_common_send_id(struct sbus_connection *conn,
                            const char *name, uint16_t version)
 {
-    DBusPendingCall *pending_reply;
-    DBusConnection *dbus_conn;
     DBusMessage *msg;
     dbus_bool_t ret;
 
-    dbus_conn = sbus_get_connection(conn);
-
     /* create the message */
     msg = dbus_message_new_method_call(NULL,
                                        MON_SRV_PATH,
@@ -137,24 +142,8 @@ int monitor_common_send_id(struct sbus_connection *conn,
         return EIO;
     }
 
-    ret = dbus_connection_send_with_reply(dbus_conn, msg, &pending_reply,
-                                          30000 /* TODO: set timeout */);
-    if (!ret || !pending_reply) {
-        /*
-         * Critical Failure
-         * We can't communicate on this connection
-         * We'll drop it using the default destructor.
-         */
-        DEBUG(0, ("D-BUS send failed.\n"));
-        dbus_message_unref(msg);
-        return EIO;
-    }
-
-    /* Set up the reply handler */
-    dbus_pending_call_set_notify(pending_reply, id_callback, NULL, NULL);
-    dbus_message_unref(msg);
-
-    return EOK;
+    return sbus_conn_send(conn, msg, id_callback,
+                          3000, NULL, NULL);
 }
 
 int monitor_common_pong(DBusMessage *message,
diff --git a/src/providers/dp_auth_util.c b/src/providers/dp_auth_util.c
index 5291176..1da51c0 100644
--- a/src/providers/dp_auth_util.c
+++ b/src/providers/dp_auth_util.c
@@ -281,6 +281,15 @@ static void id_callback(DBusPendingCall *pending, void *ptr)
 
     dbus_error_init(&dbus_error);
 
+    if (!pending) {
+        /* We were unable to send the request over the SBUS
+         * during the timeout period. This is an internal
+         * timeout, not a D-BUS timeout.
+         */
+
+        return;
+    }
+
     reply = dbus_pending_call_steal_reply(pending);
     if (!reply) {
         /* reply should never be null. This function shouldn't be called
@@ -336,13 +345,9 @@ done:
 int dp_common_send_id(struct sbus_connection *conn, uint16_t version,
                       const char *name)
 {
-    DBusPendingCall *pending_reply;
-    DBusConnection *dbus_conn;
     DBusMessage *msg;
     dbus_bool_t ret;
 
-    dbus_conn = sbus_get_connection(conn);
-
     /* create the message */
     msg = dbus_message_new_method_call(NULL,
                                        DP_PATH,
@@ -365,23 +370,7 @@ int dp_common_send_id(struct sbus_connection *conn, uint16_t version,
         return EIO;
     }
 
-    ret = dbus_connection_send_with_reply(dbus_conn, msg, &pending_reply,
-                                          30000 /* TODO: set timeout */);
-    if (!ret || !pending_reply) {
-        /*
-         * Critical Failure
-         * We can't communicate on this connection
-         * We'll drop it using the default destructor.
-         */
-        DEBUG(0, ("D-BUS send failed.\n"));
-        dbus_message_unref(msg);
-        return EIO;
-    }
-
-    /* Set up the reply handler */
-    dbus_pending_call_set_notify(pending_reply, id_callback, NULL, NULL);
-    dbus_message_unref(msg);
-
-    return EOK;
+    return sbus_conn_send(conn, msg, id_callback, 30000,
+                          NULL, NULL);
 }
 
diff --git a/src/responder/common/responder_dp.c b/src/responder/common/responder_dp.c
index d5a4ca7..c5a555f 100644
--- a/src/responder/common/responder_dp.c
+++ b/src/responder/common/responder_dp.c
@@ -457,6 +457,11 @@ static int sss_dp_send_acct_req_create(struct resp_ctx *rctx,
         return EIO;
     }
 
+    /* Don't use sbus_conn_send here, because we need
+     * to guarantee a pending_reply pointer in order
+     * to cancel pending requests if the destructors
+     * are called.
+     */
     dbret = dbus_connection_send_with_reply(dbus_conn, msg,
                                             &pending_reply, timeout);
     if (!dbret || pending_reply == NULL) {
@@ -525,6 +530,17 @@ static int sss_dp_get_reply(DBusPendingCall *pending,
 
     dbus_error_init(&dbus_error);
 
+    if (!pending) {
+        /* We were unable to send the request over the SBUS
+         * during the timeout period. This is an internal
+         * timeout, not a D-BUS timeout.
+         */
+
+        DEBUG(0, ("Timeout occurred sending DP request\n"));
+        err = EIO;
+        goto done;
+    }
+
     reply = dbus_pending_call_steal_reply(pending);
     if (!reply) {
         /* reply should never be null. This function shouldn't be called
diff --git a/src/responder/pam/pamsrv_dp.c b/src/responder/pam/pamsrv_dp.c
index 071d09b..fd3f97f 100644
--- a/src/responder/pam/pamsrv_dp.c
+++ b/src/responder/pam/pamsrv_dp.c
@@ -43,10 +43,16 @@ static void pam_dp_process_reply(DBusPendingCall *pending, void *ptr)
 
     dbus_error_init(&dbus_error);
 
-    dbus_pending_call_block(pending);
+    if (!pending) {
+        DEBUG(0, ("PAM request timed out \n"));
+        preq->pd->pam_status = PAM_SYSTEM_ERR;
+        goto done;
+    }
+
     msg = dbus_pending_call_steal_reply(pending);
     if (msg == NULL) {
-        DEBUG(0, ("Severe error. A reply callback was called but no reply was received and no timeout occurred\n"));
+        DEBUG(0, ("Severe error. A reply callback was called but no reply was"
+                  "received and no timeout occurred\n"));
         preq->pd->pam_status = PAM_SYSTEM_ERR;
         goto done;
     }
@@ -84,8 +90,6 @@ int pam_dp_send_req(struct pam_auth_req *preq, int timeout)
     struct pam_data *pd = preq->pd;
     struct be_conn *be_conn;
     DBusMessage *msg;
-    DBusPendingCall *pending_reply;
-    DBusConnection *dbus_conn;
     dbus_bool_t ret;
     int res;
 
@@ -100,7 +104,6 @@ int pam_dp_send_req(struct pam_auth_req *preq, int timeout)
                   " This maybe a bug, it shouldn't happen!\n", preq->domain));
         return EIO;
     }
-    dbus_conn = sbus_get_connection(be_conn->conn);
 
     msg = dbus_message_new_method_call(NULL,
                                        DP_PATH,
@@ -121,22 +124,7 @@ int pam_dp_send_req(struct pam_auth_req *preq, int timeout)
         return EIO;
     }
 
-    ret = dbus_connection_send_with_reply(dbus_conn, msg, &pending_reply, timeout);
-    if (!ret || pending_reply == NULL) {
-        /*
-         * Critical Failure
-         * We can't communicate on this connection
-         * We'll drop it using the default destructor.
-         */
-        DEBUG(0, ("D-BUS send failed.\n"));
-        dbus_message_unref(msg);
-        return EIO;
-    }
-
-    dbus_pending_call_set_notify(pending_reply,
-                                 pam_dp_process_reply, preq, NULL);
-    dbus_message_unref(msg);
-
-    return EOK;
+    return sbus_conn_send(be_conn->conn, msg, pam_dp_process_reply,
+                          timeout, preq, NULL);
 }
 
diff --git a/src/sbus/sssd_dbus.h b/src/sbus/sssd_dbus.h
index ac02c44..080cdec 100644
--- a/src/sbus/sssd_dbus.h
+++ b/src/sbus/sssd_dbus.h
@@ -144,6 +144,30 @@ DBusHandlerResult sbus_message_handler(DBusConnection *conn,
                                   DBusMessage *message,
                                   void *user_data);
 
+/*
+ * Send a message across the SBUS
+ *
+ * If the connection is disconnected when attempting to
+ * send, sbus_conn_send will attempt to resend the message
+ * every 50 milliseconds until the timeout passes.
+ *
+ * This is done because the sbus reconnection service may
+ * bring the connection back to life.
+ *
+ * If retry_event is not NULL, sbus_conn_send() will return
+ * a pointer to the retry event, if one is created.
+ *
+ * If waiting for a retry is unacceptable for a client, they
+ * can free this event to handle retries (if desired) on
+ * their own.
+ */
+int sbus_conn_send(struct sbus_connection *conn,
+                   DBusMessage *msg,
+                   DBusPendingCallNotifyFunction reply_handler,
+                   int timeout_ms,
+                   void *pvt,
+                   struct tevent_timer **retry_event);
+
 void sbus_conn_send_reply(struct sbus_connection *conn,
                           DBusMessage *reply);
 
diff --git a/src/sbus/sssd_dbus_connection.c b/src/sbus/sssd_dbus_connection.c
index 38ccc6a..17185ec 100644
--- a/src/sbus/sssd_dbus_connection.c
+++ b/src/sbus/sssd_dbus_connection.c
@@ -685,6 +685,177 @@ bool sbus_conn_disconnecting(struct sbus_connection *conn)
     return false;
 }
 
+struct sbus_conn_send_ctx {
+    struct sbus_connection *conn;
+    DBusMessage *msg;
+    DBusPendingCallNotifyFunction reply_handler;
+    int timeout_ms;
+    void *pvt;
+};
+
+static void sbus_conn_queue_send_done(struct tevent_context *ev,
+                                      struct tevent_timer *te,
+                                      struct timeval current_time,
+                                      void *pvt);
+static void sbus_conn_queue_timeout_done(struct tevent_context *ev,
+                                         struct tevent_timer *te,
+                                         struct timeval current_time,
+                                         void *pvt);
+
+/*
+ * Send a message across the SBUS
+ *
+ * If the connection is disconnected when attempting to
+ * send, sbus_conn_send will attempt to resend the message
+ * every 50 milliseconds until the timeout passes.
+ *
+ * This is done because the sbus reconnection service may
+ * bring the connection back to life.
+ *
+ * If retry_event is not NULL, sbus_conn_send() will return
+ * a pointer to the retry event, if one is created.
+ *
+ * If waiting for a retry is unacceptable for a client, they
+ * can free this event to handle retries (if desired) on
+ * their own.
+ */
+int sbus_conn_send(struct sbus_connection *conn,
+                   DBusMessage *msg,
+                   DBusPendingCallNotifyFunction reply_handler,
+                   int timeout_ms,
+                   void *pvt,
+                   struct tevent_timer **retry_event)
+{
+    DBusPendingCall *pending_reply;
+    DBusConnection *dbus_conn;
+    dbus_bool_t dbret;
+    struct sbus_conn_send_ctx *send_ctx = NULL;
+    struct tevent_timer *send_event;
+    struct timeval tv;
+    struct tevent_timer *timeout_event;
+
+    dbus_conn = sbus_get_connection(conn);
+
+    dbret = dbus_connection_send_with_reply(dbus_conn, msg,
+                                            &pending_reply,
+                                            timeout_ms);
+    if (!dbret) {
+        /*
+         * Critical Failure
+         * Insufficient memory to send message
+         */
+        DEBUG(0, ("D-BUS send failed.\n"));
+        return ENOMEM;
+    }
+
+    if (pending_reply) {
+        /* Set up the reply handler */
+        dbret = dbus_pending_call_set_notify(pending_reply, reply_handler,
+                                             pvt, NULL);
+        if (!dbret) {
+            /*
+             * Critical Failure
+             * Insufficient memory to create pending call notify
+             */
+            DEBUG(0, ("D-BUS send failed.\n"));
+            dbus_pending_call_cancel(pending_reply);
+            dbus_pending_call_unref(pending_reply);
+            return ENOMEM;
+        }
+
+        return EOK;
+    }
+
+    /* If pending_reply is NULL, the connection was not
+     * open for sending. Queue the message and try resending
+     * until the timeout passes.
+     */
+    else {
+        send_ctx = talloc_zero(conn, struct sbus_conn_send_ctx);
+        if (!send_ctx) {
+            return ENOMEM;
+        }
+
+        send_ctx->conn = conn;
+        send_ctx->msg = msg;
+        send_ctx->reply_handler = reply_handler;
+        send_ctx->timeout_ms = timeout_ms;
+        send_ctx->pvt = pvt;
+
+        /* Retry sending every 50ms until it goes through
+         * or fails completely
+         */
+        send_ctx->timeout_ms -= 50;
+
+        if (send_ctx->timeout_ms <= 0) {
+            tv = tevent_timeval_current();
+            timeout_event = tevent_add_timer(send_ctx->conn->ev, send_ctx,
+                                             tv, sbus_conn_queue_timeout_done,
+                                             send_ctx);
+            if (!timeout_event) {
+                talloc_zfree(send_ctx);
+                return ENOMEM;
+            }
+            return ETIMEDOUT;
+        }
+
+        tv = tevent_timeval_current_ofs(0, 50000);
+        send_event = tevent_add_timer(send_ctx->conn->ev, send_ctx,
+                                      tv, sbus_conn_queue_send_done,
+                                      send_ctx);
+        if (!send_event) {
+            talloc_zfree(send_ctx);
+            return EIO;
+        }
+
+        if (retry_event) {
+            *retry_event = send_event;
+        }
+
+        return EOK;
+    }
+}
+
+static void sbus_conn_queue_send_done(struct tevent_context *ev,
+                                      struct tevent_timer *te,
+                                      struct timeval current_time,
+                                      void *pvt)
+{
+    struct sbus_conn_send_ctx *send_ctx;
+
+    send_ctx = talloc_get_type(pvt, struct sbus_conn_send_ctx);
+    if(!send_ctx) {
+        /* Can't do anything without a context. */
+        return;
+    }
+
+    sbus_conn_send(send_ctx->conn,
+                   send_ctx->msg,
+                   send_ctx->reply_handler,
+                   send_ctx->timeout_ms,
+                   send_ctx->pvt,
+                   NULL);
+
+    talloc_zfree(send_ctx);
+}
+
+static void sbus_conn_queue_timeout_done(struct tevent_context *ev,
+                                         struct tevent_timer *te,
+                                         struct timeval current_time,
+                                         void *pvt)
+{
+    struct sbus_conn_send_ctx *send_ctx;
+
+    send_ctx = talloc_get_type(pvt, struct sbus_conn_send_ctx);
+    if(!send_ctx) {
+        /* Can't do anything without a context. */
+        return;
+    }
+
+    send_ctx->reply_handler(NULL, send_ctx->pvt);
+}
+
+
 void sbus_conn_send_reply(struct sbus_connection *conn, DBusMessage *reply)
 {
     dbus_connection_send(conn->dbus.conn, reply, NULL);
-- 
1.6.6.1

