util: make util_get_process_exec_path work on FreeBSD w/o procfs
authorGreg V <greg@unrelenting.technology>
Wed, 8 Apr 2020 22:41:00 +0000 (01:41 +0300)
committerMarge Bot <emma+marge@anholt.net>
Fri, 29 Oct 2021 06:06:05 +0000 (06:06 +0000)
sysctl is the correct way of getting the current executable's path.
procfs is not mounted by default.

Reviewed-by: Samuel Pitoiset <samuel.pitoiset@gmail.com>
Reviewed-by: Eric Engestrom <eric@engestrom.ch>
Cc: mesa-stable
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/1598>

src/util/u_process.c

index af808c7..2965e46 100644 (file)
 #include <mach-o/dyld.h>
 #endif
 
+#if DETECT_OS_FREEBSD
+#include <sys/types.h>
+#include <sys/sysctl.h>
+#endif
+
 #if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
 
 static char *path = NULL;
@@ -202,6 +207,13 @@ util_get_process_exec_path(char* process_path, size_t len)
    int result = _NSGetExecutablePath(process_path, &bufSize);
 
    return (result == 0) ? strlen(process_path) : 0;
+#elif DETECT_OS_FREEBSD
+   int mib[4] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 };
+
+   (void) sysctl(mib, 4, process_path, &len, NULL, 0);
+   process_path[len - 1] = '\0';
+
+   return len;
 #elif DETECT_OS_UNIX
    ssize_t r;