2011-09-26 11:02:59 +01:00
|
|
|
/* -*- 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>
|
2015-04-28 20:11:27 +02:00
|
|
|
#ifdef HAVE_UNISTD_H
|
2014-07-02 14:18:26 +01:00
|
|
|
#include <unistd.h>
|
2015-04-28 20:11:27 +02:00
|
|
|
#endif
|
2011-09-26 11:02:59 +01:00
|
|
|
|
|
|
|
|
#include <dbus/dbus.h>
|
|
|
|
|
|
2015-03-04 18:47:52 +00:00
|
|
|
#include "dbus/dbus-sysdeps.h"
|
2011-09-26 11:02:59 +01:00
|
|
|
#include "test-tool.h"
|
|
|
|
|
#include "tool-common.h"
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
static int sleep_ms = -1;
|
|
|
|
|
static dbus_bool_t noreply = FALSE;
|
|
|
|
|
static dbus_bool_t noread = FALSE;
|
|
|
|
|
|
2016-10-07 19:45:48 +01:00
|
|
|
static void usage_echo (int exit_with) _DBUS_GNUC_NORETURN;
|
|
|
|
|
|
2011-09-26 11:02:59 +01:00
|
|
|
static void
|
2014-07-02 14:18:26 +01:00
|
|
|
usage_echo (int exit_with)
|
2011-09-26 11:02:59 +01:00
|
|
|
{
|
|
|
|
|
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"
|
2016-02-21 23:50:49 -05:00
|
|
|
" --sleep-ms=N sleep N milliseconds before sending each reply\n"
|
2011-09-26 11:02:59 +01:00
|
|
|
"\n"
|
|
|
|
|
" --session use the session bus (default)\n"
|
|
|
|
|
" --system use the system bus\n"
|
|
|
|
|
);
|
|
|
|
|
exit (exit_with);
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 19:45:48 +01:00
|
|
|
static void usage_black_hole (int exit_with) _DBUS_GNUC_NORETURN;
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
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);
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-26 11:02:59 +01:00
|
|
|
static DBusHandlerResult
|
|
|
|
|
filter (DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
DBusMessage *reply;
|
|
|
|
|
|
|
|
|
|
if (dbus_message_get_type (message) != DBUS_MESSAGE_TYPE_METHOD_CALL)
|
|
|
|
|
return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
if (sleep_ms > 0)
|
2011-09-26 11:02:59 +01:00
|
|
|
{
|
2015-03-04 11:49:54 +01:00
|
|
|
_dbus_sleep_milliseconds (sleep_ms);
|
2011-09-26 11:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
if (!noreply)
|
|
|
|
|
{
|
|
|
|
|
reply = dbus_message_new_method_return (message);
|
2011-09-26 11:02:59 +01:00
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
if (reply == NULL)
|
|
|
|
|
tool_oom ("allocating reply");
|
2011-09-26 11:02:59 +01:00
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
if (!dbus_connection_send (connection, reply, NULL))
|
|
|
|
|
tool_oom ("sending reply");
|
2011-09-26 11:02:59 +01:00
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
dbus_message_unref (reply);
|
|
|
|
|
}
|
2011-09-26 11:02:59 +01:00
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-26 11:02:59 +01:00
|
|
|
int
|
|
|
|
|
dbus_test_tool_echo (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++)
|
|
|
|
|
{
|
|
|
|
|
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
|
|
|
|
|
{
|
2014-07-02 14:18:26 +01:00
|
|
|
usage_echo (2);
|
2011-09-26 11:02:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
connection = init_connection (type, name);
|
2011-09-26 11:02:59 +01:00
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
while (dbus_connection_read_write_dispatch (connection, -1))
|
|
|
|
|
{}
|
2011-09-26 11:02:59 +01:00
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
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++)
|
2011-09-26 11:02:59 +01:00
|
|
|
{
|
2014-07-02 14:18:26 +01:00
|
|
|
const char *arg = argv[i];
|
|
|
|
|
|
|
|
|
|
if (strcmp (arg, "--system") == 0)
|
2011-09-26 11:02:59 +01:00
|
|
|
{
|
2014-07-02 14:18:26 +01:00
|
|
|
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);
|
2011-09-26 11:02:59 +01:00
|
|
|
}
|
|
|
|
|
}
|
2014-07-02 14:18:26 +01:00
|
|
|
|
|
|
|
|
connection = init_connection (type, name);
|
|
|
|
|
|
|
|
|
|
if (noread)
|
2011-09-26 11:02:59 +01:00
|
|
|
{
|
2014-07-02 14:18:26 +01:00
|
|
|
while (1)
|
2015-04-28 20:11:27 +02:00
|
|
|
_dbus_sleep_milliseconds (3600);
|
2011-09-26 11:02:59 +01:00
|
|
|
}
|
|
|
|
|
|
2014-07-02 14:18:26 +01:00
|
|
|
noreply = TRUE;
|
2011-09-26 11:02:59 +01:00
|
|
|
|
|
|
|
|
while (dbus_connection_read_write_dispatch (connection, -1))
|
|
|
|
|
{}
|
|
|
|
|
|
|
|
|
|
dbus_connection_unref (connection);
|
|
|
|
|
return 0;
|
|
|
|
|
}
|