From 75751f48872f283b4c593d7d7e72ba98abf5342a Mon Sep 17 00:00:00 2001 From: springzfx Date: Fri, 29 May 2020 17:14:05 +0800 Subject: [PATCH] fix processing /proc//cgroup --- src/cgproxyd.hpp | 2 +- src/common.cpp | 25 +++++++++---------------- 2 files changed, 10 insertions(+), 17 deletions(-) diff --git a/src/cgproxyd.hpp b/src/cgproxyd.hpp index 417b115..d5c129a 100644 --- a/src/cgproxyd.hpp +++ b/src/cgproxyd.hpp @@ -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; } } diff --git a/src/common.cpp b/src/common.cpp index b569ede..a1a1000 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -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; }