diff options
author | Havoc Pennington <hp@redhat.com> | 2003-04-14 03:25:19 +0000 |
---|---|---|
committer | Havoc Pennington <hp@redhat.com> | 2003-04-14 03:25:19 +0000 |
commit | 7c434a849a161c4514f3759659ac7af9ecddc721 (patch) | |
tree | 44585521fe21e086e463ee33df7d0556214192a4 | |
parent | 777707ed8dff6958972a93894a87ec1945c65c14 (diff) |
2003-04-13 Havoc Pennington <hp@pobox.com>
* tools/dbus-send.c, tools/dbus-monitor.c: two utility programs
from Philip Blundell to send messages and monitor them.
-rw-r--r-- | AUTHORS | 1 | ||||
-rw-r--r-- | ChangeLog | 5 | ||||
-rw-r--r-- | Makefile.am | 2 | ||||
-rw-r--r-- | configure.in | 1 | ||||
-rw-r--r-- | tools/.cvsignore | 12 | ||||
-rw-r--r-- | tools/Makefile.am | 18 | ||||
-rw-r--r-- | tools/dbus-monitor.c | 122 | ||||
-rw-r--r-- | tools/dbus-send.c | 168 |
8 files changed, 328 insertions, 1 deletions
@@ -1,3 +1,4 @@ +Philip Blundell <pb@nexus.co.uk> Anders Carlsson <andersca@codefactory.se> Alex Larsson <alexl@redhat.com> Havoc Pennington <hp@redhat.com> @@ -1,4 +1,9 @@ 2003-04-13 Havoc Pennington <hp@pobox.com> + + * tools/dbus-send.c, tools/dbus-monitor.c: two utility programs + from Philip Blundell to send messages and monitor them. + +2003-04-13 Havoc Pennington <hp@pobox.com> * dbus/dbus-mainloop.c: fix some reentrancy issues by refcounting callbacks diff --git a/Makefile.am b/Makefile.am index 65312332..2bf9a509 100644 --- a/Makefile.am +++ b/Makefile.am @@ -8,7 +8,7 @@ if HAVE_QT QT_SUBDIR=qt endif -SUBDIRS=dbus bus test doc $(GLIB_SUBDIR) $(QT_SUBDIR) +SUBDIRS=dbus bus test doc $(GLIB_SUBDIR) $(QT_SUBDIR) tools pkgconfigdir = $(libdir)/pkgconfig pkgconfig_DATA = dbus-1.0.pc $(GLIB_PC) diff --git a/configure.in b/configure.in index ec5ea92e..615a75d8 100644 --- a/configure.in +++ b/configure.in @@ -524,6 +524,7 @@ dbus/Makefile glib/Makefile qt/Makefile bus/Makefile +tools/Makefile test/Makefile doc/Makefile dbus-1.0.pc diff --git a/tools/.cvsignore b/tools/.cvsignore new file mode 100644 index 00000000..020ad7da --- /dev/null +++ b/tools/.cvsignore @@ -0,0 +1,12 @@ +.deps +.libs +Makefile +Makefile.in +*.lo +*.la +dbus-send +dbus-monitor +*.bb +*.bbg +*.gcov +*.da diff --git a/tools/Makefile.am b/tools/Makefile.am new file mode 100644 index 00000000..f1f9dbac --- /dev/null +++ b/tools/Makefile.am @@ -0,0 +1,18 @@ +INCLUDES=-I$(top_srcdir) $(DBUS_CLIENT_CFLAGS) $(DBUS_GLIB_CFLAGS) + +if HAVE_GLIB +GLIB_TOOLS=dbus-monitor +else +GLIB_TOOLS= +endif + +bin_PROGRAMS=dbus-send $(GLIB_TOOLS) + +dbus_send_SOURCES= \ + dbus-send.c + +dbus_monitor_SOURCES= \ + dbus-monitor.c + +dbus_send_LDADD= $(top_builddir)/dbus/libdbus-1.la +dbus_monitor_LDADD= $(top_builddir)/glib/libdbus-glib-1.la diff --git a/tools/dbus-monitor.c b/tools/dbus-monitor.c new file mode 100644 index 00000000..466a0836 --- /dev/null +++ b/tools/dbus-monitor.c @@ -0,0 +1,122 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-monitor.c Utility program to monitor messages on the bus + * + * Copyright (C) 2003 Philip Blundell <philb@gnu.org> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <glib.h> +#include <dbus/dbus.h> + /* Don't copy this, for programs outside the dbus tree it's dbus/dbus-glib.h */ +#include <glib/dbus-glib.h> + +static DBusHandlerResult +handler_func (DBusMessageHandler *handler, + DBusConnection *connection, + DBusMessage *message, + void *user_data) +{ + DBusMessageIter iter; + + printf ("message name=%s; sender=%s\n", dbus_message_get_name (message), + dbus_message_get_sender (message)); + + dbus_message_iter_init (message, &iter); + + do + { + int type = dbus_message_iter_get_arg_type (&iter); + char *str; + dbus_uint32_t uint32; + dbus_int32_t int32; + double d; + unsigned char byte; + + if (type == DBUS_TYPE_INVALID) + break; + + switch (type) + { + case DBUS_TYPE_STRING: + str = dbus_message_iter_get_string (&iter); + printf ("string:%s\n", str); + break; + + case DBUS_TYPE_INT32: + int32 = dbus_message_iter_get_int32 (&iter); + printf ("int32:%d\n", int32); + break; + + case DBUS_TYPE_UINT32: + uint32 = dbus_message_iter_get_uint32 (&iter); + printf ("int32:%u\n", uint32); + break; + + case DBUS_TYPE_DOUBLE: + d = dbus_message_iter_get_double (&iter); + printf ("double:%f\n", d); + break; + + case DBUS_TYPE_BYTE: + byte = dbus_message_iter_get_byte (&iter); + printf ("byte:%d\n", byte); + break; + + default: + printf ("(unknown arg type %d)\n", type); + break; + } + } while (dbus_message_iter_next (&iter)); + + return DBUS_HANDLER_RESULT_ALLOW_MORE_HANDLERS; +} + +int +main (int argc, char *argv[]) +{ + DBusConnection *connection; + DBusError error; + DBusBusType type = DBUS_BUS_SYSTEM; + DBusMessageHandler *handler; + GMainLoop *loop; + + loop = g_main_loop_new (NULL, FALSE); + + dbus_error_init (&error); + connection = dbus_bus_get (type, &error); + if (connection == NULL) + { + fprintf (stderr, "Failed to open connection to %s message bus: %s\n", + (type == DBUS_BUS_SYSTEM) ? "system" : "session", + error.message); + dbus_error_free (&error); + exit (1); + } + + dbus_connection_setup_with_g_main (connection); + + handler = dbus_message_handler_new (handler_func, NULL, NULL); + dbus_connection_add_filter (connection, handler); + + g_main_loop_run (loop); + + exit (0); +} diff --git a/tools/dbus-send.c b/tools/dbus-send.c new file mode 100644 index 00000000..f9f9881e --- /dev/null +++ b/tools/dbus-send.c @@ -0,0 +1,168 @@ +/* -*- mode: C; c-file-style: "gnu" -*- */ +/* dbus-send.c Utility program to send messages from the command line + * + * Copyright (C) 2003 Philip Blundell <philb@gnu.org> + * + * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + */ + +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <dbus/dbus.h> + +static void +usage (char *name) +{ + fprintf (stderr, "Usage: %s [--session] [--dest=SERVICE] <message type> [contents ...]\n", name); + exit (1); +} + +int +main (int argc, char *argv[]) +{ + DBusConnection *connection; + DBusError error; + DBusMessage *message; + DBusMessageIter iter; + int i; + DBusBusType type = DBUS_BUS_SYSTEM; + char *dest = DBUS_SERVICE_BROADCAST; + char *name = NULL; + + if (argc < 2) + usage (argv[0]); + + for (i = 1; i < argc && name == NULL; i++) + { + char *arg = argv[i]; + + if (!strcmp (arg, "--session")) + type = DBUS_BUS_SESSION; + else if (strstr (arg, "--dest=") == arg) + dest = strchr (arg, '=') + 1; + else if (arg[0] == '-') + usage (argv[0]); + else + name = arg; + } + + if (name == NULL) + usage (argv[0]); + + dbus_error_init (&error); + connection = dbus_bus_get (type, &error); + if (connection == NULL) + { + fprintf (stderr, "Failed to open connection to %s message bus: %s\n", + (type == DBUS_BUS_SYSTEM) ? "system" : "session", + error.message); + dbus_error_free (&error); + exit (1); + } + + message = dbus_message_new (dest, name); + if (message == NULL) + { + fprintf (stderr, "Couldn't allocate D-BUS message\n"); + exit (1); + } + + dbus_message_append_iter_init (message, &iter); + + while (i < argc) + { + char *arg; + char *c; + int type; + dbus_uint32_t uint32; + dbus_int32_t int32; + double d; + unsigned char byte; + + type = DBUS_TYPE_INVALID; + arg = argv[i++]; + c = strchr (arg, ':'); + + if (c == NULL) + { + fprintf (stderr, "%s: Data item \"%s\" is badly formed\n", argv[0], arg); + exit (1); + } + + *(c++) = 0; + + if (arg[0] == 0 || !strcmp (arg, "string")) + type = DBUS_TYPE_STRING; + else if (!strcmp (arg, "int32")) + type = DBUS_TYPE_INT32; + else if (!strcmp (arg, "uint32")) + type = DBUS_TYPE_UINT32; + else if (!strcmp (arg, "double")) + type = DBUS_TYPE_DOUBLE; + else if (!strcmp (arg, "byte")) + type = DBUS_TYPE_BYTE; + else if (!strcmp (arg, "boolean")) + type = DBUS_TYPE_BOOLEAN; + else + { + fprintf (stderr, "%s: Unknown type \"%s\"\n", argv[0], arg); + exit (1); + } + + switch (type) + { + case DBUS_TYPE_BYTE: + byte = strtoul (c, NULL, 0); + dbus_message_iter_append_byte (&iter, byte); + break; + + case DBUS_TYPE_DOUBLE: + d = strtod (c, NULL); + dbus_message_iter_append_double (&iter, d); + break; + + case DBUS_TYPE_INT32: + int32 = strtol (c, NULL, 0); + dbus_message_iter_append_int32 (&iter, int32); + break; + + case DBUS_TYPE_UINT32: + uint32 = strtoul (c, NULL, 0); + dbus_message_iter_append_uint32 (&iter, uint32); + break; + + case DBUS_TYPE_STRING: + dbus_message_iter_append_string (&iter, c); + break; + + default: + fprintf (stderr, "%s: Unsupported data type\n", argv[0]); + exit (1); + } + } + + dbus_connection_send (connection, message, NULL); + + dbus_connection_flush (connection); + + dbus_message_unref (message); + + dbus_connection_disconnect (connection); + + exit (0); +} |