/
klogger.c
executable file
·174 lines (138 loc) · 3.6 KB
/
klogger.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#include <linux/file.h>
#include <linux/fs.h>
#include <linux/uaccess.h>
#include <linux/types.h>
#include "klogger.h"
#define derror(a, b...) printk("[ERROR] %s(): "a"\n", __func__, ##b)
#define CHECK_IF(assertion, error_action, ...)\
{\
if (assertion)\
{\
derror(__VA_ARGS__); \
{error_action;} \
}\
}
struct logger
{
struct queue mq;
int type;
bool running;
log_lv_t lv;
struct udp udp;
struct udp_addr remote;
char filepath[LOG_FILE_PATH_SIZE];
struct file* fp;
};
static void _clean_log(void* input)
{
if (input) kfree(input);
}
struct logger* logger_create(int type, char* dstip, int dstport, char* filepath)
{
struct logger* l = kzalloc(sizeof(struct logger), GFP_ATOMIC);
CHECK_IF(l == NULL, return NULL, "l is null");
queue_init(&l->mq, -1, _clean_log, QUEUE_FLAG_POP_BLOCK);
l->type = type;
l->lv = LOG_DEFAULT_LV;
if (type & LOG_UDP)
{
CHECK_IF(dstip == NULL, goto _ERROR, "LOG_UDP dstip is null");
CHECK_IF(dstport <= 0, goto _ERROR, "LOG_UDP dstport = %d invalid", dstport);
udp_init(&l->udp, NULL, UDP_PORT_ANY);
snprintf(l->remote.ipv4, INET_ADDRSTRLEN, "%s", dstip);
l->remote.port = dstport;
}
if (type & LOG_FILE)
{
mm_segment_t oldfs = get_fs();
CHECK_IF(filepath == NULL, goto _ERROR, "LOG_FILE filepath is null");
snprintf(l->filepath, LOG_FILE_PATH_SIZE, "%s", filepath);
set_fs(get_ds());
l->fp = filp_open(filepath, O_RDWR | O_CREAT, 0644);
set_fs(oldfs);
CHECK_IF(IS_ERR(l->fp), goto _ERROR, "filp_open failed");
}
return l;
_ERROR:
if (l) kfree(l);
return NULL;
}
void logger_release(struct logger* l)
{
CHECK_IF(l == NULL, return, "l is null");
if (l->running) logger_break(l);
if (l->type & LOG_UDP)
{
udp_uninit(&l->udp);
}
if (l->type & LOG_FILE)
{
mm_segment_t oldfs = get_fs();
CHECK_IF(IS_ERR(l->fp), return, "l->fp is err");
set_fs(get_ds());
filp_close(l->fp, NULL);
set_fs(oldfs);
l->fp = NULL;
}
kfree(l);
return;
}
void logger_run(struct logger* l)
{
char* msg;
CHECK_IF(l == NULL, return, "l is null");
l->running = true;
while (l->running)
{
msg = queue_pop(&l->mq);
CHECK_IF(msg == NULL, break, "msg is null");
if (l->type & LOG_PRINTK)
{
printk("%s", msg);
}
if (l->type & LOG_UDP)
{
udp_send(&l->udp, l->remote, msg, strlen(msg)+1);
}
if (l->type & LOG_FILE)
{
if (!IS_ERR(l->fp))
{
mm_segment_t oldfs = get_fs();
set_fs(get_ds());
l->fp->f_op->write(l->fp, msg, strlen(msg)+1, &l->fp->f_pos);
set_fs(oldfs);
}
else
{
derror("l->fp is err");
l->running = false;
}
}
kfree(msg);
}
l->running = false;
return;
}
void logger_break(struct logger* l)
{
CHECK_IF(l == NULL, return, "l is null");
l->running = false;
log_print(l, LOG_INVALID, "stop\n");
}
void logger_setlevel(struct logger* l, log_lv_t lv)
{
CHECK_IF(l == NULL, return, "l is null");
l->lv = lv;
}
log_lv_t logger_getlevel(struct logger* l)
{
CHECK_IF(l == NULL, return LOG_INVALID, "l is null");
return l->lv;
}
void logger_push(struct logger* l, char* str)
{
CHECK_IF(l == NULL, return, "l is null");
CHECK_IF(str == NULL, return, "str is null");
queue_push(&l->mq, str);
}