From: Jostein Kjønigsen Date: Tue, 19 Jul 2016 13:00:25 +0000 (+0200) Subject: Fix self-process identification for FreeBSD (#6314) X-Git-Tag: accepted/tizen/base/20180629.140029~4027 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=b306172d0545e2a292619b880a3c8d07bb46d396;p=platform%2Fupstream%2Fcoreclr.git 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 --- diff --git a/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp b/src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp index 92581fa..b4c54ca 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,