From 754a26884ceb5a73dc7a69a6ddb47085e2cceade Mon Sep 17 00:00:00 2001 From: fedir Date: Tue, 20 May 2025 09:57:59 +0200 Subject: [PATCH] Changed realpath to readlink /proc/pid/exe already seems to be a link to the absolute path to the executable. This fixes bugs related to containerised applications. --- src/proc_operations.c | 31 ++++++++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/proc_operations.c b/src/proc_operations.c index b440c85..8d083a9 100644 --- a/src/proc_operations.c +++ b/src/proc_operations.c @@ -8,8 +8,10 @@ #include "proc_operations.h" #include +#include #include #include +#include /** * @brief Returns the PID of the main thread (i.e., the process ID) of the @@ -54,11 +56,38 @@ char *get_process_name_by_pid(const int pid) { char path[1024]; sprintf(path, "/proc/%d/exe", pid); - char *name = realpath(path, NULL); + size_t size = 128; + char *name = malloc(size); if (name == NULL) { fprintf(stderr, "Could not get process name by pid %d", pid); perror(""); + return NULL; } + + while (1) { + ssize_t len = readlink(path, name, size); + + if (len == -1) { + fprintf(stderr, "Could not get process name by pid %d", pid); + perror(""); + free(name); + return NULL; + } + if ((size_t)len >= size) { + size *= 2; + char *new_name = realloc(name, size); + if (!new_name) { + free(name); + return NULL; + } + name = new_name; + } else { + // readlink does not set the null character + name[len] = 0; + break; + } + } + return name; }