summaryrefslogtreecommitdiffstats
path: root/src/util/u_process.c
diff options
context:
space:
mode:
authorPierre-Eric Pelloux-Prayer <[email protected]>2020-03-16 10:49:17 +0100
committerPierre-Eric Pelloux-Prayer <[email protected]>2020-03-24 08:30:34 +0100
commitf8f1413070ae079443ab31a75679cfd10cb756ed (patch)
treef1fc171c63cd2c4c4d07b6688573a796aed0b347 /src/util/u_process.c
parent2cb965e5b60dbcd767da42360a5e18acd8803f5d (diff)
util/u_process: add util_get_process_exec_path
Reviewed-by: Marek Olšák <[email protected]> Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/4181>
Diffstat (limited to 'src/util/u_process.c')
-rw-r--r--src/util/u_process.c31
1 files changed, 31 insertions, 0 deletions
diff --git a/src/util/u_process.c b/src/util/u_process.c
index b9328d58da4..686e4624429 100644
--- a/src/util/u_process.c
+++ b/src/util/u_process.c
@@ -26,12 +26,19 @@
*/
#include "u_process.h"
+#include "detect_os.h"
+#include "macros.h"
#include <string.h>
#include <errno.h>
#include <stdlib.h>
+#include <unistd.h>
#undef GET_PROGRAM_NAME
+#if DETECT_OS_WINDOWS
+#include <windows.h>
+#endif
+
#if defined(__linux__) && defined(HAVE_PROGRAM_INVOCATION_NAME)
static char *path = NULL;
@@ -152,3 +159,27 @@ util_get_process_name(void)
{
return GET_PROGRAM_NAME();
}
+
+size_t
+util_get_process_exec_path(char* process_path, size_t len)
+{
+#if DETECT_OS_WINDOWS
+ return GetModuleFileNameA(NULL, process_path, len);
+#elif DETECT_OS_UNIX
+ ssize_t r;
+
+ if ((r = readlink("/proc/self/exe", process_path, len)) > 0)
+ goto success;
+ if ((r = readlink("/proc/curproc/exe", process_path, len)) > 0)
+ goto success;
+ if ((r = readlink("/proc/curproc/file", process_path, len)) > 0)
+ goto success;
+
+ return 0;
+success:
+ process_path[r] = '\0';
+ return r;
+
+#endif
+ return 0;
+}