2006-06-14 Ross Burton <ross@openedhand.com>
[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 "dbus-print-message.h"
27
28 static DBusHandlerResult
29 filter_func (DBusConnection     *connection,
30              DBusMessage        *message,
31              void               *user_data)
32 {
33   print_message (message, FALSE);
34   
35   if (dbus_message_is_signal (message,
36                               DBUS_INTERFACE_LOCAL,
37                               "Disconnected"))
38     exit (0);
39   
40   /* Conceptually we want this to be
41    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
42    * some problems.  See bug 1719.
43    */
44   return DBUS_HANDLER_RESULT_HANDLED;
45 }
46
47 static void
48 usage (char *name, int ecode)
49 {
50   fprintf (stderr, "Usage: %s [--system | --session] [watch expressions]\n", name);
51   exit (ecode);
52 }
53
54 int
55 main (int argc, char *argv[])
56 {
57   DBusConnection *connection;
58   DBusError error;
59   DBusBusType type = DBUS_BUS_SESSION;
60
61   int i = 0, j = 0, numFilters = 0;
62   char **filters = NULL;
63   for (i = 1; i < argc; i++)
64     {
65       char *arg = argv[i];
66
67       if (!strcmp (arg, "--system"))
68         type = DBUS_BUS_SYSTEM;
69       else if (!strcmp (arg, "--session"))
70         type = DBUS_BUS_SESSION;
71       else if (!strcmp (arg, "--help"))
72         usage (argv[0], 0);
73       else if (!strcmp (arg, "--"))
74         continue;
75       else if (arg[0] == '-')
76         usage (argv[0], 1);
77       else {
78         numFilters++;
79        filters = (char **)realloc(filters, numFilters * sizeof(char *));
80         filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
81         snprintf(filters[j], strlen(arg) + 1, "%s", arg);
82         j++;
83       }
84     }
85
86   dbus_error_init (&error);
87   connection = dbus_bus_get (type, &error);
88   if (connection == NULL)
89     {
90       fprintf (stderr, "Failed to open connection to %s message bus: %s\n",
91                (type == DBUS_BUS_SYSTEM) ? "system" : "session",
92                error.message);
93       dbus_error_free (&error);
94       exit (1);
95     }
96
97   if (numFilters)
98     {
99       for (i = 0; i < j; i++)
100         {
101           dbus_bus_add_match (connection, filters[i], &error);
102           if (dbus_error_is_set (&error))
103             {
104               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
105                        filters[i], error.message);
106               dbus_error_free (&error);
107               exit (1);
108             }
109           free(filters[i]);
110         }
111     }
112   else
113     {
114       dbus_bus_add_match (connection,
115                           "type='signal'",
116                           &error);
117       if (dbus_error_is_set (&error))
118         goto lose;
119       dbus_bus_add_match (connection,
120                           "type='method_call'",
121                           &error);
122       if (dbus_error_is_set (&error))
123         goto lose;
124       dbus_bus_add_match (connection,
125                           "type='method_return'",
126                           &error);
127       if (dbus_error_is_set (&error))
128         goto lose;
129       dbus_bus_add_match (connection,
130                           "type='error'",
131                           &error);
132       if (dbus_error_is_set (&error))
133         goto lose;
134     }
135
136   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
137     fprintf (stderr, "Couldn't add filter!\n");
138     exit (1);
139   }
140   while (dbus_connection_read_write_dispatch(connection, -1))
141     ;
142   exit (0);
143  lose:
144   fprintf (stderr, "Error: %s\n", error.message);
145   exit (1);
146 }