87c1f6
commit f09542c584b121da0322fde4b55306d512b85d93
87c1f6
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
87c1f6
Date:   Mon Mar 23 15:23:20 2020 -0300
87c1f6
87c1f6
    posix: Fix system error return value [BZ #25715]
87c1f6
    
87c1f6
    It fixes 5fb7fc9635 when posix_spawn fails.
87c1f6
    
87c1f6
    Checked on x86_64-linux-gnu and i686-linux-gnu.
87c1f6
    
87c1f6
    Reviewed-by: Carlos O'Donell <carlos@redhat.com>
87c1f6
87c1f6
diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c
87c1f6
index d14839f3ec3a7bad..b61bd347df7ec46a 100644
87c1f6
--- a/stdlib/tst-system.c
87c1f6
+++ b/stdlib/tst-system.c
87c1f6
@@ -17,14 +17,128 @@
87c1f6
    <http://www.gnu.org/licenses/>.  */
87c1f6
 
87c1f6
 #include <stdlib.h>
87c1f6
+#include <unistd.h>
87c1f6
+#include <string.h>
87c1f6
+#include <signal.h>
87c1f6
+#include <paths.h>
87c1f6
 
87c1f6
+#include <support/capture_subprocess.h>
87c1f6
+#include <support/check.h>
87c1f6
+#include <support/temp_file.h>
87c1f6
+#include <support/support.h>
87c1f6
+
87c1f6
+static char *tmpdir;
87c1f6
+static long int namemax;
87c1f6
+
87c1f6
+static void
87c1f6
+do_prepare (int argc, char *argv[])
87c1f6
+{
87c1f6
+  tmpdir = support_create_temp_directory ("tst-system-");
87c1f6
+  /* Include the last '/0'.  */
87c1f6
+  namemax = pathconf (tmpdir, _PC_NAME_MAX) + 1;
87c1f6
+  TEST_VERIFY_EXIT (namemax != -1);
87c1f6
+}
87c1f6
+#define PREPARE do_prepare
87c1f6
+
87c1f6
+struct args
87c1f6
+{
87c1f6
+  const char *command;
87c1f6
+  int exit_status;
87c1f6
+  int term_sig;
87c1f6
+  const char *path;
87c1f6
+};
87c1f6
+
87c1f6
+static void
87c1f6
+call_system (void *closure)
87c1f6
+{
87c1f6
+  struct args *args = (struct args *) closure;
87c1f6
+  int ret;
87c1f6
+
87c1f6
+  if (args->path != NULL)
87c1f6
+    TEST_COMPARE (setenv ("PATH", args->path, 1), 0);
87c1f6
+  ret = system (args->command);
87c1f6
+  if (args->term_sig == 0)
87c1f6
+    {
87c1f6
+      /* Expect regular termination.  */
87c1f6
+      TEST_VERIFY (WIFEXITED (ret) != 0);
87c1f6
+      TEST_COMPARE (WEXITSTATUS (ret), args->exit_status);
87c1f6
+    }
87c1f6
+  else
87c1f6
+    {
87c1f6
+      /* status_or_signal < 0.  Expect termination by signal.  */
87c1f6
+      TEST_VERIFY (WIFSIGNALED (ret) != 0);
87c1f6
+      TEST_COMPARE (WTERMSIG (ret), args->term_sig);
87c1f6
+    }
87c1f6
+}
87c1f6
 
87c1f6
 static int
87c1f6
 do_test (void)
87c1f6
 {
87c1f6
-  return system (":");
87c1f6
-}
87c1f6
+  TEST_VERIFY (system (NULL) != 0);
87c1f6
 
87c1f6
+  {
87c1f6
+    char cmd[namemax];
87c1f6
+    memset (cmd, 'a', sizeof(cmd));
87c1f6
+    cmd[sizeof(cmd) - 1] = '\0';
87c1f6
+
87c1f6
+    struct support_capture_subprocess result;
87c1f6
+    result = support_capture_subprocess (call_system,
87c1f6
+					 &(struct args) {
87c1f6
+					   cmd, 127, 0, tmpdir
87c1f6
+					 });
87c1f6
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_stderr);
87c1f6
+
87c1f6
+    char *returnerr = xasprintf ("%s: 1: %s: not found\n",
87c1f6
+				 basename(_PATH_BSHELL), cmd);
87c1f6
+    TEST_COMPARE_STRING (result.err.buffer, returnerr);
87c1f6
+    free (returnerr);
87c1f6
+  }
87c1f6
+
87c1f6
+  {
87c1f6
+    char cmd[namemax + 1];
87c1f6
+    memset (cmd, 'a', sizeof(cmd));
87c1f6
+    cmd[sizeof(cmd) - 1] = '\0';
87c1f6
+
87c1f6
+    struct support_capture_subprocess result;
87c1f6
+    result = support_capture_subprocess (call_system,
87c1f6
+					 &(struct args) {
87c1f6
+					   cmd, 127, 0, tmpdir
87c1f6
+					 });
87c1f6
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_stderr);
87c1f6
+
87c1f6
+    char *returnerr = xasprintf ("%s: 1: %s: File name too long\n",
87c1f6
+				 basename(_PATH_BSHELL), cmd);
87c1f6
+    TEST_COMPARE_STRING (result.err.buffer, returnerr);
87c1f6
+    free (returnerr);
87c1f6
+  }
87c1f6
+
87c1f6
+  {
87c1f6
+    struct support_capture_subprocess result;
87c1f6
+    result = support_capture_subprocess (call_system,
87c1f6
+					 &(struct args) {
87c1f6
+					   "kill -USR1 $$", 0, SIGUSR1
87c1f6
+					 });
87c1f6
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_none);
87c1f6
+  }
87c1f6
+
87c1f6
+  {
87c1f6
+    struct support_capture_subprocess result;
87c1f6
+    result = support_capture_subprocess (call_system,
87c1f6
+					 &(struct args) { "echo ...", 0 });
87c1f6
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_stdout);
87c1f6
+    TEST_COMPARE_STRING (result.out.buffer, "...\n");
87c1f6
+  }
87c1f6
+
87c1f6
+  {
87c1f6
+    struct support_capture_subprocess result;
87c1f6
+    result = support_capture_subprocess (call_system,
87c1f6
+					 &(struct args) { "exit 1", 1 });
87c1f6
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_none);
87c1f6
+  }
87c1f6
+
87c1f6
+  TEST_COMPARE (system (":"), 0);
87c1f6
+
87c1f6
+  return 0;
87c1f6
+}
87c1f6
 
87c1f6
-#define TEST_FUNCTION do_test ()
87c1f6
-#include "../test-skeleton.c"
87c1f6
+#include <support/test-driver.c>
87c1f6
diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c
87c1f6
index 8a51a6b9919ec39b..7db09a05c3fbca43 100644
87c1f6
--- a/sysdeps/posix/system.c
87c1f6
+++ b/sysdeps/posix/system.c
87c1f6
@@ -97,7 +97,8 @@ cancel_handler (void *arg)
87c1f6
 static int
87c1f6
 do_system (const char *line)
87c1f6
 {
87c1f6
-  int status;
87c1f6
+  int status = -1;
87c1f6
+  int ret;
87c1f6
   pid_t pid;
87c1f6
   struct sigaction sa;
87c1f6
 #ifndef _LIBC_REENTRANT
87c1f6
@@ -140,14 +141,14 @@ do_system (const char *line)
87c1f6
   __posix_spawnattr_setflags (&spawn_attr,
87c1f6
 			      POSIX_SPAWN_SETSIGDEF | POSIX_SPAWN_SETSIGMASK);
87c1f6
 
87c1f6
-  status = __posix_spawn (&pid, SHELL_PATH, 0, &spawn_attr,
87c1f6
-			  (char *const[]){ (char*) SHELL_NAME,
87c1f6
-					   (char*) "-c",
87c1f6
-					   (char *) line, NULL },
87c1f6
-			  __environ);
87c1f6
+  ret = __posix_spawn (&pid, SHELL_PATH, 0, &spawn_attr,
87c1f6
+		       (char *const[]){ (char *) SHELL_NAME,
87c1f6
+					(char *) "-c",
87c1f6
+					(char *) line, NULL },
87c1f6
+		       __environ);
87c1f6
   __posix_spawnattr_destroy (&spawn_attr);
87c1f6
 
87c1f6
-  if (status == 0)
87c1f6
+  if (ret == 0)
87c1f6
     {
87c1f6
       /* Cancellation results in cleanup handlers running as exceptions in
87c1f6
 	 the block where they were installed, so it is safe to reference
87c1f6
@@ -182,6 +183,9 @@ do_system (const char *line)
87c1f6
     }
87c1f6
   DO_UNLOCK ();
87c1f6
 
87c1f6
+  if (ret != 0)
87c1f6
+    __set_errno (ret);
87c1f6
+
87c1f6
   return status;
87c1f6
 }
87c1f6