[libc-commits] [libc] [libc] Implement execvp in unistd (PR #232427)
Aman Maurya via libc-commits
libc-commits at lists.llvm.org
Sat Oct 17 00:47:40 PDT 2026
================
@@ -0,0 +1,131 @@
+//===----------------------------------------------------------------------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+///
+/// \file
+/// Linux implementation of execvp.
+///
+//===----------------------------------------------------------------------===//
+
+#include "src/unistd/execvp.h"
+
+#include "hdr/errno_macros.h"
+#include "hdr/limits_macros.h"
+#include "hdr/types/size_t.h"
+#include "src/__support/CPP/string_view.h"
+#include "src/__support/OSUtil/linux/syscall_wrappers/execve.h"
+#include "src/__support/common.h"
+#include "src/__support/error_or.h"
+#include "src/__support/libc_errno.h"
+#include "src/__support/macros/config.h"
+#include "src/__support/macros/null_check.h"
+#include "src/stdlib/environ_internal.h"
+#include "src/string/memory_utils/inline_memcpy.h"
+#include "src/unistd/environ.h"
+
+namespace LIBC_NAMESPACE_DECL {
+
+namespace {
+
+LIBC_INLINE ErrorOr<int>
+execve_with_fallback(const char *path, char *const argv[], char *const envp[]) {
+ auto ret = linux_syscalls::execve(path, argv, envp);
+ if (!ret && ret.error() == ENOEXEC) {
+ size_t argc = 0;
+ while (argv[argc] != nullptr)
+ ++argc;
+
+#pragma GCC diagnostic push
+#pragma GCC diagnostic ignored "-Wvla"
+#if __has_warning("-Wvla-cxx-extension")
+#pragma GCC diagnostic ignored "-Wvla-cxx-extension"
+#endif
+ char *new_argv[argc + 2];
+#pragma GCC diagnostic pop
+ new_argv[0] = const_cast<char *>("sh");
+ new_argv[1] = const_cast<char *>(path);
+ for (size_t i = 1; i <= argc; ++i)
+ new_argv[i + 1] = argv[i];
+
+ ret = linux_syscalls::execve("/bin/sh", new_argv, envp);
+ }
+ return ret;
----------------
amanmaurya92 wrote:
I have added a TODO to keep track. 😺
https://github.com/llvm/llvm-project/pull/232427
More information about the libc-commits
mailing list