Skip to content

Commit

Permalink
Add 'get_process_name()' impl for Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
martincapello authored and dacap committed Feb 6, 2024
1 parent a6c13ab commit 4e5249a
Showing 1 changed file with 17 additions and 2 deletions.
19 changes: 17 additions & 2 deletions base/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@
#include <libproc.h>
#endif

#if LAF_LINUX
#include "base/fs.h"
#include <cstdlib>
#include <cstring>
#endif

namespace base {

#if LAF_WINDOWS
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4e5249a

Please sign in to comment.