File glibc-2.26-Assume-that-pipe2-is-always-available.patch of Package glibc.27000

From 46d8874d5b2fcb7831dd84c5e2f6df51922a7936 Mon Sep 17 00:00:00 2001
From: Florian Weimer <fweimer@redhat.com>
Date: Tue, 18 Apr 2017 14:09:01 +0200
Subject: [PATCH] Assume that pipe2 is always available

The Debian patches for Hurd (which are already required to build
glibc before this commit) contain an implementation of pipe2.
---
 include/unistd.h                          |  1 -
 libio/iopopen.c                           | 80 ++++++++-----------------------
 posix/wordexp.c                           | 32 ++-----------
 socket/have_sock_cloexec.c                |  4 --
 sysdeps/unix/sysv/linux/kernel-features.h |  1 -
 6 files changed, 32 insertions(+), 96 deletions(-)

Index: glibc-2.22/include/unistd.h
===================================================================
--- glibc-2.22.orig/include/unistd.h
+++ glibc-2.22/include/unistd.h
@@ -171,7 +171,6 @@ extern int __libc_pause (void);
 extern int __pause_nocancel (void) attribute_hidden;
 
 extern int __have_sock_cloexec attribute_hidden;
-extern int __have_pipe2 attribute_hidden;
 extern int __have_dup3 attribute_hidden;
 
 extern int __getlogin_r_loginuid (char *name, size_t namesize)
Index: glibc-2.22/libio/iopopen.c
===================================================================
--- glibc-2.22.orig/libio/iopopen.c
+++ glibc-2.22/libio/iopopen.c
@@ -143,29 +143,17 @@ _IO_new_proc_open (fp, command, mode)
   if (_IO_file_is_open (fp))
     return NULL;
 
-#ifdef O_CLOEXEC
-# ifndef __ASSUME_PIPE2
-  if (__have_pipe2 >= 0)
-# endif
-    {
-      int r = __pipe2 (pipe_fds, O_CLOEXEC);
-# ifndef __ASSUME_PIPE2
-      if (__have_pipe2 == 0)
-	__have_pipe2 = r != -1 || errno != ENOSYS ? 1 : -1;
-
-      if (__have_pipe2 > 0)
-# endif
-	if (r < 0)
-	  return NULL;
-    }
-#endif
-#ifndef __ASSUME_PIPE2
-# ifdef O_CLOEXEC
-  if (__have_pipe2 < 0)
-# endif
-    if (__pipe (pipe_fds) < 0)
-      return NULL;
-#endif
+  /* Atomically set the O_CLOEXEC flag for the pipe end used by the
+     child process (to avoid leaking the file descriptor in case of a
+     concurrent fork).  This is later reverted in the child process.
+     When popen returns, the parent pipe end can be O_CLOEXEC or not,
+     depending on the 'e' open mode, but there is only one flag which
+     controls both descriptors.  The parent end is adjusted below,
+     after creating the child process.  (In the child process, the
+     parent end should be closed on execve, so O_CLOEXEC remains set
+     there.)  */
+  if (__pipe2 (pipe_fds, O_CLOEXEC) < 0)
+    return NULL;
 
   if (do_read)
     {
@@ -186,27 +174,13 @@ _IO_new_proc_open (fp, command, mode)
       int child_std_end = do_read ? 1 : 0;
       struct _IO_proc_file *p;
 
-#ifndef __ASSUME_PIPE2
-      /* If we have pipe2 the descriptor is marked for close-on-exec.  */
-      _IO_close (parent_end);
-#endif
       if (child_end != child_std_end)
-	{
-	  _IO_dup2 (child_end, child_std_end);
-#ifndef __ASSUME_PIPE2
-	  _IO_close (child_end);
-#endif
-	}
+	_IO_dup2 (child_end, child_std_end);
 #ifdef O_CLOEXEC
       else
-	{
-	  /* The descriptor is already the one we will use.  But it must
-	     not be marked close-on-exec.  Undo the effects.  */
-# ifndef __ASSUME_PIPE2
-	  if (__have_pipe2 > 0)
-# endif
-	    __fcntl (child_end, F_SETFD, 0);
-	}
+	/* The descriptor is already the one we will use.  But it must
+	   not be marked close-on-exec.  Undo the effects.  */
+	__fcntl (child_end, F_SETFD, 0);
 #endif
       /* POSIX.2:  "popen() shall ensure that any streams from previous
          popen() calls that remain open in the parent process are closed
@@ -232,26 +206,10 @@ _IO_new_proc_open (fp, command, mode)
       return NULL;
     }
 
-  if (do_cloexec)
-    {
-#ifndef __ASSUME_PIPE2
-# ifdef O_CLOEXEC
-      if (__have_pipe2 < 0)
-# endif
-	__fcntl (parent_end, F_SETFD, FD_CLOEXEC);
-#endif
-    }
-  else
-    {
-#ifdef O_CLOEXEC
-      /* Undo the effects of the pipe2 call which set the
-	 close-on-exec flag.  */
-# ifndef __ASSUME_PIPE2
-      if (__have_pipe2 > 0)
-# endif
-	__fcntl (parent_end, F_SETFD, 0);
-#endif
-    }
+  if (!do_cloexec)
+    /* Undo the effects of the pipe2 call which set the
+       close-on-exec flag.  */
+    __fcntl (parent_end, F_SETFD, 0);
 
   _IO_fileno (fp) = parent_end;
 
Index: glibc-2.22/posix/wordexp.c
===================================================================
--- glibc-2.22.orig/posix/wordexp.c
+++ glibc-2.22/posix/wordexp.c
@@ -836,10 +836,7 @@ exec_comm_child (char *comm, int *fildes
     {
 #ifdef O_CLOEXEC
       /* Reset the close-on-exec flag (if necessary).  */
-# ifndef __ASSUME_PIPE2
-      if (__have_pipe2 > 0)
-# endif
-	__fcntl (fildes[1], F_SETFD, 0);
+      __fcntl (fildes[1], F_SETFD, 0);
 #endif
     }
 
@@ -905,31 +902,8 @@ exec_comm (char *comm, char **word, size
   if (!comm || !*comm)
     return 0;
 
-#ifdef O_CLOEXEC
-# ifndef __ASSUME_PIPE2
-  if (__have_pipe2 >= 0)
-# endif
-    {
-      int r = __pipe2 (fildes, O_CLOEXEC);
-# ifndef __ASSUME_PIPE2
-      if (__have_pipe2 == 0)
-	__have_pipe2 = r != -1 || errno != ENOSYS ? 1 : -1;
-
-      if (__have_pipe2 > 0)
-# endif
-	if (r < 0)
-	  /* Bad */
-	  return WRDE_NOSPACE;
-    }
-#endif
-#ifndef __ASSUME_PIPE2
-# ifdef O_CLOEXEC
-  if (__have_pipe2 < 0)
-# endif
-    if (__pipe (fildes) < 0)
-      /* Bad */
-      return WRDE_NOSPACE;
-#endif
+  if (__pipe2 (fildes, O_CLOEXEC) < 0)
+    return WRDE_NOSPACE;
 
  again:
   if ((pid = __fork ()) < 0)
Index: glibc-2.22/socket/have_sock_cloexec.c
===================================================================
--- glibc-2.22.orig/socket/have_sock_cloexec.c
+++ glibc-2.22/socket/have_sock_cloexec.c
@@ -23,10 +23,6 @@
 int __have_sock_cloexec;
 #endif
 
-#if defined O_CLOEXEC && !defined __ASSUME_PIPE2
-int __have_pipe2;
-#endif
-
 #if defined O_CLOEXEC && !defined __ASSUME_DUP3
 int __have_dup3;
 #endif
Index: glibc-2.22/sysdeps/unix/sysv/linux/kernel-features.h
===================================================================
--- glibc-2.22.orig/sysdeps/unix/sysv/linux/kernel-features.h
+++ glibc-2.22/sysdeps/unix/sysv/linux/kernel-features.h
@@ -90,7 +90,6 @@
    2.6.27.  */
 #define __ASSUME_SOCK_CLOEXEC	1
 #define __ASSUME_IN_NONBLOCK	1
-#define __ASSUME_PIPE2		1
 #define __ASSUME_EVENTFD2	1
 #define __ASSUME_SIGNALFD4	1
 #define __ASSUME_DUP3		1
openSUSE Build Service is sponsored by