From dfbfecb6baa8190297c0081296ff07949b56815b Mon Sep 17 00:00:00 2001 From: Brad Smith Date: Thu, 24 Sep 2026 14:03:29 -0400 Subject: [PATCH] OpenBSD: use `getexecpath()` (#3379) Apparently it's more kosher than looking at `/proc`. --- CMakeLists.txt | 6 ++++++ rts/System/Platform/Misc.cpp | 11 +++++++++++ 2 files changed, 17 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 49e7b0acd8f..da60d1095f1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -344,6 +344,12 @@ if (NOT HAVE_STRNLEN) message(FATAL_ERROR "this compiler/environment isn't supported, the function strnlen is missing!") endif() +include (CheckFunctionExists) +check_symbol_exists(getexecpath unistd.h HAVE_GETEXECPATH) +if (HAVE_GETEXECPATH) + add_definitions(-DHAVE_GETEXECPATH) +endif() + ### Add Link-Time-Optimization flags, if supported (GCC >= 4.5) and enabled option(LTO "Link Time Optimizations (LTO)" FALSE) if (LTO) diff --git a/rts/System/Platform/Misc.cpp b/rts/System/Platform/Misc.cpp index e78c15dd399..ab3eb32d63e 100644 --- a/rts/System/Platform/Misc.cpp +++ b/rts/System/Platform/Misc.cpp @@ -168,6 +168,7 @@ namespace Platform // Mac OS X: _NSGetExecutablePath() (man 3 dyld) // Linux: readlink /proc/self/exe // Solaris: getexecname() + // OpenBSD: getexecpath() // FreeBSD: sysctl CTL_KERN KERN_PROC KERN_PROC_PATHNAME -1 // BSD with procfs: readlink /proc/curproc/file // Windows: GetModuleFileName() with hModule = NULL @@ -212,6 +213,16 @@ namespace Platform procExeFilePath = path; + #elif defined(__OpenBSD__) && defined(HAVE_GETEXECPATH) + char path[PATH_MAX]; + + if (getexecpath(path, sizeof(path)) == 0) { + procExeFilePath = path; + } else { + error = "[openbsd] execve(2) could not determine the executable pathname"; + } + + #elif defined(__FreeBSD__) const int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1}; const long maxpath = pathconf("/", _PC_PATH_MAX);