summaryrefslogtreecommitdiff
path: root/lang/intel-compute-runtime/files/patch-getPath
blob: 3b648a653c65db1393d0cc402271a6d4ff6ac577 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
Implement getPath on BSDs

--- shared/offline_compiler/source/utilities/linux/get_path.cpp.orig	2020-02-28 16:16:42 UTC
+++ shared/offline_compiler/source/utilities/linux/get_path.cpp
@@ -10,11 +10,33 @@
 #include <sys/types.h>
 #include <unistd.h>
 
+#if defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__)
+#include <sys/sysctl.h>
+#endif
+
 std::string getPath() {
+#if defined(KERN_PROC_PATHNAME)
+    char exepath[PATH_MAX + 1] = {0};
+    size_t len = sizeof(exepath);
+    int mib[] = {
+        CTL_KERN,
+#if defined(__NetBSD__)
+        KERN_PROC_ARGS,
+        -1,
+        KERN_PROC_PATHNAME,
+#else
+        KERN_PROC,
+        KERN_PROC_PATHNAME,
+        -1,
+#endif
+    };
+    if (sysctl(mib, sizeof(mib)/sizeof(mib[0]), exepath, &len, NULL, 0) != -1) {
+#else // Linux
     char exepath[128] = {0};
     std::stringstream ss;
     ss << "/proc/" << getpid() << "/exe";
     if (readlink(ss.str().c_str(), exepath, 128) != -1) {
+#endif
         std::string path = std::string(exepath);
         path = path.substr(0, path.find_last_of('/') + 1);
         return path;