From 67fdbea2d806ceca823b2e7bdce328a1b3480ab8 Mon Sep 17 00:00:00 2001 From: Simon McVittie Date: Wed, 22 Jun 2011 18:44:24 +0100 Subject: [PATCH 3/4] dbus-echo, dbus-spam: merge into a single binary, dbus-test-tool This is installed by default, but easy to filter out for embedded systems or whatever. --- tools/.gitignore | 3 +- tools/Makefile.am | 19 ++++++----- tools/dbus-echo.c | 50 ++++++----------------------- tools/dbus-spam.c | 45 +++++++++++---------------- tools/test-tool.c | 86 +++++++++++++++++++++++++++++++++++++++++++++++++++ tools/test-tool.h | 29 +++++++++++++++++ tools/tool-common.c | 59 +++++++++++++++++++++++++++++++++++ tools/tool-common.h | 37 ++++++++++++++++++++++ 8 files changed, 251 insertions(+), 77 deletions(-) create mode 100644 tools/test-tool.c create mode 100644 tools/test-tool.h create mode 100644 tools/tool-common.c create mode 100644 tools/tool-common.h diff --git a/tools/.gitignore b/tools/.gitignore index 84252df..06dbc2e 100644 --- a/tools/.gitignore +++ b/tools/.gitignore @@ -18,5 +18,4 @@ dbus-glib-bindings.h run-with-tmp-session-bus.conf print-introspect dbus-bus-introspect.xml -dbus-spam -dbus-echo +dbus-test-tool diff --git a/tools/Makefile.am b/tools/Makefile.am index 46b3dec..a4a4432 100644 --- a/tools/Makefile.am +++ b/tools/Makefile.am @@ -11,8 +11,7 @@ bin_PROGRAMS = \ dbus-launch \ dbus-send \ dbus-monitor \ - dbus-echo \ - dbus-spam \ + dbus-test-tool \ $(extra_bin_programs) dbus_send_SOURCES= \ @@ -54,13 +53,15 @@ dbus_uuidgen_LDFLAGS=@R_DYNAMIC_LDFLAG@ dbus_launch_LDADD= $(DBUS_X_LIBS) $(DBUS_CLIENT_LIBS) dbus_launch_LDFLAGS=@R_DYNAMIC_LDFLAG@ -dbus_echo_SOURCES= \ - dbus-echo.c -dbus_echo_LDADD = $(top_builddir)/dbus/libdbus-1.la - -dbus_spam_SOURCES= \ - dbus-spam.c -dbus_spam_LDADD = $(top_builddir)/dbus/libdbus-1.la +dbus_test_tool_SOURCES = \ + dbus-echo.c \ + dbus-spam.c \ + tool-common.c \ + tool-common.h \ + test-tool.c \ + test-tool.h \ + $(NULL) +dbus_test_tool_LDADD = $(top_builddir)/dbus/libdbus-1.la EXTRA_DIST = run-with-tmp-session-bus.sh strtoll.c strtoull.c CLEANFILES = \ diff --git a/tools/dbus-echo.c b/tools/dbus-echo.c index 43723b3..2277ded 100644 --- a/tools/dbus-echo.c +++ b/tools/dbus-echo.c @@ -28,15 +28,14 @@ #include -#ifdef DBUS_WIN -#include -#endif +#include "test-tool.h" +#include "tool-common.h" static void usage (int exit_with) { fprintf (stderr, - "Usage: dbus-echo [OPTIONS]\n" + "Usage: dbus-test-tool echo [OPTIONS]\n" "\n" "Respond to all method calls with an empty reply.\n" "\n" @@ -52,25 +51,6 @@ usage (int exit_with) exit (exit_with); } -/* a hack to avoid having to depend on the static -util version of libdbus; - * it's useful for ancillary programs to be able to use the shared library */ -static void -millisleep (int ms) -{ -#ifdef DBUS_WIN - Sleep (ms); -#else - fd_set nothing; - struct timeval tv; - - tv.tv_sec = ms / 1000; - tv.tv_usec = (ms % 1000) * 1000; - - FD_ZERO (¬hing); - select (1, ¬hing, ¬hing, ¬hing, &tv); -#endif -} - static DBusHandlerResult filter (DBusConnection *connection, DBusMessage *message, @@ -85,22 +65,16 @@ filter (DBusConnection *connection, if (*sleep_ms > 0) { - millisleep (*sleep_ms); + tool_millisleep (*sleep_ms); } reply = dbus_message_new_method_return (message); if (reply == NULL) - { - fprintf (stderr, "OOM allocating reply!\n"); - exit (1); - } + tool_oom ("allocating reply"); if (!dbus_connection_send (connection, reply, NULL)) - { - fprintf (stderr, "OOM sending reply!\n"); - exit (1); - } + tool_oom ("sending reply"); dbus_message_unref (reply); @@ -114,7 +88,7 @@ typedef enum { } ConnectionMode; int -main (int argc, char **argv) +dbus_test_tool_echo (int argc, char **argv) { DBusConnection *connection; DBusError error = DBUS_ERROR_INIT; @@ -123,7 +97,9 @@ main (int argc, char **argv) int sleep_ms = -1; const char *name = NULL; - for (i = 1; i < argc; i++) + /* argv[1] is the tool name, so start from 2 */ + + for (i = 2; i < argc; i++) { const char *arg = argv[i]; @@ -174,10 +150,7 @@ main (int argc, char **argv) } if (!dbus_connection_add_filter (connection, filter, &sleep_ms, NULL)) - { - fprintf (stderr, "OOM while adding message filter\n"); - return 1; - } + tool_oom ("adding message filter"); while (dbus_connection_read_write_dispatch (connection, -1)) {} @@ -185,4 +158,3 @@ main (int argc, char **argv) dbus_connection_unref (connection); return 0; } - diff --git a/tools/dbus-spam.c b/tools/dbus-spam.c index ab88635..5c0f8aa 100644 --- a/tools/dbus-spam.c +++ b/tools/dbus-spam.c @@ -29,17 +29,14 @@ #include -#if 0 -#define VERBOSE fprintf -#else -#define VERBOSE(...) do {} while (0) -#endif +#include "test-tool.h" +#include "tool-common.h" static void usage (int ecode) { fprintf (stderr, - "Usage: dbus-spam [OPTIONS]\n" + "Usage: dbus-test-tool spam [OPTIONS]\n" "\n" "Repeatedly call com.example.Spam() on the given D-Bus service.\n" "\n" @@ -73,13 +70,6 @@ usage (int ecode) } static void -oom (const char *doing) -{ - fprintf (stderr, "OOM while %s\n", doing); - exit (1); -} - -static void pc_notify (DBusPendingCall *pc, void *data) { @@ -103,7 +93,7 @@ consume_stdin (char **payload_p, buf = dbus_malloc (len); if (buf == NULL) - oom ("reading payload from stdin"); + tool_oom ("reading payload from stdin"); while (1) { @@ -112,7 +102,7 @@ consume_stdin (char **payload_p, char *tmp = dbus_realloc (buf, len + BLOCK_SIZE); if (tmp == NULL) - oom ("reading payload from stdin"); + tool_oom ("reading payload from stdin"); buf = tmp; len += BLOCK_SIZE; @@ -134,7 +124,7 @@ consume_stdin (char **payload_p, } int -main (int argc, char **argv) +dbus_test_tool_spam (int argc, char **argv) { DBusConnection *connection; DBusError error = DBUS_ERROR_INIT; @@ -155,7 +145,9 @@ main (int argc, char **argv) int n_random_sizes = 0; unsigned int *random_sizes = NULL; - for (i = 1; i < argc; i++) + /* argv[1] is the tool name, so start from 2 */ + + for (i = 2; i < argc; i++) { const char *arg = argv[i]; @@ -226,7 +218,7 @@ main (int argc, char **argv) random_sizes = dbus_new0 (int, n_random_sizes); if (random_sizes == NULL) - oom ("allocating array of message lengths"); + tool_oom ("allocating array of message lengths"); for (p = payload_buf, i = 0; p < payload_buf + payload_len && i < n_random_sizes; @@ -245,7 +237,7 @@ main (int argc, char **argv) payload = payload_buf; if (payload_buf == NULL) - oom ("allocating maximum-sized payload"); + tool_oom ("allocating maximum-sized payload"); memset (payload_buf, 'X', payload_len); payload_buf[payload_len - 1] = '\0'; @@ -319,7 +311,7 @@ main (int argc, char **argv) message = dbus_message_copy (template); if (message == NULL) - oom ("copying message"); + tool_oom ("copying message"); dbus_message_set_no_reply (message, no_reply); } @@ -335,7 +327,7 @@ main (int argc, char **argv) "Spam"); if (message == NULL) - oom ("allocating message"); + tool_oom ("allocating message"); dbus_message_set_no_reply (message, no_reply); @@ -384,13 +376,13 @@ main (int argc, char **argv) } if (!mem) - oom ("building message"); + tool_oom ("building message"); } if (no_reply) { if (!dbus_connection_send (connection, message, NULL)) - oom ("sending message"); + tool_oom ("sending message"); sent++; } @@ -402,19 +394,19 @@ main (int argc, char **argv) message, &pc, 0x7FFFFFFF)) - oom ("sending message"); + tool_oom ("sending message"); VERBOSE (stderr, "sent message #%d\n", sent); sent++; if (pc == NULL) - oom ("sending message"); + tool_oom ("sending message"); if (dbus_pending_call_get_completed (pc)) pc_notify (pc, &received); else if (!dbus_pending_call_set_notify (pc, pc_notify, &received, NULL)) - oom ("setting pending call notifier"); + tool_oom ("setting pending call notifier"); dbus_pending_call_unref (pc); } @@ -443,4 +435,3 @@ main (int argc, char **argv) dbus_shutdown (); return 0; } - diff --git a/tools/test-tool.c b/tools/test-tool.c new file mode 100644 index 0000000..86af15f --- /dev/null +++ b/tools/test-tool.c @@ -0,0 +1,86 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-test-tool - D-Bus swiss army knife + * + * Copyright © 2003 Philip Blundell + * Copyright © 2011 Nokia Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include "test-tool.h" + +#include +#include +#include + +#include + +static struct { + const char *name; + int (*callback) (int, char **); +} subcommands[] = { + { "echo", dbus_test_tool_echo }, + { "spam", dbus_test_tool_spam }, + { NULL, NULL } +}; + +static void usage (int exit_with) _DBUS_GNUC_NORETURN; + +static void +usage (int exit_with) +{ + int i; + + fprintf (stderr, + "Usage: dbus-test-tool SUBCOMMAND [OPTIONS]\n" + "\n" + "Known SUBCOMMANDs are:\n" + "\n" + ); + + for (i = 0; subcommands[i].name != NULL; i++) + { + fprintf (stderr, "- %s\n", subcommands[i].name); + } + + fprintf (stderr, + "\n" + "For more information: dbus-test-tool SUBCOMMAND --help\n" + ); + + exit (exit_with); +} + +int +main (int argc, char **argv) +{ + int i; + + if (argc < 2) + { + usage (2); + } + + for (i = 0; subcommands[i].name != NULL; i++) + { + if (!strcmp (argv[1], subcommands[i].name)) + return subcommands[i].callback (argc, argv); + } + + usage (2); + return 2; +} diff --git a/tools/test-tool.h b/tools/test-tool.h new file mode 100644 index 0000000..a6a4073 --- /dev/null +++ b/tools/test-tool.h @@ -0,0 +1,29 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* dbus-test-tool - D-Bus swiss army knife + * + * Copyright © 2003 Philip Blundell + * Copyright © 2011 Nokia Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef DBUS_TEST_TOOL_H +#define DBUS_TEST_TOOL_H + +int dbus_test_tool_echo (int argc, char **argv); +int dbus_test_tool_spam (int argc, char **argv); + +#endif diff --git a/tools/tool-common.c b/tools/tool-common.c new file mode 100644 index 0000000..2e48371 --- /dev/null +++ b/tools/tool-common.c @@ -0,0 +1,59 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* tool-common - common functionality for dbus-test-tool modules + * + * Copyright © 2003 Philip Blundell + * Copyright © 2011 Nokia Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#include +#include "tool-common.h" + +#include +#include +#include +#include + +#ifdef DBUS_WIN +#include +#endif + +/* a hack to avoid having to depend on the static -util version of libdbus; + * it's useful for ancillary programs to be able to use the shared library */ +void +tool_millisleep (int ms) +{ +#ifdef DBUS_WIN + Sleep (ms); +#else + fd_set nothing; + struct timeval tv; + + tv.tv_sec = ms / 1000; + tv.tv_usec = (ms % 1000) * 1000; + + FD_ZERO (¬hing); + select (1, ¬hing, ¬hing, ¬hing, &tv); +#endif +} + +void +tool_oom (const char *doing) +{ + fprintf (stderr, "OOM while %s\n", doing); + exit (1); +} diff --git a/tools/tool-common.h b/tools/tool-common.h new file mode 100644 index 0000000..ae639ed --- /dev/null +++ b/tools/tool-common.h @@ -0,0 +1,37 @@ +/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */ +/* tool-common - common functionality for dbus-test-tool modules + * + * Copyright © 2003 Philip Blundell + * Copyright © 2011 Nokia Corporation + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * + */ + +#ifndef DBUS_TOOL_COMMON_H +#define DBUS_TOOL_COMMON_H + +#include + +#if 1 +#define VERBOSE fprintf +#else +#define VERBOSE(...) do {} while (0) +#endif + +void tool_millisleep (int ms); +void tool_oom (const char *doing); + +#endif -- 1.7.5.4