From dac08490ee7bcbebddb3656fcadeab08a81e3858 Mon Sep 17 00:00:00 2001 From: rdb Date: Fri, 24 Nov 2017 23:43:15 +0100 Subject: [PATCH] deploy-ng: implement getting executable path on Linux and macOS --- pandatool/src/deploy-stub/deploy-stub.c | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/pandatool/src/deploy-stub/deploy-stub.c b/pandatool/src/deploy-stub/deploy-stub.c index 5d6eaa6139..c582ef0978 100644 --- a/pandatool/src/deploy-stub/deploy-stub.c +++ b/pandatool/src/deploy-stub/deploy-stub.c @@ -11,6 +11,10 @@ #include #endif +#ifdef __APPLE__ +#include +#endif + #include #include @@ -266,9 +270,21 @@ static void *map_blob(off_t offset, size_t size) { return NULL; } runtime = fopen(buffer, "rb"); +#elif defined(__APPLE__) + char buffer[4096]; + uint32_t bufsize = sizeof(buffer); + if (_NSGetExecutablePath(buffer, &bufsize) != 0) { + return NULL; + } + runtime = fopen(buffer, "rb"); #else - // Let's hope that it was invoked with the executable name as first arg. - runtime = fopen(argv[0], "rb"); + char buffer[4096]; + ssize_t pathlen = readlink("/proc/self/exe", buffer, sizeof(buffer) - 1); + if (pathlen <= 0) { + perror("readlink(/proc/self/exe)"); + return NULL; + } + runtime = fopen(buffer, "rb"); #endif // Get offsets. In version 0, we read it from the end of the file.