From 4e5249a84900607520de71b6097bde80bbe45469 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mart=C3=ADn=20Capello?= Date: Tue, 16 Jan 2024 15:59:40 -0300 Subject: [PATCH] Add 'get_process_name()' impl for Linux --- base/process.cpp | 19 +++++++++++++++++-- 1 file changed, 17 insertions(+), 2 deletions(-) diff --git a/base/process.cpp b/base/process.cpp index 8bd87dc9c..0862fac85 100644 --- a/base/process.cpp +++ b/base/process.cpp @@ -26,6 +26,12 @@ #include #endif +#if LAF_LINUX + #include "base/fs.h" + #include + #include +#endif + namespace base { #if LAF_WINDOWS @@ -95,8 +101,17 @@ std::string get_process_name(pid pid) std::string get_process_name(pid pid) { - // TODO implement for Linux - return std::string(); + char path[128]; + std::memset(path, 0, 128); + std::sprintf(path, "/proc/%d/exe", pid); + char* exepath = realpath(path, nullptr); + if (!exepath) + return std::string(); + + const std::string exename = base::get_file_name(exepath); + free(exepath); + + return exename; } #endif