[glibmm] Avoid compiler warning in resolver example



commit 6978bd38faae5dcb626929334b747f97e644717f
Author: Daniel Elstner <daniel kitta gmail com>
Date:   Sat Feb 27 22:59:15 2010 +0100

    Avoid compiler warning in resolver example
    
    * examples/network/resolver.cc (interrupted): Check the return value
    of the write() call to avoid a warning from GCC.  Save and restore
    the value of errno to make the signal handler reentrant.  Get rid of
    the signal() call to re-establish the default handler, as there is
    not much of a point in doing so for this handler.
    (main): Create the pipe before installing the signal handler.

 ChangeLog                    |   11 +++++++++++
 examples/network/resolver.cc |   26 ++++++++++++++++----------
 2 files changed, 27 insertions(+), 10 deletions(-)
---
diff --git a/ChangeLog b/ChangeLog
index 81986cc..a228e08 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,14 @@
+2010-02-27  Daniel Elstner  <daniel kitta gmail com>
+
+	Avoid compiler warning in resolver example
+
+	* examples/network/resolver.cc (interrupted): Check the return value
+	of the write() call to avoid a warning from GCC.  Save and restore
+	the value of errno to make the signal handler reentrant.  Get rid of
+	the signal() call to re-establish the default handler, as there is
+	not much of a point in doing so for this handler.
+	(main): Create the pipe before installing the signal handler.
+
 2010-02-22  Michael  <space3000 gmail com>
 
 	Gio::MemoryInputStream::add_data(): Minor documentation fix.
diff --git a/examples/network/resolver.cc b/examples/network/resolver.cc
index a77be37..b05200c 100644
--- a/examples/network/resolver.cc
+++ b/examples/network/resolver.cc
@@ -19,14 +19,15 @@
  * Boston, MA 02111-1307, USA.
  */
 
-#include "config.h"
+#include <config.h>
 #include <giomm.h>
 #include <iostream>
 
-#include <signal.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
+#include <cerrno>
+#include <csignal>
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
 #include <unistd.h>
 
 #include <gio/gio.h>
@@ -434,13 +435,15 @@ do_connectable (const std::string& arg, gboolean synchronous)
 }
 
 #ifdef G_OS_UNIX
-static int cancel_fds[2];
+static volatile int cancel_fd;
 
 static void
 interrupted (int /*sig*/)
 {
-  signal (SIGINT, SIG_DFL);
-  write (cancel_fds[1], "x", 1);
+  const int save_errno = errno;
+  while (write(cancel_fd, "", 1) < 0 && errno == EINTR)
+  {}
+  errno = save_errno;
 }
 
 static bool
@@ -494,13 +497,16 @@ main (int argc, char **argv)
     /* Set up cancellation; we want to cancel if the user ^C's the
      * program, but we can't cancel directly from an interrupt.
      */
-    signal (SIGINT, interrupted);
+    int cancel_fds[2];
 
-    if (pipe (cancel_fds) == -1)
+    if (pipe (cancel_fds) < 0)
     {
         perror ("pipe");
         exit (1);
     }
+    cancel_fd = cancel_fds[1];
+    signal (SIGINT, interrupted);
+
     chan = Glib::IOChannel::create_from_fd (cancel_fds[0]);
     Glib::RefPtr<Glib::IOSource> source = chan->create_watch (Glib::IO_IN);
     watch_conn = source->connect (sigc::bind (sigc::ptr_fun (async_cancel), cancellable));



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]