Advertisement
AntonioVillanueva

Analisis PID directorio proc/PID/cmdline

Feb 6th, 2018
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.19 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <dirent.h> // DIR
  3. #include <string>
  4. #include <iostream>
  5. using namespace std;
  6.  
  7. //proc/pid/cmdline
  8. int main(int argc, char* argv[])
  9. {
  10.     DIR* proc;// /proc
  11.    
  12.     FILE *fd ;
  13.     //char cmdline[128];
  14.     string cmdline;
  15.   char *arg = 0; //Lecture cmdline dans le pid  proc/pid/cmdline
  16.     size_t argsize = 0;    
  17.    
  18.     struct dirent *ent;
  19.  
  20.     proc = opendir("/proc");
  21.     while ((ent = readdir(proc)))
  22.       {
  23.         if ((*ent->d_name>'0') && (*ent->d_name<='9')) //numerique ,est un pid
  24.           {
  25.       cout <<"PID NUM="<< ent->d_name<<endl;
  26.      
  27.       //sprintf(cmdline, "/proc/%s/cmdline", ent->d_name);//cmdline
  28.       cmdline+="/proc/"+string (ent->d_name)+"/cmdline";
  29.      
  30.       cout <<"cmdline = "<<cmdline<<endl;
  31.      
  32.       fd = fopen(cmdline.c_str(), "r");//ouvrur cmdline dans le pid
  33.      
  34.       if (fd == NULL) {cout <<"cmdline Error"<<endl;}
  35.       else {      
  36.         while(getdelim(&arg, &argsize, '\0', fd) != -1){
  37.           cout<<'\t'<<" cmdline ="<<arg<<endl;
  38.         }
  39.       }
  40.       if (fd!=NULL) {fclose (fd);};//fermer file descriptor cmdline
  41.           }
  42.           cmdline.clear();
  43.       }
  44.     closedir(proc);
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement