clang format

This commit is contained in:
springzfx
2020-05-25 17:03:17 +08:00
parent 16a341205f
commit 221a75ae7b
7 changed files with 83 additions and 60 deletions

View File

@@ -4,10 +4,12 @@
#include "cgroup_attach.h"
#include "common.h"
#include "config.h"
#include "execsnoop.h"
#include "socket_server.h"
#include <algorithm>
#include <csignal>
#include <cstdlib>
#include <dlfcn.h>
#include <exception>
#include <fstream>
#include <functional>
@@ -16,33 +18,35 @@
#include <sched.h>
#include <sys/file.h>
#include <unistd.h>
#include <dlfcn.h>
#include "execsnoop.h"
using namespace std;
using json = nlohmann::json;
using namespace ::CGPROXY::SOCKET;
using namespace ::CGPROXY::CONFIG;
using namespace ::CGPROXY::CGROUP;
// using namespace ::CGPROXY::EXESNOOP;
// using namespace ::CGPROXY::EXECSNOOP;
namespace CGPROXY::EXECSNOOP{
typedef void* (*startThread_t)(void *arg);
startThread_t _startThread;
bool loadExecsnoopLib(){
try {
info("loading %s",LIBEXECSNOOP_SO);
void* handle_dl=dlopen(LIBEXECSNOOP_SO,RTLD_NOW);
if (handle_dl==NULL) {error("dlopen %s failed: %s",LIBEXECSNOOP_SO, dlerror());return false;}
_startThread= reinterpret_cast<startThread_t> (dlsym(handle_dl, "_startThread"));
if (_startThread==NULL){error("dlsym startThread failed: %s",dlerror());return false;}
info("dlsym startThread success");
return true;
} catch (exception &e) {
namespace CGPROXY::EXECSNOOP {
typedef void *(*startThread_t)(void *arg);
startThread_t _startThread;
bool loadExecsnoopLib() {
try {
info("loading %s", LIBEXECSNOOP_SO);
void *handle_dl = dlopen(LIBEXECSNOOP_SO, RTLD_NOW);
if (handle_dl == NULL) {
error("dlopen %s failed: %s", LIBEXECSNOOP_SO, dlerror());
return false;
}
}
_startThread = reinterpret_cast<startThread_t>(dlsym(handle_dl, "_startThread"));
if (_startThread == NULL) {
error("dlsym startThread failed: %s", dlerror());
return false;
}
info("dlsym startThread success");
return true;
} catch (exception &e) { return false; }
}
} // namespace CGPROXY::EXECSNOOP
namespace CGPROXY::CGPROXYD {
@@ -52,10 +56,10 @@ bool enable_execsnoop = false;
class cgproxyd {
SOCKET::thread_arg socketserver_thread_arg;
pthread_t socket_thread_id = -1;
pthread_t socket_thread_id = THREAD_UNDEF;
EXECSNOOP::thread_arg execsnoop_thread_arg;
pthread_t execsnoop_thread_id = -1;
pthread_t execsnoop_thread_id = THREAD_UNDEF;
Config config;
@@ -77,8 +81,8 @@ class cgproxyd {
}
int handle_pid(int pid) {
auto path=realpath(to_str("/proc/",pid,"/exe").c_str(), NULL);
if (path==NULL) {
auto path = realpath(to_str("/proc/", pid, "/exe").c_str(), NULL);
if (path == NULL) {
debug("pid %d live life too short", pid);
return 0;
}
@@ -88,13 +92,13 @@ class cgproxyd {
v = config.program_noproxy;
if (find(v.begin(), v.end(), path) != v.end()) {
info("exesnoop noproxy: %d %s", pid, path);
info("execsnoop noproxy: %d %s", pid, path);
free(path);
return attach(pid, config.cgroup_noproxy_preserved);
}
v = config.program_proxy;
if (find(v.begin(), v.end(), path) != v.end()) {
info("exesnoop proxied: %d %s", pid, path);
info("execsnoop proxied: %d %s", pid, path);
free(path);
return attach(pid, config.cgroup_proxy_preserved);
}
@@ -179,32 +183,41 @@ class cgproxyd {
pthread_t thread_id;
int status =
pthread_create(&thread_id, NULL, &SOCKET::startThread, &socketserver_thread_arg);
if (status != 0) error("socket thread create failed");
if (status != 0) {
error("socket thread create failed");
return THREAD_UNDEF;
}
return thread_id;
}
pthread_t startExecsnoopThread() {
if (!EXECSNOOP::loadExecsnoopLib()||EXECSNOOP::_startThread==NULL) {error("execsnoop start failed");exit(EXIT_FAILURE);}
if (!EXECSNOOP::loadExecsnoopLib() || EXECSNOOP::_startThread == NULL) {
error("execsnoop start failed, maybe bcc not installed");
return THREAD_UNDEF;
}
execsnoop_thread_arg.handle_pid = &handle_pid_static;
pthread_t thread_id;
int status =
pthread_create(&thread_id, NULL, EXECSNOOP::_startThread, &execsnoop_thread_arg);
if (status != 0) error("execsnoop thread create failed");
if (status != 0) {
error("execsnoop thread create failed");
return THREAD_UNDEF;
}
return thread_id;
}
void processRunningProgram(){
debug("process running program")
for (auto &path:config.program_noproxy)
for (auto &pid:bash_pidof(path)){
int status=attach(pid, config.cgroup_noproxy_preserved);
if (status==0) info("noproxy running process %d %s",pid, path.c_str());
}
for (auto &path:config.program_proxy)
for (auto &pid:bash_pidof(path)){
int status=attach(pid, config.cgroup_proxy_preserved);
if (status==0) info("proxied running process %d %s",pid, path.c_str());
void processRunningProgram() {
debug("process running program") for (auto &path :
config.program_noproxy) for (auto &pid :
bash_pidof(path)) {
int status = attach(pid, config.cgroup_noproxy_preserved);
if (status == 0) info("noproxy running process %d %s", pid, path.c_str());
}
for (auto &path : config.program_proxy)
for (auto &pid : bash_pidof(path)) {
int status = attach(pid, config.cgroup_proxy_preserved);
if (status == 0) info("proxied running process %d %s", pid, path.c_str());
}
}
@@ -223,10 +236,16 @@ public:
applyConfig();
processRunningProgram();
if (enable_socketserver) { socket_thread_id = startSocketListeningThread(); }
if (enable_execsnoop) { execsnoop_thread_id = startExecsnoopThread(); }
cout<<flush;
if (enable_socketserver) {
socket_thread_id = startSocketListeningThread();
if (socket_thread_id > 0) info("socket server listening thread started");
}
if (enable_execsnoop) {
execsnoop_thread_id = startExecsnoopThread();
if (execsnoop_thread_id > 0) info("execsnoop thread started");
}
cout << flush;
pthread_join(socket_thread_id, NULL);
pthread_join(execsnoop_thread_id, NULL);
return 0;

View File

@@ -25,9 +25,10 @@ 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[64];
while (fgets(buf, 64, fp) != NULL) { buffer << buf; }
pclose(fp);
string s=buffer.str();
string s = buffer.str();
s.pop_back(); // remove newline character
return s;
}

View File

@@ -1,10 +1,10 @@
#include "common.h"
#include <fstream>
#include <limits.h>
#include <linux/limits.h>
#include <regex>
#include <sys/stat.h>
#include <unistd.h>
#include <limits.h>
bool enable_debug = false;
bool enable_info = true;
@@ -49,12 +49,13 @@ bool dirExist(const string &path) {
return (stat(path.c_str(), &st) == 0 && S_ISDIR(st.st_mode));
}
vector<int> bash_pidof(const string &path){
vector<int> bash_pidof(const string &path) {
vector<int> pids;
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); }
char buf[64];
while (fscanf(fp, "%d", &pid) != EOF) { pids.push_back(pid); }
pclose(fp);
return pids;
}
@@ -63,9 +64,10 @@ 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[64];
while (fgets(buf, 64, fp) != NULL) { buffer << buf; }
pclose(fp);
string s=buffer.str();
string s = buffer.str();
s.pop_back(); // remove newline character
return s;
}
@@ -74,9 +76,10 @@ 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[64];
while (fgets(buf, 64, fp) != NULL) { buffer << buf; }
pclose(fp);
string s=buffer.str();
string s = buffer.str();
s.pop_back(); // remove newline character
return s;
}

View File

@@ -19,6 +19,8 @@ using namespace std;
#define CGROUP_PROXY_PRESVERED "/proxy.slice"
#define CGROUP_NOPROXY_PRESVERED "/noproxy.slice"
#define THREAD_UNDEF 0
#define MSG_TYPE_CONFIG_JSON 1
#define MSG_TYPE_CONFIG_PATH 2
#define MSG_TYPE_PROXY_PID 3

View File

@@ -145,9 +145,10 @@ void Config::toRealProgramPath(vector<string> &v) {
for (auto &p : v) {
auto rpath = getRealExistPath(p);
if (!rpath.empty()) tmp.push_back(rpath);
else error("%s not exist or broken link", p.c_str());
else
error("%s not exist or broken link", p.c_str());
}
v=tmp;
v = tmp;
}
} // namespace CGPROXY::CONFIG

View File

@@ -1,3 +1,4 @@
#include "execsnoop.h"
#include "bcc/BPF.h"
#include "common.h"
#include <bcc/libbpf.h>
@@ -6,7 +7,6 @@
#include <iostream>
#include <string>
#include <unistd.h>
#include "execsnoop.h"
using namespace std;
namespace CGPROXY::EXECSNOOP {
@@ -97,9 +97,6 @@ void *startThread(void *arg) {
return (void *)0;
}
} // namespace CGPROXY::EXECSNOOP
} // namespace CGPROXY::EXESNOOP
extern "C" void *_startThread(void *arg) {
return CGPROXY::EXECSNOOP::startThread(arg);
}
extern "C" void *_startThread(void *arg) { return CGPROXY::EXECSNOOP::startThread(arg); }

View File

@@ -11,12 +11,12 @@ extern const string BPF_PROGRAM;
struct data_t;
extern function<int(int)> callback;
void handle_events(void *cb_cookie, void *data, int data_size);
int execsnoop();
int execsnoop();
struct thread_arg {
function<int(int)> handle_pid;
};
void *startThread(void *arg);
} // namespace CGPROXY::EXESNOOP
} // namespace CGPROXY::EXECSNOOP
#endif