@@ -414,14 +414,22 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
414
414
getTopratedProcesses (&memory_tasks_, &p2);
415
415
}
416
416
417
- bool ProcessMonitor::getCommandLineFromPiD (const std::string & pid, std::string * command)
417
+ bool ProcessMonitor::getCommandLineFromPiD (const std::string & pid, std::string & command)
418
418
{
419
419
std::string commandLineFilePath = " /proc/" + pid + " /cmdline" ;
420
- std::ifstream commandFile (commandLineFilePath);
420
+ std::ifstream commandFile (commandLineFilePath, std::ios::in | std::ios::binary);
421
+
421
422
if (commandFile.is_open ()) {
422
- std::getline (commandFile, *command);
423
+ std::vector<uint8_t > buffer;
424
+ std::copy (
425
+ std::istream_iterator<uint8_t >(commandFile), std::istream_iterator<uint8_t >(),
426
+ std::back_inserter (buffer));
423
427
commandFile.close ();
424
- return true ;
428
+ std::replace (
429
+ buffer.begin (), buffer.end (), ' \0 ' ,
430
+ ' ' ); // 0x00 is used as delimiter in /cmdline instead of 0x20 (space)
431
+ command = std::string (buffer.begin (), buffer.end ());
432
+ return (buffer.size () > 0 ) ? true : false ; // cmdline is empty if it is kernel process
425
433
} else {
426
434
return false ;
427
435
}
@@ -478,7 +486,7 @@ void ProcessMonitor::getTopratedProcesses(
478
486
std::string program_name;
479
487
std::getline (stream, program_name);
480
488
481
- bool flag_find_command_line = getCommandLineFromPiD (info.processId , & info.commandName );
489
+ bool flag_find_command_line = getCommandLineFromPiD (info.processId , info.commandName );
482
490
483
491
if (!flag_find_command_line) {
484
492
info.commandName = program_name; // if command line is not found, use program name instead
0 commit comments