077c9d
This patch is a rework of the following upstream patch:
077c9d
077c9d
commit 0e169691290a6d2187a4ff41495fc5678cbfdcdc
077c9d
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
077c9d
Date:   Fri Apr 12 17:39:53 2019 -0300
077c9d
077c9d
    support: Add support_capture_subprogram
077c9d
077c9d
    Its API is similar to support_capture_subprocess, but rather creates a
077c9d
    new process based on the input path and arguments.  Under the hoods it
077c9d
    uses posix_spawn to create the new process.
077c9d
077c9d
    It also allows the use of other support_capture_* functions to check
077c9d
    for expected results and free the resources.
077c9d
077c9d
    Checked on x86_64-linux-gnu.
077c9d
077c9d
        * support/Makefile (libsupport-routines): Add support_subprocess,
077c9d
        xposix_spawn, xposix_spawn_file_actions_addclose, and
077c9d
        xposix_spawn_file_actions_adddup2.
077c9d
        (tst-support_capture_subprocess-ARGS): New rule.
077c9d
        * support/capture_subprocess.h (support_capture_subprogram): New
077c9d
        prototype.
077c9d
        * support/support_capture_subprocess.c (support_capture_subprocess):
077c9d
        Refactor to use support_subprocess and support_capture_poll.
077c9d
        (support_capture_subprogram): New function.
077c9d
        * support/tst-support_capture_subprocess.c (write_mode_to_str,
077c9d
        str_to_write_mode, test_common, parse_int, handle_restart,
077c9d
        do_subprocess, do_subprogram, do_multiple_tests): New functions.
077c9d
        (do_test): Add support_capture_subprogram tests.
077c9d
        * support/subprocess.h: New file.
077c9d
        * support/support_subprocess.c: Likewise.
077c9d
        * support/xposix_spawn.c: Likewise.
077c9d
        * support/xposix_spawn_file_actions_addclose.c: Likewise.
077c9d
        * support/xposix_spawn_file_actions_adddup2.c: Likewise.
077c9d
        * support/xspawn.h: Likewise.
077c9d
077c9d
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
077c9d
077c9d
077c9d
077c9d
diff -Nrup a/support/capture_subprocess.h b/support/capture_subprocess.h
077c9d
--- a/support/capture_subprocess.h	2018-08-01 01:10:47.000000000 -0400
077c9d
+++ b/support/capture_subprocess.h	2019-05-16 23:37:19.903845257 -0400
077c9d
@@ -35,6 +35,12 @@ struct support_capture_subprocess
077c9d
 struct support_capture_subprocess support_capture_subprocess
077c9d
   (void (*callback) (void *), void *closure);
077c9d
 
077c9d
+/* Issue FILE with ARGV arguments by using posix_spawn and capture standard
077c9d
+   output, standard error, and the exit status.  The out.buffer and err.buffer
077c9d
+   are handle as support_capture_subprocess.  */
077c9d
+struct support_capture_subprocess support_capture_subprogram
077c9d
+  (const char *file, char *const argv[]);
077c9d
+
077c9d
 /* Deallocate the subprocess data captured by
077c9d
    support_capture_subprocess.  */
077c9d
 void support_capture_subprocess_free (struct support_capture_subprocess *);
077c9d
diff -Nrup a/support/Makefile b/support/Makefile
077c9d
--- a/support/Makefile	2019-05-16 23:36:51.066665814 -0400
077c9d
+++ b/support/Makefile	2019-05-17 10:31:24.268313761 -0400
077c9d
@@ -63,6 +63,7 @@ libsupport-routines = \
077c9d
   support_record_failure \
077c9d
   support_run_diff \
077c9d
   support_shared_allocate \
077c9d
+  support_subprocess \
077c9d
   support_test_compare_blob \
077c9d
   support_test_compare_failure \
077c9d
   support_test_compare_string \
077c9d
@@ -147,6 +148,9 @@ libsupport-routines = \
077c9d
   xsigaction \
077c9d
   xsignal \
077c9d
   xsocket \
077c9d
+  xposix_spawn \
077c9d
+  xposix_spawn_file_actions_addclose \
077c9d
+  xposix_spawn_file_actions_adddup2 \
077c9d
   xstrdup \
077c9d
   xstrndup \
077c9d
   xsymlink \
077c9d
@@ -221,4 +225,6 @@ endif
077c9d
 
077c9d
 $(objpfx)tst-support_format_dns_packet: $(common-objpfx)resolv/libresolv.so
077c9d
 
077c9d
+tst-support_capture_subprocess-ARGS = -- $(host-test-program-cmd)
077c9d
+
077c9d
 include ../Rules
077c9d
diff -Nrup a/support/subprocess.h b/support/subprocess.h
077c9d
--- a/support/subprocess.h	1969-12-31 19:00:00.000000000 -0500
077c9d
+++ b/support/subprocess.h	2019-05-16 23:37:19.903845257 -0400
077c9d
@@ -0,0 +1,49 @@
077c9d
+/* Create a subprocess.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#ifndef SUPPORT_SUBPROCESS_H
077c9d
+#define SUPPORT_SUBPROCESS_H
077c9d
+
077c9d
+#include <sys/types.h>
077c9d
+
077c9d
+struct support_subprocess
077c9d
+{
077c9d
+  int stdout_pipe[2];
077c9d
+  int stderr_pipe[2];
077c9d
+  pid_t pid;
077c9d
+};
077c9d
+
077c9d
+/* Invoke CALLBACK (CLOSURE) in a subprocess created with fork and return
077c9d
+   its PID, a pipe redirected to STDOUT, and a pipe redirected to STDERR.  */
077c9d
+struct support_subprocess support_subprocess
077c9d
+  (void (*callback) (void *), void *closure);
077c9d
+
077c9d
+/* Issue FILE with ARGV arguments by using posix_spawn and return is PID, a
077c9d
+   pipe redirected to STDOUT, and a pipe redirected to STDERR.  */
077c9d
+struct support_subprocess support_subprogram
077c9d
+  (const char *file, char *const argv[]);
077c9d
+
077c9d
+/* Wait for the subprocess indicated by PROC::PID.  Return the status
077c9d
+   indicate by waitpid call.  */
077c9d
+int support_process_wait (struct support_subprocess *proc);
077c9d
+
077c9d
+/* Terminate the subprocess indicated by PROC::PID, first with a SIGTERM and
077c9d
+   then with a SIGKILL.  Return the status as for waitpid call.  */
077c9d
+int support_process_terminate (struct support_subprocess *proc);
077c9d
+
077c9d
+#endif
077c9d
diff -Nrup a/support/support_capture_subprocess.c b/support/support_capture_subprocess.c
077c9d
--- a/support/support_capture_subprocess.c	2019-05-16 23:36:50.672677025 -0400
077c9d
+++ b/support/support_capture_subprocess.c	2019-05-16 23:38:34.298728367 -0400
077c9d
@@ -16,6 +16,7 @@
077c9d
    License along with the GNU C Library; if not, see
077c9d
    <http://www.gnu.org/licenses/>.  */
077c9d
 
077c9d
+#include <support/subprocess.h>
077c9d
 #include <support/capture_subprocess.h>
077c9d
 
077c9d
 #include <errno.h>
077c9d
@@ -23,6 +24,7 @@
077c9d
 #include <support/check.h>
077c9d
 #include <support/xunistd.h>
077c9d
 #include <support/xsocket.h>
077c9d
+#include <support/xspawn.h>
077c9d
 
077c9d
 static void
077c9d
 transfer (const char *what, struct pollfd *pfd, struct xmemstream *stream)
077c9d
@@ -50,59 +52,52 @@ transfer (const char *what, struct pollf
077c9d
     }
077c9d
 }
077c9d
 
077c9d
-struct support_capture_subprocess
077c9d
-support_capture_subprocess (void (*callback) (void *), void *closure)
077c9d
+static void
077c9d
+support_capture_poll (struct support_capture_subprocess *result,
077c9d
+                     struct support_subprocess *proc)
077c9d
 {
077c9d
-  struct support_capture_subprocess result;
077c9d
-  xopen_memstream (&result.out);
077c9d
-  xopen_memstream (&result.err);
077c9d
-
077c9d
-  int stdout_pipe[2];
077c9d
-  xpipe (stdout_pipe);
077c9d
-  TEST_VERIFY (stdout_pipe[0] > STDERR_FILENO);
077c9d
-  TEST_VERIFY (stdout_pipe[1] > STDERR_FILENO);
077c9d
-  int stderr_pipe[2];
077c9d
-  xpipe (stderr_pipe);
077c9d
-  TEST_VERIFY (stderr_pipe[0] > STDERR_FILENO);
077c9d
-  TEST_VERIFY (stderr_pipe[1] > STDERR_FILENO);
077c9d
-
077c9d
-  TEST_VERIFY (fflush (stdout) == 0);
077c9d
-  TEST_VERIFY (fflush (stderr) == 0);
077c9d
-
077c9d
-  pid_t pid = xfork ();
077c9d
-  if (pid == 0)
077c9d
-    {
077c9d
-      xclose (stdout_pipe[0]);
077c9d
-      xclose (stderr_pipe[0]);
077c9d
-      xdup2 (stdout_pipe[1], STDOUT_FILENO);
077c9d
-      xdup2 (stderr_pipe[1], STDERR_FILENO);
077c9d
-      xclose (stdout_pipe[1]);
077c9d
-      xclose (stderr_pipe[1]);
077c9d
-      callback (closure);
077c9d
-      _exit (0);
077c9d
-    }
077c9d
-  xclose (stdout_pipe[1]);
077c9d
-  xclose (stderr_pipe[1]);
077c9d
-
077c9d
   struct pollfd fds[2] =
077c9d
     {
077c9d
-      { .fd = stdout_pipe[0], .events = POLLIN },
077c9d
-      { .fd = stderr_pipe[0], .events = POLLIN },
077c9d
+      { .fd = proc->stdout_pipe[0], .events = POLLIN },
077c9d
+      { .fd = proc->stderr_pipe[0], .events = POLLIN },
077c9d
     };
077c9d
 
077c9d
   do
077c9d
     {
077c9d
       xpoll (fds, 2, -1);
077c9d
-      transfer ("stdout", &fds[0], &result.out);
077c9d
-      transfer ("stderr", &fds[1], &result.err);
077c9d
+      transfer ("stdout", &fds[0], &result->out);
077c9d
+      transfer ("stderr", &fds[1], &result->err);
077c9d
     }
077c9d
   while (fds[0].events != 0 || fds[1].events != 0);
077c9d
-  xclose (stdout_pipe[0]);
077c9d
-  xclose (stderr_pipe[0]);
077c9d
+  xfclose_memstream (&result->out);
077c9d
+  xfclose_memstream (&result->err);
077c9d
+
077c9d
+  result->status = support_process_wait (proc);
077c9d
+}
077c9d
+
077c9d
+struct support_capture_subprocess
077c9d
+support_capture_subprocess (void (*callback) (void *), void *closure)
077c9d
+{
077c9d
+  struct support_capture_subprocess result;
077c9d
+  xopen_memstream (&result.out);
077c9d
+  xopen_memstream (&result.err);
077c9d
+
077c9d
+  struct support_subprocess proc = support_subprocess (callback, closure);
077c9d
+
077c9d
+  support_capture_poll (&result, &proc;;
077c9d
+  return result;
077c9d
+}
077c9d
+
077c9d
+struct support_capture_subprocess
077c9d
+support_capture_subprogram (const char *file, char *const argv[])
077c9d
+{
077c9d
+  struct support_capture_subprocess result;
077c9d
+  xopen_memstream (&result.out);
077c9d
+  xopen_memstream (&result.err);
077c9d
+
077c9d
+  struct support_subprocess proc = support_subprogram (file, argv);
077c9d
 
077c9d
-  xfclose_memstream (&result.out);
077c9d
-  xfclose_memstream (&result.err);
077c9d
-  xwaitpid (pid, &result.status, 0);
077c9d
+  support_capture_poll (&result, &proc;;
077c9d
   return result;
077c9d
 }
077c9d
 
077c9d
diff -Nrup a/support/support_subprocess.c b/support/support_subprocess.c
077c9d
--- a/support/support_subprocess.c	1969-12-31 19:00:00.000000000 -0500
077c9d
+++ b/support/support_subprocess.c	2019-05-16 23:37:19.903845257 -0400
077c9d
@@ -0,0 +1,152 @@
077c9d
+/* Create subprocess.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <stdio.h>
077c9d
+#include <signal.h>
077c9d
+#include <time.h>
077c9d
+#include <sys/wait.h>
077c9d
+#include <stdbool.h>
077c9d
+#include <support/xspawn.h>
077c9d
+#include <support/check.h>
077c9d
+#include <support/xunistd.h>
077c9d
+#include <support/subprocess.h>
077c9d
+
077c9d
+static struct support_subprocess
077c9d
+support_suprocess_init (void)
077c9d
+{
077c9d
+  struct support_subprocess result;
077c9d
+
077c9d
+  xpipe (result.stdout_pipe);
077c9d
+  TEST_VERIFY (result.stdout_pipe[0] > STDERR_FILENO);
077c9d
+  TEST_VERIFY (result.stdout_pipe[1] > STDERR_FILENO);
077c9d
+
077c9d
+  xpipe (result.stderr_pipe);
077c9d
+  TEST_VERIFY (result.stderr_pipe[0] > STDERR_FILENO);
077c9d
+  TEST_VERIFY (result.stderr_pipe[1] > STDERR_FILENO);
077c9d
+
077c9d
+  TEST_VERIFY (fflush (stdout) == 0);
077c9d
+  TEST_VERIFY (fflush (stderr) == 0);
077c9d
+
077c9d
+  return result;
077c9d
+}
077c9d
+
077c9d
+struct support_subprocess
077c9d
+support_subprocess (void (*callback) (void *), void *closure)
077c9d
+{
077c9d
+  struct support_subprocess result = support_suprocess_init ();
077c9d
+
077c9d
+  result.pid = xfork ();
077c9d
+  if (result.pid == 0)
077c9d
+    {
077c9d
+      xclose (result.stdout_pipe[0]);
077c9d
+      xclose (result.stderr_pipe[0]);
077c9d
+      xdup2 (result.stdout_pipe[1], STDOUT_FILENO);
077c9d
+      xdup2 (result.stderr_pipe[1], STDERR_FILENO);
077c9d
+      xclose (result.stdout_pipe[1]);
077c9d
+      xclose (result.stderr_pipe[1]);
077c9d
+      callback (closure);
077c9d
+     _exit (0);
077c9d
+    }
077c9d
+  xclose (result.stdout_pipe[1]);
077c9d
+  xclose (result.stderr_pipe[1]);
077c9d
+
077c9d
+  return result;
077c9d
+}
077c9d
+
077c9d
+struct support_subprocess
077c9d
+support_subprogram (const char *file, char *const argv[])
077c9d
+{
077c9d
+  struct support_subprocess result = support_suprocess_init ();
077c9d
+
077c9d
+  posix_spawn_file_actions_t fa;
077c9d
+  /* posix_spawn_file_actions_init does not fail.  */
077c9d
+  posix_spawn_file_actions_init (&fa);
077c9d
+
077c9d
+  xposix_spawn_file_actions_addclose (&fa, result.stdout_pipe[0]);
077c9d
+  xposix_spawn_file_actions_addclose (&fa, result.stderr_pipe[0]);
077c9d
+  xposix_spawn_file_actions_adddup2 (&fa, result.stdout_pipe[1], STDOUT_FILENO);
077c9d
+  xposix_spawn_file_actions_adddup2 (&fa, result.stderr_pipe[1], STDERR_FILENO);
077c9d
+  xposix_spawn_file_actions_addclose (&fa, result.stdout_pipe[1]);
077c9d
+  xposix_spawn_file_actions_addclose (&fa, result.stderr_pipe[1]);
077c9d
+
077c9d
+  result.pid = xposix_spawn (file, &fa, NULL, argv, NULL);
077c9d
+
077c9d
+  xclose (result.stdout_pipe[1]);
077c9d
+  xclose (result.stderr_pipe[1]);
077c9d
+
077c9d
+  return result;
077c9d
+}
077c9d
+
077c9d
+int
077c9d
+support_process_wait (struct support_subprocess *proc)
077c9d
+{
077c9d
+  xclose (proc->stdout_pipe[0]);
077c9d
+  xclose (proc->stderr_pipe[0]);
077c9d
+
077c9d
+  int status;
077c9d
+  xwaitpid (proc->pid, &status, 0);
077c9d
+  return status;
077c9d
+}
077c9d
+
077c9d
+
077c9d
+static bool
077c9d
+support_process_kill (int pid, int signo, int *status)
077c9d
+{
077c9d
+  /* Kill the whole process group.  */
077c9d
+  kill (-pid, signo);
077c9d
+  /* In case setpgid failed in the child, kill it individually too.  */
077c9d
+  kill (pid, signo);
077c9d
+
077c9d
+  /* Wait for it to terminate.  */
077c9d
+  pid_t killed;
077c9d
+  for (int i = 0; i < 5; ++i)
077c9d
+    {
077c9d
+      int status;
077c9d
+      killed = xwaitpid (pid, &status, WNOHANG|WUNTRACED);
077c9d
+      if (killed != 0)
077c9d
+        break;
077c9d
+
077c9d
+      /* Delay, give the system time to process the kill.  If the
077c9d
+         nanosleep() call return prematurely, all the better.  We
077c9d
+         won't restart it since this probably means the child process
077c9d
+         finally died.  */
077c9d
+      nanosleep (&((struct timespec) { 0, 100000000 }), NULL);
077c9d
+    }
077c9d
+  if (killed != 0 && killed != pid)
077c9d
+    return false;
077c9d
+
077c9d
+  return true;
077c9d
+}
077c9d
+
077c9d
+int
077c9d
+support_process_terminate (struct support_subprocess *proc)
077c9d
+{
077c9d
+  xclose (proc->stdout_pipe[0]);
077c9d
+  xclose (proc->stderr_pipe[0]);
077c9d
+
077c9d
+  int status;
077c9d
+  pid_t killed = xwaitpid (proc->pid, &status, WNOHANG|WUNTRACED);
077c9d
+  if (killed != 0 && killed == proc->pid)
077c9d
+    return status;
077c9d
+
077c9d
+  /* Subprocess is still running, terminate it.  */
077c9d
+  if (!support_process_kill (proc->pid, SIGTERM, &status) )
077c9d
+    support_process_kill (proc->pid, SIGKILL, &status);
077c9d
+
077c9d
+  return status;
077c9d
+}
077c9d
diff -Nrup a/support/tst-support_capture_subprocess.c b/support/tst-support_capture_subprocess.c
077c9d
--- a/support/tst-support_capture_subprocess.c	2018-08-01 01:10:47.000000000 -0400
077c9d
+++ b/support/tst-support_capture_subprocess.c	2019-05-16 23:37:19.904845228 -0400
077c9d
@@ -23,8 +23,20 @@
077c9d
 #include <support/capture_subprocess.h>
077c9d
 #include <support/check.h>
077c9d
 #include <support/support.h>
077c9d
+#include <support/temp_file.h>
077c9d
 #include <sys/wait.h>
077c9d
 #include <unistd.h>
077c9d
+#include <paths.h>
077c9d
+#include <getopt.h>
077c9d
+#include <limits.h>
077c9d
+#include <errno.h>
077c9d
+#include <array_length.h>
077c9d
+
077c9d
+/* Nonzero if the program gets called via 'exec'.  */
077c9d
+static int restart;
077c9d
+
077c9d
+/* Hold the four initial argument used to respawn the process.  */
077c9d
+static char *initial_argv[5];
077c9d
 
077c9d
 /* Write one byte at *P to FD and advance *P.  Do nothing if *P is
077c9d
    '\0'.  */
077c9d
@@ -42,6 +54,30 @@ transfer (const unsigned char **p, int f
077c9d
 enum write_mode { out_first, err_first, interleave,
077c9d
                   write_mode_last =  interleave };
077c9d
 
077c9d
+static const char *
077c9d
+write_mode_to_str (enum write_mode mode)
077c9d
+{
077c9d
+  switch (mode)
077c9d
+    {
077c9d
+    case out_first:  return "out_first";
077c9d
+    case err_first:  return "err_first";
077c9d
+    case interleave: return "interleave";
077c9d
+    default:         return "write_mode_last";
077c9d
+    }
077c9d
+}
077c9d
+
077c9d
+static enum write_mode
077c9d
+str_to_write_mode (const char *mode)
077c9d
+{
077c9d
+  if (strcmp (mode, "out_first") == 0)
077c9d
+    return out_first;
077c9d
+  else if (strcmp (mode, "err_first") == 0)
077c9d
+    return err_first;
077c9d
+  else if (strcmp (mode, "interleave") == 0)
077c9d
+    return interleave;
077c9d
+  return write_mode_last;
077c9d
+}
077c9d
+
077c9d
 /* Describe what to write in the subprocess.  */
077c9d
 struct test
077c9d
 {
077c9d
@@ -52,11 +88,9 @@ struct test
077c9d
   int status;
077c9d
 };
077c9d
 
077c9d
-/* For use with support_capture_subprocess.  */
077c9d
-static void
077c9d
-callback (void *closure)
077c9d
+_Noreturn static void
077c9d
+test_common (const struct test *test)
077c9d
 {
077c9d
-  const struct test *test = closure;
077c9d
   bool mode_ok = false;
077c9d
   switch (test->write_mode)
077c9d
     {
077c9d
@@ -95,6 +129,40 @@ callback (void *closure)
077c9d
   exit (test->status);
077c9d
 }
077c9d
 
077c9d
+static int
077c9d
+parse_int (const char *str)
077c9d
+{
077c9d
+  char *endptr;
077c9d
+  long int ret = strtol (str, &endptr, 10);
077c9d
+  TEST_COMPARE (errno, 0);
077c9d
+  TEST_VERIFY (ret >= 0 && ret <= INT_MAX);
077c9d
+  return ret;
077c9d
+}
077c9d
+
077c9d
+/* For use with support_capture_subprogram.  */
077c9d
+_Noreturn static void
077c9d
+handle_restart (char *out, char *err, const char *write_mode,
077c9d
+               const char *signal, const char *status)
077c9d
+{
077c9d
+  struct test test =
077c9d
+    {
077c9d
+      out,
077c9d
+      err,
077c9d
+      str_to_write_mode (write_mode),
077c9d
+      parse_int (signal),
077c9d
+      parse_int (status)
077c9d
+    };
077c9d
+  test_common (&test);
077c9d
+}
077c9d
+
077c9d
+/* For use with support_capture_subprocess.  */
077c9d
+_Noreturn static void
077c9d
+callback (void *closure)
077c9d
+{
077c9d
+  const struct test *test = closure;
077c9d
+  test_common (test);
077c9d
+}
077c9d
+
077c9d
 /* Create a heap-allocated random string of letters.  */
077c9d
 static char *
077c9d
 random_string (size_t length)
077c9d
@@ -130,12 +198,59 @@ check_stream (const char *what, const st
077c9d
     }
077c9d
 }
077c9d
 
077c9d
+static struct support_capture_subprocess
077c9d
+do_subprocess (struct test *test)
077c9d
+{
077c9d
+  return support_capture_subprocess (callback, test);
077c9d
+}
077c9d
+
077c9d
+static struct support_capture_subprocess
077c9d
+do_subprogram (const struct test *test)
077c9d
+{
077c9d
+  /* Three digits per byte plus null terminator.  */
077c9d
+  char signalstr[3 * sizeof(int) + 1];
077c9d
+  snprintf (signalstr, sizeof (signalstr), "%d", test->signal);
077c9d
+  char statusstr[3 * sizeof(int) + 1];
077c9d
+  snprintf (statusstr, sizeof (statusstr), "%d", test->status);
077c9d
+
077c9d
+  int argc = 0;
077c9d
+  enum {
077c9d
+    /* 4 elements from initial_argv (path to ld.so, '--library-path', the
077c9d
+       path', and application name'), 2 for restart argument ('--direct',
077c9d
+       '--restart'), 5 arguments plus NULL.  */
077c9d
+    argv_size = 12
077c9d
+  };
077c9d
+  char *args[argv_size];
077c9d
+
077c9d
+  for (char **arg = initial_argv; *arg != NULL; arg++)
077c9d
+    args[argc++] = *arg;
077c9d
+
077c9d
+  args[argc++] = (char*) "--direct";
077c9d
+  args[argc++] = (char*) "--restart";
077c9d
+
077c9d
+  args[argc++] = test->out;
077c9d
+  args[argc++] = test->err;
077c9d
+  args[argc++] = (char*) write_mode_to_str (test->write_mode);
077c9d
+  args[argc++] = signalstr;
077c9d
+  args[argc++] = statusstr;
077c9d
+  args[argc]   = NULL;
077c9d
+  TEST_VERIFY (argc < argv_size);
077c9d
+
077c9d
+  return support_capture_subprogram (args[0], args);
077c9d
+}
077c9d
+
077c9d
+enum test_type
077c9d
+{
077c9d
+  subprocess,
077c9d
+  subprogram,
077c9d
+};
077c9d
+
077c9d
 static int
077c9d
-do_test (void)
077c9d
+do_multiple_tests (enum test_type type)
077c9d
 {
077c9d
   const int lengths[] = {0, 1, 17, 512, 20000, -1};
077c9d
 
077c9d
-  /* Test multiple combinations of support_capture_subprocess.
077c9d
+  /* Test multiple combinations of support_capture_sub{process,program}.
077c9d
 
077c9d
      length_idx_stdout: Index into the lengths array above,
077c9d
        controls how many bytes are written by the subprocess to
077c9d
@@ -164,8 +279,10 @@ do_test (void)
077c9d
               TEST_VERIFY (strlen (test.out) == lengths[length_idx_stdout]);
077c9d
               TEST_VERIFY (strlen (test.err) == lengths[length_idx_stderr]);
077c9d
 
077c9d
-              struct support_capture_subprocess result
077c9d
-                = support_capture_subprocess (callback, &test);
077c9d
+             struct support_capture_subprocess result
077c9d
+               = type == subprocess ? do_subprocess (&test)
077c9d
+                                    : do_subprogram (&test);
077c9d
+
077c9d
               check_stream ("stdout", &result.out, test.out);
077c9d
               check_stream ("stderr", &result.err, test.err);
077c9d
               if (test.signal != 0)
077c9d
@@ -185,4 +302,54 @@ do_test (void)
077c9d
   return 0;
077c9d
 }
077c9d
 
077c9d
+static int
077c9d
+do_test (int argc, char *argv[])
077c9d
+{
077c9d
+  /* We must have either:
077c9d
+
077c9d
+     - one or four parameters if called initially:
077c9d
+       + argv[1]: path for ld.so        optional
077c9d
+       + argv[2]: "--library-path"      optional
077c9d
+       + argv[3]: the library path      optional
077c9d
+       + argv[4]: the application name
077c9d
+
077c9d
+     - six parameters left if called through re-execution:
077c9d
+       + argv[1]: the application name
077c9d
+       + argv[2]: the stdout to print
077c9d
+       + argv[3]: the stderr to print
077c9d
+       + argv[4]: the write mode to use
077c9d
+       + argv[5]: the signal to issue
077c9d
+       + argv[6]: the exit status code to use
077c9d
+
077c9d
+     * When built with --enable-hardcoded-path-in-tests or issued without
077c9d
+       using the loader directly.
077c9d
+  */
077c9d
+
077c9d
+  if (argc != (restart ? 6 : 5) && argc != (restart ? 6 : 2))
077c9d
+    FAIL_EXIT1 ("wrong number of arguments (%d)", argc);
077c9d
+
077c9d
+  if (restart)
077c9d
+    {
077c9d
+      handle_restart (argv[1],  /* stdout  */
077c9d
+                     argv[2],  /* stderr  */
077c9d
+                     argv[3],  /* write_mode  */
077c9d
+                     argv[4],  /* signal  */
077c9d
+                     argv[5]); /* status  */
077c9d
+    }
077c9d
+
077c9d
+  initial_argv[0] = argv[1]; /* path for ld.so  */
077c9d
+  initial_argv[1] = argv[2]; /* "--library-path"  */
077c9d
+  initial_argv[2] = argv[3]; /* the library path  */
077c9d
+  initial_argv[3] = argv[4]; /* the application name  */
077c9d
+  initial_argv[4] = NULL;
077c9d
+
077c9d
+  do_multiple_tests (subprocess);
077c9d
+  do_multiple_tests (subprogram);
077c9d
+
077c9d
+  return 0;
077c9d
+}
077c9d
+
077c9d
+#define CMDLINE_OPTIONS \
077c9d
+  { "restart", no_argument, &restart, 1 },
077c9d
+#define TEST_FUNCTION_ARGV do_test
077c9d
 #include <support/test-driver.c>
077c9d
diff -Nrup a/support/xposix_spawn.c b/support/xposix_spawn.c
077c9d
--- a/support/xposix_spawn.c	1969-12-31 19:00:00.000000000 -0500
077c9d
+++ b/support/xposix_spawn.c	2019-05-16 23:37:19.904845228 -0400
077c9d
@@ -0,0 +1,32 @@
077c9d
+/* xposix_spawn implementation.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <support/xspawn.h>
077c9d
+#include <support/check.h>
077c9d
+
077c9d
+pid_t
077c9d
+xposix_spawn (const char *file, const posix_spawn_file_actions_t *fa,
077c9d
+             const posix_spawnattr_t *attr, char *const args[],
077c9d
+             char *const envp[])
077c9d
+{
077c9d
+  pid_t pid;
077c9d
+  int status = posix_spawn (&pid, file, fa, attr, args, envp);
077c9d
+  if (status != 0)
077c9d
+    FAIL_EXIT1 ("posix_spawn to %s file failed: %m", file);
077c9d
+  return pid;
077c9d
+}
077c9d
diff -Nrup a/support/xposix_spawn_file_actions_addclose.c b/support/xposix_spawn_file_actions_addclose.c
077c9d
--- a/support/xposix_spawn_file_actions_addclose.c	1969-12-31 19:00:00.000000000 -0500
077c9d
+++ b/support/xposix_spawn_file_actions_addclose.c	2019-05-16 23:37:19.904845228 -0400
077c9d
@@ -0,0 +1,29 @@
077c9d
+/* xposix_spawn_file_actions_addclose implementation.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <support/xspawn.h>
077c9d
+#include <support/check.h>
077c9d
+
077c9d
+int
077c9d
+xposix_spawn_file_actions_addclose (posix_spawn_file_actions_t *fa, int fd)
077c9d
+{
077c9d
+  int status = posix_spawn_file_actions_addclose (fa, fd);
077c9d
+  if (status == -1)
077c9d
+    FAIL_EXIT1 ("posix_spawn_file_actions_addclose failed: %m\n");
077c9d
+  return status;
077c9d
+}
077c9d
diff -Nrup a/support/xposix_spawn_file_actions_adddup2.c b/support/xposix_spawn_file_actions_adddup2.c
077c9d
--- a/support/xposix_spawn_file_actions_adddup2.c	1969-12-31 19:00:00.000000000 -0500
077c9d
+++ b/support/xposix_spawn_file_actions_adddup2.c	2019-05-16 23:37:19.904845228 -0400
077c9d
@@ -0,0 +1,30 @@
077c9d
+/* xposix_spawn_file_actions_adddup2 implementation.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#include <support/xspawn.h>
077c9d
+#include <support/check.h>
077c9d
+
077c9d
+int
077c9d
+xposix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *fa, int fd,
077c9d
+                                  int newfd)
077c9d
+{
077c9d
+  int status = posix_spawn_file_actions_adddup2 (fa, fd, newfd);
077c9d
+  if (status == -1)
077c9d
+    FAIL_EXIT1 ("posix_spawn_file_actions_adddup2 failed: %m\n");
077c9d
+  return status;
077c9d
+}
077c9d
diff -Nrup a/support/xspawn.h b/support/xspawn.h
077c9d
--- a/support/xspawn.h	1969-12-31 19:00:00.000000000 -0500
077c9d
+++ b/support/xspawn.h	2019-05-16 23:37:19.904845228 -0400
077c9d
@@ -0,0 +1,34 @@
077c9d
+/* posix_spawn with support checks.
077c9d
+   Copyright (C) 2019 Free Software Foundation, Inc.
077c9d
+   This file is part of the GNU C Library.
077c9d
+
077c9d
+   The GNU C Library is free software; you can redistribute it and/or
077c9d
+   modify it under the terms of the GNU Lesser General Public
077c9d
+   License as published by the Free Software Foundation; either
077c9d
+   version 2.1 of the License, or (at your option) any later version.
077c9d
+
077c9d
+   The GNU C Library is distributed in the hope that it will be useful,
077c9d
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
077c9d
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
077c9d
+   Lesser General Public License for more details.
077c9d
+
077c9d
+   You should have received a copy of the GNU Lesser General Public
077c9d
+   License along with the GNU C Library; if not, see
077c9d
+   <http://www.gnu.org/licenses/>.  */
077c9d
+
077c9d
+#ifndef SUPPORT_XSPAWN_H
077c9d
+#define SUPPORT_XSPAWN_H
077c9d
+
077c9d
+#include <spawn.h>
077c9d
+
077c9d
+__BEGIN_DECLS
077c9d
+
077c9d
+int xposix_spawn_file_actions_addclose (posix_spawn_file_actions_t *, int);
077c9d
+int xposix_spawn_file_actions_adddup2 (posix_spawn_file_actions_t *, int, int);
077c9d
+
077c9d
+pid_t xposix_spawn (const char *, const posix_spawn_file_actions_t *,
077c9d
+                   const posix_spawnattr_t *, char *const [], char *const []);
077c9d
+
077c9d
+__END_DECLS
077c9d
+
077c9d
+#endif