From 208fdd64ac908a1f9e259cd7851bedc3915d7e16 Mon Sep 17 00:00:00 2001 From: apio Date: Sat, 8 Apr 2023 12:28:26 +0200 Subject: [PATCH] execvpe: Execute a shell if errno == ENOEXEC --- libc/src/unistd.cpp | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/libc/src/unistd.cpp b/libc/src/unistd.cpp index fba619e9..415e95e8 100644 --- a/libc/src/unistd.cpp +++ b/libc/src/unistd.cpp @@ -68,9 +68,21 @@ extern "C" int err = errno; execve(file.chars(), argv, envp); - if (errno != ENOENT && errno != EACCES) return -1; + if (errno != ENOENT && errno != EACCES && errno != ENOEXEC) return -1; - // FIXME: POSIX says that if errno == ENOEXEC, we should run /bin/sh . + if (errno == ENOEXEC) + { + Vector shell_argv; + shell_argv.try_append(const_cast("sh")); + char* const* arg = argv; + do { + shell_argv.try_append(*arg); + } while (*(arg++)); + + execve("/bin/sh", shell_argv.data(), envp); + errno = ENOEXEC; + return -1; + } if (err == EACCES) errno = err; }