28653a
commit 14d0e87d9b8caaa2eca7ca81f1189596671fe4fb
28653a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
28653a
Date:   Wed Sep 12 10:32:05 2018 -0300
28653a
28653a
    posix: Use posix_spawn on popen
28653a
    
28653a
    This patch uses posix_spawn on popen instead of fork and execl.  On Linux
28653a
    this has the advantage of much lower memory consumption (usually 32 Kb
28653a
    minimum for the mmap stack area).
28653a
    
28653a
    Two issues are also fixed with this change:
28653a
    
28653a
      * BZ#17490: although POSIX pthread_atfork description only list 'fork'
28653a
        as the function that should execute the atfork handlers, popen
28653a
        description states that:
28653a
    
28653a
          '[...] shall be *as if* a child process were created within the popen()
28653a
           call using the fork() function [...]'
28653a
    
28653a
        Other libc/system seems to follow the idea atfork handlers should not be
28653a
        executed for popen:
28653a
    
28653a
        libc/system | run atfork handles   | notes
28653a
        ------------|----------------------|---------------------------------------
28653a
        Freebsd     |        no            | uses vfork
28653a
        Solaris 11  |        no            |
28653a
        MacOSX 11   |        no            | implemented through posix_spawn syscall
28653a
        ------------|----------------------|----------------------------------------
28653a
    
28653a
        Similar to posix_spawn and system, popen idea is to spawn a different
28653a
        binary so all the POSIX rationale to run the atfork handlers to avoid
28653a
        internal process inconsistency is not really required and in some cases
28653a
        might be unsafe.
28653a
    
28653a
      * BZ#22834: the described scenario, where the forked process might access
28653a
        invalid memory due an inconsistent state in multithreaded environment,
28653a
        should not happen because posix_spawn does not access the affected
28653a
        data structure (proc_file_chain).
28653a
    
28653a
    Checked on x86_64-linux-gnu and i686-linux-gnu.
28653a
    
28653a
            [BZ #22834]
28653a
            [BZ #17490]
28653a
            * NEWS: Add new semantic for atfork with popen and system.
28653a
            * libio/iopopen.c (_IO_new_proc_open): use posix_spawn instead of
28653a
            fork and execl.
28653a
28653a
diff --git a/libio/iopopen.c b/libio/iopopen.c
28653a
index 2eff45b4c80b5cd6..c768295180fdf809 100644
28653a
--- a/libio/iopopen.c
28653a
+++ b/libio/iopopen.c
28653a
@@ -34,7 +34,8 @@
28653a
 #include <not-cancel.h>
28653a
 #include <sys/types.h>
28653a
 #include <sys/wait.h>
28653a
-#include <kernel-features.h>
28653a
+#include <spawn.h>
28653a
+#include <paths.h>
28653a
 
28653a
 struct _IO_proc_file
28653a
 {
28653a
@@ -59,13 +60,60 @@ unlock (void *not_used)
28653a
 }
28653a
 #endif
28653a
 
28653a
+/* POSIX states popen shall ensure that any streams from previous popen()
28653a
+   calls that remain open in the parent process should be closed in the new
28653a
+   child process.
28653a
+   To avoid a race-condition between checking which file descriptors need to
28653a
+   be close (by transversing the proc_file_chain list) and the insertion of a
28653a
+   new one after a successful posix_spawn this function should be called
28653a
+   with proc_file_chain_lock acquired.  */
28653a
+static bool
28653a
+spawn_process (posix_spawn_file_actions_t *fa, FILE *fp, const char *command,
28653a
+	       int do_cloexec, int pipe_fds[2], int parent_end, int child_end,
28653a
+	       int child_pipe_fd)
28653a
+{
28653a
+
28653a
+  for (struct _IO_proc_file *p = proc_file_chain; p; p = p->next)
28653a
+    {
28653a
+      int fd = _IO_fileno ((FILE *) p);
28653a
+
28653a
+      /* If any stream from previous popen() calls has fileno
28653a
+	 child_pipe_fd, it has been already closed by the adddup2 action
28653a
+	 above.  */
28653a
+      if (fd != child_pipe_fd
28653a
+	  && __posix_spawn_file_actions_addclose (fa, fd) != 0)
28653a
+	return false;
28653a
+    }
28653a
+
28653a
+  if (__posix_spawn (&((_IO_proc_file *) fp)->pid, _PATH_BSHELL, fa, 0,
28653a
+		     (char *const[]){ (char*) "sh", (char*) "-c",
28653a
+		     (char *) command, NULL }, __environ) != 0)
28653a
+    return false;
28653a
+
28653a
+  __close_nocancel (pipe_fds[child_end]);
28653a
+
28653a
+  if (!do_cloexec)
28653a
+    /* Undo the effects of the pipe2 call which set the
28653a
+       close-on-exec flag.  */
28653a
+    __fcntl (pipe_fds[parent_end], F_SETFD, 0);
28653a
+
28653a
+  _IO_fileno (fp) = pipe_fds[parent_end];
28653a
+
28653a
+  ((_IO_proc_file *) fp)->next = proc_file_chain;
28653a
+  proc_file_chain = (_IO_proc_file *) fp;
28653a
+
28653a
+  return true;
28653a
+}
28653a
+
28653a
 FILE *
28653a
 _IO_new_proc_open (FILE *fp, const char *command, const char *mode)
28653a
 {
28653a
   int read_or_write;
28653a
+  /* These are indexes for pipe_fds.  */
28653a
   int parent_end, child_end;
28653a
   int pipe_fds[2];
28653a
-  pid_t child_pid;
28653a
+  int child_pipe_fd;
28653a
+  bool spawn_ok;
28653a
 
28653a
   int do_read = 0;
28653a
   int do_write = 0;
28653a
@@ -108,72 +156,62 @@ _IO_new_proc_open (FILE *fp, const char *command, const char *mode)
28653a
 
28653a
   if (do_read)
28653a
     {
28653a
-      parent_end = pipe_fds[0];
28653a
-      child_end = pipe_fds[1];
28653a
+      parent_end = 0;
28653a
+      child_end = 1;
28653a
       read_or_write = _IO_NO_WRITES;
28653a
+      child_pipe_fd = 1;
28653a
     }
28653a
   else
28653a
     {
28653a
-      parent_end = pipe_fds[1];
28653a
-      child_end = pipe_fds[0];
28653a
+      parent_end = 1;
28653a
+      child_end = 0;
28653a
       read_or_write = _IO_NO_READS;
28653a
+      child_pipe_fd = 0;
28653a
     }
28653a
 
28653a
-  ((_IO_proc_file *) fp)->pid = child_pid = __fork ();
28653a
-  if (child_pid == 0)
28653a
-    {
28653a
-      int child_std_end = do_read ? 1 : 0;
28653a
-      struct _IO_proc_file *p;
28653a
-
28653a
-      if (child_end != child_std_end)
28653a
-	__dup2 (child_end, child_std_end);
28653a
-      else
28653a
-	/* The descriptor is already the one we will use.  But it must
28653a
-	   not be marked close-on-exec.  Undo the effects.  */
28653a
-	__fcntl (child_end, F_SETFD, 0);
28653a
-      /* POSIX.2:  "popen() shall ensure that any streams from previous
28653a
-         popen() calls that remain open in the parent process are closed
28653a
-	 in the new child process." */
28653a
-      for (p = proc_file_chain; p; p = p->next)
28653a
-	{
28653a
-	  int fd = _IO_fileno ((FILE *) p);
28653a
+  posix_spawn_file_actions_t fa;
28653a
+  /* posix_spawn_file_actions_init does not fail.  */
28653a
+  __posix_spawn_file_actions_init (&fa);
28653a
 
28653a
-	  /* If any stream from previous popen() calls has fileno
28653a
-	     child_std_end, it has been already closed by the dup2 syscall
28653a
-	     above.  */
28653a
-	  if (fd != child_std_end)
28653a
-	    __close_nocancel (fd);
28653a
-	}
28653a
-
28653a
-      execl ("/bin/sh", "sh", "-c", command, (char *) 0);
28653a
-      _exit (127);
28653a
-    }
28653a
-  __close_nocancel (child_end);
28653a
-  if (child_pid < 0)
28653a
+  /* The descriptor is already the one the child will use.  In this case
28653a
+     it must be moved to another one otherwise, there is no safe way to
28653a
+     remove the close-on-exec flag in the child without creating a FD leak
28653a
+     race in the parent.  */
28653a
+  if (pipe_fds[child_end] == child_pipe_fd)
28653a
     {
28653a
-      __close_nocancel (parent_end);
28653a
-      return NULL;
28653a
+      int tmp = __fcntl (child_pipe_fd, F_DUPFD_CLOEXEC, 0);
28653a
+      if (tmp < 0)
28653a
+	goto spawn_failure;
28653a
+      __close_nocancel (pipe_fds[child_end]);
28653a
+      pipe_fds[child_end] = tmp;
28653a
     }
28653a
 
28653a
-  if (!do_cloexec)
28653a
-    /* Undo the effects of the pipe2 call which set the
28653a
-       close-on-exec flag.  */
28653a
-    __fcntl (parent_end, F_SETFD, 0);
28653a
+  if (__posix_spawn_file_actions_adddup2 (&fa, pipe_fds[child_end],
28653a
+      child_pipe_fd) != 0)
28653a
+    goto spawn_failure;
28653a
 
28653a
-  _IO_fileno (fp) = parent_end;
28653a
-
28653a
-  /* Link into proc_file_chain. */
28653a
 #ifdef _IO_MTSAFE_IO
28653a
   _IO_cleanup_region_start_noarg (unlock);
28653a
   _IO_lock_lock (proc_file_chain_lock);
28653a
 #endif
28653a
-  ((_IO_proc_file *) fp)->next = proc_file_chain;
28653a
-  proc_file_chain = (_IO_proc_file *) fp;
28653a
+  spawn_ok = spawn_process (&fa, fp, command, do_cloexec, pipe_fds,
28653a
+			    parent_end, child_end, child_pipe_fd);
28653a
 #ifdef _IO_MTSAFE_IO
28653a
   _IO_lock_unlock (proc_file_chain_lock);
28653a
   _IO_cleanup_region_end (0);
28653a
 #endif
28653a
 
28653a
+  __posix_spawn_file_actions_destroy (&fa);
28653a
+
28653a
+  if (!spawn_ok)
28653a
+    {
28653a
+    spawn_failure:
28653a
+      __close_nocancel (pipe_fds[child_end]);
28653a
+      __close_nocancel (pipe_fds[parent_end]);
28653a
+      __set_errno (ENOMEM);
28653a
+      return NULL;
28653a
+    }
28653a
+
28653a
   _IO_mask_flags (fp, read_or_write, _IO_NO_READS|_IO_NO_WRITES);
28653a
   return fp;
28653a
 }