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