From 87cd5a6d990921b1d2cdcd661a4cccd8fc816d4e Mon Sep 17 00:00:00 2001 From: fancy Date: Sat, 16 May 2020 00:17:37 +0800 Subject: [PATCH] missing one file --- CMakeLists.txt | 2 +- cgroup_attach.h | 96 +++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 97 insertions(+), 1 deletion(-) create mode 100644 cgroup_attach.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 7b84937..51401e2 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -31,7 +31,7 @@ install(FILES cgnoproxy.sh DESTINATION /usr/bin install(FILES cgproxy.service DESTINATION /usr/lib/systemd/system/) -install(FILES cgproxy.json +install(FILES config.json DESTINATION /etc/cgproxy/) install(FILES cgroup-tproxy.sh DESTINATION /usr/share/cgproxy/scripts/) diff --git a/cgroup_attach.h b/cgroup_attach.h new file mode 100644 index 0000000..b254611 --- /dev/null +++ b/cgroup_attach.h @@ -0,0 +1,96 @@ +#ifndef CGPROUP_ATTACH_H +#define CGPROUP_ATTACH_H + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "common.h" +using namespace std; + +namespace CGPROXY::CGROUP{ + +bool exist(string path) { + struct stat st; + if (stat(path.c_str(), &st) != -1) { + return S_ISDIR(st.st_mode); + } + return false; +} + +bool validate(string pid, string cgroup) { + bool pid_v = validPid(pid); + bool cg_v = validCgroup(cgroup); + if (pid_v && cg_v) + return true; + + error("paramater validate error"); + return_error +} + +string get_cgroup2_mount_point(int &status){ + char cgroup2_mount_point[100]=""; + FILE* fp = popen("findmnt -t cgroup2 -n -o TARGET", "r"); + int count=fscanf(fp,"%s",&cgroup2_mount_point); + fclose(fp); + if (count=0){ + error("cgroup2 not supported"); + status=-1; + return NULL; + } + status=0; + return cgroup2_mount_point; +} + +int attach(const string pid, const string cgroup_target) { + if (getuid()!=0) { + error("need root to attach cgroup"); + return_error + } + int status; + validate(pid, cgroup_target); + string cgroup_mount_point = get_cgroup2_mount_point(status); + if (status!=0) return_error + string cgroup_target_path = cgroup_mount_point + cgroup_target; + string cgroup_target_procs = cgroup_target_path + "/cgroup.procs"; + + // check if exist, we will create it if not exist + if (!exist(cgroup_target_path)) { + if (mkdir(cgroup_target_path.c_str(), + S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) { + debug("created cgroup %s success", cgroup_target.c_str()); + } else { + error("created cgroup %s failed, errno %d", cgroup_target.c_str(), errno); + return_error + } + // error("cgroup %s not exist",cgroup_target.c_str()); + // return_error + } + + // put pid to target cgroup + ofstream procs(cgroup_target_procs, ofstream::app); + if (!procs.is_open()) { + error("open file %s failed", cgroup_target_procs.c_str()); + return_error + } + procs << pid.c_str() << endl; + procs.close(); + + // maybe there some write error, for example process pid may not exist + if (!procs) { + error("write %s to %s failed, maybe process %s not exist", + pid.c_str(), cgroup_target_procs.c_str(), pid.c_str()); + return_error + } + return_success +} + +} + +#endif \ No newline at end of file