replace pthread with c++ thread

This commit is contained in:
springzfx
2020-05-30 00:14:18 +08:00
parent cc83c1ae55
commit ec9609cec8
5 changed files with 45 additions and 62 deletions

View File

@@ -13,6 +13,7 @@
#include <exception>
#include <fstream>
#include <functional>
#include <future>
#include <nlohmann/json.hpp>
#include <pthread.h>
#include <sched.h>
@@ -35,13 +36,12 @@ bool loadExecsnoopLib() {
error("dlopen %s failed: %s", LIBEXECSNOOP_SO, dlerror());
return false;
}
_startThread =
reinterpret_cast<decltype(&startThread)>(dlsym(handle_dl, "startThread"));
_startThread = reinterpret_cast<startThread_t *>(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<void> status;
future<void> 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<void> status;
future<void> 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() {

View File

@@ -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<int(int)> c, promise<void> status) {
status.set_value();
callback = c;
execsnoop();
return (void *)0;
}
} // namespace CGPROXY::EXECSNOOP

View File

@@ -2,6 +2,7 @@
#define EXECSNOOP_HPP 1
#include <functional>
#include <future>
#include <string>
using namespace std;
@@ -13,11 +14,9 @@ extern function<int(int)> callback;
void handle_events(void *cb_cookie, void *data, int data_size);
int execsnoop();
struct thread_arg {
function<int(int)> handle_pid;
};
extern "C" void *startThread(void *arg);
decltype(&startThread) _startThread; // only for dlsym()
extern "C" void startThread(function<int(int)> c, promise<void> status);
typedef void startThread_t(function<int(int)>, promise<void>);
startThread_t *_startThread; // only for dlsym()
} // namespace CGPROXY::EXECSNOOP
#endif

View File

@@ -49,17 +49,16 @@ void SocketServer::socketListening(function<int(char *)> 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<int(char *)> callback, promise<void> status) {
status.set_value();
SocketServer server;
server.socketListening(callback);
}
} // namespace CGPROXY::SOCKET

View File

@@ -2,6 +2,7 @@
#define SOCKET_SERVER_H
#include <functional>
#include <future>
#include <stdlib.h>
#include <sys/un.h>
using namespace std;
@@ -14,11 +15,6 @@ namespace CGPROXY::SOCKET {
continue; \
}
struct thread_arg {
function<int(char *)> 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<int(char *)> callback, promise<void> status);
} // namespace CGPROXY::SOCKET
#endif