[glib/glib-2-20] Use pipe2 when available



commit d5444c9b49e1281a6f5c958da4cce439c5410bb2
Author: Matthias Clasen <mclasen redhat com>
Date:   Fri Jun 19 23:44:29 2009 -0400

    Use pipe2 when available
    
    This avoids a small window for races between pipe and fdset.
    Reported in bug 579933 by Thiago Macieira.

 configure.in |   11 ++---------
 glib/gmain.c |   20 +++++++++++++++-----
 2 files changed, 17 insertions(+), 14 deletions(-)
---
diff --git a/configure.in b/configure.in
index babd07c..78a811f 100644
--- a/configure.in
+++ b/configure.in
@@ -559,15 +559,8 @@ AC_HEADER_STDC
 # Checks for library functions.
 AC_FUNC_VPRINTF
 AC_FUNC_ALLOCA
-AC_CHECK_FUNCS(mmap)
-AC_CHECK_FUNCS(posix_memalign)
-AC_CHECK_FUNCS(memalign)
-AC_CHECK_FUNCS(valloc)
-AC_CHECK_FUNCS(fsync)
-
-AC_CHECK_FUNCS(atexit on_exit)
-
-AC_CHECK_FUNCS(timegm gmtime_r)
+AC_CHECK_FUNCS(mmap posix_memalign memalign valloc fsync pipe2)
+AC_CHECK_FUNCS(atexit on_exit timegm gmtime_r)
 
 AC_CHECK_SIZEOF(char)
 AC_CHECK_SIZEOF(short)
diff --git a/glib/gmain.c b/glib/gmain.c
index 331c0a8..6d6b594 100644
--- a/glib/gmain.c
+++ b/glib/gmain.c
@@ -46,6 +46,8 @@
 #define G_MAIN_POLL_DEBUG
 #endif
 
+#define _GNU_SOURCE  /* for pipe2 */
+
 #include "glib.h"
 #include "gthreadprivate.h"
 #include <signal.h>
@@ -402,12 +404,20 @@ g_main_context_init_pipe (GMainContext *context)
 # ifndef G_OS_WIN32
   if (context->wake_up_pipe[0] != -1)
     return;
-  if (pipe (context->wake_up_pipe) < 0)
-    g_error ("Cannot create pipe main loop wake-up: %s\n",
-	     g_strerror (errno));
+
+#ifdef HAVE_PIPE2
+  /* if this fails, we fall through and try pipe */
+  pipe2 (context->wake_up_pipe, O_CLOEXEC);
+#endif
+  if (context->wake_up_pipe[0] == -1)
+    {
+      if (pipe (context->wake_up_pipe) < 0)
+        g_error ("Cannot create pipe main loop wake-up: %s\n",
+  	         g_strerror (errno));
  
-  fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC);
-  fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC);
+      fcntl (context->wake_up_pipe[0], F_SETFD, FD_CLOEXEC);
+      fcntl (context->wake_up_pipe[1], F_SETFD, FD_CLOEXEC);
+    }
 
   context->wake_up_rec.fd = context->wake_up_pipe[0];
   context->wake_up_rec.events = G_IO_IN;



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