From 29ccea6b32ec29ab9f34e0375fa64cd5eed0de2f Mon Sep 17 00:00:00 2001 From: Eric Long Date: Wed, 11 Dec 2024 21:41:16 +0800 Subject: [PATCH] Cleanups --- common/defs.h | 41 +++++++++++++++++++++++++++++------------ src/config.c | 4 ++-- src/log.c | 2 +- src/notify.c | 2 +- src/queue.c | 3 ++- src/run.c | 13 +++++++------ src/show.c | 8 ++++---- tools/extract-btf.c | 4 ++-- 8 files changed, 48 insertions(+), 29 deletions(-) diff --git a/common/defs.h b/common/defs.h index 11d1001..0f6d5a0 100644 --- a/common/defs.h +++ b/common/defs.h @@ -19,12 +19,32 @@ #include #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) \ ({ \ @@ -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 diff --git a/src/config.c b/src/config.c index 299f25d..1ad7e7c 100644 --- a/src/config.c +++ b/src/config.c @@ -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; @@ -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; diff --git a/src/log.c b/src/log.c index 4ee0f6e..cfc30d9 100644 --- a/src/log.c +++ b/src/log.c @@ -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) { diff --git a/src/notify.c b/src/notify.c index 5687523..4174868 100644 --- a/src/notify.c +++ b/src/notify.c @@ -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; diff --git a/src/queue.c b/src/queue.c index 3e2e1d4..ec5f245 100644 --- a/src/queue.c +++ b/src/queue.c @@ -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); diff --git a/src/run.c b/src/run.c index b535292..ae898c4 100644 --- a/src/run.c +++ b/src/run.c @@ -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) { @@ -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), @@ -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; @@ -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; @@ -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]; @@ -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) { diff --git a/src/show.c b/src/show.c index e65d0a6..e98180b 100644 --- a/src/show.c +++ b/src/show.c @@ -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)); } @@ -170,7 +170,7 @@ 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); @@ -178,8 +178,8 @@ int subcmd_show(struct show_args* args) { } 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; diff --git a/tools/extract-btf.c b/tools/extract-btf.c index 3c5a102..78071f7 100644 --- a/tools/extract-btf.c +++ b/tools/extract-btf.c @@ -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;