Skip to content

Commit

Permalink
[cdev-config] update whereami
Browse files Browse the repository at this point in the history
  • Loading branch information
mateoconlechuga committed Oct 7, 2024
1 parent 96423f9 commit 78f0e82
Show file tree
Hide file tree
Showing 2 changed files with 165 additions and 39 deletions.
198 changes: 161 additions & 37 deletions tools/cedev-config/src/whereami.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
extern "C" {
#endif

#if defined(__linux__) || defined(__CYGWIN__)
#undef _DEFAULT_SOURCE
#define _DEFAULT_SOURCE
#elif defined(__APPLE__)
#undef _DARWIN_C_SOURCE
#define _DARWIN_C_SOURCE
#define _DARWIN_BETTER_REALPATH
#endif

#if !defined(WAI_MALLOC) || !defined(WAI_FREE) || !defined(WAI_REALLOC)
#include <stdlib.h>
#endif
Expand Down Expand Up @@ -55,20 +64,27 @@ extern "C" {
#pragma warning(push, 3)
#endif
#include <windows.h>
// The following may not always be available and doesn't seem to be necessary:
// #include <intrin.h>
#include <intrin.h>
#if defined(_MSC_VER)
#pragma warning(pop)
#endif
#if (_MSC_VER >= 1900)
#include <stdbool.h>
#else
#define bool int
#define false 0
#define true 1
#endif

static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, int* dirname_length)
{
wchar_t buffer1[MAX_PATH];
wchar_t buffer2[MAX_PATH];
wchar_t* path = NULL;
int length = -1;
bool ok;

for (;;)
for (ok = false; !ok; ok = true)
{
DWORD size;
int length_, length__;
Expand Down Expand Up @@ -124,14 +140,12 @@ static int WAI_PREFIX(getModulePath_)(HMODULE module, char* out, int capacity, i
}

length = length__;

break;
}

if (path != buffer1)
WAI_FREE(path);

return length;
return ok ? length : -1;
}

WAI_NOINLINE WAI_FUNCSPEC
Expand Down Expand Up @@ -175,6 +189,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#define __STDC_FORMAT_MACROS
#endif
#include <inttypes.h>
#include <stdbool.h>

#if !defined(WAI_PROC_SELF_EXE)
#if defined(__sun)
Expand All @@ -190,8 +205,9 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
char buffer[PATH_MAX];
char* resolved = NULL;
int length = -1;
bool ok;

for (;;)
for (ok = false; !ok; ok = true)
{
resolved = realpath(WAI_PROC_SELF_EXE, buffer);
if (!resolved)
Expand All @@ -216,11 +232,9 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
}
}
}

break;
}

return length;
return ok ? length : -1;
}

#if !defined(WAI_PROC_SELF_MAPS_RETRY)
Expand All @@ -235,11 +249,17 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
#endif
#endif

#if !defined(WAI_STRINGIZE)
#define WAI_STRINGIZE(s)
#define WAI_STRINGIZE_(s) #s
#endif

#if defined(__ANDROID__) || defined(ANDROID)
#include <fcntl.h>
#include <sys/mman.h>
#include <unistd.h>
#endif
#include <stdbool.h>

WAI_NOINLINE WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
Expand All @@ -255,20 +275,20 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)

for (;;)
{
char buffer[PATH_MAX < 1024 ? 1024 : PATH_MAX];
uint64_t low, high;
char buffer[128 + PATH_MAX];
uintptr_t low, high;
char perms[5];
uint64_t offset;
uint32_t major, minor;
char path[PATH_MAX];
uint32_t inode;
uint32_t major, minor, inode;
char path[PATH_MAX + 1];

if (!fgets(buffer, sizeof(buffer), maps))
break;

if (sscanf(buffer, "%" PRIx64 "-%" PRIx64 " %s %" PRIx64 " %x:%x %u %s\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
if (sscanf(buffer, "%" SCNxPTR "-%" SCNxPTR " %s %" SCNx64 " %x:%x %u %" WAI_STRINGIZE(PATH_MAX) "[^\n]\n", &low, &high, perms, &offset, &major, &minor, &inode, path) == 8)
{
uint64_t addr = (uintptr_t)WAI_RETURN_ADDRESS();
void* _addr = WAI_RETURN_ADDRESS();
uintptr_t addr = (uintptr_t)_addr;
if (low <= addr && addr <= high)
{
char* resolved;
Expand Down Expand Up @@ -355,20 +375,17 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
break;
}

if (maps)
fclose(maps);

return length;
}

#elif defined(__APPLE__)

#define _DARWIN_BETTER_REALPATH
#include <mach-o/dyld.h>
#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <stdbool.h>

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
Expand All @@ -378,8 +395,9 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
char* path = buffer1;
char* resolved = NULL;
int length = -1;
bool ok;

for (;;)
for (ok = false; !ok; ok = true)
{
uint32_t size = (uint32_t)sizeof(buffer1);
if (_NSGetExecutablePath(path, &size) == -1)
Expand Down Expand Up @@ -412,14 +430,12 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
}
}
}

break;
}

if (path != buffer1)
WAI_FREE(path);

return length;
return ok ? length : -1;
}

WAI_NOINLINE WAI_FUNCSPEC
Expand Down Expand Up @@ -473,6 +489,7 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
#include <stdlib.h>
#include <string.h>
#include <dlfcn.h>
#include <stdbool.h>

#if !defined(WAI_PROC_SELF_EXE)
#define WAI_PROC_SELF_EXE "/proc/self/exefile"
Expand All @@ -486,8 +503,9 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
char* resolved = NULL;
FILE* self_exe = NULL;
int length = -1;
bool ok;

for (;;)
for (ok = false; !ok; ok = true)
{
self_exe = fopen(WAI_PROC_SELF_EXE, "r");
if (!self_exe)
Expand Down Expand Up @@ -519,13 +537,11 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
}
}
}

break;
}

fclose(self_exe);

return length;
return ok ? length : -1;
}

WAI_FUNCSPEC
Expand Down Expand Up @@ -573,14 +589,124 @@ int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
}

#elif defined(__DragonFly__) || defined(__FreeBSD__) || \
defined(__FreeBSD_kernel__) || defined(__NetBSD__)
defined(__FreeBSD_kernel__) || defined(__NetBSD__) || defined(__OpenBSD__)

#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/sysctl.h>
#include <dlfcn.h>
#include <stdbool.h>

#if defined(__OpenBSD__)

#include <unistd.h>

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
{
char buffer1[4096];
char buffer2[PATH_MAX];
char buffer3[PATH_MAX];
char** argv = (char**)buffer1;
char* resolved = NULL;
int length = -1;
bool ok;

for (ok = false; !ok; ok = true)
{
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, getpid(), KERN_PROC_ARGV };
size_t size;

if (sysctl(mib, 4, NULL, &size, NULL, 0) != 0)
break;

if (size > sizeof(buffer1))
{
argv = (char**)WAI_MALLOC(size);
if (!argv)
break;
}

if (sysctl(mib, 4, argv, &size, NULL, 0) != 0)
break;

if (strchr(argv[0], '/'))
{
resolved = realpath(argv[0], buffer2);
if (!resolved)
break;
}
else
{
const char* PATH = getenv("PATH");
if (!PATH)
break;

size_t argv0_length = strlen(argv[0]);

const char* begin = PATH;
while (1)
{
const char* separator = strchr(begin, ':');
const char* end = separator ? separator : begin + strlen(begin);

if (end - begin > 0)
{
if (*(end -1) == '/')
--end;

if (((end - begin) + 1 + argv0_length + 1) <= sizeof(buffer2))
{
memcpy(buffer2, begin, end - begin);
buffer2[end - begin] = '/';
memcpy(buffer2 + (end - begin) + 1, argv[0], argv0_length + 1);

resolved = realpath(buffer2, buffer3);
if (resolved)
break;
}
}

if (!separator)
break;

begin = ++separator;
}

if (!resolved)
break;
}

length = (int)strlen(resolved);
if (length <= capacity)
{
memcpy(out, resolved, length);

if (dirname_length)
{
int i;

for (i = length - 1; i >= 0; --i)
{
if (out[i] == '/')
{
*dirname_length = i;
break;
}
}
}
}
}

if (argv != (char**)buffer1)
WAI_FREE(argv);

return ok ? length : -1;
}

#else

WAI_FUNCSPEC
int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
Expand All @@ -590,8 +716,9 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
char* path = buffer1;
char* resolved = NULL;
int length = -1;
bool ok;

for (;;)
for (ok = false; !ok; ok = true)
{
#if defined(__NetBSD__)
int mib[4] = { CTL_KERN, KERN_PROC_ARGS, -1, KERN_PROC_PATHNAME };
Expand Down Expand Up @@ -626,16 +753,13 @@ int WAI_PREFIX(getExecutablePath)(char* out, int capacity, int* dirname_length)
}
}
}

break;
}

if (path != buffer1)
WAI_FREE(path);

return length;
return ok ? length : -1;
}

#endif

WAI_NOINLINE WAI_FUNCSPEC
int WAI_PREFIX(getModulePath)(char* out, int capacity, int* dirname_length)
{
Expand Down
Loading

0 comments on commit 78f0e82

Please sign in to comment.