40 lines
782 B
C++
40 lines
782 B
C++
#include <fstream>
|
|
#include <map>
|
|
#include <string>
|
|
|
|
class pid_cmdline pid_cmdline;
|
|
|
|
class pid_cmdline {
|
|
private:
|
|
std::map<int, std::string> cmdlines;
|
|
|
|
public:
|
|
void clear(void);
|
|
std::string& get_pid_cmdline(int pid);
|
|
};
|
|
|
|
static string unknow_symbol("UNKNOWN");
|
|
|
|
void pid_cmdline::clear(void) { cmdlines.clear(); }
|
|
|
|
std::string& pid_cmdline::get_pid_cmdline(int pid) {
|
|
if (cmdlines.count(pid) == 0) {
|
|
int i;
|
|
char buf[255];
|
|
char file[255];
|
|
std::fstream ifs;
|
|
|
|
snprintf(file, sizeof(file), "/proc/%d/cmdline", pid);
|
|
ifs.open(file, ios::binary | ios::in);
|
|
ifs.getline(buf, 255);
|
|
for (i = 0; i < ifs.gcount() && i < 255; i++) {
|
|
if (buf[i] < ' ') {
|
|
buf[i] = ' ';
|
|
}
|
|
}
|
|
|
|
cmdlines[pid] = buf;
|
|
}
|
|
|
|
return cmdlines[pid];
|
|
} |