Add -l option to show full command line (fixes #23)

This commit is contained in:
anomen
2016-06-13 22:22:51 +02:00
parent 276d648c33
commit 5e2c35700b
8 changed files with 107 additions and 17 deletions

View File

@@ -116,12 +116,32 @@ static std::string read_file(const char *filepath) {
return contents;
}
std::string getprogname(pid_t pid) {
std::string getcmdline(pid_t pid) {
const int maxfilenamelen = 14 + MAX_PID_LENGTH + 1;
char filename[maxfilenamelen];
std::snprintf(filename, maxfilenamelen, "/proc/%d/cmdline", pid);
return read_file(filename);
bool replace_null = false;
std::string cmdline = read_file(filename);
// join parameters, keep prgname separate, don't overwrite trailing null
for (int idx = 0; idx < (cmdline.length() - 1); idx++) {
if (cmdline[idx] == 0x00) {
if (replace_null) {
cmdline[idx] = ' ';
}
replace_null = true;
}
}
if (cmdline.length() == 0 || (cmdline[cmdline.length() - 1] != 0x00)) {
// invalid content of cmdline file. Add null char to allow further
// processing.
cmdline.append("\0");
}
return cmdline;
}
void setnode(unsigned long inode, pid_t pid) {
@@ -131,7 +151,7 @@ void setnode(unsigned long inode, pid_t pid) {
prg_node *newnode = new prg_node;
newnode->inode = inode;
newnode->pid = pid;
newnode->name = getprogname(pid);
newnode->cmdline = getcmdline(pid);
inodeproc[inode] = newnode;
delete current_value;