Skip to content

Commit

Permalink
drop() -> raii()
Browse files Browse the repository at this point in the history
"drop" is way too common.
  • Loading branch information
hack3ric committed Dec 11, 2024
1 parent 29ccea6 commit 98c6eba
Show file tree
Hide file tree
Showing 7 changed files with 16 additions and 16 deletions.
2 changes: 1 addition & 1 deletion common/defs.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ static inline void freep(void** ptr) {
}
static inline void freestrp(char** ptr) { freep((void*)ptr); }

#define drop(f) __attribute__((__cleanup__(f)))
#define raii(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;
char* line drop(freestrp) = NULL;
char* line raii(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) {
char* line drop(freestrp) = NULL;
char* line raii(freestrp) = NULL;
size_t len = 0;
ssize_t read;

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;

int sk drop(closep) = socket(AF_UNIX, SOCK_DGRAM | SOCK_CLOEXEC, 0);
int sk raii(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
2 changes: 1 addition & 1 deletion src/queue.c
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ int packet_buf_consume(struct packet_buf* buf, bool* consumed) {
return 0;
}

int sk drop(closep) =
int sk raii(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
12 changes: 6 additions & 6 deletions src/run.c
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ 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.
int sk drop(closep) =
int sk raii(closep) =
try(socket(ip_proto(&s->conn.local), SOCK_RAW | SOCK_NONBLOCK, IPPROTO_TCP));

int level = SOL_IP, opt = IP_FREEBIND, yes = 1;
Expand All @@ -184,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;

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

static inline bool is_kmod_loaded() {
FILE* modules drop(fclosep) = fopen("/proc/modules", "r");
FILE* modules raii(fclosep) = fopen("/proc/modules", "r");
char buf[256];
while (fgets(buf, sizeof(buf), modules))
if (strncmp("mimic ", buf, 6) == 0) return true;
Expand All @@ -482,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;
drop(closep) int epfd = -1, sfd = -1, timer = -1;
raii(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 @@ -702,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) {
FILE* lock_file drop(fclosep) = fopen(lock_path, "r");
FILE* lock_file raii(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 @@ -741,7 +741,7 @@ int subcmd_run(struct run_args* args) {
if (!ifindex) ret(-1, _("no interface named '%s'"), args->ifname);

if (args->file) {
FILE* conf drop(fclosep) = fopen(args->file, "r");
FILE* conf raii(fclosep) = fopen(args->file, "r");
if (conf) {
try(parse_config_file(conf, args), _("failed to read configuration file"));
} else if (errno == ENOENT) {
Expand Down
6 changes: 3 additions & 3 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);
{
FILE* lock_file drop(fclosep) =
FILE* lock_file raii(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,15 +170,15 @@ 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);
int whitelist_fd drop(closep) =
int whitelist_fd raii(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) {
int conns_fd drop(closep) = try(bpf_map_get_fd_by_id(lock_content.conns_id),
int conns_fd raii(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];
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 drop(fclosep) =
FILE* file raii(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 drop(freestrp) = try_p(malloc(size), "cannot malloc: %s", strret);
char* buf raii(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 98c6eba

Please sign in to comment.