gkdbus reply timeout changes 06/84106/3
authorKonrad Lipinski <konrad.l@samsung.com>
Tue, 16 Aug 2016 15:35:33 +0000 (17:35 +0200)
committerKonrad Lipinski <konrad.l@samsung.com>
Wed, 17 Aug 2016 11:12:43 +0000 (13:12 +0200)
- drop userspace timeouts on unix/kdbus (rely on kdbus instead)
- propagate user-provided timeouts to kdbus
- translate infinite timeouts (G_MAXINT) into 0x7fffffffffffffff ns
  (292 years)

Change-Id: Id769802925522cf82c26354dae9d8ee29fcb972d

gio/gdbusconnection.c
gio/gdbusconnection.h
gio/gkdbus.c

index 67edf97..72a4407 100755 (executable)
@@ -2302,7 +2302,8 @@ g_dbus_connection_send_message_unlocked (GDBusConnection   *connection,
                                          GDBusMessage      *message,
                                          GDBusSendMessageFlags flags,
                                          volatile guint32  *out_serial,
-                                         GError           **error)
+                                         GError           **error,
+                                         gint timeout_msec)
 {
   guchar *blob;
   gsize blob_size;
@@ -2399,7 +2400,7 @@ g_dbus_connection_send_message_unlocked (GDBusConnection   *connection,
 #ifdef G_OS_UNIX
   else if (connection->kdbus_worker)
     {
-      ret = _g_kdbus_worker_send_message (connection->kdbus_worker, message, -1, error);
+      ret = _g_kdbus_worker_send_message (connection->kdbus_worker, message, timeout_msec, error);
     }
 #endif
   else
@@ -2462,7 +2463,7 @@ g_dbus_connection_send_message (GDBusConnection        *connection,
   g_return_val_if_fail (error == NULL || *error == NULL, FALSE);
 
   CONNECTION_LOCK (connection);
-  ret = g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, error);
+  ret = g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, error, -1);
   CONNECTION_UNLOCK (connection);
   return ret;
 }
@@ -2662,9 +2663,6 @@ g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection     *connect
   if (out_serial == NULL)
     out_serial = &serial;
 
-  if (timeout_msec == -1)
-    timeout_msec = 25 * 1000;
-
   simple = g_simple_async_result_new (G_OBJECT (connection),
                                       callback,
                                       user_data,
@@ -2683,7 +2681,7 @@ g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection     *connect
     }
 
   error = NULL;
-  if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error))
+  if (!g_dbus_connection_send_message_unlocked (connection, message, flags, out_serial, &error, timeout_msec))
     {
       g_simple_async_result_take_error (simple, error);
       g_simple_async_result_complete_in_idle (simple);
@@ -2707,9 +2705,14 @@ g_dbus_connection_send_message_with_reply_unlocked (GDBusConnection     *connect
                                                             (GDestroyNotify) send_message_data_unref);
     }
 
-  if (timeout_msec != G_MAXINT)
+  if (timeout_msec != G_MAXINT
+#ifdef G_OS_UNIX
+        /* Userspace timeouts unnecessary on unix/kdbus - kdbus handles timeouts. */
+        && !connection->kdbus_worker
+#endif
+     )
     {
-      data->timeout_source = g_timeout_source_new (timeout_msec);
+      data->timeout_source = g_timeout_source_new (timeout_msec == -1 ? DBUS_DEFAULT_TIMEOUT_MSEC : timeout_msec);
       g_source_set_priority (data->timeout_source, G_PRIORITY_DEFAULT);
       g_source_set_callback (data->timeout_source,
                              send_message_with_reply_timeout_cb,
@@ -4192,7 +4195,8 @@ add_match_rule (GDBusConnection *connection,
                                                 message,
                                                 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
                                                 NULL,
-                                                &error))
+                                                &error,
+                                                -1))
     {
       g_critical ("Error while sending AddMatch() message: %s", error->message);
       g_error_free (error);
@@ -4224,7 +4228,8 @@ remove_match_rule (GDBusConnection *connection,
                                                 message,
                                                 G_DBUS_SEND_MESSAGE_FLAGS_NONE,
                                                 NULL,
-                                                &error))
+                                                &error,
+                                                -1))
     {
       /* If we could get G_IO_ERROR_CLOSED here, it wouldn't be reasonable to
        * critical; but we're holding the lock, and our caller checked whether
@@ -5196,7 +5201,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection            *connect
                                                "org.freedesktop.DBus.Error.InvalidArgs",
                                                _("No such property '%s'"),
                                                property_name);
-      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_object_unref (reply);
       handled = TRUE;
       goto out;
@@ -5208,7 +5213,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection            *connect
                                                "org.freedesktop.DBus.Error.InvalidArgs",
                                                _("Property '%s' is not readable"),
                                                property_name);
-      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_object_unref (reply);
       handled = TRUE;
       goto out;
@@ -5219,7 +5224,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection            *connect
                                                "org.freedesktop.DBus.Error.InvalidArgs",
                                                _("Property '%s' is not writable"),
                                                property_name);
-      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_object_unref (reply);
       handled = TRUE;
       goto out;
@@ -5240,7 +5245,7 @@ validate_and_maybe_schedule_property_getset (GDBusConnection            *connect
                                                    _("Error setting property '%s': Expected type '%s' but got '%s'"),
                                                    property_name, property_info->signature,
                                                    g_variant_get_type_string (value));
-          g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+          g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
           g_variant_unref (value);
           g_object_unref (reply);
           handled = TRUE;
@@ -5344,7 +5349,7 @@ handle_getset_property (GDBusConnection *connection,
                                                "org.freedesktop.DBus.Error.InvalidArgs",
                                                _("No such interface '%s'"),
                                                interface_name);
-      g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_object_unref (reply);
       handled = TRUE;
       goto out;
@@ -5552,7 +5557,7 @@ handle_get_all_properties (GDBusConnection *connection,
                                                "org.freedesktop.DBus.Error.InvalidArgs",
                                                _("No such interface"),
                                                interface_name);
-      g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (eo->connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_object_unref (reply);
       handled = TRUE;
       goto out;
@@ -5742,7 +5747,7 @@ handle_introspect (GDBusConnection *connection,
 
   reply = g_dbus_message_new_method_reply (message);
   g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
-  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
   g_object_unref (reply);
   g_string_free (s, TRUE);
 
@@ -5868,7 +5873,7 @@ validate_and_maybe_schedule_method_call (GDBusConnection            *connection,
                                                "org.freedesktop.DBus.Error.UnknownMethod",
                                                _("No such method '%s'"),
                                                g_dbus_message_get_member (message));
-      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_object_unref (reply);
       handled = TRUE;
       goto out;
@@ -5900,7 +5905,7 @@ validate_and_maybe_schedule_method_call (GDBusConnection            *connection,
                                                _("Type of message, '%s', does not match expected type '%s'"),
                                                g_variant_get_type_string (parameters),
                                                type_string);
-      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+      g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
       g_variant_type_free (in_type);
       g_variant_unref (parameters);
       g_object_unref (reply);
@@ -7581,7 +7586,7 @@ handle_generic_ping_unlocked (GDBusConnection *connection,
 {
   GDBusMessage *reply;
   reply = g_dbus_message_new_method_reply (message);
-  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
   g_object_unref (reply);
 }
 
@@ -7614,7 +7619,7 @@ handle_generic_get_machine_id_unlocked (GDBusConnection *connection,
       reply = g_dbus_message_new_method_reply (message);
       g_dbus_message_set_body (reply, g_variant_new ("(s)", connection->machine_id));
     }
-  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
   g_object_unref (reply);
 }
 
@@ -7641,7 +7646,7 @@ handle_generic_introspect_unlocked (GDBusConnection *connection,
 
   reply = g_dbus_message_new_method_reply (message);
   g_dbus_message_set_body (reply, g_variant_new ("(s)", s->str));
-  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
   g_object_unref (reply);
   g_string_free (s, TRUE);
 }
@@ -7778,7 +7783,7 @@ distribute_method_call (GDBusConnection *connection,
                                            _("No such interface '%s' on object at path %s"),
                                            interface_name,
                                            object_path);
-  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL);
+  g_dbus_connection_send_message_unlocked (connection, reply, G_DBUS_SEND_MESSAGE_FLAGS_NONE, NULL, NULL, -1);
   g_object_unref (reply);
 
  out:
index ee51357..e5ef808 100644 (file)
@@ -29,6 +29,8 @@
 
 G_BEGIN_DECLS
 
+#define DBUS_DEFAULT_TIMEOUT_MSEC   25000U
+
 #define G_TYPE_DBUS_CONNECTION         (g_dbus_connection_get_type ())
 #define G_DBUS_CONNECTION(o)           (G_TYPE_CHECK_INSTANCE_CAST ((o), G_TYPE_DBUS_CONNECTION, GDBusConnection))
 #define G_IS_DBUS_CONNECTION(o)        (G_TYPE_CHECK_INSTANCE_TYPE ((o), G_TYPE_DBUS_CONNECTION))
index 2402740..ab902a8 100755 (executable)
@@ -23,6 +23,7 @@
  */
 
 #include "config.h"
+#include "gdbusconnection.h" /* DBUS_DEFAULT_TIMEOUT_MSEC */
 #include "gkdbus.h"
 #include "glib-unix.h"
 #include "glib-linux.h"
@@ -63,7 +64,7 @@
 #include "gunixfdmessage.h"
 
 #define KDBUS_MSG_MAX_SIZE         8192
-#define KDBUS_DEFAULT_TIMEOUT_NS   25000000000LLU
+#define KDBUS_INFINITE_TIMEOUT_NS   0x7fffffffffffffffLLU
 #define KDBUS_POOL_SIZE            (16 * 1024LU * 1024LU)
 #define KDBUS_MEMFD_THRESHOLD      (512 * 1024LU)
 #define KDBUS_ALIGN8(l)            (((l) + 7) & ~7)
@@ -3287,16 +3288,15 @@ _g_kdbus_send (GKDBusWorker  *worker,
                 ((g_dbus_message_get_flags (message) & G_DBUS_MESSAGE_FLAGS_NO_AUTO_START) ? KDBUS_MSG_NO_AUTO_START : 0);
 
   if ((msg->flags) & KDBUS_MSG_EXPECT_REPLY)
-    {
-      if (timeout_msec != -1)
-        msg->timeout_ns = 1000LU * g_get_monotonic_time() + (timeout_msec * 1000000LLU);
-      else
-        msg->timeout_ns = 1000LU * g_get_monotonic_time() + KDBUS_DEFAULT_TIMEOUT_NS;
-    }
+    msg->timeout_ns = 1U | ( /* ensure nonzero */
+                        1000U * g_get_monotonic_time() + (
+                          timeout_msec == -1 ? DBUS_DEFAULT_TIMEOUT_MSEC * 1000000LLU :
+                          timeout_msec == G_MAXINT ? KDBUS_INFINITE_TIMEOUT_NS :
+                          (guint64)timeout_msec * 1000000U
+                        )
+                      );
   else
-    {
-      msg->cookie_reply = g_dbus_message_get_reply_serial(message);
-    }
+    msg->cookie_reply = g_dbus_message_get_reply_serial(message);
 
   /*
    * append bloom filter item for broadcast signals