Fix spelling/grammatical mistakes detected by Debian's lintian(1)
[platform/upstream/dbus.git] / tools / dbus-monitor.c
1 /* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
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., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  */
21
22 #include <config.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26
27 #ifdef DBUS_WIN
28 #include <winsock2.h>
29 #undef interface
30 #else
31 #include <sys/time.h>
32 #endif
33
34 #include <time.h>
35
36 #include "dbus-print-message.h"
37
38 #ifdef DBUS_WIN
39
40 /* gettimeofday is not defined on windows */
41 #define DBUS_SECONDS_SINCE_1601 11644473600LL
42 #define DBUS_USEC_IN_SEC        1000000LL
43
44 #ifdef DBUS_WINCE
45
46 #ifndef _IOLBF
47 #define _IOLBF 0x40
48 #endif
49 #ifndef _IONBF
50 #define _IONBF 0x04
51 #endif
52
53 void
54 GetSystemTimeAsFileTime (LPFILETIME ftp)
55 {
56   SYSTEMTIME st;
57   GetSystemTime (&st);
58   SystemTimeToFileTime (&st, ftp);
59 }
60 #endif
61
62 static int
63 gettimeofday (struct timeval *__p,
64               void *__t)
65 {
66   union {
67       unsigned long long ns100; /*time since 1 Jan 1601 in 100ns units */
68       FILETIME           ft;
69     } now;
70
71   GetSystemTimeAsFileTime (&now.ft);
72   __p->tv_usec = (long) ((now.ns100 / 10LL) % DBUS_USEC_IN_SEC);
73   __p->tv_sec  = (long)(((now.ns100 / 10LL) / DBUS_SECONDS_SINCE_1601) - DBUS_SECONDS_SINCE_1601);
74
75   return 0;
76 }
77 #endif
78
79 static DBusHandlerResult
80 monitor_filter_func (DBusConnection     *connection,
81                      DBusMessage        *message,
82                      void               *user_data)
83 {
84   print_message (message, FALSE);
85   
86   if (dbus_message_is_signal (message,
87                               DBUS_INTERFACE_LOCAL,
88                               "Disconnected"))
89     exit (0);
90   
91   /* Conceptually we want this to be
92    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
93    * some problems.  See bug 1719.
94    */
95   return DBUS_HANDLER_RESULT_HANDLED;
96 }
97
98 #ifdef __APPLE__
99 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%d"
100 #else
101 #define PROFILE_TIMED_FORMAT "%s\t%lu\t%lu"
102 #endif
103 #define TRAP_NULL_STRING(str) ((str) ? (str) : "<none>")
104
105 typedef enum
106 {
107   PROFILE_ATTRIBUTE_FLAG_SERIAL = 1,
108   PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL = 2,
109   PROFILE_ATTRIBUTE_FLAG_SENDER = 4,
110   PROFILE_ATTRIBUTE_FLAG_DESTINATION = 8,
111   PROFILE_ATTRIBUTE_FLAG_PATH = 16,
112   PROFILE_ATTRIBUTE_FLAG_INTERFACE = 32,
113   PROFILE_ATTRIBUTE_FLAG_MEMBER = 64,
114   PROFILE_ATTRIBUTE_FLAG_ERROR_NAME = 128
115 } ProfileAttributeFlags;
116
117 static void
118 profile_print_with_attrs (const char *type, DBusMessage *message,
119   struct timeval *t, ProfileAttributeFlags attrs)
120 {
121   printf (PROFILE_TIMED_FORMAT, type, t->tv_sec, t->tv_usec);
122
123   if (attrs & PROFILE_ATTRIBUTE_FLAG_SERIAL)
124     printf ("\t%u", dbus_message_get_serial (message));
125
126   if (attrs & PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL)
127     printf ("\t%u", dbus_message_get_reply_serial (message));
128
129   if (attrs & PROFILE_ATTRIBUTE_FLAG_SENDER)
130     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_sender (message)));
131
132   if (attrs & PROFILE_ATTRIBUTE_FLAG_DESTINATION)
133     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_destination (message)));
134
135   if (attrs & PROFILE_ATTRIBUTE_FLAG_PATH)
136     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_path (message)));
137
138   if (attrs & PROFILE_ATTRIBUTE_FLAG_INTERFACE)
139     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_interface (message)));
140
141   if (attrs & PROFILE_ATTRIBUTE_FLAG_MEMBER)
142     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_member (message)));
143
144   if (attrs & PROFILE_ATTRIBUTE_FLAG_ERROR_NAME)
145     printf ("\t%s", TRAP_NULL_STRING (dbus_message_get_error_name (message)));
146
147   printf ("\n");
148 }
149
150 static void
151 print_message_profile (DBusMessage *message)
152 {
153   struct timeval t;
154
155   if (gettimeofday (&t, NULL) < 0)
156     {
157       printf ("un\n");
158       return;
159     }
160
161   switch (dbus_message_get_type (message))
162     {
163       case DBUS_MESSAGE_TYPE_METHOD_CALL:
164         profile_print_with_attrs ("mc", message, &t,
165           PROFILE_ATTRIBUTE_FLAG_SERIAL |
166           PROFILE_ATTRIBUTE_FLAG_SENDER |
167           PROFILE_ATTRIBUTE_FLAG_PATH |
168           PROFILE_ATTRIBUTE_FLAG_INTERFACE |
169           PROFILE_ATTRIBUTE_FLAG_MEMBER);
170         break;
171       case DBUS_MESSAGE_TYPE_METHOD_RETURN:
172         profile_print_with_attrs ("mr", message, &t,
173           PROFILE_ATTRIBUTE_FLAG_SERIAL |
174           PROFILE_ATTRIBUTE_FLAG_DESTINATION |
175           PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
176         break;
177       case DBUS_MESSAGE_TYPE_ERROR:
178         profile_print_with_attrs ("err", message, &t,
179           PROFILE_ATTRIBUTE_FLAG_SERIAL |
180           PROFILE_ATTRIBUTE_FLAG_DESTINATION |
181           PROFILE_ATTRIBUTE_FLAG_REPLY_SERIAL);
182         break;
183       case DBUS_MESSAGE_TYPE_SIGNAL:
184         profile_print_with_attrs ("sig", message, &t,
185           PROFILE_ATTRIBUTE_FLAG_SERIAL |
186           PROFILE_ATTRIBUTE_FLAG_PATH |
187           PROFILE_ATTRIBUTE_FLAG_INTERFACE |
188           PROFILE_ATTRIBUTE_FLAG_MEMBER);
189         break;
190       default:
191         printf (PROFILE_TIMED_FORMAT "\n", "tun", t.tv_sec, t.tv_usec);
192         break;
193     }
194 }
195
196 static DBusHandlerResult
197 profile_filter_func (DBusConnection     *connection,
198                      DBusMessage        *message,
199                      void               *user_data)
200 {
201   print_message_profile (message);
202
203   if (dbus_message_is_signal (message,
204                               DBUS_INTERFACE_LOCAL,
205                               "Disconnected"))
206     exit (0);
207
208   return DBUS_HANDLER_RESULT_HANDLED;
209 }
210
211 static void
212 usage (char *name, int ecode)
213 {
214   fprintf (stderr, "Usage: %s [--system | --session | --address ADDRESS] [--monitor | --profile ] [watch expressions]\n", name);
215   exit (ecode);
216 }
217
218 static void
219 only_one_type (dbus_bool_t *seen_bus_type,
220                char        *name)
221 {
222   if (*seen_bus_type)
223     {
224       fprintf (stderr, "I only support monitoring one bus at a time!\n");
225       usage (name, 1);
226     }
227   else
228     {
229       *seen_bus_type = TRUE;
230     }
231 }
232
233 static dbus_bool_t sigint_received = FALSE;
234
235 static void
236 sigint_handler (int signum)
237 {
238   sigint_received = TRUE;
239 }
240
241 int
242 main (int argc, char *argv[])
243 {
244   DBusConnection *connection;
245   DBusError error;
246   DBusBusType type = DBUS_BUS_SESSION;
247   DBusHandleMessageFunction filter_func = monitor_filter_func;
248   char *address = NULL;
249   dbus_bool_t seen_bus_type = FALSE;
250   
251   int i = 0, j = 0, numFilters = 0;
252   char **filters = NULL;
253
254   /* Set stdout to be unbuffered; this is basically so that if people
255    * do dbus-monitor > file, then send SIGINT via Control-C, they
256    * don't lose the last chunk of messages.
257    */
258
259 #ifdef DBUS_WIN
260   setvbuf (stdout, NULL, _IONBF, 0);
261 #else
262   setvbuf (stdout, NULL, _IOLBF, 0);
263 #endif
264
265   for (i = 1; i < argc; i++)
266     {
267       char *arg = argv[i];
268
269       if (!strcmp (arg, "--system"))
270         {
271           only_one_type (&seen_bus_type, argv[0]);
272           type = DBUS_BUS_SYSTEM;
273         }
274       else if (!strcmp (arg, "--session"))
275         {
276           only_one_type (&seen_bus_type, argv[0]);
277           type = DBUS_BUS_SESSION;
278         }
279       else if (!strcmp (arg, "--address"))
280         {
281           only_one_type (&seen_bus_type, argv[0]);
282
283           if (i+1 < argc)
284             {
285               address = argv[i+1];
286               i++;
287             }
288           else
289             usage (argv[0], 1);
290         }
291       else if (!strcmp (arg, "--help"))
292         usage (argv[0], 0);
293       else if (!strcmp (arg, "--monitor"))
294         filter_func = monitor_filter_func;
295       else if (!strcmp (arg, "--profile"))
296         filter_func = profile_filter_func;
297       else if (!strcmp (arg, "--"))
298         continue;
299       else if (arg[0] == '-')
300         usage (argv[0], 1);
301       else {
302         numFilters++;
303        filters = (char **)realloc(filters, numFilters * sizeof(char *));
304         filters[j] = (char *)malloc((strlen(arg) + 1) * sizeof(char *));
305         snprintf(filters[j], strlen(arg) + 1, "%s", arg);
306         j++;
307       }
308     }
309
310   dbus_error_init (&error);
311   
312   if (address != NULL)
313     {
314       connection = dbus_connection_open (address, &error);
315       if (connection)
316         {
317           if (!dbus_bus_register (connection, &error))
318             {
319               fprintf (stderr, "Failed to register connection to bus at %s: %s\n",
320                        address, error.message);
321               dbus_error_free (&error);
322               exit (1);
323             }
324         }
325     }
326   else
327     connection = dbus_bus_get (type, &error);
328   if (connection == NULL)
329     {
330       const char *where;
331       if (address != NULL)
332         where = address;
333       else
334         {
335           switch (type)
336             {
337             case DBUS_BUS_SYSTEM:
338               where = "system bus";
339               break;
340             case DBUS_BUS_SESSION:
341               where = "session bus";
342               break;
343             default:
344               where = "";
345             }
346         }
347       fprintf (stderr, "Failed to open connection to %s: %s\n",
348                where,
349                error.message);
350       dbus_error_free (&error);
351       exit (1);
352     }
353
354   if (numFilters)
355     {
356       for (i = 0; i < j; i++)
357         {
358           dbus_bus_add_match (connection, filters[i], &error);
359           if (dbus_error_is_set (&error))
360             {
361               fprintf (stderr, "Failed to setup match \"%s\": %s\n",
362                        filters[i], error.message);
363               dbus_error_free (&error);
364               exit (1);
365             }
366           free(filters[i]);
367         }
368     }
369   else
370     {
371       dbus_bus_add_match (connection,
372                           "type='signal'",
373                           &error);
374       if (dbus_error_is_set (&error))
375         goto lose;
376       dbus_bus_add_match (connection,
377                           "type='method_call'",
378                           &error);
379       if (dbus_error_is_set (&error))
380         goto lose;
381       dbus_bus_add_match (connection,
382                           "type='method_return'",
383                           &error);
384       if (dbus_error_is_set (&error))
385         goto lose;
386       dbus_bus_add_match (connection,
387                           "type='error'",
388                           &error);
389       if (dbus_error_is_set (&error))
390         goto lose;
391     }
392
393   if (!dbus_connection_add_filter (connection, filter_func, NULL, NULL)) {
394     fprintf (stderr, "Couldn't add filter!\n");
395     exit (1);
396   }
397
398   while (dbus_connection_read_write_dispatch(connection, -1))
399     ;
400   exit (0);
401  lose:
402   fprintf (stderr, "Error: %s\n", error.message);
403   exit (1);
404 }
405