From 5bba98d45d0c65779d1f7391128babc5d69ca644 Mon Sep 17 00:00:00 2001 From: Alban Crequy Date: Wed, 2 Jul 2014 14:18:26 +0100 Subject: [PATCH 6/7] dbus-test-tool echo: add --no-reply and --no-listen https://bugs.freedesktop.org/show_bug.cgi?id=34140 --- tools/dbus-echo.c | 50 +++++++++++++++++++++++++++++++++++++------------- 1 file changed, 37 insertions(+), 13 deletions(-) diff --git a/tools/dbus-echo.c b/tools/dbus-echo.c index 9cda22d..b406cc9 100644 --- a/tools/dbus-echo.c +++ b/tools/dbus-echo.c @@ -25,12 +25,17 @@ #include #include #include +#include #include #include "test-tool.h" #include "tool-common.h" +static int sleep_ms = -1; +static dbus_bool_t noreply = FALSE; +static dbus_bool_t nolisten = FALSE; + static void usage (int exit_with) { @@ -44,6 +49,8 @@ usage (int exit_with) " --name=NAME claim this well-known name first\n" "\n" " --sleep=N sleep N milliseconds before sending each reply\n" + " --no-reply discard received request without replying\n" + " --no-listen don't listen on the D-Bus socket\n" "\n" " --session use the session bus (default)\n" " --system use the system bus\n" @@ -57,25 +64,27 @@ filter (DBusConnection *connection, 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; } @@ -87,7 +96,6 @@ dbus_test_tool_echo (int argc, char **argv) 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 */ @@ -112,6 +120,14 @@ dbus_test_tool_echo (int argc, char **argv) { sleep_ms = atoi (arg + strlen ("--sleep-ms=")); } + else if (strcmp (arg, "--no-reply") == 0) + { + noreply = TRUE; + } + else if (strcmp (arg, "--no-listen") == 0) + { + nolisten = TRUE; + } else { usage (2); @@ -142,11 +158,19 @@ dbus_test_tool_echo (int argc, char **argv) printf ("%s\n", dbus_bus_get_unique_name (connection)); } - if (!dbus_connection_add_filter (connection, filter, &sleep_ms, NULL)) + if (!dbus_connection_add_filter (connection, filter, NULL, NULL)) tool_oom ("adding message filter"); - while (dbus_connection_read_write_dispatch (connection, -1)) - {} + if (nolisten) + { + while (1) + sleep (3600); + } + else + { + while (dbus_connection_read_write_dispatch (connection, -1)) + {} + } dbus_connection_unref (connection); return 0; -- 1.8.5.3