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