summaryrefslogtreecommitdiffstats
path: root/rtemstoolkit/libiberty/pex-unix.c
diff options
context:
space:
mode:
Diffstat (limited to 'rtemstoolkit/libiberty/pex-unix.c')
-rw-r--r--rtemstoolkit/libiberty/pex-unix.c444
1 files changed, 327 insertions, 117 deletions
diff --git a/rtemstoolkit/libiberty/pex-unix.c b/rtemstoolkit/libiberty/pex-unix.c
index 0b96370..ee52789 100644
--- a/rtemstoolkit/libiberty/pex-unix.c
+++ b/rtemstoolkit/libiberty/pex-unix.c
@@ -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-2017 Free Software Foundation, Inc.
+ Copyright (C) 1996-2023 Free Software Foundation, Inc.
This file is part of the libiberty library.
Libiberty is free software; you can redistribute it and/or
@@ -58,6 +58,9 @@ extern int errno;
#ifdef HAVE_PROCESS_H
#include <process.h>
#endif
+#ifdef HAVE_SPAWN_H
+#include <spawn.h>
+#endif
#ifdef vfork /* Autoconf may define this to fork for us. */
# define VFORK_STRING "fork"
@@ -76,7 +79,7 @@ __attribute__ ((mode (SI)));
typedef __char_ptr32 *__char_ptr_char_ptr32
__attribute__ ((mode (SI)));
-/* Return a 32 bit pointer to an array of 32 bit pointers
+/* Return a 32 bit pointer to an array of 32 bit pointers
given a 64 bit pointer to an array of 64 bit pointers. */
static __char_ptr_char_ptr32
@@ -262,7 +265,7 @@ pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time)
pt.user_seconds = r2.ru_utime.tv_sec - r1.ru_utime.tv_sec;
pt.user_microseconds = r2.ru_utime.tv_usec - r1.ru_utime.tv_usec;
- if (r2.ru_utime.tv_usec < r1.ru_utime.tv_usec)
+ if (pt.user_microseconds < 0)
{
--pt.user_seconds;
pt.user_microseconds += 1000000;
@@ -270,7 +273,7 @@ pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time)
pt.system_seconds = r2.ru_stime.tv_sec - r1.ru_stime.tv_sec;
pt.system_microseconds = r2.ru_stime.tv_usec - r1.ru_stime.tv_usec;
- if (r2.ru_stime.tv_usec < r1.ru_stime.tv_usec)
+ if (pt.system_microseconds < 0)
{
--pt.system_seconds;
pt.system_microseconds += 1000000;
@@ -298,8 +301,6 @@ pex_wait (struct pex_obj *obj, pid_t pid, int *status, struct pex_time *time)
#endif /* ! defined (HAVE_WAITPID) */
#endif /* ! defined (HAVE_WAIT4) */
-static void pex_child_error (struct pex_obj *, const char *, const char *, int)
- ATTRIBUTE_NORETURN;
static int pex_unix_open_read (struct pex_obj *, const char *, int);
static int pex_unix_open_write (struct pex_obj *, const char *, int, int);
static pid_t pex_unix_exec_child (struct pex_obj *, int, const char *,
@@ -366,28 +367,6 @@ pex_unix_close (struct pex_obj *obj ATTRIBUTE_UNUSED, int fd)
return close (fd);
}
-/* Report an error from a child process. We don't use stdio routines,
- because we might be here due to a vfork call. */
-
-static void
-pex_child_error (struct pex_obj *obj, const char *executable,
- const char *errmsg, int err)
-{
- int retval = 0;
-#define writeerr(s) retval |= (write (STDERR_FILE_NO, s, strlen (s)) < 0)
- writeerr (obj->pname);
- writeerr (": error trying to exec '");
- writeerr (executable);
- writeerr ("': ");
- writeerr (errmsg);
- writeerr (": ");
- writeerr (xstrerror (err));
- writeerr ("\n");
-#undef writeerr
- /* Exit with -2 if the error output failed, too. */
- _exit (retval == 0 ? -1 : -2);
-}
-
/* Execute a child. */
#if defined(HAVE_SPAWNVE) && defined(HAVE_SPAWNVPE)
@@ -583,6 +562,171 @@ pex_unix_exec_child (struct pex_obj *obj ATTRIBUTE_UNUSED,
return (pid_t) -1;
}
+#elif defined(HAVE_POSIX_SPAWN) && defined(HAVE_POSIX_SPAWNP)
+/* Implementation of pex->exec_child using posix_spawn. */
+
+static pid_t
+pex_unix_exec_child (struct pex_obj *obj ATTRIBUTE_UNUSED,
+ int flags, const char *executable,
+ char * const * argv, char * const * env,
+ int in, int out, int errdes,
+ int toclose, const char **errmsg, int *err)
+{
+ int ret;
+ pid_t pid = -1;
+ posix_spawnattr_t attr;
+ posix_spawn_file_actions_t actions;
+ int attr_initialized = 0, actions_initialized = 0;
+
+ *err = 0;
+
+ ret = posix_spawnattr_init (&attr);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawnattr_init";
+ goto exit;
+ }
+ attr_initialized = 1;
+
+ /* Use vfork() on glibc <=2.24. */
+#ifdef POSIX_SPAWN_USEVFORK
+ ret = posix_spawnattr_setflags (&attr, POSIX_SPAWN_USEVFORK);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawnattr_setflags";
+ goto exit;
+ }
+#endif
+
+ ret = posix_spawn_file_actions_init (&actions);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_init";
+ goto exit;
+ }
+ actions_initialized = 1;
+
+ if (in != STDIN_FILE_NO)
+ {
+ ret = posix_spawn_file_actions_adddup2 (&actions, in, STDIN_FILE_NO);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_adddup2";
+ goto exit;
+ }
+
+ ret = posix_spawn_file_actions_addclose (&actions, in);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_addclose";
+ goto exit;
+ }
+ }
+
+ if (out != STDOUT_FILE_NO)
+ {
+ ret = posix_spawn_file_actions_adddup2 (&actions, out, STDOUT_FILE_NO);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_adddup2";
+ goto exit;
+ }
+
+ ret = posix_spawn_file_actions_addclose (&actions, out);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_addclose";
+ goto exit;
+ }
+ }
+
+ if (errdes != STDERR_FILE_NO)
+ {
+ ret = posix_spawn_file_actions_adddup2 (&actions, errdes, STDERR_FILE_NO);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_adddup2";
+ goto exit;
+ }
+
+ ret = posix_spawn_file_actions_addclose (&actions, errdes);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_addclose";
+ goto exit;
+ }
+ }
+
+ if (toclose >= 0)
+ {
+ ret = posix_spawn_file_actions_addclose (&actions, toclose);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_addclose";
+ goto exit;
+ }
+ }
+
+ if ((flags & PEX_STDERR_TO_STDOUT) != 0)
+ {
+ ret = posix_spawn_file_actions_adddup2 (&actions, STDOUT_FILE_NO, STDERR_FILE_NO);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn_file_actions_adddup2";
+ goto exit;
+ }
+ }
+
+ if ((flags & PEX_SEARCH) != 0)
+ {
+ ret = posix_spawnp (&pid, executable, &actions, &attr, argv, env ? env : environ);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawnp";
+ goto exit;
+ }
+ }
+ else
+ {
+ ret = posix_spawn (&pid, executable, &actions, &attr, argv, env ? env : environ);
+ if (ret)
+ {
+ *err = ret;
+ *errmsg = "posix_spawn";
+ goto exit;
+ }
+ }
+
+exit:
+ if (actions_initialized)
+ posix_spawn_file_actions_destroy (&actions);
+ if (attr_initialized)
+ posix_spawnattr_destroy (&attr);
+
+ if (!*err && in != STDIN_FILE_NO)
+ if (close (in))
+ *errmsg = "close", *err = errno, pid = -1;
+ if (!*err && out != STDOUT_FILE_NO)
+ if (close (out))
+ *errmsg = "close", *err = errno, pid = -1;
+ if (!*err && errdes != STDERR_FILE_NO)
+ if (close (errdes))
+ *errmsg = "close", *err = errno, pid = -1;
+
+ return pid;
+}
#else
/* Implementation of pex->exec_child using standard vfork + exec. */
@@ -592,21 +736,53 @@ pex_unix_exec_child (struct pex_obj *obj, int flags, const char *executable,
int in, int out, int errdes,
int toclose, const char **errmsg, int *err)
{
- pid_t pid;
+ 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. */
- volatile int sleep_interval;
+ volatile int sleep_interval = 1;
volatile int retries;
/* We vfork and then set environ in the child before calling execvp.
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;
+ environment as a parameter, but that may have portability
+ 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;
- sleep_interval = 1;
- pid = -1;
for (retries = 0; retries < 4; ++retries)
{
pid = vfork ();
@@ -619,104 +795,138 @@ 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 (in != STDIN_FILE_NO)
- {
- if (dup2 (in, STDIN_FILE_NO) < 0)
- pex_child_error (obj, executable, "dup2", errno);
- if (close (in) < 0)
- pex_child_error (obj, executable, "close", errno);
- }
- if (out != STDOUT_FILE_NO)
- {
- if (dup2 (out, STDOUT_FILE_NO) < 0)
- pex_child_error (obj, executable, "dup2", errno);
- if (close (out) < 0)
- pex_child_error (obj, executable, "close", errno);
- }
- if (errdes != STDERR_FILE_NO)
- {
- if (dup2 (errdes, STDERR_FILE_NO) < 0)
- pex_child_error (obj, executable, "dup2", errno);
- if (close (errdes) < 0)
- pex_child_error (obj, executable, "close", errno);
- }
- if (toclose >= 0)
- {
- if (close (toclose) < 0)
- pex_child_error (obj, executable, "close", errno);
- }
- if ((flags & PEX_STDERR_TO_STDOUT) != 0)
- {
- if (dup2 (STDOUT_FILE_NO, STDERR_FILE_NO) < 0)
- pex_child_error (obj, executable, "dup2", errno);
- }
-
- 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));
- pex_child_error (obj, executable, "execvp", errno);
- }
- else
- {
- execv (executable, to_ptr32 (argv));
- pex_child_error (obj, executable, "execv", errno);
- }
+ {
+ 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;
+
+ 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 (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);
+ }
/* NOTREACHED */
return (pid_t) -1;
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 (in != STDIN_FILE_NO)
- {
+ {
+ /* 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)
- {
- *err = errno;
- *errmsg = "close";
- return (pid_t) -1;
- }
- }
- if (out != STDOUT_FILE_NO)
- {
+ failed.fn = "close", failed.err = errno;
+ if (!failed.fn && out != STDOUT_FILE_NO)
if (close (out) < 0)
- {
- *err = errno;
- *errmsg = "close";
- return (pid_t) -1;
- }
- }
- if (errdes != STDERR_FILE_NO)
- {
+ failed.fn = "close", failed.err = errno;
+ if (!failed.fn && errdes != STDERR_FILE_NO)
if (close (errdes) < 0)
- {
- *err = errno;
- *errmsg = "close";
- return (pid_t) -1;
- }
- }
-
+ failed.fn = "close", failed.err = errno;
+
+ if (failed.fn)
+ {
+ *err = failed.err;
+ *errmsg = failed.fn;
+ return (pid_t) -1;
+ }
+ }
return pid;
}
}