blob: 8b84cdef9b14fa536502e7e8dd2c66edb74eddd9 [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001/*
2 * Copyright (C) 2013 Felix Fietkau <nbd@openwrt.org>
3 * Copyright (C) 2013 John Crispin <blogic@openwrt.org>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU Lesser General Public License version 2.1
7 * as published by the Free Software Foundation
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 */
14
15#include <sys/types.h>
16#include <sys/stat.h>
17
18#include <fcntl.h>
19#include <time.h>
20#include <stdio.h>
21#include <unistd.h>
22#include <sys/types.h>
23#include <sys/socket.h>
24
25#define SYSLOG_NAMES
26#include <syslog.h>
27
28#include <libubox/ustream.h>
29#include <libubox/blobmsg_json.h>
30#include <libubox/usock.h>
31#include <libubox/uloop.h>
32#include "libubus.h"
33#include "syslog.h"
34#include "log_config.h"
35
xf.li44e08692024-01-30 01:54:44 -080036#define LOG_CONFIG_LEN 50
liubin281ac462023-07-19 14:22:54 +080037enum {
38 LOG_STDOUT,
39 LOG_FILE,
40 LOG_NET,
41};
42
43enum {
44 LOG_MSG,
45 LOG_ID,
46 LOG_PRIO,
47 LOG_SOURCE,
48 LOG_TIME,
49 __LOG_MAX
50};
51
52static const struct blobmsg_policy log_policy[] = {
53 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
54 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
55 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
56 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
57 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
58};
59
60static struct uloop_timeout retry;
61static struct uloop_fd sender;
xf.li44e08692024-01-30 01:54:44 -080062//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
63static char log_file[LOG_CONFIG_LEN], log_ip[LOG_CONFIG_LEN], log_port[LOG_CONFIG_LEN], log_prefix[LOG_CONFIG_LEN], pid_file[LOG_CONFIG_LEN], hostname[LOG_CONFIG_LEN];
liubin281ac462023-07-19 14:22:54 +080064static int log_type = LOG_STDOUT;
65static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
66static struct file_list_t file_list;
67static struct filter_list_t *filter_log = NULL;
68static char tmp_log[48] = {0};
69
70static const char* getcodetext(int value, CODE *codetable) {
71 CODE *i;
72
73 if (value >= 0)
74 for (i = codetable; i->c_val != -1; i++)
75 if (i->c_val == value)
76 return (i->c_name);
77 return "<unknown>";
78};
79
80static void log_handle_reconnect(struct uloop_timeout *timeout)
81{
82 sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
83 if (sender.fd < 0) {
84 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
85 uloop_timeout_set(&retry, 1000);
86 } else {
87 uloop_fd_add(&sender, ULOOP_READ);
88 syslog(0, "Logread connected to %s:%s\n", log_ip, log_port);
89 }
90}
91
92static void log_handle_fd(struct uloop_fd *u, unsigned int events)
93{
94 if (u->eof) {
95 uloop_fd_delete(u);
96 close(sender.fd);
97 sender.fd = -1;
98 uloop_timeout_set(&retry, 1000);
99 }
100}
101
102static int filter_char_to_pri(char c)
103{
104 switch (c) {
105 case 'v':
106 return 8;
107 case 'd':
108 return LOG_DEBUG;
109 case 'i':
110 return LOG_INFO;
111 case 'w':
112 return LOG_WARNING;
113 case 'e':
114 return LOG_ERR;
115 case 'f':
116 return LOG_ALERT;
117 case '*':
118 default:
119 return 8;
120 }
121}
122
123static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
124{
125 struct filter_list_t *_filter = filter;
126
127 while(_filter)
128 {
129 int p = filter_char_to_pri(_filter->priority);
xf.li43643772024-03-04 19:39:53 -0800130 int len = strlen(_filter->tag);
131 if(len > 0)
liubin281ac462023-07-19 14:22:54 +0800132 {
xf.li43643772024-03-04 19:39:53 -0800133 if(0 == memcmp(_filter->tag, tag, len))
134 {
135 if((pri < p) || (pri == p))
136 {
137 return 0;
138 }
139 else
140 {
141 return -1;
142 }
143 }
liubin281ac462023-07-19 14:22:54 +0800144 }else{ // have no tag
145 if(pri > p)
146 return -1;
147 else
148 return 0;
149 }
150 _filter = _filter->next;
151 }
152
153 return -1;
154}
155static int log_notify(struct blob_attr *msg)
156{
157 struct blob_attr *tb[__LOG_MAX];
158 struct stat s;
xf.li43643772024-03-04 19:39:53 -0800159 char buf[512] = {'\0'};
liubin281ac462023-07-19 14:22:54 +0800160 uint32_t p;
161 char *str;
162 time_t t;
163 char *c, *m;
164
165 if (sender.fd < 0)
166 return 0;
167
168 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
169 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
170 return 1;
171
172 if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
173 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
174 if (sender.fd < 0) {
175 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
176 exit(-1);
177 }
178 }
179
180 m = blobmsg_get_string(tb[LOG_MSG]);
181 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
182 c = ctime(&t);
183 p = blobmsg_get_u32(tb[LOG_PRIO]);
184 c[strlen(c) - 1] = '\0';
185 str = blobmsg_format_json(msg, true);
186
187 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
188 {
189 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800190 return 0;
191 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800192 }
193 if (log_type == LOG_NET) {
194 int err;
195
196 snprintf(buf, sizeof(buf), "<%u>", p);
197 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800198 if (strlen(hostname) > 0) {
liubin281ac462023-07-19 14:22:54 +0800199 strncat(buf, hostname, sizeof(buf));
200 strncat(buf, " ", sizeof(buf));
201 }
xf.li44e08692024-01-30 01:54:44 -0800202 if (strlen(log_prefix) > 0) {
liubin281ac462023-07-19 14:22:54 +0800203 strncat(buf, log_prefix, sizeof(buf));
204 strncat(buf, ": ", sizeof(buf));
205 }
206 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
207 strncat(buf, "kernel: ", sizeof(buf));
208 strncat(buf, m, sizeof(buf));
209 if (log_udp)
210 err = write(sender.fd, buf, strlen(buf));
211 else
212 err = send(sender.fd, buf, strlen(buf), 0);
213
214 if (err < 0) {
215 syslog(0, "failed to send log data to %s:%s via %s\n",
216 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
217 uloop_fd_delete(&sender);
218 close(sender.fd);
219 sender.fd = -1;
220 uloop_timeout_set(&retry, 1000);
221 }
222 } else {
223 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
224 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
225 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
226 write(sender.fd, buf, strlen(buf));
227 }
228
229 free(str);
230 if (log_type == LOG_FILE)
231 fsync(sender.fd);
232
233 return 0;
234}
235
236static void logread_fd_data_cb(struct ustream *s, int bytes)
237{
238 while (true) {
239 int len;
240 struct blob_attr *a;
241
242 a = (void*) ustream_get_read_buf(s, &len);
243 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
244 break;
245 log_notify(a);
246 ustream_consume(s, blob_len(a) + sizeof(*a));
247 }
248 if (!log_follow)
249 uloop_end();
250}
251
252static void logread_fd_cb(struct ubus_request *req, int fd)
253{
254 static struct ustream_fd test_fd;
255
256 test_fd.stream.notify_read = logread_fd_data_cb;
257 ustream_fd_init(&test_fd, fd);
258}
259
260static void logread_complete_cb(struct ubus_request *req, int ret)
261{
262}
263
264void* syslog_main(void* argv)
265{
266 static struct ubus_request req;
267 struct ubus_context *ctx;
268 uint32_t id;
269 const char *ubus_socket = NULL;
270 int ch, ret, lines = 0;
271 static struct blob_buf b;
xf.li44e08692024-01-30 01:54:44 -0800272 int tries = 60;
liubin281ac462023-07-19 14:22:54 +0800273 log_config_entry *config = (log_config_entry *)argv;
274
275 pthread_detach(pthread_self());
276
277 if (NULL == argv)
278 return NULL;
279
280 signal(SIGPIPE, SIG_IGN);
281 uloop_init();
282
xf.li44e08692024-01-30 01:54:44 -0800283 //log_file = config->out_path;
284 memset(log_file, 0, sizeof(log_file));
285 memset(log_ip, 0, sizeof(log_ip));
286 memset(log_port, 0, sizeof(log_port));
287 memset(log_prefix, 0, sizeof(log_prefix));
288 memset(pid_file, 0, sizeof(pid_file));
289 memset(hostname, 0, sizeof(hostname));
290
291 if(config->out_path != NULL)
292 {
293 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
294 }
295
liubin281ac462023-07-19 14:22:54 +0800296 memset(&file_list, 0, sizeof(struct file_list_t));
297 file_list.total = config->rotate_file_count;
298 if(config->rotate_file_size)
299 log_size = config->rotate_file_size;
300 if(config->ip)
301 {
302 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
xf.li44e08692024-01-30 01:54:44 -0800303 //log_ip = config->ip;
304 strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
305 //log_port = config->port;
306 if(config->port != NULL)
307 {
308 strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
309 }
liubin281ac462023-07-19 14:22:54 +0800310 }
311 filter_log = config->filter_list;
312 // Follow log messages
313 log_follow = 1;
314 ctx = ubus_connect(ubus_socket);
315 if (!ctx) {
316 fprintf(stderr, "Failed to connect to ubus\n");
317 return -1;
318 }
319 ubus_add_uloop(ctx);
320
321 printf("syslog log start...\n");
322 /* ugly ugly ugly ... we need a real reconnect logic */
323 do {
324 ret = ubus_lookup_id(ctx, "log", &id);
325 if (ret) {
326 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
327 sleep(1);
328 continue;
329 }
liubin281ac462023-07-19 14:22:54 +0800330 blob_buf_init(&b, 0);
331 if (lines)
332 blobmsg_add_u32(&b, "lines", lines);
333 else if (log_follow)
334 blobmsg_add_u32(&b, "lines", 0);
335 if (log_follow) {
xf.li44e08692024-01-30 01:54:44 -0800336 if (strlen(pid_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800337 FILE *fp = fopen(pid_file, "w+");
338 if (fp) {
339 fprintf(fp, "%d", getpid());
340 fclose(fp);
341 }
342 }
343 }
344
xf.li44e08692024-01-30 01:54:44 -0800345 if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
liubin281ac462023-07-19 14:22:54 +0800346 openlog("logread", LOG_PID, LOG_DAEMON);
347 log_type = LOG_NET;
348 sender.cb = log_handle_fd;
349 retry.cb = log_handle_reconnect;
350 uloop_timeout_set(&retry, 1000);
xf.li44e08692024-01-30 01:54:44 -0800351 } else if (strlen(log_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800352 log_type = LOG_FILE;
353 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
xf.li44e08692024-01-30 01:54:44 -0800354 sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
liubin281ac462023-07-19 14:22:54 +0800355 sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
356 if (sender.fd < 0) {
357 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
358 exit(-1);
359 }
360 } else {
361 sender.fd = STDOUT_FILENO;
362 }
363
364 ubus_invoke_async(ctx, id, "read", b.head, &req);
365 req.fd_cb = logread_fd_cb;
366 req.complete_cb = logread_complete_cb;
367 ubus_complete_request_async(ctx, &req);
xf.li44e08692024-01-30 01:54:44 -0800368
liubin281ac462023-07-19 14:22:54 +0800369 uloop_run();
370 ubus_free(ctx);
371 uloop_done();
372
373 } while (ret && tries--);
374
375 pthread_exit(NULL);
376 return NULL;
377}