From b306172d0545e2a292619b880a3c8d07bb46d396 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20Kj=C3=B8nigsen?= Date: Tue, 19 Jul 2016 15:00:25 +0200 Subject: Fix self-process identification for FreeBSD (#6314) FreeBSD does not come with procfs enabled by default, and should use sysctl() for this purpose. While it has similarities with NetBSD's implementation, there are a few subtle differences, which justifies leaving this implementation under its own guard. It's also worth noting that on FreeBSD sysctl.h MUST be present, which is unlike NetBSD. Therefore the HAVE_SYS_SYSCTL_H define is not checked for or used. This commit fixes https://github.com/dotnet/coreclr/issues/6184. This commit is based on the following commit from core-setup: https://github.com/dotnet/core-setup/commit/d5ce08014a174b006a3b409b8bb93d003ae583a0 --- .../hosts/unixcoreruncommon/coreruncommon.cpp | 24 +++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp b/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp index 92581faa64..b4c54ca6e3 100644 --- a/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp +++ b/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp @@ -16,7 +16,11 @@ #include #include #include -#ifdef HAVE_SYS_SYSCTL_H +#if defined(__FreeBSD__) +#include +#include +#endif +#if defined(HAVE_SYS_SYSCTL_H) || defined(__FreeBSD__) #include #endif #include "coreruncommon.h" @@ -75,6 +79,24 @@ bool GetEntrypointExecutableAbsolutePath(std::string& entrypointExecutable) result = true; } } +#elif defined (__FreeBSD__) + static const int name[] = { + CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 + }; + char path[PATH_MAX]; + size_t len; + + len = sizeof(path); + if (sysctl(name, 4, path, &len, nullptr, 0) == 0) + { + entrypointExecutable.assign(path); + result = true; + } + else + { + // ENOMEM + result = false; + } #elif defined(__NetBSD__) && defined(KERN_PROC_PATHNAME) static const int name[] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME, -- cgit v1.2.3