diff --git a/src/cgproxyd.hpp b/src/cgproxyd.hpp index 769786f..d36b006 100644 --- a/src/cgproxyd.hpp +++ b/src/cgproxyd.hpp @@ -13,6 +13,7 @@ #include #include #include +#include #include #include #include @@ -35,13 +36,12 @@ bool loadExecsnoopLib() { error("dlopen %s failed: %s", LIBEXECSNOOP_SO, dlerror()); return false; } - _startThread = - reinterpret_cast(dlsym(handle_dl, "startThread")); + _startThread = reinterpret_cast(dlsym(handle_dl, "startThread")); if (_startThread == NULL) { - error("dlsym startThread failed: %s", dlerror()); + error("dlsym startThread func failed: %s", dlerror()); return false; } - info("dlsym startThread success"); + info("dlsym startThread func success"); return true; } catch (exception &e) { debug("exception: %s", e.what()); @@ -57,11 +57,8 @@ bool enable_socketserver = true; bool enable_execsnoop = false; class cgproxyd { - SOCKET::thread_arg socketserver_thread_arg; - pthread_t socket_thread_id = THREAD_UNDEF; - - EXECSNOOP::thread_arg execsnoop_thread_arg; - pthread_t execsnoop_thread_id = THREAD_UNDEF; + thread socketserver_thread; + thread execsnoop_thread; Config config; @@ -185,33 +182,29 @@ class cgproxyd { } } - pthread_t startSocketListeningThread() { - socketserver_thread_arg.handle_msg = &handle_msg_static; - pthread_t thread_id; - int status = - pthread_create(&thread_id, NULL, &SOCKET::startThread, &socketserver_thread_arg); - if (status != 0) { - error("socket thread create failed"); - return THREAD_UNDEF; - } - return thread_id; + void startSocketListeningThread() { + promise status; + future status_f = status.get_future(); + thread th(SOCKET::startThread, handle_msg_static, move(status)); + socketserver_thread = move(th); + + status_f.wait(); + info("socketserver thread started"); } - pthread_t startExecsnoopThread() { + void startExecsnoopThread() { if (!EXECSNOOP::loadExecsnoopLib() || EXECSNOOP::_startThread == NULL) { - error("execsnoop start failed, maybe bcc not installed"); - return THREAD_UNDEF; + error("execsnoop not ready to start, maybe bcc not installed"); + return; } - 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"); - return THREAD_UNDEF; - } - return thread_id; + promise status; + future status_f = status.get_future(); + thread th(EXECSNOOP::_startThread, handle_pid_static, move(status)); + execsnoop_thread = move(th); + + status_f.wait(); + info("execsnoop thread started"); } void processRunningProgram() { @@ -247,17 +240,12 @@ public: applyConfig(); processRunningProgram(); - 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"); - } + if (enable_socketserver) startSocketListeningThread(); + if (enable_execsnoop) startExecsnoopThread(); + + if (socketserver_thread.joinable()) socketserver_thread.join(); + if (execsnoop_thread.joinable()) execsnoop_thread.join(); - pthread_join(socket_thread_id, NULL); - pthread_join(execsnoop_thread_id, NULL); return 0; } int applyConfig() { diff --git a/src/execsnoop.cpp b/src/execsnoop.cpp index 75f8caa..f32ecc8 100644 --- a/src/execsnoop.cpp +++ b/src/execsnoop.cpp @@ -90,11 +90,10 @@ int execsnoop() { return 0; } -void *startThread(void *arg) { - thread_arg *p = (thread_arg *)arg; - callback = p->handle_pid; +void startThread(function c, promise status) { + status.set_value(); + callback = c; execsnoop(); - return (void *)0; } } // namespace CGPROXY::EXECSNOOP \ No newline at end of file diff --git a/src/execsnoop.h b/src/execsnoop.h index d3d53ed..93eb22e 100644 --- a/src/execsnoop.h +++ b/src/execsnoop.h @@ -2,6 +2,7 @@ #define EXECSNOOP_HPP 1 #include +#include #include using namespace std; @@ -13,11 +14,9 @@ extern function callback; void handle_events(void *cb_cookie, void *data, int data_size); int execsnoop(); -struct thread_arg { - function handle_pid; -}; -extern "C" void *startThread(void *arg); -decltype(&startThread) _startThread; // only for dlsym() +extern "C" void startThread(function c, promise status); +typedef void startThread_t(function, promise); +startThread_t *_startThread; // only for dlsym() } // namespace CGPROXY::EXECSNOOP #endif \ No newline at end of file diff --git a/src/socket_server.cpp b/src/socket_server.cpp index e8d0fad..f20a6ce 100644 --- a/src/socket_server.cpp +++ b/src/socket_server.cpp @@ -49,17 +49,16 @@ void SocketServer::socketListening(function callback) { } } -void *startThread(void *arg) { - thread_arg *p = (thread_arg *)arg; - SocketServer server; - server.socketListening(p->handle_msg); - return (void *)0; -} - SocketServer::~SocketServer() { close(sfd); close(cfd); unlink(SOCKET_PATH); } +void startThread(function callback, promise status) { + status.set_value(); + SocketServer server; + server.socketListening(callback); +} + } // namespace CGPROXY::SOCKET \ No newline at end of file diff --git a/src/socket_server.h b/src/socket_server.h index b5f84a1..166d503 100644 --- a/src/socket_server.h +++ b/src/socket_server.h @@ -2,6 +2,7 @@ #define SOCKET_SERVER_H #include +#include #include #include using namespace std; @@ -14,11 +15,6 @@ namespace CGPROXY::SOCKET { continue; \ } -struct thread_arg { - function handle_msg; -}; -void *startThread(void *arg); - class SocketServer { public: int sfd = -1, cfd = -1, flag = -1; @@ -28,6 +24,8 @@ public: ~SocketServer(); }; +void startThread(function callback, promise status); + } // namespace CGPROXY::SOCKET #endif \ No newline at end of file