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