]> git.ipfire.org Git - thirdparty/gcc.git/blobdiff - libiberty/pex-unix.c
Daily bump.
[thirdparty/gcc.git] / libiberty / pex-unix.c
index ea5ee4c141bbac682e0dd89e7e4ae65e1db45f22..850d344ba5899c3911dabed1dd201e0520479ed9 100644 (file)
@@ -1,7 +1,7 @@
 /* Utilities to execute a program in a subprocess (possibly linked by pipes
    with other subprocesses), and wait for it.  Generic Unix version
    (also used for UWIN and VMS).
-   Copyright (C) 1996-2018 Free Software Foundation, Inc.
+   Copyright (C) 1996-2022 Free Software Foundation, Inc.
 
 This file is part of the libiberty library.
 Libiberty is free software; you can redistribute it and/or
@@ -569,6 +569,38 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
                     int toclose, const char **errmsg, int *err)
 {
   pid_t pid = -1;
+  /* Tuple to communicate error from child to parent.  We can safely
+     transfer string literal pointers as both run with identical
+     address mappings.  */
+  struct fn_err 
+  {
+    const char *fn;
+    int err;
+  };
+  volatile int do_pipe = 0;
+  volatile int pipes[2]; /* [0]:reader,[1]:writer.  */
+#ifdef O_CLOEXEC
+  do_pipe = 1;
+#endif
+  if (do_pipe)
+    {
+#ifdef HAVE_PIPE2
+      if (pipe2 ((int *)pipes, O_CLOEXEC))
+       do_pipe = 0;
+#else
+      if (pipe ((int *)pipes))
+       do_pipe = 0;
+      else
+       {
+         if (fcntl (pipes[1], F_SETFD, FD_CLOEXEC) == -1)
+           {
+             close (pipes[0]);
+             close (pipes[1]);
+             do_pipe = 0;
+           }
+       }
+#endif
+    }
 
   /* We declare these to be volatile to avoid warnings from gcc about
      them being clobbered by vfork.  */
@@ -579,10 +611,9 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
      This clobbers the parent's environ so we need to restore it.
      It would be nice to use one of the exec* functions that takes an
      environment as a parameter, but that may have portability
-     issues.   */
-  char **save_environ = environ;
-
-  const char *bad_fn = NULL;
+     issues.  It is marked volatile so the child doesn't consider it a
+     dead variable and therefore clobber where ever it is stored.  */
+  char **volatile save_environ = environ;
 
   for (retries = 0; retries < 4; ++retries)
     {
@@ -596,78 +627,93 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
   switch (pid)
     {
     case -1:
+      if (do_pipe)
+       {
+         close (pipes[0]);
+         close (pipes[1]);
+       }
       *err = errno;
       *errmsg = VFORK_STRING;
       return (pid_t) -1;
 
     case 0:
       /* Child process.  */
-      if (!bad_fn && in != STDIN_FILE_NO)
-       {
-         if (dup2 (in, STDIN_FILE_NO) < 0)
-           bad_fn = "dup2";
-         else if (close (in) < 0)
-           bad_fn = "close";
-       }
-      if (!bad_fn && out != STDOUT_FILE_NO)
-       {
-         if (dup2 (out, STDOUT_FILE_NO) < 0)
-           bad_fn = "dup2";
-         else if (close (out) < 0)
-           bad_fn = "close";
-       }
-      if (!bad_fn && errdes != STDERR_FILE_NO)
-       {
-         if (dup2 (errdes, STDERR_FILE_NO) < 0)
-           bad_fn = "dup2";
-         else if (close (errdes) < 0)
-           bad_fn = "close";
-       }
-      if (!bad_fn && toclose >= 0)
-       {
-         if (close (toclose) < 0)
-           bad_fn = "close";
-       }
-      if (!bad_fn && (flags & PEX_STDERR_TO_STDOUT) != 0)
-       {
-         if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
-           bad_fn = "dup2";
-       }
-      if (!bad_fn)
-       {
-         if (env)
-           /* NOTE: In a standard vfork implementation this clobbers
-              the parent's copy of environ "too" (in reality there's
-              only one copy).  This is ok as we restore it below.  */
-           environ = (char**) env;
-         if ((flags & PEX_SEARCH) != 0)
-           {
-             execvp (executable, to_ptr32 (argv));
-             bad_fn = "execvp";
-           }
-         else
-           {
-             execv (executable, to_ptr32 (argv));
-             bad_fn = "execv";
-           }
-       }
-
-      /* Something failed, report an error.  We don't use stdio
-        routines, because we might be here due to a vfork call.  */
       {
+       struct fn_err failed;
+       failed.fn = NULL;
+
+       if (do_pipe)
+         close (pipes[0]);
+       if (!failed.fn && in != STDIN_FILE_NO)
+         {
+           if (dup2 (in, STDIN_FILE_NO) < 0)
+             failed.fn = "dup2", failed.err = errno;
+           else if (close (in) < 0)
+             failed.fn = "close", failed.err = errno;
+         }
+       if (!failed.fn && out != STDOUT_FILE_NO)
+         {
+           if (dup2 (out, STDOUT_FILE_NO) < 0)
+             failed.fn = "dup2", failed.err = errno;
+           else if (close (out) < 0)
+             failed.fn = "close", failed.err = errno;
+         }
+       if (!failed.fn && errdes != STDERR_FILE_NO)
+         {
+           if (dup2 (errdes, STDERR_FILE_NO) < 0)
+             failed.fn = "dup2", failed.err = errno;
+           else if (close (errdes) < 0)
+             failed.fn = "close", failed.err = errno;
+         }
+       if (!failed.fn && toclose >= 0)
+         {
+           if (close (toclose) < 0)
+             failed.fn = "close", failed.err = errno;
+         }
+       if (!failed.fn && (flags & PEX_STDERR_TO_STDOUT) != 0)
+         {
+           if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
+             failed.fn = "dup2", failed.err = errno;
+         }
+       if (!failed.fn)
+         {
+           if (env)
+             /* NOTE: In a standard vfork implementation this clobbers
+                the parent's copy of environ "too" (in reality there's
+                only one copy).  This is ok as we restore it below.  */
+             environ = (char**) env;
+           if ((flags & PEX_SEARCH) != 0)
+             {
+               execvp (executable, to_ptr32 (argv));
+               failed.fn = "execvp", failed.err = errno;
+             }
+           else
+             {
+               execv (executable, to_ptr32 (argv));
+               failed.fn = "execv", failed.err = errno;
+             }
+         }
+
+       /* Something failed, report an error.  We don't use stdio
+          routines, because we might be here due to a vfork call.  */
        ssize_t retval = 0;
-       int err = errno;
 
+       if (!do_pipe
+           || write (pipes[1], &failed, sizeof (failed)) != sizeof (failed))
+         {
+           /* The parent will not see our scream above, so write to
+              stdout.  */
 #define writeerr(s) (retval |= write (STDERR_FILE_NO, s, strlen (s)))
-       writeerr (obj->pname);
-       writeerr (": error trying to exec '");
-       writeerr (executable);
-       writeerr ("': ");
-       writeerr (bad_fn);
-       writeerr (": ");
-       writeerr (xstrerror (err));
-       writeerr ("\n");
+           writeerr (obj->pname);
+           writeerr (": error trying to exec '");
+           writeerr (executable);
+           writeerr ("': ");
+           writeerr (failed.fn);
+           writeerr (": ");
+           writeerr (xstrerror (failed.err));
+           writeerr ("\n");
 #undef writeerr
+         }
 
        /* Exit with -2 if the error output failed, too.  */
        _exit (retval < 0 ? -2 : -1);
@@ -677,31 +723,42 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
 
     default:
       /* Parent process.  */
-
-      /* Restore environ.
-        Note that the parent either doesn't run until the child execs/exits
-        (standard vfork behaviour), or if it does run then vfork is behaving
-        more like fork.  In either case we needn't worry about clobbering
-        the child's copy of environ.  */
-      environ = save_environ;
-
-      if (!bad_fn && in != STDIN_FILE_NO)
-       if (close (in) < 0)
-         bad_fn = "close";
-      if (!bad_fn && out != STDOUT_FILE_NO)
-       if (close (out) < 0)
-         bad_fn = "close";
-      if (!bad_fn && errdes != STDERR_FILE_NO)
-       if (close (errdes) < 0)
-         bad_fn = "close";
-
-      if (bad_fn)
-       {
-         *err = errno;
-         *errmsg = bad_fn;
-         return (pid_t) -1;
-       }
-
+      {
+       /* Restore environ.  Note that the parent either doesn't run
+          until the child execs/exits (standard vfork behaviour), or
+          if it does run then vfork is behaving more like fork.  In
+          either case we needn't worry about clobbering the child's
+          copy of environ.  */
+       environ = save_environ;
+
+       struct fn_err failed;
+       failed.fn = NULL;
+       if (do_pipe)
+         {
+           close (pipes[1]);
+           ssize_t len = read (pipes[0], &failed, sizeof (failed));
+           if (len < 0)
+             failed.fn = NULL;
+           close (pipes[0]);
+         }
+
+       if (!failed.fn && in != STDIN_FILE_NO)
+         if (close (in) < 0)
+           failed.fn = "close", failed.err = errno;
+       if (!failed.fn && out != STDOUT_FILE_NO)
+         if (close (out) < 0)
+           failed.fn = "close", failed.err = errno;
+       if (!failed.fn && errdes != STDERR_FILE_NO)
+         if (close (errdes) < 0)
+           failed.fn = "close", failed.err = errno;
+
+       if (failed.fn)
+         {
+           *err = failed.err;
+           *errmsg = failed.fn;
+           return (pid_t) -1;
+         }
+      }
       return pid;
     }
 }