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.
This commit is contained in:
parent
2f82ab63ac
commit
754a26884c
@ -8,8 +8,10 @@
|
||||
|
||||
#include "proc_operations.h"
|
||||
#include <linux/limits.h>
|
||||
#include <stddef.h>
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <unistd.h>
|
||||
|
||||
/**
|
||||
* @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;
|
||||
}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user