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