| 1 |
/* |
| 2 |
* Copyright (c) 2025 The Khronos Group Inc. |
| 3 |
* Copyright (c) 2025 Valve Corporation |
| 4 |
* Copyright (c) 2025 LunarG, Inc. |
| 5 |
* |
| 6 |
* Permission is hereby granted, free of charge, to any person obtaining a copy |
| 7 |
* of this software and/or associated documentation files (the "Materials"), to |
| 8 |
* deal in the Materials without restriction, including without limitation the |
| 9 |
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or |
| 10 |
* sell copies of the Materials, and to permit persons to whom the Materials are |
| 11 |
* furnished to do so, subject to the following conditions: |
| 12 |
* |
| 13 |
* The above copyright notice(s) and this permission notice shall be included in |
| 14 |
* all copies or substantial portions of the Materials. |
| 15 |
* |
| 16 |
* THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 17 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 18 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. |
| 19 |
* |
| 20 |
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
| 21 |
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR |
| 22 |
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE MATERIALS OR THE |
| 23 |
* USE OR OTHER DEALINGS IN THE MATERIALS. |
| 24 |
* |
| 25 |
*/ |
| 26 |
|
| 27 |
#include "get_executable_path.h" |
| 28 |
|
| 29 |
#if defined(__linux__) || defined(__GNU__) |
| 30 |
|
| 31 |
#include <unistd.h> |
| 32 |
|
| 33 |
// find application path + name. Path cannot be longer than 1024, returns NULL if it is greater than that. |
| 34 |
std::string test_platform_executable_path() { |
| 35 |
std::string buffer; |
| 36 |
buffer.resize(1024); |
| 37 |
ssize_t count = readlink("/proc/self/exe", &buffer[0], buffer.size()); |
| 38 |
if (count == -1) return NULL; |
| 39 |
if (count == 0) return NULL; |
| 40 |
buffer[count] = '\0'; |
| 41 |
buffer.resize(count); |
| 42 |
return buffer; |
| 43 |
} |
| 44 |
#elif defined(__APPLE__) |
| 45 |
|
| 46 |
#include <libproc.h> |
| 47 |
#include <unistd.h> |
| 48 |
|
| 49 |
std::string test_platform_executable_path() { |
| 50 |
std::string buffer; |
| 51 |
buffer.resize(1024); |
| 52 |
pid_t pid = getpid(); |
| 53 |
int ret = proc_pidpath(pid, &buffer[0], static_cast<uint32_t>(buffer.size())); |
| 54 |
if (ret <= 0) return NULL; |
| 55 |
buffer[ret] = '\0'; |
| 56 |
buffer.resize(ret); |
| 57 |
return buffer; |
| 58 |
} |
| 59 |
#elif defined(__DragonFly__) || defined(__FreeBSD__) || defined(__NetBSD__) |
| 60 |
#include <sys/sysctl.h> |
| 61 |
std::string test_platform_executable_path() { |
| 62 |
int mib[] = { |
| 63 |
CTL_KERN, |
| 64 |
#if defined(__NetBSD__) |
| 65 |
KERN_PROC_ARGS, |
| 66 |
-1, |
| 67 |
KERN_PROC_PATHNAME, |
| 68 |
#else |
| 69 |
KERN_PROC, |
| 70 |
KERN_PROC_PATHNAME, |
| 71 |
-1, |
| 72 |
#endif |
| 73 |
}; |
| 74 |
std::string buffer; |
| 75 |
buffer.resize(1024); |
| 76 |
size_t size = buffer.size(); |
| 77 |
if (sysctl(mib, sizeof(mib) / sizeof(mib[0]), &buffer[0], &size, NULL, 0) < 0) { |
| 78 |
return NULL; |
| 79 |
} |
| 80 |
buffer.resize(size); |
| 81 |
|
| 82 |
return buffer; |
| 83 |
} |
| 84 |
#elif defined(__Fuchsia__) || defined(__OpenBSD__) |
| 85 |
std::string test_platform_executable_path() { return {}; } |
| 86 |
#elif defined(__QNX__) |
| 87 |
|
| 88 |
#ifndef SYSCONFDIR |
| 89 |
#define SYSCONFDIR "/etc" |
| 90 |
#endif |
| 91 |
|
| 92 |
#include <fcntl.h> |
| 93 |
#include <sys/stat.h> |
| 94 |
|
| 95 |
std::string test_platform_executable_path() { |
| 96 |
std::string buffer; |
| 97 |
buffer.resize(1024); |
| 98 |
int fd = open("/proc/self/exefile", O_RDONLY); |
| 99 |
ssize_t rdsize; |
| 100 |
|
| 101 |
if (fd == -1) { |
| 102 |
return NULL; |
| 103 |
} |
| 104 |
|
| 105 |
rdsize = read(fd, &buffer[0], buffer.size()); |
| 106 |
if (rdsize < 0) { |
| 107 |
return NULL; |
| 108 |
} |
| 109 |
buffer[rdsize] = 0x00; |
| 110 |
close(fd); |
| 111 |
buffer.resize(rdsize); |
| 112 |
|
| 113 |
return buffer; |
| 114 |
} |
| 115 |
#endif // defined (__QNX__) |
| 116 |
|
| 117 |
#if defined(_WIN32) |
| 118 |
|
| 119 |
#include <filesystem> |
| 120 |
|
| 121 |
#include <Windows.h> |
| 122 |
|
| 123 |
#include "wide_char_handling.h" |
| 124 |
|
| 125 |
std::string test_platform_executable_path() { |
| 126 |
std::string buffer; |
| 127 |
buffer.resize(1024); |
| 128 |
DWORD ret = GetModuleFileName(NULL, static_cast<LPSTR>(&buffer[0]), (DWORD)buffer.size()); |
| 129 |
if (ret == 0) return NULL; |
| 130 |
if (ret > buffer.size()) return NULL; |
| 131 |
buffer.resize(ret); |
| 132 |
buffer[ret] = '\0'; |
| 133 |
return narrow(std::filesystem::path(buffer).native()); |
| 134 |
} |
| 135 |
|
| 136 |
#endif |