#include #include #include // for opendir(), readdir(), closedir() #include // for stat() #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include "proc_read.hh" #define PROC_DIRECTORY "/proc/" #define CASE_SENSITIVE 1 #define CASE_INSENSITIVE 0 #define EXACT_MATCH 1 #define INEXACT_MATCH 0 //------------------------------------------------------------ std::string getUserName(uid_t uid) { // uid_t uid = geteuid(); struct passwd *pw = getpwuid(uid); if (pw) { return std::string(pw->pw_name); } return ""; } //------------------------------------------------------------ int IsNumeric(const char* cc_CharacterList) { for ( ; *cc_CharacterList; cc_CharacterList++) if (*cc_CharacterList < '0' || *cc_CharacterList > '9') return 0; // false return 1; // true } int strcmp_Wrapper(const char *s1, const char *s2, int intCaseSensitive) { if (intCaseSensitive) return !strcmp(s1, s2); else return !strcasecmp(s1, s2); } int strstr_Wrapper(const char* haystack, const char* needle, int intCaseSensitive) { if (intCaseSensitive) if (strstr(haystack, needle)) return 1; else return 0; else if (strcasestr(haystack, needle)) return 1; else return 0; } //--------------------------------------------------------------------------------------------- const int LENSTR=512; std::vector GetPIDbyName(std::string c_ProcessName, int intCaseSensitiveness, int intExactMatch) { char str_CommandLinePath[LENSTR] ; char str_NameOfProcess[LENSTR] ; char* chrptr_StringToCompare = NULL ; pid_t pid_ProcessIdentifier = (pid_t) -1 ; struct dirent* de_DirEntity = NULL ; DIR* dir_proc = NULL ; std::vector PIDV; printf("GetPIDbyName:2: %s \n",c_ProcessName.c_str()); int (*CompareFunction) (const char*, const char*, int) ; if (intExactMatch) CompareFunction = &strcmp_Wrapper; else CompareFunction = &strstr_Wrapper; dir_proc = opendir(PROC_DIRECTORY) ; if (dir_proc == NULL) { perror("Couldn't open the " PROC_DIRECTORY " directory") ; return PIDV ; } // Loop while not NULL while ( (de_DirEntity = readdir(dir_proc)) ) { if (de_DirEntity->d_type == DT_DIR) { //printf("GetPIDbyName:4a: %s \n",de_DirEntity->d_name); if (IsNumeric(de_DirEntity->d_name)) { //printf("GetPIDbyName:4b: yes %s \n",de_DirEntity->d_name); //-- read status file --- strcpy(str_CommandLinePath, PROC_DIRECTORY) ; strcat(str_CommandLinePath, de_DirEntity->d_name) ; strcat(str_CommandLinePath, "/status") ; std::ifstream *fs = new std::ifstream(str_CommandLinePath,std::ios_base::in); if(fs->is_open() != true) { //--- !iniFile) { printf("ERROR:: Cannot open input file=%s\n",str_CommandLinePath); continue; } else { std::string str,F0,F1; PLIST pid; int FLAG=0; int lncnt=0; while( std::getline(*fs,str) ) { //copy String from inifile //std::cout << "status:: " << str << std::endl; lncnt++; // construct a stream from the string std::stringstream strstr(str); // use stream iterators to copy the stream to the vector as whitespace separated strings std::istream_iterator it(strstr); std::istream_iterator end; std::vector words(it, end); //--------------- skip empty lines and less than 2 params !!! ---- if (words.size()<2) continue; //----------------------- F0=words.at(0); F1=words.at(1); //----------- Single Word !!! ----- if (F0=="Name:") { //if (c_ProcessName==F1 ) { std::size_t found = F1.find(c_ProcessName); if (found==std::string::npos) { /*printf("string %s not founf in %s\n",c_ProcessName.c_str(),F1.c_str());*/ break;} if ((F1.size()>=c_ProcessName.size() && c_ProcessName==F1.substr(0,c_ProcessName.size())) || intExactMatch==0 ) { printf("Naaaaaaaaaaaaaaaaaaaaaaame: %s\n",F1.c_str()); pid.Name=F1; pid.PID = (pid_t) atoi(de_DirEntity->d_name) ; std::string rol = GetPIDenvironment("CODA_ROL=",pid.PID); std::cout << "found:rol: " << rol << " pid=" << pid.PID << " line=" << lncnt <> pid.VmSize; if (FLAG && F0=="PPid:") std::istringstream (F1) >> pid.PPID; if (FLAG && F0=="Uid:") std::istringstream (F1) >> pid.UID; if (FLAG && F0=="Threads:") std::istringstream (F1) >> pid.Threads; if (FLAG && F0=="State:") { pid.State=F1; printf("State:1: %s %s\n",F1.c_str(),pid.State.c_str()); } } // while loop lines if (FLAG) { PIDV.push_back(pid); printf("State:2: %s \n",pid.State.c_str() ); } fs->close(); delete fs; } } // if (IsNumeric(de_DirEntity->d_name)) } // if dirEntry } // while dirEntry closedir(dir_proc) ; return PIDV ; } //------------------------------------------------------- std::vector GetPIDbyName(std::string c_ProcessName) { //printf("GetPIDbyName:1: %s \n",c_ProcessName.c_str()); return GetPIDbyName(c_ProcessName, CASE_INSENSITIVE, EXACT_MATCH) ; } //------------------------------------------------------- std::string GetPIDenvironment (char *ENVAR, pid_t pid) { // CODA_ROL=/home/furletov/ttt/daq_pro_vers/daq/vme/src/rol_1 char filename[256]; char environment[8192]; size_t length; char* next_var; std::string str; snprintf(filename, sizeof (filename), "/proc/%d/environ", (int) pid); std::ifstream *fs = new std::ifstream(filename,std::ios_base::in); if(fs->is_open() != true) { //--- printf("ERROR:: Cannot open input file=%s\n",filename); } else { while( std::getline(*fs,str,'\0') ) { //copy String from inifile std::cout << "environ:: " << str << std::endl; std::string str2 (ENVAR); std::size_t found = str.find(str2); if (found!=std::string::npos) { std::cout << "found:: " << str << std::endl; break; } } } fs->close(); delete fs; return str; } //===================================== /* int main(int argc,char ** argv) { const char *ps = "Monitor"; //printf("argc=%d proc=%s\n",argc,ps); std::vector VPID; if (argc > 1) { printf(" search process: %s \n",argv[1]); ps=argv[1]; } else { printf("\n Usage:: %s \n\n",argv[0]); exit(1); } VPID = GetPIDbyName(std::string(ps)) ; printf("PID size=%d proc=%s\n", VPID.size(),ps); std::string tmp; for (int ip=0; ip