From 221a75ae7bb7f268bc417158daae6383d0d26b0b Mon Sep 17 00:00:00 2001 From: springzfx Date: Mon, 25 May 2020 17:03:17 +0800 Subject: [PATCH] clang format --- src/cgproxyd.hpp | 101 +++++++++++++++++++++++++----------------- src/cgroup_attach.cpp | 5 ++- src/common.cpp | 17 ++++--- src/common.h | 2 + src/config.cpp | 5 ++- src/execsnoop.cpp | 9 ++-- src/execsnoop.h | 4 +- 7 files changed, 83 insertions(+), 60 deletions(-) diff --git a/src/cgproxyd.hpp b/src/cgproxyd.hpp index 5ed6bf2..6214de5 100644 --- a/src/cgproxyd.hpp +++ b/src/cgproxyd.hpp @@ -4,10 +4,12 @@ #include "cgroup_attach.h" #include "common.h" #include "config.h" +#include "execsnoop.h" #include "socket_server.h" #include #include #include +#include #include #include #include @@ -16,33 +18,35 @@ #include #include #include -#include -#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 (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(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< 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; diff --git a/src/cgroup_attach.cpp b/src/cgroup_attach.cpp index ac06585..09b0e0a 100644 --- a/src/cgroup_attach.cpp +++ b/src/cgroup_attach.cpp @@ -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< +#include #include #include #include #include -#include 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 bash_pidof(const string &path){ +vector bash_pidof(const string &path) { vector 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< &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 \ No newline at end of file diff --git a/src/execsnoop.cpp b/src/execsnoop.cpp index 0d91540..ef96582 100644 --- a/src/execsnoop.cpp +++ b/src/execsnoop.cpp @@ -1,3 +1,4 @@ +#include "execsnoop.h" #include "bcc/BPF.h" #include "common.h" #include @@ -6,7 +7,6 @@ #include #include #include -#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); -} \ No newline at end of file +extern "C" void *_startThread(void *arg) { return CGPROXY::EXECSNOOP::startThread(arg); } \ No newline at end of file diff --git a/src/execsnoop.h b/src/execsnoop.h index ef6c64c..eec3561 100644 --- a/src/execsnoop.h +++ b/src/execsnoop.h @@ -11,12 +11,12 @@ extern const string BPF_PROGRAM; struct data_t; extern function callback; void handle_events(void *cb_cookie, void *data, int data_size); -int execsnoop(); +int execsnoop(); struct thread_arg { function handle_pid; }; void *startThread(void *arg); -} // namespace CGPROXY::EXESNOOP +} // namespace CGPROXY::EXECSNOOP #endif \ No newline at end of file