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