2005-10-29 Robert McQueen <robot101@debian.org>
[platform/upstream/dbus.git] / tools / dbus-monitor.c
1 /* -*- mode: C; c-file-style: "gnu" -*- */
2 /* dbus-monitor.c  Utility program to monitor messages on the bus
3  *
4  * Copyright (C) 2003 Philip Blundell <philb@gnu.org>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <glib.h>
27 #include <dbus/dbus-glib-lowlevel.h>
28 #include "dbus-print-message.h"
29
30 static DBusHandlerResult
31 filter_func (DBusConnection     *connection,
32              DBusMessage        *message,
33              void               *user_data)
34 {
35   print_message (message, FALSE);
36   
37   if (dbus_message_is_signal (message,
38                               DBUS_INTERFACE_LOCAL,
39                               "Disconnected"))
40     exit (0);
41   
42   /* Conceptually we want this to be
43    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
44    * some problems.  See bug 1719.
45    */
46   return DBUS_HANDLER_RESULT_HANDLED;
47 }
48
49 static void
50 usage (char *name, int ecode)
51 {
52   fprintf (stderr, "Usage: %s [--system | --session] [watch expressions]\n", name);
53   exit (ecode);
54 }
55
56 int
57 main (int argc, char *argv[])
58 {
59   DBusConnection *connection;
60   DBusError error;
61   DBusBusType type = DBUS_BUS_SESSION;
62   GMainLoop *loop;
63   int i = 0, j = 0, numFilters = 0;
64   char **filters = NULL;
65   for (i = 1; i < argc; i++)
66     {
67       char *arg = argv[i];
68
69       if (!strcmp (arg, "--system"))
70         type = DBUS_BUS_SYSTEM;
71       else if (!strcmp (arg, "--session"))
72         type = DBUS_BUS_SESSION;
73       else if (!strcmp (arg, "--help"))
74         usage (argv[0], 0);
75       else if (!strcmp (arg, "--"))
76         continue;
77       else if (arg[0] == '-')
78         usage (argv[0], 1);
79       else {
80         numFilters++;
81        filters = (char **)realloc(filters, numFilters * sizeof(char *));
82         filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
83         snprintf(filters[j], strlen(arg) + 1, "%s", arg);
84         j++;
85       }
86     }
87
88   loop = g_main_loop_new (NULL, FALSE);
89
90   dbus_error_init (&error);
91   connection = dbus_bus_get (type, &error);
92   if (connection == NULL)
93     {
94       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
95                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
96                error.message);
97       dbus_error_free (&error);
98       exit (1);
99     }
100
101   dbus_connection_setup_with_g_main (connection, NULL);
102
103   if (numFilters)
104     {
105       for (i = 0; i < j; i++)
106         {
107           dbus_bus_add_match (connection, filters[i], &error);
108           if (dbus_error_is_set (&error))
109             {
110               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
111                        filters[i], error.message);
112               dbus_error_free (&error);
113               exit (1);
114             }
115           free(filters[i]);
116         }
117     }
118   else
119     {
120       dbus_bus_add_match (connection,
121                           "type='signal'",
122                           &error);
123       if (dbus_error_is_set (&error))
124         goto lose;
125       dbus_bus_add_match (connection,
126                           "type='method_call'",
127                           &error);
128       if (dbus_error_is_set (&error))
129         goto lose;
130       dbus_bus_add_match (connection,
131                           "type='method_return'",
132                           &error);
133       if (dbus_error_is_set (&error))
134         goto lose;
135       dbus_bus_add_match (connection,
136                           "type='error'",
137                           &error);
138       if (dbus_error_is_set (&error))
139         goto lose;
140     }
141
142   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
143     fprintf (stderr, "Couldn't add filter!\n");
144     exit (1);
145   }
146
147   g_main_loop_run (loop);
148
149   exit (0);
150  lose:
151   fprintf (stderr, "Error: %s\n", error.message);
152   exit (1);
153 }