fix processing /proc/<pid>/cgroup

This commit is contained in:
springzfx
2020-05-29 17:14:05 +08:00
parent af78ad2012
commit 75751f4887
2 changed files with 10 additions and 17 deletions

View File

@@ -45,7 +45,7 @@ bool loadExecsnoopLib() {
info("dlsym startThread success");
return true;
} catch (exception &e) {
debug("exception: %s",e.what());
debug("exception: %s", e.what());
return false;
}
}

View File

@@ -108,24 +108,17 @@ string getCgroup(const string &pid) {
string cgroup_f = to_str("/proc/", pid, "/cgroup");
if (!fileExist(cgroup_f)) return "";
stringstream buffer;
string cgroup;
FILE *f = fopen(cgroup_f.c_str(), "r");
char buf[READ_SIZE_MAX] = "";
char *flag = buf;
while (flag != NULL) {
buffer.clear();
while (!flag || buf[strlen(buf) - 1] != '\n') {
flag = fgets(buf, READ_SIZE_MAX, f);
if (flag) buffer << buf;
}
string line = buffer.str();
if (line[0] == '0') { // 0::/user.slice/user-1000.slice
cgroup = (*(line.end() - 1) == '\n') ? line.substr(3, line.length() - 4)
: line.substr(3);
string cgroup, line;
ifstream ifs(cgroup_f);
debug("prcessing file %s", cgroup_f.c_str());
while (ifs.good() && getline(ifs, line)) {
debug("process line: %s", line.c_str());
if (line[0] == '0') {
cgroup = line.substr(3);
debug("get cgroup of %s: %s", pid.c_str(), cgroup.c_str());
break;
}
}
fclose(f);
ifs.close();
return cgroup;
}