1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
|
/* -*- 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.
* SPDX-License-Identifier: GPL-2.0-or-later
*
* 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>
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#include <dbus/dbus.h>
#include "dbus/dbus-sysdeps.h"
#include "test-tool.h"
#include "tool-common.h"
static int sleep_ms = -1;
static dbus_bool_t noreply = FALSE;
static dbus_bool_t noread = FALSE;
static void usage_echo (int exit_with) _DBUS_GNUC_NORETURN;
static void
usage_echo (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-ms=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 void usage_black_hole (int exit_with) _DBUS_GNUC_NORETURN;
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);
}
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;
if (sleep_ms > 0)
{
_dbus_sleep_milliseconds (sleep_ms);
}
if (!noreply)
{
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;
}
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;
}
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
{
usage_echo (2);
}
}
connection = init_connection (type, name);
while (dbus_connection_read_write_dispatch (connection, -1))
{}
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++)
{
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 (strcmp (arg, "--no-read") == 0)
{
noread = TRUE;
}
else
{
usage_black_hole (2);
}
}
connection = init_connection (type, name);
if (noread)
{
while (1)
_dbus_sleep_milliseconds (3600);
}
noreply = TRUE;
while (dbus_connection_read_write_dispatch (connection, -1))
{}
dbus_connection_unref (connection);
return 0;
}
|