Skip to content

Commit 27a9c6a

Browse files
Eris A.axtloss
authored andcommitted
Minor code refactor (mainly formatting)
1 parent ea9145f commit 27a9c6a

File tree

1 file changed

+46
-38
lines changed

1 file changed

+46
-38
lines changed

src/main.c

Lines changed: 46 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,54 @@
11
// copyright axtlos <axtlos@disroot.org>
22
// SPDX-LICENSE: LGPL-3.0-ONLY
33

4-
#include <stdlib.h>
5-
#include <stdio.h>
6-
#include "calls.h"
7-
#include <libsyscall_intercept_hook_point.h>
84
#include <errno.h>
5+
#include <stdbool.h>
6+
#include <stdio.h>
7+
#include <stdlib.h>
98
#include <time.h>
109
#include <unistd.h>
10+
#include <libsyscall_intercept_hook_point.h>
11+
#include "calls.h"
1112

12-
bool
13-
has_flag(long search_flag, long all_flags) {
13+
inline bool
14+
has_flag(long search_flag, long all_flags)
15+
{
1416
return (all_flags & search_flag) == search_flag;
1517
}
1618

1719
bool
18-
argcmp(int matchtype, long config_arg_long, char *config_arg_char, long sys_arg) {
19-
if (!config_arg_char && config_arg_long != -1) {
20-
return has_flag(config_arg_long, sys_arg);
21-
} else if (config_arg_char) {
20+
argcmp(int matchtype, long config_arg_long, char *config_arg_char, long sys_arg)
21+
{
22+
bool return_val = false;
23+
24+
if (!config_arg_char && config_arg_long != -1)
25+
return_val = has_flag(config_arg_long, sys_arg);
26+
else if (config_arg_char)
2227
switch (matchtype) {
2328
case -1:
24-
return strncmp(config_arg_char, (char*)sys_arg, strlen(config_arg_char)) == 0;
29+
if (strncmp(config_arg_char, (char*)sys_arg, strlen(config_arg_char)) == 0)
30+
return_val = true;
31+
break;
2532
case 0:
2633
if (strcmp((char *)sys_arg, config_arg_char) == 0)
27-
return true;
34+
return_val = true;
2835
break;
2936
case 1:
3037
if (strstr((char *)sys_arg, config_arg_char) != NULL)
31-
return true;
38+
return_val = true;
3239
break;
3340
default:
34-
return false;
41+
return_val = false;
3542
}
36-
}
37-
return false;
43+
44+
return return_val;
3845
}
3946

4047
// Function origin:
4148
// https://www.gnu.org/software/libc/manual/html_node/Symbolic-Links.html#index-readlink
42-
char *readlink_malloc(const char *filename) {
49+
char *
50+
readlink_malloc(const char *filename)
51+
{
4352
size_t size = 50;
4453
char *buffer = NULL;
4554

@@ -61,19 +70,23 @@ char *readlink_malloc(const char *filename) {
6170
}
6271

6372
char *
64-
get_fdesc(long arg) {
73+
get_fdesc(long arg)
74+
{
6575
char *linkname, *procstat;
6676
pid_t pid = getpid();
6777
int proc = asprintf(&procstat, "/proc/%d/fd/%ld", pid, arg);
68-
if (proc < 0) {
69-
return NULL;
70-
}
71-
linkname = readlink_malloc(procstat);
78+
79+
if (proc < 0)
80+
linkname = NULL;
81+
else
82+
linkname = readlink_malloc(procstat);
83+
7284
return linkname;
7385
}
7486

7587
bool
76-
match_args(conf_syscall *call, long arg0, long arg1, long arg2, long arg3, long arg4, long arg5) {
88+
match_args(conf_syscall *call, long arg0, long arg1, long arg2, long arg3, long arg4, long arg5)
89+
{
7790
bool arg0_match = true, arg1_match = true, arg2_match = true, arg3_match = true, arg4_match = true, arg5_match = true;
7891
if (call->arg0 || call->arg0_long != -1) {
7992
long arg = arg0;
@@ -138,7 +151,8 @@ match_args(conf_syscall *call, long arg0, long arg1, long arg2, long arg3, long
138151
return arg0_match && arg1_match && arg2_match && arg3_match && arg4_match && arg5_match;
139152
}
140153

141-
void log_call(conf_syscall *call) {
154+
void log_call(conf_syscall *call)
155+
{
142156
FILE *log_file;
143157
time_t t = time(NULL);
144158
struct tm timestruc = *localtime(&t);
@@ -151,33 +165,27 @@ void log_call(conf_syscall *call) {
151165
fclose(log_file);
152166
}
153167
static int
154-
hook (long syscall_number,
155-
long arg0, long arg1,
156-
long arg2, long arg3,
157-
long arg4, long arg5,
158-
long *result) {
159-
168+
hook(long syscall_number, long arg0, long arg1, long arg2, long arg3, long arg4, long arg5, long *result)
169+
{
160170
conf_syscall *call = get_calls();
161171
while(true) {
162-
if (call->callnum == syscall_number
163-
&& match_args(call, arg0, arg1, arg2, arg3, arg4, arg5))
164-
{
165-
if (call->log) {
172+
if (call->callnum == syscall_number && match_args(call, arg0, arg1, arg2, arg3, arg4, arg5)) {
173+
if (call->log)
166174
log_call(call);
167-
}
168175
if (call->block) {
169176
*result = -ENOTSUP;
170177
return 0;
171178
}
172179
}
173-
if (!call->next) {
180+
if (!call->next)
174181
return 1;
175-
}
182+
176183
call = call->next;
177184
}
178185
}
179186

180187
static __attribute__((constructor)) void
181-
init(void) {
188+
init(void)
189+
{
182190
intercept_hook_point = &hook;
183191
}

0 commit comments

Comments
 (0)