remove path max len limit

This commit is contained in:
springzfx
2020-05-26 23:04:12 +08:00
parent 4fea0d39a2
commit 1d29828d1b
4 changed files with 28 additions and 20 deletions

View File

@@ -5,8 +5,8 @@
#include <nlohmann/json.hpp>
#include <unistd.h>
using json = nlohmann::json;
using namespace CGPROXY;
using namespace CGPROXY::CONFIG;
using namespace ::CGPROXY;
using namespace ::CGPROXY::CONFIG;
namespace CGPROXY::CGPROXY {

View File

@@ -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) {

View File

@@ -1,7 +1,5 @@
#include "common.h"
#include <fstream>
#include <limits.h>
#include <linux/limits.h>
#include <regex>
#include <sys/stat.h>
#include <unistd.h>
@@ -54,7 +52,6 @@ vector<int> 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

View File

@@ -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
#endif