2007-07-14 02:44:01 +00:00
|
|
|
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
|
2003-04-14 03:25:19 +00:00
|
|
|
/* 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
|
2009-07-10 19:32:38 -04:00
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
2003-04-14 03:25:19 +00:00
|
|
|
*
|
|
|
|
|
*/
|
|
|
|
|
|
2006-12-31 11:31:12 +00:00
|
|
|
#include <config.h>
|
2015-01-23 19:32:31 +00:00
|
|
|
|
|
|
|
|
#include "dbus/dbus-internals.h" /* just for the macros */
|
|
|
|
|
|
2003-04-14 03:25:19 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
2006-12-31 12:20:54 +00:00
|
|
|
#ifdef DBUS_WIN
|
|
|
|
|
#include <winsock2.h>
|
|
|
|
|
#undef interface
|
|
|
|
|
#else
|
2006-11-08 15:49:30 +00:00
|
|
|
#include <sys/time.h>
|
2006-12-31 12:20:54 +00:00
|
|
|
#endif
|
|
|
|
|
|
2006-11-08 15:49:30 +00:00
|
|
|
#include <time.h>
|
|
|
|
|
|
2003-05-16 20:09:25 +00:00
|
|
|
#include "dbus-print-message.h"
|
2015-02-04 14:18:39 +00:00
|
|
|
#include "tool-common.h"
|
2003-04-14 03:25:19 +00:00
|
|
|
|
2011-07-07 16:10:54 +01:00
|
|
|
#define EAVESDROPPING_RULE "eavesdrop=true"
|
|
|
|
|
|
2015-01-23 19:32:31 +00:00
|
|
|
#ifndef STDOUT_FILENO
|
|
|
|
|
#define STDOUT_FILENO 1
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
/* http://www.tcpdump.org/linktypes.html */
|
|
|
|
|
#define LINKTYPE_DBUS 231
|
|
|
|
|
|
2003-04-14 03:25:19 +00:00
|
|
|
static DBusHandlerResult
|
2006-11-08 15:49:30 +00:00
|
|
|
monitor_filter_func (DBusConnection *connection,
|
2015-02-16 11:52:01 +00:00
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
2003-04-14 03:25:19 +00:00
|
|
|
{
|
2015-02-23 22:00:46 +01:00
|
|
|
long sec = 0, usec = 0;
|
|
|
|
|
|
|
|
|
|
_dbus_get_real_time (&sec, &usec);
|
|
|
|
|
|
|
|
|
|
print_message (message, FALSE, sec, usec);
|
2003-05-16 20:09:25 +00:00
|
|
|
|
2003-08-18 22:43:30 +00:00
|
|
|
if (dbus_message_is_signal (message,
|
2005-02-17 21:19:49 +00:00
|
|
|
DBUS_INTERFACE_LOCAL,
|
2003-08-18 22:43:30 +00:00
|
|
|
"Disconnected"))
|
2003-05-16 20:09:25 +00:00
|
|
|
exit (0);
|
2015-02-02 20:08:07 +00:00
|
|
|
|
|
|
|
|
/* Monitors must not allow libdbus to reply to messages, so we eat
|
|
|
|
|
* the message. See bug 1719.
|
2004-10-29 18:52:30 +00:00
|
|
|
*/
|
2004-10-29 18:50:29 +00:00
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
2003-04-14 03:25:19 +00:00
|
|
|
}
|
|
|
|
|
|
2006-11-08 15:49:30 +00:00
|
|
|
#define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
|
|
|
|
|
|
|
|
|
|
typedef enum
|
|
|
|
|
{
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_PATH = 16,
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
|
2008-04-16 10:49:25 -04:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
|
2006-11-08 15:49:30 +00:00
|
|
|
} ProfileAttributeFlags;
|
|
|
|
|
|
2015-02-16 11:23:00 +01:00
|
|
|
static void
|
|
|
|
|
profile_print_headers (void)
|
|
|
|
|
{
|
2015-02-16 13:54:39 +01:00
|
|
|
printf ("#type\ttimestamp\tserial\tsender\tdestination\tpath\tinterface\tmember\n");
|
2015-02-16 15:08:22 +01:00
|
|
|
printf ("#\t\t\t\t\tin_reply_to\n");
|
2015-02-16 11:23:00 +01:00
|
|
|
}
|
|
|
|
|
|
2006-11-08 15:49:30 +00:00
|
|
|
static void
|
|
|
|
|
profile_print_with_attrs (const char *type, DBusMessage *message,
|
2015-02-23 22:00:46 +01:00
|
|
|
long sec, long usec, ProfileAttributeFlags attrs)
|
2006-11-08 15:49:30 +00:00
|
|
|
{
|
2015-02-23 22:00:46 +01:00
|
|
|
printf ("%s\t%ld.%06ld", type, sec, usec);
|
2006-11-08 15:49:30 +00:00
|
|
|
|
|
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
|
|
|
|
|
printf ("\t%u", dbus_message_get_serial (message));
|
|
|
|
|
|
|
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
|
|
|
|
|
printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
|
|
|
|
|
|
|
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
|
|
|
|
|
printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
|
|
|
|
|
|
2015-02-16 11:23:00 +01:00
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
|
|
|
|
|
printf ("\t%u", dbus_message_get_reply_serial (message));
|
|
|
|
|
|
2006-11-08 15:49:30 +00:00
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
|
|
|
|
|
printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
|
|
|
|
|
|
|
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
|
|
|
|
|
printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
|
|
|
|
|
|
|
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
|
|
|
|
|
printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
|
|
|
|
|
|
|
|
|
|
if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
|
|
|
|
|
printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
|
|
|
|
|
|
|
|
|
|
printf ("\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
print_message_profile (DBusMessage *message)
|
|
|
|
|
{
|
2015-02-16 11:23:00 +01:00
|
|
|
static dbus_bool_t first = TRUE;
|
2015-02-23 22:00:46 +01:00
|
|
|
long sec = 0, usec = 0;
|
2006-11-08 15:49:30 +00:00
|
|
|
|
2015-02-16 11:23:00 +01:00
|
|
|
if (first)
|
|
|
|
|
{
|
2015-02-16 12:53:57 +00:00
|
|
|
profile_print_headers ();
|
2015-02-16 11:23:00 +01:00
|
|
|
first = FALSE;
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-23 22:00:46 +01:00
|
|
|
_dbus_get_real_time (&sec, &usec);
|
|
|
|
|
|
2006-11-08 15:49:30 +00:00
|
|
|
switch (dbus_message_get_type (message))
|
|
|
|
|
{
|
|
|
|
|
case DBUS_MESSAGE_TYPE_METHOD_CALL:
|
2015-02-23 22:00:46 +01:00
|
|
|
profile_print_with_attrs ("mc", message, sec, usec,
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SERIAL |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_SENDER |
|
2015-02-16 11:23:00 +01:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_DESTINATION |
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_PATH |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_INTERFACE |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_MEMBER);
|
|
|
|
|
break;
|
2006-11-08 15:49:30 +00:00
|
|
|
case DBUS_MESSAGE_TYPE_METHOD_RETURN:
|
2015-02-23 22:00:46 +01:00
|
|
|
profile_print_with_attrs ("mr", message, sec, usec,
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SERIAL |
|
2015-02-16 11:23:00 +01:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SENDER |
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_DESTINATION |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
|
|
|
|
|
break;
|
2006-11-08 15:49:30 +00:00
|
|
|
case DBUS_MESSAGE_TYPE_ERROR:
|
2015-02-23 22:00:46 +01:00
|
|
|
profile_print_with_attrs ("err", message, sec, usec,
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SERIAL |
|
2015-02-16 11:23:00 +01:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SENDER |
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_DESTINATION |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
|
|
|
|
|
break;
|
2006-11-08 15:49:30 +00:00
|
|
|
case DBUS_MESSAGE_TYPE_SIGNAL:
|
2015-02-23 22:00:46 +01:00
|
|
|
profile_print_with_attrs ("sig", message, sec, usec,
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SERIAL |
|
2015-02-16 11:23:00 +01:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_SENDER |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_DESTINATION |
|
2015-02-16 11:52:01 +00:00
|
|
|
PROFILE_ATTRIBUTE_FLAG_PATH |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_INTERFACE |
|
|
|
|
|
PROFILE_ATTRIBUTE_FLAG_MEMBER);
|
|
|
|
|
break;
|
2006-11-08 15:49:30 +00:00
|
|
|
default:
|
2015-02-23 22:00:46 +01:00
|
|
|
printf ("%s\t%ld.%06ld", "tun", sec, usec);
|
2015-02-16 11:52:01 +00:00
|
|
|
break;
|
2006-11-08 15:49:30 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult
|
2015-02-16 11:52:01 +00:00
|
|
|
profile_filter_func (DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
2006-11-08 15:49:30 +00:00
|
|
|
{
|
|
|
|
|
print_message_profile (message);
|
|
|
|
|
|
|
|
|
|
if (dbus_message_is_signal (message,
|
|
|
|
|
DBUS_INTERFACE_LOCAL,
|
|
|
|
|
"Disconnected"))
|
|
|
|
|
exit (0);
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
2015-01-23 19:32:31 +00:00
|
|
|
typedef enum {
|
|
|
|
|
BINARY_MODE_NOT,
|
|
|
|
|
BINARY_MODE_RAW,
|
|
|
|
|
BINARY_MODE_PCAP
|
|
|
|
|
} BinaryMode;
|
|
|
|
|
|
|
|
|
|
static DBusHandlerResult
|
|
|
|
|
binary_filter_func (DBusConnection *connection,
|
|
|
|
|
DBusMessage *message,
|
|
|
|
|
void *user_data)
|
|
|
|
|
{
|
|
|
|
|
BinaryMode mode = _DBUS_POINTER_TO_INT (user_data);
|
|
|
|
|
char *blob;
|
|
|
|
|
int len;
|
|
|
|
|
|
|
|
|
|
/* It would be nice if we could do a zero-copy "peek" one day, but libdbus
|
|
|
|
|
* is so copy-happy that this isn't really a big deal.
|
|
|
|
|
*/
|
|
|
|
|
if (!dbus_message_marshal (message, &blob, &len))
|
|
|
|
|
tool_oom ("retrieving message");
|
|
|
|
|
|
|
|
|
|
switch (mode)
|
|
|
|
|
{
|
|
|
|
|
case BINARY_MODE_PCAP:
|
|
|
|
|
{
|
2015-03-04 12:10:17 +00:00
|
|
|
long tv_sec, tv_usec;
|
2015-01-23 19:32:31 +00:00
|
|
|
/* seconds, microseconds, bytes captured (possibly truncated),
|
|
|
|
|
* original length.
|
|
|
|
|
* http://wiki.wireshark.org/Development/LibpcapFileFormat
|
|
|
|
|
*/
|
|
|
|
|
dbus_uint32_t header[4] = { 0, 0, len, len };
|
|
|
|
|
|
|
|
|
|
/* If this gets padded then we'd need to write it out in pieces */
|
|
|
|
|
_DBUS_STATIC_ASSERT (sizeof (header) == 16);
|
|
|
|
|
|
2015-03-04 12:10:17 +00:00
|
|
|
_dbus_get_real_time (&tv_sec, &tv_usec);
|
|
|
|
|
header[0] = tv_sec;
|
|
|
|
|
header[1] = tv_usec;
|
2015-01-23 19:32:31 +00:00
|
|
|
|
|
|
|
|
if (!tool_write_all (STDOUT_FILENO, header, sizeof (header)))
|
|
|
|
|
{
|
|
|
|
|
perror ("dbus-monitor: write");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BINARY_MODE_RAW:
|
|
|
|
|
default:
|
|
|
|
|
/* nothing special, just the raw message stream */
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!tool_write_all (STDOUT_FILENO, blob, len))
|
|
|
|
|
{
|
|
|
|
|
perror ("dbus-monitor: write");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_free (blob);
|
|
|
|
|
|
|
|
|
|
if (dbus_message_is_signal (message,
|
|
|
|
|
DBUS_INTERFACE_LOCAL,
|
|
|
|
|
"Disconnected"))
|
|
|
|
|
exit (0);
|
|
|
|
|
|
|
|
|
|
return DBUS_HANDLER_RESULT_HANDLED;
|
|
|
|
|
}
|
|
|
|
|
|
2016-10-07 19:45:48 +01:00
|
|
|
static void usage (char *name, int ecode) _DBUS_GNUC_NORETURN;
|
|
|
|
|
|
2003-05-16 16:01:56 +00:00
|
|
|
static void
|
|
|
|
|
usage (char *name, int ecode)
|
|
|
|
|
{
|
2015-01-23 19:32:31 +00:00
|
|
|
fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile | --pcap | --binary ] [watch expressions]\n", name);
|
2003-05-16 16:01:56 +00:00
|
|
|
exit (ecode);
|
|
|
|
|
}
|
|
|
|
|
|
2010-02-12 19:19:05 +00:00
|
|
|
static void
|
|
|
|
|
only_one_type (dbus_bool_t *seen_bus_type,
|
|
|
|
|
char *name)
|
|
|
|
|
{
|
|
|
|
|
if (*seen_bus_type)
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "I only support monitoring one bus at a time!\n");
|
|
|
|
|
usage (name, 1);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
*seen_bus_type = TRUE;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-02 20:08:07 +00:00
|
|
|
static dbus_bool_t
|
|
|
|
|
become_monitor (DBusConnection *connection,
|
|
|
|
|
int numFilters,
|
|
|
|
|
const char * const *filters)
|
|
|
|
|
{
|
|
|
|
|
DBusError error = DBUS_ERROR_INIT;
|
|
|
|
|
DBusMessage *m;
|
|
|
|
|
DBusMessage *r;
|
|
|
|
|
int i;
|
|
|
|
|
dbus_uint32_t zero = 0;
|
|
|
|
|
DBusMessageIter appender, array_appender;
|
|
|
|
|
|
|
|
|
|
m = dbus_message_new_method_call (DBUS_SERVICE_DBUS,
|
|
|
|
|
DBUS_PATH_DBUS, DBUS_INTERFACE_MONITORING, "BecomeMonitor");
|
|
|
|
|
|
|
|
|
|
if (m == NULL)
|
|
|
|
|
tool_oom ("becoming a monitor");
|
|
|
|
|
|
|
|
|
|
dbus_message_iter_init_append (m, &appender);
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_iter_open_container (&appender, DBUS_TYPE_ARRAY, "s",
|
|
|
|
|
&array_appender))
|
|
|
|
|
tool_oom ("opening string array");
|
|
|
|
|
|
|
|
|
|
for (i = 0; i < numFilters; i++)
|
|
|
|
|
{
|
|
|
|
|
if (!dbus_message_iter_append_basic (&array_appender, DBUS_TYPE_STRING,
|
|
|
|
|
&filters[i]))
|
|
|
|
|
tool_oom ("adding filter to array");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!dbus_message_iter_close_container (&appender, &array_appender) ||
|
|
|
|
|
!dbus_message_iter_append_basic (&appender, DBUS_TYPE_UINT32, &zero))
|
|
|
|
|
tool_oom ("finishing arguments");
|
|
|
|
|
|
|
|
|
|
r = dbus_connection_send_with_reply_and_block (connection, m, -1, &error);
|
|
|
|
|
|
|
|
|
|
if (r != NULL)
|
|
|
|
|
{
|
|
|
|
|
dbus_message_unref (r);
|
|
|
|
|
}
|
|
|
|
|
else if (dbus_error_has_name (&error, DBUS_ERROR_UNKNOWN_INTERFACE))
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "dbus-monitor: unable to enable new-style monitoring, "
|
|
|
|
|
"your dbus-daemon is too old. Falling back to eavesdropping.\n");
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
fprintf (stderr, "dbus-monitor: unable to enable new-style monitoring: "
|
|
|
|
|
"%s: \"%s\". Falling back to eavesdropping.\n",
|
|
|
|
|
error.name, error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
dbus_message_unref (m);
|
|
|
|
|
|
|
|
|
|
return (r != NULL);
|
|
|
|
|
}
|
|
|
|
|
|
2003-04-14 03:25:19 +00:00
|
|
|
int
|
|
|
|
|
main (int argc, char *argv[])
|
|
|
|
|
{
|
|
|
|
|
DBusConnection *connection;
|
|
|
|
|
DBusError error;
|
2003-05-18 02:39:47 +00:00
|
|
|
DBusBusType type = DBUS_BUS_SESSION;
|
2006-11-08 15:49:30 +00:00
|
|
|
DBusHandleMessageFunction filter_func = monitor_filter_func;
|
2009-07-13 14:06:26 -04:00
|
|
|
char *address = NULL;
|
2010-02-12 19:19:05 +00:00
|
|
|
dbus_bool_t seen_bus_type = FALSE;
|
2015-01-23 19:32:31 +00:00
|
|
|
BinaryMode binary_mode = BINARY_MODE_NOT;
|
2005-10-29 17:02:58 +00:00
|
|
|
int i = 0, j = 0, numFilters = 0;
|
|
|
|
|
char **filters = NULL;
|
2009-07-31 12:26:57 -04:00
|
|
|
|
|
|
|
|
/* Set stdout to be unbuffered; this is basically so that if people
|
|
|
|
|
* do dbus-monitor > file, then send SIGINT via Control-C, they
|
|
|
|
|
* don't lose the last chunk of messages.
|
|
|
|
|
*/
|
2010-05-10 22:56:11 +02:00
|
|
|
|
|
|
|
|
#ifdef DBUS_WIN
|
|
|
|
|
setvbuf (stdout, NULL, _IONBF, 0);
|
|
|
|
|
#else
|
2009-07-31 12:26:57 -04:00
|
|
|
setvbuf (stdout, NULL, _IOLBF, 0);
|
2010-05-10 22:56:11 +02:00
|
|
|
#endif
|
2009-07-31 12:26:57 -04:00
|
|
|
|
2003-05-16 16:01:56 +00:00
|
|
|
for (i = 1; i < argc; i++)
|
|
|
|
|
{
|
|
|
|
|
char *arg = argv[i];
|
|
|
|
|
|
2003-05-28 21:26:03 +00:00
|
|
|
if (!strcmp (arg, "--system"))
|
2010-02-12 19:19:05 +00:00
|
|
|
{
|
|
|
|
|
only_one_type (&seen_bus_type, argv[0]);
|
|
|
|
|
type = DBUS_BUS_SYSTEM;
|
|
|
|
|
}
|
2003-06-19 22:22:37 +00:00
|
|
|
else if (!strcmp (arg, "--session"))
|
2010-02-12 19:19:05 +00:00
|
|
|
{
|
|
|
|
|
only_one_type (&seen_bus_type, argv[0]);
|
|
|
|
|
type = DBUS_BUS_SESSION;
|
|
|
|
|
}
|
2009-07-13 14:06:26 -04:00
|
|
|
else if (!strcmp (arg, "--address"))
|
2010-02-12 19:19:05 +00:00
|
|
|
{
|
|
|
|
|
only_one_type (&seen_bus_type, argv[0]);
|
|
|
|
|
|
|
|
|
|
if (i+1 < argc)
|
|
|
|
|
{
|
|
|
|
|
address = argv[i+1];
|
|
|
|
|
i++;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
usage (argv[0], 1);
|
|
|
|
|
}
|
2003-05-16 16:01:56 +00:00
|
|
|
else if (!strcmp (arg, "--help"))
|
2015-02-16 11:52:01 +00:00
|
|
|
usage (argv[0], 0);
|
2006-11-08 15:49:30 +00:00
|
|
|
else if (!strcmp (arg, "--monitor"))
|
2015-01-23 19:32:31 +00:00
|
|
|
{
|
|
|
|
|
filter_func = monitor_filter_func;
|
|
|
|
|
binary_mode = BINARY_MODE_NOT;
|
|
|
|
|
}
|
2006-11-08 15:49:30 +00:00
|
|
|
else if (!strcmp (arg, "--profile"))
|
2015-01-23 19:32:31 +00:00
|
|
|
{
|
|
|
|
|
filter_func = profile_filter_func;
|
|
|
|
|
binary_mode = BINARY_MODE_NOT;
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (arg, "--binary"))
|
|
|
|
|
{
|
|
|
|
|
filter_func = binary_filter_func;
|
|
|
|
|
binary_mode = BINARY_MODE_RAW;
|
|
|
|
|
}
|
|
|
|
|
else if (!strcmp (arg, "--pcap"))
|
|
|
|
|
{
|
|
|
|
|
filter_func = binary_filter_func;
|
|
|
|
|
binary_mode = BINARY_MODE_PCAP;
|
|
|
|
|
}
|
2003-05-16 16:01:56 +00:00
|
|
|
else if (!strcmp (arg, "--"))
|
2015-02-16 11:52:01 +00:00
|
|
|
continue;
|
2003-05-16 16:01:56 +00:00
|
|
|
else if (arg[0] == '-')
|
2015-02-16 11:52:01 +00:00
|
|
|
usage (argv[0], 1);
|
2005-10-29 17:02:58 +00:00
|
|
|
else {
|
2011-07-07 16:10:54 +01:00
|
|
|
unsigned int filter_len;
|
|
|
|
|
numFilters++;
|
|
|
|
|
/* Prepend a rule (and a comma) to enable the monitor to eavesdrop.
|
|
|
|
|
* Prepending allows the user to add eavesdrop=false at command line
|
|
|
|
|
* in order to disable eavesdropping when needed */
|
|
|
|
|
filter_len = strlen (EAVESDROPPING_RULE) + 1 + strlen (arg) + 1;
|
|
|
|
|
|
|
|
|
|
filters = (char **) realloc (filters, numFilters * sizeof (char *));
|
|
|
|
|
if (filters == NULL)
|
2015-02-04 14:18:39 +00:00
|
|
|
tool_oom ("adding a new filter slot");
|
2013-09-16 12:43:40 +01:00
|
|
|
filters[j] = (char *) malloc (filter_len);
|
2011-07-07 16:10:54 +01:00
|
|
|
if (filters[j] == NULL)
|
2015-02-04 14:18:39 +00:00
|
|
|
tool_oom ("adding a new filter");
|
2011-07-07 16:10:54 +01:00
|
|
|
snprintf (filters[j], filter_len, "%s,%s", EAVESDROPPING_RULE, arg);
|
|
|
|
|
j++;
|
2005-10-29 17:02:58 +00:00
|
|
|
}
|
2003-05-16 16:01:56 +00:00
|
|
|
}
|
|
|
|
|
|
2003-04-14 03:25:19 +00:00
|
|
|
dbus_error_init (&error);
|
2009-07-13 14:06:26 -04:00
|
|
|
|
|
|
|
|
if (address != NULL)
|
|
|
|
|
{
|
|
|
|
|
connection = dbus_connection_open (address, &error);
|
|
|
|
|
if (connection)
|
|
|
|
|
{
|
|
|
|
|
if (!dbus_bus_register (connection, &error))
|
2015-02-16 11:52:01 +00:00
|
|
|
{
|
2009-07-13 14:06:26 -04:00
|
|
|
fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
|
2015-02-16 11:52:01 +00:00
|
|
|
address, error.message);
|
2009-07-13 14:06:26 -04:00
|
|
|
dbus_error_free (&error);
|
|
|
|
|
exit (1);
|
2015-02-16 11:52:01 +00:00
|
|
|
}
|
2009-07-13 14:06:26 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
connection = dbus_bus_get (type, &error);
|
2003-04-14 03:25:19 +00:00
|
|
|
if (connection == NULL)
|
|
|
|
|
{
|
2009-07-13 14:06:26 -04:00
|
|
|
const char *where;
|
|
|
|
|
if (address != NULL)
|
|
|
|
|
where = address;
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
switch (type)
|
|
|
|
|
{
|
|
|
|
|
case DBUS_BUS_SYSTEM:
|
|
|
|
|
where = "system bus";
|
|
|
|
|
break;
|
|
|
|
|
case DBUS_BUS_SESSION:
|
|
|
|
|
where = "session bus";
|
|
|
|
|
break;
|
|
|
|
|
default:
|
|
|
|
|
where = "";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
fprintf (stderr, "Failed to open connection to %s: %s\n",
|
|
|
|
|
where,
|
2003-04-14 03:25:19 +00:00
|
|
|
error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-03 13:50:04 +01:00
|
|
|
/* Receive o.fd.Peer messages as normal messages, rather than having
|
|
|
|
|
* libdbus handle them internally, which is the wrong thing for
|
|
|
|
|
* a monitor */
|
|
|
|
|
dbus_connection_set_route_peer_messages (connection, TRUE);
|
|
|
|
|
|
2015-01-23 19:32:31 +00:00
|
|
|
if (!dbus_connection_add_filter (connection, filter_func,
|
|
|
|
|
_DBUS_INT_TO_POINTER (binary_mode), NULL))
|
2015-02-02 20:08:07 +00:00
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Couldn't add filter!\n");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (become_monitor (connection, numFilters,
|
|
|
|
|
(const char * const *) filters))
|
|
|
|
|
{
|
|
|
|
|
/* no more preparation needed */
|
|
|
|
|
}
|
|
|
|
|
else if (numFilters)
|
2005-10-29 17:02:58 +00:00
|
|
|
{
|
2013-10-09 10:44:12 +08:00
|
|
|
size_t offset = 0;
|
2005-10-29 17:02:58 +00:00
|
|
|
for (i = 0; i < j; i++)
|
|
|
|
|
{
|
2013-10-09 10:44:12 +08:00
|
|
|
dbus_bus_add_match (connection, filters[i] + offset, &error);
|
|
|
|
|
if (dbus_error_is_set (&error) && i == 0 && offset == 0)
|
|
|
|
|
{
|
|
|
|
|
/* We might be talking to a pre-1.5.6 dbus-daemon
|
|
|
|
|
* which wouldn't understand eavesdrop=true.
|
|
|
|
|
* If this works, carry on with offset > 0
|
|
|
|
|
* on the remaining iterations. */
|
|
|
|
|
offset = strlen (EAVESDROPPING_RULE) + 1;
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
dbus_bus_add_match (connection, filters[i] + offset, &error);
|
|
|
|
|
}
|
|
|
|
|
|
2015-02-16 11:52:01 +00:00
|
|
|
if (dbus_error_is_set (&error))
|
2005-10-29 17:02:58 +00:00
|
|
|
{
|
|
|
|
|
fprintf (stderr, "Failed to setup match \"%s\": %s\n",
|
|
|
|
|
filters[i], error.message);
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
2013-10-09 10:44:12 +08:00
|
|
|
free(filters[i]);
|
2005-10-29 17:02:58 +00:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
dbus_bus_add_match (connection,
|
2013-10-09 10:44:12 +08:00
|
|
|
EAVESDROPPING_RULE,
|
|
|
|
|
&error);
|
2005-10-29 17:02:58 +00:00
|
|
|
if (dbus_error_is_set (&error))
|
2013-10-09 10:44:12 +08:00
|
|
|
{
|
|
|
|
|
dbus_error_free (&error);
|
|
|
|
|
dbus_bus_add_match (connection,
|
|
|
|
|
"",
|
|
|
|
|
&error);
|
|
|
|
|
if (dbus_error_is_set (&error))
|
|
|
|
|
goto lose;
|
|
|
|
|
}
|
2005-10-29 17:02:58 +00:00
|
|
|
}
|
|
|
|
|
|
2015-01-23 19:32:31 +00:00
|
|
|
switch (binary_mode)
|
|
|
|
|
{
|
|
|
|
|
case BINARY_MODE_NOT:
|
|
|
|
|
case BINARY_MODE_RAW:
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case BINARY_MODE_PCAP:
|
|
|
|
|
{
|
|
|
|
|
/* We're not using libpcap because the file format is simple
|
|
|
|
|
* enough not to need it.
|
|
|
|
|
* http://wiki.wireshark.org/Development/LibpcapFileFormat */
|
|
|
|
|
struct {
|
|
|
|
|
dbus_uint32_t magic;
|
|
|
|
|
dbus_uint16_t major_version;
|
|
|
|
|
dbus_uint16_t minor_version;
|
|
|
|
|
dbus_int32_t timezone;
|
|
|
|
|
dbus_uint32_t precision;
|
|
|
|
|
dbus_uint32_t max_length;
|
|
|
|
|
dbus_uint32_t link_type;
|
|
|
|
|
} header = {
|
|
|
|
|
0xA1B2C3D4U, /* magic number */
|
|
|
|
|
2, 4, /* v2.4 */
|
|
|
|
|
0, /* capture in GMT */
|
|
|
|
|
0, /* no opinion on timestamp precision */
|
|
|
|
|
(1 << 27), /* D-Bus spec says so */
|
|
|
|
|
LINKTYPE_DBUS
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
/* Assert that there is no padding */
|
|
|
|
|
_DBUS_STATIC_ASSERT (sizeof (header) == 24);
|
|
|
|
|
|
|
|
|
|
if (!tool_write_all (STDOUT_FILENO, &header, sizeof (header)))
|
|
|
|
|
{
|
|
|
|
|
perror ("dbus-monitor: write");
|
|
|
|
|
exit (1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
break;
|
|
|
|
|
}
|
2006-11-08 23:10:48 +00:00
|
|
|
|
2009-07-31 12:26:57 -04:00
|
|
|
while (dbus_connection_read_write_dispatch(connection, -1))
|
2006-06-14 14:39:51 +00:00
|
|
|
;
|
2003-04-14 03:25:19 +00:00
|
|
|
exit (0);
|
2004-05-25 18:02:17 +00:00
|
|
|
lose:
|
|
|
|
|
fprintf (stderr, "Error: %s\n", error.message);
|
|
|
|
|
exit (1);
|
2003-04-14 03:25:19 +00:00
|
|
|
}
|
2006-11-08 15:49:30 +00:00
|
|
|
|