]> git.ipfire.org Git - thirdparty/dbus.git/commitdiff
dbus-test-tool: add black-hole mode
authorAlban Crequy <alban.crequy@collabora.co.uk>
Wed, 2 Jul 2014 13:18:26 +0000 (14:18 +0100)
committerSimon McVittie <simon.mcvittie@collabora.co.uk>
Tue, 14 Oct 2014 12:53:59 +0000 (13:53 +0100)
Bug: https://bugs.freedesktop.org/show_bug.cgi?id=34140
Reviewed-by: Simon McVittie <simon.mcvittie@collabora.co.uk>
tools/dbus-echo.c
tools/test-tool.c
tools/test-tool.h

index 05af6011ba7176af8cab60ad32026dc8f98bcb99..72973cdb7b9e1731b13401f1b846d40622e1e2f1 100644 (file)
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
+#include <unistd.h>
 
 #include <dbus/dbus.h>
 
 #include "test-tool.h"
 #include "tool-common.h"
 
+static int sleep_ms = -1;
+static dbus_bool_t noreply = FALSE;
+static dbus_bool_t noread = FALSE;
+
 static void
-usage (int exit_with)
+usage_echo (int exit_with)
 {
   fprintf (stderr,
            "Usage: dbus-test-tool echo [OPTIONS]\n"
@@ -52,43 +57,99 @@ usage (int exit_with)
   exit (exit_with);
 }
 
+static void
+usage_black_hole (int exit_with)
+{
+  fprintf (stderr,
+           "Usage: dbus-test-tool black-hole [OPTIONS]\n"
+           "\n"
+           "Receive method calls but do not reply.\n"
+           "\n"
+           "Options:\n"
+           "\n"
+           "    --name=NAME   claim this well-known name first\n"
+           "\n"
+           "    --no-read     don't read anything on the D-Bus socket\n"
+           "\n"
+           "    --session     use the session bus (default)\n"
+           "    --system      use the system bus\n"
+           );
+  exit (exit_with);
+}
+
 static DBusHandlerResult
 filter (DBusConnection *connection,
     DBusMessage *message,
     void *user_data)
 {
   DBusMessage *reply;
-  int *sleep_ms = user_data;
 
   if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
 
-  if (*sleep_ms > 0)
+  if (sleep_ms > 0)
     {
-      tool_millisleep (*sleep_ms);
+      tool_millisleep (sleep_ms);
     }
 
-  reply = dbus_message_new_method_return (message);
+  if (!noreply)
+    {
+      reply = dbus_message_new_method_return (message);
 
-  if (reply == NULL)
-    tool_oom ("allocating reply");
+      if (reply == NULL)
+        tool_oom ("allocating reply");
 
-  if (!dbus_connection_send (connection, reply, NULL))
-    tool_oom ("sending reply");
+      if (!dbus_connection_send (connection, reply, NULL))
+        tool_oom ("sending reply");
 
-  dbus_message_unref (reply);
+      dbus_message_unref (reply);
+    }
 
   return DBUS_HANDLER_RESULT_HANDLED;
 }
 
+static DBusConnection *
+init_connection (DBusBusType type, const char *name)
+{
+  DBusConnection *connection;
+  DBusError error = DBUS_ERROR_INIT;
+
+  connection = dbus_bus_get (type, &error);
+
+  if (connection == NULL)
+    {
+      fprintf (stderr, "Failed to connect to bus: %s: %s\n",
+               error.name, error.message);
+      dbus_error_free (&error);
+      exit (1);
+    }
+
+  if (name != NULL)
+    {
+      if (dbus_bus_request_name (connection, name, DBUS_NAME_FLAG_DO_NOT_QUEUE,
+                                 NULL) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+        {
+          fprintf (stderr, "failed to take bus name %s\n", name);
+          exit (1);
+        }
+    }
+  else
+    {
+      printf ("%s\n", dbus_bus_get_unique_name (connection));
+    }
+
+  if (!dbus_connection_add_filter (connection, filter, NULL, NULL))
+    tool_oom ("adding message filter");
+
+  return connection;
+}
+
 int
 dbus_test_tool_echo (int argc, char **argv)
 {
   DBusConnection *connection;
-  DBusError error = DBUS_ERROR_INIT;
   DBusBusType type = DBUS_BUS_SESSION;
   int i;
-  int sleep_ms = -1;
   const char *name = NULL;
 
   /* argv[1] is the tool name, so start from 2 */
@@ -115,36 +176,64 @@ dbus_test_tool_echo (int argc, char **argv)
         }
       else
         {
-          usage (2);
+          usage_echo (2);
         }
     }
 
-  connection = dbus_bus_get (type, &error);
+  connection = init_connection (type, name);
 
-  if (connection == NULL)
-    {
-      fprintf (stderr, "Failed to connect to bus: %s: %s\n",
-               error.name, error.message);
-      dbus_error_free (&error);
-      return 1;
-    }
+  while (dbus_connection_read_write_dispatch (connection, -1))
+    {}
 
-  if (name != NULL)
+  dbus_connection_unref (connection);
+  return 0;
+}
+
+int
+dbus_test_tool_black_hole (int argc, char **argv)
+{
+  DBusConnection *connection;
+  DBusBusType type = DBUS_BUS_SESSION;
+  int i;
+  const char *name = NULL;
+
+  /* argv[1] is the tool name, so start from 2 */
+
+  for (i = 2; i < argc; i++)
     {
-      if (dbus_bus_request_name (connection, name, DBUS_NAME_FLAG_DO_NOT_QUEUE,
-                                 NULL) != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
+      const char *arg = argv[i];
+
+      if (strcmp (arg, "--system") == 0)
         {
-          fprintf (stderr, "failed to take bus name %s\n", name);
-          exit (1);
+          type = DBUS_BUS_SYSTEM;
+        }
+      else if (strcmp (arg, "--session") == 0)
+        {
+          type = DBUS_BUS_SESSION;
+        }
+      else if (strstr (arg, "--name=") == arg)
+        {
+          name = arg + strlen ("--name=");
+        }
+      else if (strcmp (arg, "--no-read") == 0)
+        {
+          noread = TRUE;
+        }
+      else
+        {
+          usage_black_hole (2);
         }
     }
-  else
+
+  connection = init_connection (type, name);
+
+  if (noread)
     {
-      printf ("%s\n", dbus_bus_get_unique_name (connection));
+      while (1)
+        sleep (3600);
     }
 
-  if (!dbus_connection_add_filter (connection, filter, &sleep_ms, NULL))
-    tool_oom ("adding message filter");
+  noreply = TRUE;
 
   while (dbus_connection_read_write_dispatch (connection, -1))
     {}
index 1893b78c1d07d26970357bfbcf3584e2fc04d487..149c10ab55f543cae1926e19c848819f2a2db871 100644 (file)
@@ -34,8 +34,9 @@ static struct {
     const char *name;
     int (*callback) (int, char **);
 } subcommands[] = {
-      { "echo", dbus_test_tool_echo },
-      { "spam", dbus_test_tool_spam },
+      { "black-hole", dbus_test_tool_black_hole },
+      { "echo",       dbus_test_tool_echo },
+      { "spam",       dbus_test_tool_spam },
       { NULL, NULL }
 };
 
index b6d51c22ad5167b66de4bc85d005cad21b935edf..8143cd5047bd2f310f3e6341a002f026b13b92f3 100644 (file)
@@ -24,6 +24,7 @@
 #ifndef DBUS_TEST_TOOL_H
 #define DBUS_TEST_TOOL_H
 
+int dbus_test_tool_black_hole (int argc, char **argv);
 int dbus_test_tool_echo (int argc, char **argv);
 int dbus_test_tool_spam (int argc, char **argv);