execsnoop as library

This commit is contained in:
springzfx
2020-05-25 13:49:40 +08:00
parent f501c7e476
commit 1c72a204a1
6 changed files with 58 additions and 25 deletions

View File

@@ -3,12 +3,17 @@ find_package(nlohmann_json REQUIRED)
include_directories(${PROJECT_SOURCE_DIR})
include_directories(${CMAKE_CURRENT_SOURCE_DIR})
add_library(optional SHARED optional.cpp execsnoop.cpp)
target_link_libraries(optional bcc)
install(TARGETS optional DESTINATION /usr/lib/cgproxy/ PERMISSIONS ${basic_permission})
add_executable(main main.cpp
common.cpp config.cpp cgroup_attach.cpp
common.cpp config.cpp cgroup_attach.cpp
socket_client.cpp socket_server.cpp)
target_link_libraries(main nlohmann_json::nlohmann_json Threads::Threads bcc)
target_link_libraries(main PRIVATE nlohmann_json::nlohmann_json Threads::Threads)
target_link_libraries(main PRIVATE optional)
set_target_properties(main PROPERTIES LINKER_LANGUAGE CXX)
set_target_properties(main PROPERTIES OUTPUT_NAME cgproxy)
install(TARGETS main DESTINATION /usr/bin PERMISSIONS ${basic_permission})
install(TARGETS main DESTINATION /usr/bin PERMISSIONS ${basic_permission})

View File

@@ -4,7 +4,6 @@
#include "cgroup_attach.h"
#include "common.h"
#include "config.h"
#include "execsnoop.hpp"
#include "socket_server.h"
#include <algorithm>
#include <csignal>
@@ -15,6 +14,7 @@
#include <sched.h>
#include <sys/file.h>
#include <unistd.h>
#include "optional.h"
using namespace std;
using json = nlohmann::json;

View File

@@ -1,7 +1,3 @@
#ifndef EXECSNOOP_HPP
#define EXECSNOOP_HPP 1
#include <exception>
#include "bcc/BPF.h"
#include "common.h"
#include <bcc/libbpf.h>
@@ -93,19 +89,4 @@ int execsnoop() {
return 0;
}
struct thread_arg {
function<int(int)> handle_pid;
};
void *startThread(void *arg) {
thread_arg *p = (thread_arg *)arg;
callback = p->handle_pid;
try {
execsnoop();
} catch (exception &e) {
error("bcc may not be installed, %s",e.what());
}
return (void *)0;
}
} // namespace CGPROXY::EXESNOOP
#endif
} // namespace CGPROXY::EXESNOOP

17
src/execsnoop.h Normal file
View File

@@ -0,0 +1,17 @@
#ifndef EXECSNOOP_HPP
#define EXECSNOOP_HPP 1
#include <functional>
#include <string>
using namespace std;
namespace CGPROXY::EXESNOOP {
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();
} // namespace CGPROXY::EXESNOOP
#endif

14
src/optional.cpp Normal file
View File

@@ -0,0 +1,14 @@
#include "optional.h"
#include "common.h"
#include "execsnoop.h"
namespace CGPROXY::EXESNOOP {
void *startThread(void *arg) {
thread_arg *p = (thread_arg *)arg;
callback = p->handle_pid;
execsnoop();
return (void *)0;
}
}

16
src/optional.h Normal file
View File

@@ -0,0 +1,16 @@
#ifndef OPTIONAL_H
#define OPTIONAL_H 1
#include <functional>
using namespace std;
namespace CGPROXY::EXESNOOP {
struct thread_arg {
function<int(int)> handle_pid;
};
void *startThread(void *arg);
}
#endif