[PATCH] dbus-monitor: get rid of SIGINT madness

Lennart Poettering mzqohf at 0pointer.de
Mon Jul 27 12:46:25 PDT 2009


The current SIGINT handling of dbus-monitor ain't making too many people
happy since it defers the exit to the next msg received -- which might
be quite some time away often enough.

This patch replaces the SIGINT handling by simply enabling line-buffered
IO for STDOUT so that even if you redirect dbus-monitor into a file no
lines get accidently lost and the effect of C-c is still immediate.

halfline came up with the great idea to use setvbuf here instead of
fflush()ing after each printf().

(Oh and the old signal handler was broken anyway, the flag should have
been of type sigatomic_t and be marked volatile)

---
 tools/dbus-monitor.c |   22 ++++++----------------
 1 files changed, 6 insertions(+), 16 deletions(-)

diff --git a/tools/dbus-monitor.c b/tools/dbus-monitor.c
index cbd7a48..03ad6e5 100644
--- a/tools/dbus-monitor.c
+++ b/tools/dbus-monitor.c
@@ -33,8 +33,6 @@
 
 #include <time.h>
 
-#include <signal.h>
-
 #include "dbus-print-message.h"
 
 #ifdef DBUS_WIN
@@ -66,12 +64,12 @@ monitor_filter_func (DBusConnection     *connection,
 		     void               *user_data)
 {
   print_message (message, FALSE);
-  
+
   if (dbus_message_is_signal (message,
                               DBUS_INTERFACE_LOCAL,
                               "Disconnected"))
     exit (0);
-  
+
   /* Conceptually we want this to be
    * DBUS_HANDLER_RESULT_NOT_YET_HANDLED, but this raises
    * some problems.  See bug 1719.
@@ -195,14 +193,6 @@ usage (char *name, int ecode)
   exit (ecode);
 }
 
-static dbus_bool_t sigint_received = FALSE;
-
-static void
-sigint_handler (int signum)
-{
-  sigint_received = TRUE;
-}
-
 int
 main (int argc, char *argv[])
 {
@@ -213,6 +203,9 @@ main (int argc, char *argv[])
 
   int i = 0, j = 0, numFilters = 0;
   char **filters = NULL;
+
+  setvbuf(stdout, NULL, _IOLBF, 0);
+
   for (i = 1; i < argc; i++)
     {
       char *arg = argv[i];
@@ -296,13 +289,10 @@ main (int argc, char *argv[])
   }
 
   /* we handle SIGINT so exit() is reached and flushes stdout */
-  signal (SIGINT, sigint_handler);
-  while (dbus_connection_read_write_dispatch(connection, -1)
-          && !sigint_received)
+  while (dbus_connection_read_write_dispatch(connection, -1))
     ;
   exit (0);
  lose:
   fprintf (stderr, "Error: %s\n", error.message);
   exit (1);
 }
-
-- 
1.6.3.3



Lennart

-- 
Lennart Poettering                        Red Hat, Inc.
lennart [at] poettering [dot] net
http://0pointer.net/lennart/           GnuPG 0x1A015CC4


More information about the dbus mailing list