fix typo, and clang-format cpp

This commit is contained in:
fancy
2020-04-22 20:06:01 +08:00
parent 1724da50f2
commit 891fdcfc93
4 changed files with 77 additions and 76 deletions

View File

@@ -1,80 +1,83 @@
#include <iostream> #include <errno.h>
#include <fstream> #include <fstream>
#include <unistd.h> #include <iostream>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <regex> #include <regex>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
using namespace std; using namespace std;
void print_usage(){ void print_usage() { fprintf(stderr, "usage: cgattach <pid> <cgroup>\n"); }
fprintf(stderr, "usage: cgattach <pid> <cgroup>\n");
bool exist(string path) {
struct stat st;
if (stat(path.c_str(), &st) != -1) {
return S_ISDIR(st.st_mode);
}
return false;
} }
bool exist(string path){ bool validate(string pid, string cgroup) {
struct stat st; bool pid_v = regex_match(pid, regex("^[0-9]+$"));
if (stat(path.c_str(),&st)!=-1){ bool cg_v = regex_match(cgroup, regex("^\\/[a-zA-Z0-9\\-_./@]+$"));
return S_ISDIR(st.st_mode); if (pid_v && cg_v)
} return true;
return false; // cout<<pid_v<<" "<<cg_v<<endl;
print_usage();
exit(EXIT_FAILURE);
} }
bool validate(string pid, string cgroup){ int main(int argc, char *argv[]) {
bool pid_v=regex_match(pid,regex("^[0-9]+$")); setuid(0);
bool cg_v=regex_match(cgroup,regex("^\\/[a-zA-Z0-9\\-_./@]+$")); setgid(0);
if (pid_v && cg_v) return true; if (getuid() != 0 || getgid() != 0) {
// cout<<pid_v<<" "<<cg_v<<endl; fprintf(stderr, "cgattach need setuid sticky bit\n");
exit(EXIT_FAILURE);
}
if (argc != 3) {
print_usage(); print_usage();
exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }
int main(int argc,char *argv[]){ string pid = string(argv[1]);
setuid(0); string cgroup_target = string(argv[2]);
setgid(0); validate(pid, cgroup_target);
if (getuid()!=0||getgid()!=0){ string cgroup_mount_point = "/sys/fs/cgroup";
fprintf(stderr, "cgattach need setuid sticky bit\n"); string cgroup_target_path = cgroup_mount_point + cgroup_target;
exit(EXIT_FAILURE); string cgroup_target_procs = cgroup_target_path + "/cgroup.procs";
}
// check if exist, we won't create it if not exist
if (argc!=3){ if (!exist(cgroup_target_path)) {
print_usage(); if (mkdir(cgroup_target_path.c_str(),
exit(EXIT_FAILURE); S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH) == 0) {
} fprintf(stdout, "created cgroup %s success\n", cgroup_target.c_str());
} else {
string pid=string(argv[1]); fprintf(stderr, "created cgroup %s failed, errno %d\n",
string cgroup_target=string(argv[2]); cgroup_target.c_str(), errno);
validate(pid, cgroup_target); exit(EXIT_FAILURE);
string cgroup_mount_point="/sys/fs/cgroup"; }
string cgroup_target_path=cgroup_mount_point+cgroup_target; // fprintf(stderr, "cgroup %s not exist\n",cgroup_target.c_str());
string cgroup_target_procs=cgroup_target_path+"/cgroup.procs"; // only support cgroup v2 // exit(EXIT_FAILURE);
}
// check if exist, we won't create it if not exist
if (!exist(cgroup_target_path)){ // put pid to target cgroup
if (mkdir(cgroup_target_path.c_str(), S_IRWXU|S_IRGRP|S_IXGRP|S_IROTH|S_IXOTH)==0){ ofstream procs(cgroup_target_procs, ofstream::app);
fprintf(stdout, "created cgroup %s success\n",cgroup_target.c_str()); if (!procs.is_open()) {
}else{ fprintf(stderr, "open file %s failed\n", cgroup_target_procs.c_str());
fprintf(stderr, "created cgroup %s failed\n",cgroup_target.c_str()); exit(EXIT_FAILURE);
exit(EXIT_FAILURE); }
} procs << pid.c_str() << endl;
// fprintf(stderr, "cgroup %s not exist\n",cgroup_target.c_str()); procs.close();
// exit(EXIT_FAILURE);
} // maybe there some write error, for example process pid not exist
if (!procs) {
// put pid to target cgroup fprintf(stderr, "write %s to %s failed, maybe process %s not exist\n",
ofstream procs(cgroup_target_procs,ofstream::app); pid.c_str(), cgroup_target_procs.c_str(), pid.c_str());
if (!procs.is_open()){ exit(EXIT_FAILURE);
fprintf(stderr, "open file %s failed\n",cgroup_target_procs.c_str()); }
exit(EXIT_FAILURE); return EXIT_SUCCESS;
}
procs<<pid.c_str()<<endl;
procs.close();
// maybe there some write error, for example process pid not exist
if (!procs){
fprintf(stderr, "write %s to %s failed, maybe process %s not exist\n",pid.c_str(),cgroup_target_procs.c_str(),pid.c_str());
exit(EXIT_FAILURE);
}
return EXIT_SUCCESS;
} }

View File

@@ -20,5 +20,4 @@ v2ray_so_mark=255
## do not modify this if you don't known what you are doing ## do not modify this if you don't known what you are doing
table=100 table=100
mark=100 mark=100
mark_newin=1 mark_newin=1
v2ray_so_mark=255

View File

@@ -51,7 +51,7 @@ It is alreay in [archlinux AUR](https://aur.archlinux.org/packages/cgproxy/).
sudo systemctl status cgproxy.service sudo systemctl status cgproxy.service
``` ```
- Then prefix with cgproxy with you command, just like proxychains - Then prefix with cgproxy with your command, just like proxychains
``` ```
cgproxy <CMD> cgproxy <CMD>
@@ -89,7 +89,6 @@ v2ray_so_mark=255
table=100 table=100
mark=100 mark=100
mark_newin=1 mark_newin=1
v2ray_so_mark=255
``` ```
If you changed config, remember to restart service If you changed config, remember to restart service
@@ -111,9 +110,9 @@ sudo systemctl restart cgproxy.service
- `run_in_cgroup` run command in specific cgroup which will create if not exist , cgroup can be only one level down exist cgroup, otherwise created fail. - `run_in_cgroup` run command in specific cgroup which will create if not exist , cgroup can be only one level down exist cgroup, otherwise created fail.
```bash ```bash
run_in_cgroup --cggroup=CGROUP <COMMAND> run_in_cgroup --cgroup=CGROUP <COMMAND>
# example # example
run_in_cgroup --cggroup=/mycgroup.slice ping 127.0.0.1 run_in_cgroup --cgroup=/mycgroup.slice ping 127.0.0.1
``` ```

View File

@@ -3,7 +3,7 @@
print_help(){ print_help(){
cat << 'DOC' cat << 'DOC'
usage: usage:
run_in_cgroup --cggroup=CGROUP <COMMAND> run_in_cgroup --cgroup=CGROUP <COMMAND>
run_in_cgroup --help run_in_cgroup --help
note: note:
CGROUP must start will slash '/' , and no special character CGROUP must start will slash '/' , and no special character