Skip to content

Commit

Permalink
Cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
hack3ric committed Dec 11, 2024
1 parent 5ef0a96 commit 29ccea6
Show file tree
Hide file tree
Showing 8 changed files with 48 additions and 29 deletions.
41 changes: 29 additions & 12 deletions common/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,32 @@
#include <unistd.h>
#endif

#define min(x, y) ((x) < (y) ? (x) : (y))
#define max(x, y) ((x) < (y) ? (y) : (x))
#define cmp(x, y) ((x) > (y) - (x) < (y))
#define min(x, y) \
({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
_x < _y ? _x : _y; \
})
#define max(x, y) \
({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
_x < _y ? _y : _x; \
})
#define cmp(x, y) \
({ \
typeof(x) _x = (x); \
typeof(y) _y = (y); \
_x > _y - _x < _y; \
})

#define sizeof_array(arr) (sizeof(arr) / sizeof(arr[0]))
#define round_to_mul(val, mul) ((val) % (mul) == 0) ? (val) : ((val) + ((mul) - (val) % (mul)))
#define round_to_mul(val, mul) \
({ \
typeof(val) _val = (val); \
typeof(mul) _mul = (mul); \
(_val % _mul == 0) ? _val : (_val + (_mul - _val % mul)); \
})

#define swap(x, y) \
({ \
Expand Down Expand Up @@ -118,21 +138,18 @@

// Cleanup utilities

static inline void cleanup_fd(int* fd) {
static inline void closep(int* fd) {
if (*fd >= 0) close(*fd);
}
static inline void cleanup_file(FILE** file) {
static inline void fclosep(FILE** file) {
if (*file) fclose(*file);
}
static inline void cleanup_malloc(void** ptr) {
static inline void freep(void** ptr) {
if (*ptr) free(*ptr);
}
static inline void cleanup_malloc_str(char** ptr) { cleanup_malloc((void*)ptr); }
static inline void freestrp(char** ptr) { freep((void*)ptr); }

#define _cleanup_fd __attribute__((__cleanup__(cleanup_fd)))
#define _cleanup_file __attribute__((__cleanup__(cleanup_file)))
#define _cleanup_malloc __attribute__((__cleanup__(cleanup_malloc)))
#define _cleanup_malloc_str __attribute__((__cleanup__(cleanup_malloc_str)))
#define drop(f) __attribute__((__cleanup__(f)))

#endif // MIMIC_BPF

Expand Down
4 changes: 2 additions & 2 deletions src/config.c
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ int parse_xdp_mode(const char* mode) {

int parse_config_file(FILE* file, struct run_args* args) {
int ret;
_cleanup_malloc_str char* line = NULL;
char* line drop(freestrp) = NULL;
size_t len = 0;
ssize_t read;

Expand Down Expand Up @@ -301,7 +301,7 @@ int parse_config_file(FILE* file, struct run_args* args) {
}

int parse_lock_file(FILE* file, struct lock_content* c) {
_cleanup_malloc_str char* line = NULL;
char* line drop(freestrp) = NULL;
size_t len = 0;
ssize_t read;

Expand Down
2 changes: 1 addition & 1 deletion src/log.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ int libbpf_print_fn(enum libbpf_print_level bpf_level, const char* format, va_li
ret = ret < 0 ? ret : vfprintf(stderr, format, args);
if (level >= LOG_TRACE) ret = ret < 0 ? ret : fprintf(stderr, RESET);
}
return ret < 0 ? ret : 0;
return min(ret, 0);
}

static inline const char* log_type_to_str(enum log_type type) {
Expand Down
2 changes: 1 addition & 1 deletion src/notify.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static int notify_systemd(const char* msg) {
memcpy(addr.sun_path, socket_path, sizeof(addr.sun_path));
if (socket_path[0] == '@') addr.sun_path[0] = 0;

_cleanup_fd int sk = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
int sk drop(closep) = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (sk < 0) return -errno;

socklen_t sock_len = offsetof(struct sockaddr_un, sun_path) + path_len;
Expand Down
3 changes: 2 additions & 1 deletion src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,8 @@ int packet_buf_consume(struct packet_buf* buf, bool* consumed) {
return 0;
}

_cleanup_fd int sk = try(socket(ip_proto(&buf->conn.local), SOCK_RAW | SOCK_NONBLOCK, IPPROTO_UDP));
int sk drop(closep) =
try(socket(ip_proto(&buf->conn.local), SOCK_RAW | SOCK_NONBLOCK, IPPROTO_UDP));
struct sockaddr_storage saddr, daddr;
conn_tuple_to_addrs(&buf->conn, &saddr, &daddr);

Expand Down
13 changes: 7 additions & 6 deletions src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,8 @@ static int handle_send_ctrl_packet(struct send_options* s, const char* ifname) {
//
// Maybe setting reception buffer size to 0 will help, but it's just prevent packets from storing
// and they will be forwarded to the socket and discarded anyway.
_cleanup_fd int sk = try(socket(ip_proto(&s->conn.local), SOCK_RAW | SOCK_NONBLOCK, IPPROTO_TCP));
int sk drop(closep) =
try(socket(ip_proto(&s->conn.local), SOCK_RAW | SOCK_NONBLOCK, IPPROTO_TCP));

int level = SOL_IP, opt = IP_FREEBIND, yes = 1;
if (ip_proto(&s->conn.local) == AF_INET6) {
Expand All @@ -183,7 +184,7 @@ static int handle_send_ctrl_packet(struct send_options* s, const char* ifname) {
size_t buf_len = sizeof(struct tcphdr) + (flags & TCP_FLAG_SYN ? 3 * 4 : 0);
csum += buf_len;

_cleanup_malloc void* buf = malloc(buf_len);
void* buf drop(freep) = malloc(buf_len);
struct tcphdr* tcp = (typeof(tcp))buf;
*tcp = (typeof(*tcp)){
.source = htons(s->conn.local_port),
Expand Down Expand Up @@ -458,7 +459,7 @@ cleanup:;
_("failed to get info of map '%s': %s"))

static inline bool is_kmod_loaded() {
_cleanup_file FILE* modules = fopen("/proc/modules", "r");
FILE* modules drop(fclosep) = fopen("/proc/modules", "r");
char buf[256];
while (fgets(buf, sizeof(buf), modules))
if (strncmp("mimic ", buf, 6) == 0) return true;
Expand All @@ -481,7 +482,7 @@ static inline int terminate_all_conns(int mimic_conns_fd, const char* ifname) {
static inline int run_bpf(struct run_args* args, int lock_fd, const char* ifname, int ifindex) {
int retcode;
struct mimic_bpf* skel = NULL;
_cleanup_fd int epfd = -1, sfd = -1, timer = -1;
drop(closep) int epfd = -1, sfd = -1, timer = -1;

// These fds are actually reference of skel, so no need to use _cleanup_fd
int egress_handler_fd = -1, ingress_handler_fd = -1;
Expand Down Expand Up @@ -701,7 +702,7 @@ static inline int _lock(const char* restrict lock_path, const char* restrict ifn
bool check_lock = false, check_process = false;
struct lock_content lock_content;
if (errno == EEXIST) {
_cleanup_file FILE* lock_file = fopen(lock_path, "r");
FILE* lock_file drop(fclosep) = fopen(lock_path, "r");
if (lock_file) {
if (parse_lock_file(lock_file, &lock_content) == 0) {
char proc_path[32];
Expand Down Expand Up @@ -740,7 +741,7 @@ int subcmd_run(struct run_args* args) {
if (!ifindex) ret(-1, _("no interface named '%s'"), args->ifname);

if (args->file) {
_cleanup_file FILE* conf = fopen(args->file, "r");
FILE* conf drop(fclosep) = fopen(args->file, "r");
if (conf) {
try(parse_config_file(conf, args), _("failed to read configuration file"));
} else if (errno == ENOENT) {
Expand Down
8 changes: 4 additions & 4 deletions src/show.c
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ int subcmd_show(struct show_args* args) {
struct lock_content lock_content;
get_lock_file_name(lock_path, sizeof(lock_path), ifindex);
{
_cleanup_file FILE* lock_file =
FILE* lock_file drop(fclosep) =
try_p(fopen(lock_path, "r"), _("failed to open lock file at %s: %s"), lock_path, strret);
try(parse_lock_file(lock_file, &lock_content));
}
Expand All @@ -170,16 +170,16 @@ int subcmd_show(struct show_args* args) {
if (args->show_process) {
printf(_("%sMimic%s running on %s\n"), BOLD GREEN, RESET, args->ifname);
printf(_(" %sPID:%s %d\n"), BOLD, RESET, lock_content.pid);
_cleanup_fd int whitelist_fd =
int whitelist_fd drop(closep) =
try(bpf_map_get_fd_by_id(lock_content.whitelist_id), _("failed to get fd of map '%s': %s"),
"mimic_whitelist", strret);
show_overview(ifindex, whitelist_fd, &lock_content.settings, -1);
printf("\n");
}

if (args->show_command) {
_cleanup_fd int conns_fd = try(bpf_map_get_fd_by_id(lock_content.conns_id),
_("failed to get fd of map '%s': %s"), "mimic_conns", strret);
int conns_fd drop(closep) = try(bpf_map_get_fd_by_id(lock_content.conns_id),
_("failed to get fd of map '%s': %s"), "mimic_conns", strret);

char local[IP_PORT_MAX_LEN], remote[IP_PORT_MAX_LEN];
struct conn_tuple key;
Expand Down
4 changes: 2 additions & 2 deletions tools/extract-btf.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ int main(int argc, char** argv) {
ret(-1, "unknown endianness '%s'", argv[2]);

const char* path = argv[1];
FILE* file _cleanup_file =
FILE* file drop(fclosep) =
try_p(fopen(path, "rb"), "failed to open file at %s: %s", path, strret);

long size = try(get_file_size(file), "failed to get file size: %s", strret);
char* buf _cleanup_malloc_str = try_p(malloc(size), "cannot malloc: %s", strret);
char* buf drop(freestrp) = try_p(malloc(size), "cannot malloc: %s", strret);
try_e(fread(buf, 1, size, file), "failed to read file content: %s", strret);

struct btf_header* btf_hdr = NULL;
Expand Down

0 comments on commit 29ccea6

Please sign in to comment.