Process::Process(pid_t pid) { //************************************************** // Open and read /proc/<pid>/stat // pid(%d) exe(%s) state(%c) ppid(%d) m_pid = pid; char fn[40]; sprintf(fn,"/proc/%d/stat",m_pid); FILE* fp = fopen(fn,"r"); char* buf = NULL; size_t sz = 0; xalt_fgets_alloc(fp, &buf, &sz); fclose(fp); //************************************************** // extract 2nd (exe) and 4th (ppid) fields from stat char* p = strchr(buf,' '); char* start = ++p; if (*start == '(') start++; p = strchr(p,' '); int n = p - start - 1; if (*p == ')') --n; m_name.assign(start, n); m_parent = (int) strtol(p+3, (char **) NULL, 10); free(buf); }
void Process::cmdline(std::vector<std::string>& cmdlineA) { char fn[40]; //************************************************** // read command line from /proc/<pid>/cmdline sprintf(fn,"/proc/%d/cmdline",m_pid); FILE* fp = fopen(fn,"r"); char* buf = NULL; size_t sz = 0; xalt_fgets_alloc(fp, &buf, &sz); fclose(fp); char * p = buf; while (*p) { cmdlineA.push_back(p); p += strlen(p) + 1; } free(buf); buf = NULL; }
void parseProcMaps(pid_t pid, std::vector<Libpair>& libA, double& t_maps, double& t_sha1) { std::string path; char * buf = NULL; size_t sz = 0; char * fn; double t1 = epoch(); asprintf(&fn,"/proc/%d/maps",pid); FILE* fp = fopen(fn,"r"); if (!fp) return; Set soSet; while(xalt_fgets_alloc(fp, &buf, &sz)) { // Step a: // find the start of the file name char *p = strstr(buf," /"); if (p == NULL) continue; p += 4; // move to the leading slash // Step b: Remove all lines that have "(deleted)" in them const char *q = strstr(p,"(deleted)"); if (q) continue; // Step c: find the filename in the directory q = strrchr(p,'/'); if (q == NULL) continue; // Step d: Find the .so in the file const char * so = strstr(++q,".so"); if (so == NULL) continue; // Step e: make sure that any trailing chars are numbers or a period so += 3; size_t tail = strspn(so,"1234567890."); if (0 < tail && tail < 2) continue; // Step f: remove libxalt_init.so const char *xalt_so = strstr(q,"libxalt_init.so"); if (xalt_so) continue; path.assign(p); soSet.insert(path); } for ( auto const & it : soSet) argV.push_back(Arg(it)); long fnSzG = argV.size(); double t2 = epoch(); t_maps = t2 - t1; compute_sha1_master(fnSzG); // compute sha1sum for all files. for (long i = 0; i < fnSzG; ++i) { Libpair libpair(argV[i].fn, argV[i].sha1); libA.push_back(libpair); } t_sha1 = epoch() - t2; free(buf); sz = 0; buf = NULL; fclose(fp); }