Commit 10f8b67a authored by Alban Crequy's avatar Alban Crequy Committed by Simon McVittie

Add dbus-test-tool, currently with "echo" and "spam" modes

This is installed by default, but easy to filter out for embedded systems
or whatever.

Based on earlier work by Simon McVittie and Will Thompson

Bug: https://bugs.freedesktop.org/show_bug.cgi?id=34140Reviewed-by: default avatarSimon McVittie <simon.mcvittie@collabora.co.uk>
parent c88525f8
......@@ -19,3 +19,4 @@ dbus-glib-bindings.h
run-with-tmp-session-bus.conf
print-introspect
dbus-bus-introspect.xml
dbus-test-tool
......@@ -15,6 +15,7 @@ bin_PROGRAMS = \
dbus-launch \
dbus-monitor \
dbus-send \
dbus-test-tool \
$(NULL)
if DBUS_UNIX
......@@ -81,6 +82,16 @@ dist_examples_SCRIPTS = \
GetAllMatchRules.py \
$(NULL)
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 = \
run-with-tmp-session-bus.conf
......
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-echo.c - a plain libdbus echo server
*
* Copyright © 2003 Philip Blundell <philb@gnu.org>
* Copyright © 2011 Nokia Corporation
* Copyright © 2014 Collabora Ltd.
*
* 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 <config.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus.h>
#include "test-tool.h"
#include "tool-common.h"
static void
usage (int exit_with)
{
fprintf (stderr,
"Usage: dbus-test-tool echo [OPTIONS]\n"
"\n"
"Respond to all method calls with an empty reply.\n"
"\n"
"Options:\n"
"\n"
" --name=NAME claim this well-known name first\n"
"\n"
" --sleep=N sleep N milliseconds before sending each reply\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)
{
tool_millisleep (*sleep_ms);
}
reply = dbus_message_new_method_return (message);
if (reply == NULL)
tool_oom ("allocating reply");
if (!dbus_connection_send (connection, reply, NULL))
tool_oom ("sending reply");
dbus_message_unref (reply);
return DBUS_HANDLER_RESULT_HANDLED;
}
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 */
for (i = 2; i < argc; i++)
{
const char *arg = argv[i];
if (strcmp (arg, "--system") == 0)
{
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 (strstr (arg, "--sleep-ms=") == arg)
{
sleep_ms = atoi (arg + strlen ("--sleep-ms="));
}
else
{
usage (2);
}
}
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);
return 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, &sleep_ms, NULL))
tool_oom ("adding message filter");
while (dbus_connection_read_write_dispatch (connection, -1))
{}
dbus_connection_unref (connection);
return 0;
}
This diff is collapsed.
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-test-tool - D-Bus swiss army knife
*
* Copyright © 2003 Philip Blundell <philb@gnu.org>
* Copyright © 2011 Nokia Corporation
* Copyright © 2014 Collabora Ltd.
*
* 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 <config.h>
#include "test-tool.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dbus/dbus.h>
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;
}
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* dbus-test-tool - D-Bus swiss army knife
*
* Copyright © 2003 Philip Blundell <philb@gnu.org>
* Copyright © 2011 Nokia Corporation
* Copyright © 2014 Collabora Ltd.
*
* 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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* tool-common - common functionality for dbus-test-tool modules
*
* Copyright © 2003 Philip Blundell <philb@gnu.org>
* Copyright © 2011 Nokia Corporation
* Copyright © 2014 Collabora Ltd.
*
* 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 <config.h>
#include "tool-common.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#ifdef DBUS_WIN
#include <windows.h>
#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 (&nothing);
select (1, &nothing, &nothing, &nothing, &tv);
#endif
}
void
tool_oom (const char *doing)
{
fprintf (stderr, "OOM while %s\n", doing);
exit (1);
}
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/* tool-common - common functionality for dbus-test-tool modules
*
* Copyright © 2003 Philip Blundell <philb@gnu.org>
* Copyright © 2011 Nokia Corporation
* Copyright © 2014 Collabora Ltd.
*
* 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 <dbus/dbus.h>
#if 0
#define VERBOSE fprintf
#else
#define VERBOSE(...) do {} while (0)
#endif
void tool_millisleep (int ms);
void tool_oom (const char *doing);
#endif
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment