28653a
commit 42dda89dcb0407f6799dbfd0b9dab1529666ad51
28653a
Author: Adhemerval Zanella <adhemerval.zanella@linaro.org>
28653a
Date:   Fri Dec 11 15:23:05 2020 -0300
28653a
28653a
    posix: Fix return value of system if shell can not be executed [BZ #27053]
28653a
    
28653a
    POSIX states that system returned code for failure to execute the shell
28653a
    shall be as if the shell had terminated using _exit(127).  This
28653a
    behaviour was removed with 5fb7fc96350575.
28653a
    
28653a
    Checked on x86_64-linux-gnu.
28653a
28653a
diff --git a/stdlib/tst-system.c b/stdlib/tst-system.c
28653a
index 194e09828dd5c206..8681584f15ef3b47 100644
28653a
--- a/stdlib/tst-system.c
28653a
+++ b/stdlib/tst-system.c
28653a
@@ -26,6 +26,7 @@
28653a
 #include <support/check.h>
28653a
 #include <support/temp_file.h>
28653a
 #include <support/support.h>
28653a
+#include <support/xunistd.h>
28653a
 
28653a
 static char *tmpdir;
28653a
 static long int namemax;
28653a
@@ -138,6 +139,22 @@ do_test (void)
28653a
     support_capture_subprocess_check (&result, "system", 0, sc_allow_none);
28653a
   }
28653a
 
28653a
+  {
28653a
+    struct stat64 st;
28653a
+    xstat (_PATH_BSHELL, &st);
28653a
+    mode_t mode = st.st_mode;
28653a
+    xchmod (_PATH_BSHELL, mode & ~(S_IXUSR | S_IXGRP | S_IXOTH));
28653a
+
28653a
+    struct support_capture_subprocess result;
28653a
+    result = support_capture_subprocess (call_system,
28653a
+					 &(struct args) {
28653a
+					   "exit 1", 127, 0
28653a
+					 });
28653a
+    support_capture_subprocess_check (&result, "system", 0, sc_allow_none);
28653a
+
28653a
+    xchmod (_PATH_BSHELL, st.st_mode);
28653a
+  }
28653a
+
28653a
   TEST_COMPARE (system (""), 0);
28653a
 
28653a
   return 0;
28653a
diff --git a/support/Makefile b/support/Makefile
28653a
index 4875f52495ef292d..09b41b0d57e9239a 100644
28653a
--- a/support/Makefile
28653a
+++ b/support/Makefile
28653a
@@ -86,6 +86,7 @@ libsupport-routines = \
28653a
   xchroot \
28653a
   xclone \
28653a
   xclose \
28653a
+  xchmod \
28653a
   xconnect \
28653a
   xcopy_file_range \
28653a
   xdlfcn \
28653a
diff --git a/sysdeps/posix/system.c b/sysdeps/posix/system.c
28653a
index 7db09a05c3fbca43..047ded4badfddcab 100644
28653a
--- a/sysdeps/posix/system.c
28653a
+++ b/sysdeps/posix/system.c
28653a
@@ -171,6 +171,10 @@ do_system (const char *line)
28653a
       __libc_cleanup_region_end (0);
28653a
 #endif
28653a
     }
28653a
+  else
28653a
+   /* POSIX states that failure to execute the shell should return
28653a
+      as if the shell had terminated using _exit(127).  */
28653a
+   status = W_EXITCODE (127, 0);
28653a
 
28653a
   DO_LOCK ();
28653a
   if (SUB_REF () == 0)