Fix self-process identification for FreeBSD (#6314)
authorJostein Kjønigsen <jostein@kjonigsen.net>
Tue, 19 Jul 2016 13:00:25 +0000 (15:00 +0200)
committerJan Vorlicek <janvorli@microsoft.com>
Tue, 19 Jul 2016 13:00:25 +0000 (15:00 +0200)
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

src/coreclr/hosts/unixcoreruncommon/coreruncommon.cpp

index 92581fa..b4c54ca 100644 (file)
 #include <string>
 #include <string.h>
 #include <sys/stat.h>
-#ifdef HAVE_SYS_SYSCTL_H
+#if defined(__FreeBSD__)
+#include <sys/types.h>
+#include <sys/param.h>
+#endif
+#if defined(HAVE_SYS_SYSCTL_H) || defined(__FreeBSD__)
 #include <sys/sysctl.h>
 #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,