Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clean up and optimize code #53

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 9 additions & 9 deletions execsnoop-bcc/execsnoop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
#include <functional>
#include <iostream>
#include <string>
#include <utility>
#include <unistd.h>
using namespace std;

namespace CGPROXY::EXECSNOOP {

const string BPF_PROGRAM = R"(
const std::string BPF_PROGRAM = R"(
#include <linux/fs.h>
#include <linux/sched.h>
#include <uapi/linux/ptrace.h>
Expand Down Expand Up @@ -47,8 +47,8 @@ struct data_t {
int pid;
};

function<int(int)> callback = NULL;
promise<void> status;
std::function<int(int)> callback = NULL;
std::promise<void> status;

void handle_events(void *cb_cookie, void *data, int data_size) {
auto event = static_cast<data_t *>(data);
Expand All @@ -68,7 +68,7 @@ int execsnoop() {
return 1;
}

string execve_fnname = bpf.get_syscall_fnname("execve");
std::string execve_fnname = bpf.get_syscall_fnname("execve");
// auto attach_res = bpf.attach_kprobe(execve_fnname, "syscall_execve");
auto attach_res =
bpf.attach_kprobe(execve_fnname, "ret_syscall_execve", 0, BPF_PROBE_RETURN);
Expand All @@ -95,10 +95,10 @@ int execsnoop() {
return 0;
}

void startThread(function<int(int)> c, promise<void> _status) {
status = move(_status);
callback = c;
void startThread(std::function<int(int)> c, std::promise<void> _status) {
status = std::move(_status);
callback = std::move(c);
execsnoop();
}

} // namespace CGPROXY::EXECSNOOP
CoelacanthusHex marked this conversation as resolved.
Show resolved Hide resolved
} // namespace CGPROXY::EXECSNOOP
13 changes: 5 additions & 8 deletions execsnoop-bcc/execsnoop.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,20 @@
#ifndef EXECSNOOP_HPP
#define EXECSNOOP_HPP 1
#pragma once

#include <functional>
#include <future>
#include <string>
using namespace std;

namespace CGPROXY::EXECSNOOP {

extern const string BPF_PROGRAM;
extern const std::string BPF_PROGRAM;
struct data_t;
extern function<int(int)> callback;
extern std::function<int(int)> callback;
void handle_events(void *cb_cookie, void *data, int data_size);
int execsnoop();

extern "C" void startThread(function<int(int)> c, promise<void> _status);
// typedef void startThread_t(function<int(int)>, promise<void>);
extern "C" void startThread(std::function<int(int)> c, std::promise<void> _status);
// typedef void startThread_t(std::function<int(int)>, std::promise<void>);
using startThread_t=decltype(startThread);
startThread_t *_startThread; // only for dlsym()

} // namespace CGPROXY::EXECSNOOP
#endif
16 changes: 9 additions & 7 deletions execsnoop-kernel/execsnoop_share.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <bpf/libbpf.h>
#include <sys/resource.h>

#include <utility>

#if defined(__x86_64__)
#include "x86_64/execsnoop_kern_skel.h"
#elif defined(__aarch64__)
Expand All @@ -23,19 +25,19 @@ struct event {
uid_t uid;
};

function<int(int)> callback = NULL;
promise<void> status;
std::function<int(int)> callback = nullptr;
std::promise<void> status;

static void handle_event(void *ctx, int cpu, void *data, __u32 size) {
auto e = static_cast<event*>(data);
auto *e = static_cast<event*>(data);
if (callback) callback(e->pid);
}

void handle_lost_events(void *ctx, int cpu, __u64 lost_cnt) {
fprintf(stderr, "Lost %llu events on CPU #%d!\n", lost_cnt, cpu);
}

int bump_memlock_rlimit(void) {
int bump_memlock_rlimit() {
struct rlimit rlim_new = { RLIM_INFINITY, RLIM_INFINITY };
return setrlimit(RLIMIT_MEMLOCK, &rlim_new);
}
Expand Down Expand Up @@ -87,9 +89,9 @@ int execsnoop() {
return err;
}

void startThread(function<int(int)> c, promise<void> _status) {
status = move(_status);
callback = c;
void startThread(std::function<int(int)> c, std::promise<void> _status) {
status = std::move(_status);
callback = std::move(c);
execsnoop();
}

Expand Down
7 changes: 2 additions & 5 deletions execsnoop-kernel/execsnoop_share.h
Original file line number Diff line number Diff line change
@@ -1,14 +1,12 @@
#ifndef EXECSNOOP_SHARE_HPP
#define EXECSNOOP_SHARE_HPP 1
#pragma once

#include <functional>
#include <future>
#include <string>
using namespace std;

namespace CGPROXY::EXECSNOOP {

extern "C" void startThread(function<int(int)> c, promise<void> _status);
extern "C" void startThread(std::function<int(int)> c, std::promise<void> _status);

#ifdef BUIlD_EXECSNOOP_DL
// only for dlsym()
Expand All @@ -17,4 +15,3 @@ startThread_t *_startThread;
#endif

} // namespace CGPROXY::EXECSNOOP
#endif
Loading