From 1d29828d1b63fb350c24440ad7bd037f33cf1364 Mon Sep 17 00:00:00 2001 From: springzfx Date: Tue, 26 May 2020 23:04:12 +0800 Subject: [PATCH] remove path max len limit --- src/cgproxy.hpp | 4 ++-- src/cgroup_attach.cpp | 30 ++++++++++++++++++++---------- src/common.cpp | 11 ++++------- src/common.h | 3 ++- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/src/cgproxy.hpp b/src/cgproxy.hpp index d44769c..6d5d699 100644 --- a/src/cgproxy.hpp +++ b/src/cgproxy.hpp @@ -5,8 +5,8 @@ #include #include using json = nlohmann::json; -using namespace CGPROXY; -using namespace CGPROXY::CONFIG; +using namespace ::CGPROXY; +using namespace ::CGPROXY::CONFIG; namespace CGPROXY::CGPROXY { diff --git a/src/cgroup_attach.cpp b/src/cgroup_attach.cpp index 1f64169..2667727 100644 --- a/src/cgroup_attach.cpp +++ b/src/cgroup_attach.cpp @@ -19,8 +19,8 @@ string get_cgroup2_mount_point() { stringstream buffer; FILE *fp = popen("findmnt -t cgroup2 -n -o TARGET", "r"); if (!fp) return ""; - char buf[64]; - while (fgets(buf, 64, fp) != NULL) { buffer << buf; } + char buf[READ_SIZE_MAX]; + while (fgets(buf, READ_SIZE_MAX, fp) != NULL) { buffer << buf; } pclose(fp); string s = buffer.str(); s.pop_back(); // remove newline character @@ -32,17 +32,27 @@ string getCgroup(const pid_t &pid) { return getCgroup(to_str(pid)); } string getCgroup(const string &pid) { string cgroup_f = to_str("/proc/", pid, "/cgroup"); if (!fileExist(cgroup_f)) return ""; - char buf[128]; + + stringstream buffer; + string cgroup; FILE *f = fopen(cgroup_f.c_str(), "r"); - int id; - char cgroup[256]; - while (fgets(buf, 128, f) != NULL) { - if (buf[0] == '0') { - if (sscanf(buf, "%*i::%s", cgroup) == 1) return cgroup; - error("sscanf %s failed", buf); + 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); + break; } } - return ""; + fclose(f); + return cgroup; } bool validate(string pid, string cgroup) { diff --git a/src/common.cpp b/src/common.cpp index efcc6f6..2879597 100644 --- a/src/common.cpp +++ b/src/common.cpp @@ -1,7 +1,5 @@ #include "common.h" #include -#include -#include #include #include #include @@ -54,7 +52,6 @@ vector bash_pidof(const string &path) { FILE *fp = popen(to_str("pidof ", path).c_str(), "r"); if (!fp) return pids; int pid; - char buf[64]; while (fscanf(fp, "%d", &pid) != EOF) { pids.push_back(pid); } pclose(fp); return pids; @@ -64,8 +61,8 @@ string bash_which(const string &name) { stringstream buffer; FILE *fp = popen(to_str("which ", name).c_str(), "r"); if (!fp) return ""; - char buf[64]; - while (fgets(buf, 64, fp) != NULL) { buffer << buf; } + char buf[READ_SIZE_MAX]; + while (fgets(buf, READ_SIZE_MAX, fp) != NULL) { buffer << buf; } pclose(fp); string s = buffer.str(); s.pop_back(); // remove newline character @@ -76,8 +73,8 @@ string bash_readlink(const string &path) { stringstream buffer; FILE *fp = popen(to_str("readlink -e ", path).c_str(), "r"); if (!fp) return ""; - char buf[64]; - while (fgets(buf, 64, fp) != NULL) { buffer << buf; } + char buf[READ_SIZE_MAX]; + while (fgets(buf, READ_SIZE_MAX, fp) != NULL) { buffer << buf; } pclose(fp); string s = buffer.str(); s.pop_back(); // remove newline character diff --git a/src/common.h b/src/common.h index 24c97db..9d297db 100644 --- a/src/common.h +++ b/src/common.h @@ -15,6 +15,7 @@ using namespace std; #define SOCKET_PATH "/tmp/cgproxy_unix_socket" #define LISTEN_BACKLOG 64 #define DEFAULT_CONFIG_FILE "/etc/cgproxy/config.json" +#define READ_SIZE_MAX 128 #define CGROUP_PROXY_PRESVERED "/proxy.slice" #define CGROUP_NOPROXY_PRESVERED "/noproxy.slice" @@ -87,4 +88,4 @@ string bash_which(const string &name); string bash_readlink(const string &path); string getRealExistPath(const string &name); -#endif \ No newline at end of file +#endif