[libc-commits] [libc] [libc] implement execvp (PR #232008)

Pavel Labath via libc-commits libc-commits at lists.llvm.org
Thu Oct 15 06:12:37 PDT 2026


================
@@ -0,0 +1,101 @@
+//===----------------------------------------------------------------------===//
+//
+// 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/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 {
+
+LLVM_LIBC_FUNCTION(int, execvp, (const char *file, char *const argv[])) {
+  LIBC_CRASH_ON_NULLPTR(file);
+  LIBC_CRASH_ON_NULLPTR(argv);
+
+  cpp::string_view file_sv(file);
+  if (file_sv.empty()) {
+    libc_errno = ENOENT;
+    return -1;
+  }
+
+  // if there's a / in file, treat it as a path.
+  if (file_sv.find_first_of('/') != cpp::string_view::npos) {
+    auto ret = linux_syscalls::execve(
+        file, argv, const_cast<char *const *>(LIBC_NAMESPACE::environ));
+    if (!ret) {
+      libc_errno = ret.error();
+      return -1;
+    }
+    return *ret;
+  }
+
+  // else treat file as the name of a file in a directory, search the
+  // directories listed in PATH.
+  constexpr cpp::string_view DEFAULT_PATH = "/bin:/usr/bin";
+  const char *env_path =
+      internal::EnvironmentManager::get_instance().get("PATH");
+  cpp::string_view path_env =
+      env_path != nullptr ? cpp::string_view(env_path) : DEFAULT_PATH;
+
+  char buf[PATH_MAX];
+  bool seen_eacces = false;
+  int last_error = ENOENT;
+
+  for (size_t colon_pos = 0; colon_pos != cpp::string_view::npos;
+       path_env = path_env.substr(colon_pos + 1)) {
+    colon_pos = path_env.find_first_of(':');
+    cpp::string_view dir = path_env.substr(0, colon_pos);
+    if (dir.empty())
+      dir = ".";
----------------
labath wrote:

I noticed that glibc handles this case by executing the input path directly:
```
$ PATH=/foo:/bar::/baz `which strace` -e trace=execve ./A B
execve("./A", ["./A", "B"], 0x7ffe87388848 /* 49 vars */) = 0
execve("/foo/B", ["B"], 0x7ffec4ecfef0 /* 49 vars */) = -1 ENOENT (No such file or directory)
execve("/bar/B", ["B"], 0x7ffec4ecfef0 /* 49 vars */) = -1 ENOENT (No such file or directory)
execve("B", ["B"], 0x7ffec4ecfef0 /* 49 vars */) = 0
```

I think the only difference in what gets passed via AT_EXECFN. I don't think we have to be compatible with that, but I'm noting this in case you'd like to change it.

https://github.com/llvm/llvm-project/pull/232008


More information about the libc-commits mailing list