Skip to content

Commit

Permalink
Get binary path, working directory and arguments from /proc
Browse files Browse the repository at this point in the history
  • Loading branch information
i-ky committed Feb 23, 2023
1 parent de1b03c commit 36f5277
Showing 1 changed file with 39 additions and 1 deletion.
40 changes: 39 additions & 1 deletion src/basset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@
#include <sys/ptrace.h>
#include <sys/wait.h>

#include <linux/limits.h>
#include <linux/ptrace.h>

#undef PTRACE_CONT
#undef PTRACE_SETOPTIONS
#undef PTRACE_TRACEME

#include <csignal>
#include <fstream>
#include <iostream>
#include <string>

using std::cerr;
using std::ifstream;
using std::string;
using std::to_string;

int main(int argc, char *argv[]) {
argv++;
Expand Down Expand Up @@ -63,8 +67,42 @@ int main(int argc, char *argv[]) {

if (WSTOPSIG(wstatus) == SIGTRAP) {
switch (wstatus >> 16) {
case PTRACE_EVENT_EXEC: {
char exe[PATH_MAX];
auto ret = readlink(("/proc/" + to_string(pid) + "/exe").c_str(),
exe, sizeof(exe));

if (ret == -1) {
perror("cannot readlink(\"/proc/[pid]/exe\")");
return -1;
}

cerr << string(exe, ret) << '\n';

char cwd[PATH_MAX];
ret = readlink(("/proc/" + to_string(pid) + "/cwd").c_str(), cwd,
sizeof(cwd));

if (ret == -1) {
perror("cannot readlink(\"/proc/[pid]/cwd\")");
return -1;
}

cerr << string(cwd, ret) << '\n';

ifstream cmdline("/proc/" + to_string(pid) + "/cmdline");

for (string arg; getline(cmdline, arg, '\0');) {
cerr << '\t' << arg.data() << '\n';
}

if (!cmdline.eof()) {
cerr << "failed to read /proc/[pid]/cmdline\n";
}

break;
}
case PTRACE_EVENT_CLONE:
case PTRACE_EVENT_EXEC:
case PTRACE_EVENT_FORK:
case PTRACE_EVENT_VFORK:
break;
Expand Down

0 comments on commit 36f5277

Please sign in to comment.