blob: e89a1c907bfffa2b09189893e31276a3339c06fc [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>
xf.li4bd9ee42024-04-13 01:31:44 -070023#include <sys/un.h>
liubin281ac462023-07-19 14:22:54 +080024#include <sys/socket.h>
25
26#define SYSLOG_NAMES
27#include <syslog.h>
xf.li4bd9ee42024-04-13 01:31:44 -070028#include "json/json.h"
29#include "json/printbuf.h"
liubin281ac462023-07-19 14:22:54 +080030
31#include <libubox/ustream.h>
32#include <libubox/blobmsg_json.h>
33#include <libubox/usock.h>
34#include <libubox/uloop.h>
35#include "libubus.h"
b.liu63a4a322024-03-07 19:00:53 +080036#include "syslog.h"
liubin281ac462023-07-19 14:22:54 +080037#include "log_config.h"
b.liu9a306862024-03-06 16:49:40 +080038//#include "lynq/liblog.h"
liubin281ac462023-07-19 14:22:54 +080039
b.liu63a4a322024-03-07 19:00:53 +080040enum {
41 SOURCE_KLOG = 0,
42 SOURCE_SYSLOG = 1,
43 SOURCE_INTERNAL = 2,
44 SOURCE_ANY = 0xff,
45};
46
xf.li44e08692024-01-30 01:54:44 -080047#define LOG_CONFIG_LEN 50
liubin281ac462023-07-19 14:22:54 +080048enum {
49 LOG_STDOUT,
50 LOG_FILE,
51 LOG_NET,
52};
53
54enum {
55 LOG_MSG,
56 LOG_ID,
57 LOG_PRIO,
58 LOG_SOURCE,
59 LOG_TIME,
60 __LOG_MAX
61};
62
63static const struct blobmsg_policy log_policy[] = {
64 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
65 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
66 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
67 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
68 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
69};
70
71static struct uloop_timeout retry;
72static struct uloop_fd sender;
xf.li44e08692024-01-30 01:54:44 -080073//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
74static 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 +080075static int log_type = LOG_STDOUT;
76static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
77static struct file_list_t file_list;
78static struct filter_list_t *filter_log = NULL;
79static char tmp_log[48] = {0};
xf.li4bd9ee42024-04-13 01:31:44 -070080pthread_t attr = -1;
liubin281ac462023-07-19 14:22:54 +080081
82static const char* getcodetext(int value, CODE *codetable) {
83 CODE *i;
84
85 if (value >= 0)
86 for (i = codetable; i->c_val != -1; i++)
87 if (i->c_val == value)
88 return (i->c_name);
89 return "<unknown>";
90};
91
92static void log_handle_reconnect(struct uloop_timeout *timeout)
93{
94 sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
95 if (sender.fd < 0) {
96 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
97 uloop_timeout_set(&retry, 1000);
98 } else {
99 uloop_fd_add(&sender, ULOOP_READ);
100 syslog(0, "Logread connected to %s:%s\n", log_ip, log_port);
101 }
102}
103
104static void log_handle_fd(struct uloop_fd *u, unsigned int events)
105{
106 if (u->eof) {
107 uloop_fd_delete(u);
108 close(sender.fd);
109 sender.fd = -1;
110 uloop_timeout_set(&retry, 1000);
111 }
112}
113
114static int filter_char_to_pri(char c)
115{
116 switch (c) {
117 case 'v':
118 return 8;
119 case 'd':
120 return LOG_DEBUG;
121 case 'i':
122 return LOG_INFO;
123 case 'w':
124 return LOG_WARNING;
125 case 'e':
126 return LOG_ERR;
127 case 'f':
128 return LOG_ALERT;
129 case '*':
130 default:
131 return 8;
132 }
133}
134
135static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
136{
137 struct filter_list_t *_filter = filter;
xf.li4bd9ee42024-04-13 01:31:44 -0700138 struct filter_list_t *_filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800139
140 while(_filter)
141 {
142 int p = filter_char_to_pri(_filter->priority);
xf.li43643772024-03-04 19:39:53 -0800143 int len = strlen(_filter->tag);
144 if(len > 0)
liubin281ac462023-07-19 14:22:54 +0800145 {
xf.li43643772024-03-04 19:39:53 -0800146 if(0 == memcmp(_filter->tag, tag, len))
147 {
148 if((pri < p) || (pri == p))
149 {
150 return 0;
151 }
152 else
153 {
154 return -1;
155 }
156 }
liubin281ac462023-07-19 14:22:54 +0800157 }else{ // have no tag
xf.li4bd9ee42024-04-13 01:31:44 -0700158 _filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800159 }
160 _filter = _filter->next;
161 }
xf.li4bd9ee42024-04-13 01:31:44 -0700162 //common tag
163 int p = filter_char_to_pri(_filter_common->priority);
164 if(pri > p)
165 return -1;
liubin281ac462023-07-19 14:22:54 +0800166
xf.li4bd9ee42024-04-13 01:31:44 -0700167 return 0;
liubin281ac462023-07-19 14:22:54 +0800168}
169static int log_notify(struct blob_attr *msg)
170{
171 struct blob_attr *tb[__LOG_MAX];
172 struct stat s;
xf.li43643772024-03-04 19:39:53 -0800173 char buf[512] = {'\0'};
xf.li127b6fa2024-04-28 01:48:45 -0700174 char tmp_buf[48] = {0};
liubin281ac462023-07-19 14:22:54 +0800175 uint32_t p;
176 char *str;
177 time_t t;
178 char *c, *m;
179
xf.li127b6fa2024-04-28 01:48:45 -0700180 sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
181 if(access(tmp_buf, W_OK) != 0)
182 {
183 sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
184 }
185
186 if (sender.fd < 0)
187 {
188 return 0;
189 }
liubin281ac462023-07-19 14:22:54 +0800190
191 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
192 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
193 return 1;
194
195 if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
196 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
197 if (sender.fd < 0) {
198 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
199 exit(-1);
200 }
201 }
202
203 m = blobmsg_get_string(tb[LOG_MSG]);
204 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
205 c = ctime(&t);
206 p = blobmsg_get_u32(tb[LOG_PRIO]);
207 c[strlen(c) - 1] = '\0';
208 str = blobmsg_format_json(msg, true);
209
210 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
211 {
212 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800213 return 0;
214 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800215 }
216 if (log_type == LOG_NET) {
217 int err;
218
219 snprintf(buf, sizeof(buf), "<%u>", p);
220 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800221 if (strlen(hostname) > 0) {
liubin281ac462023-07-19 14:22:54 +0800222 strncat(buf, hostname, sizeof(buf));
223 strncat(buf, " ", sizeof(buf));
224 }
xf.li44e08692024-01-30 01:54:44 -0800225 if (strlen(log_prefix) > 0) {
liubin281ac462023-07-19 14:22:54 +0800226 strncat(buf, log_prefix, sizeof(buf));
227 strncat(buf, ": ", sizeof(buf));
228 }
229 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
230 strncat(buf, "kernel: ", sizeof(buf));
231 strncat(buf, m, sizeof(buf));
232 if (log_udp)
233 err = write(sender.fd, buf, strlen(buf));
234 else
235 err = send(sender.fd, buf, strlen(buf), 0);
236
237 if (err < 0) {
238 syslog(0, "failed to send log data to %s:%s via %s\n",
239 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
240 uloop_fd_delete(&sender);
241 close(sender.fd);
242 sender.fd = -1;
243 uloop_timeout_set(&retry, 1000);
244 }
245 } else {
246 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
247 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
248 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
249 write(sender.fd, buf, strlen(buf));
250 }
251
252 free(str);
253 if (log_type == LOG_FILE)
254 fsync(sender.fd);
255
256 return 0;
257}
258
259static void logread_fd_data_cb(struct ustream *s, int bytes)
260{
261 while (true) {
262 int len;
263 struct blob_attr *a;
264
265 a = (void*) ustream_get_read_buf(s, &len);
266 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
267 break;
268 log_notify(a);
269 ustream_consume(s, blob_len(a) + sizeof(*a));
270 }
271 if (!log_follow)
272 uloop_end();
273}
274
275static void logread_fd_cb(struct ubus_request *req, int fd)
276{
277 static struct ustream_fd test_fd;
278
279 test_fd.stream.notify_read = logread_fd_data_cb;
280 ustream_fd_init(&test_fd, fd);
281}
282
283static void logread_complete_cb(struct ubus_request *req, int ret)
284{
285}
286
xf.li4bd9ee42024-04-13 01:31:44 -0700287int lynq_update_log_level()
288{
289 json_object* jsonobj = NULL;
290 json_object* tmpjson = NULL;
291 json_object* datajson = NULL;
292 json_object* listjson = NULL;
293 json_object* fileterjson = NULL;
294 json_object* fileter_listjson = NULL;
295 struct filter_list_t* filter_list_head = NULL;
296 struct filter_list_t* tmp_filter_list = NULL;
297 struct filter_list_t* _filter_list = NULL;
298
299 int n;
300 int array_length;
301 char* tmp_string = NULL;
302
303 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
304 if (NULL == jsonobj) {
305 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
306 return -1;
307 }
308 /***获取data***/
309 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
310 datajson = json_object_array_get_idx(tmpjson, 0);//syslog index is 0
311 if (NULL == datajson) {
312 json_object_put(jsonobj);
313 return -1;
314 }
315 json_object_object_get_ex(datajson, "filter_list", &listjson);
316 if (NULL == listjson) {
317 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
318 json_object_put(listjson);
319 return -1;
320 }
321 filter_list_head = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
322 _filter_list = filter_list_head;
323
324 array_length = json_object_array_length(listjson);
325 for (n = 0 ; n < array_length; n++) {
326 fileterjson = json_object_array_get_idx(listjson, n);
327 if (NULL == fileterjson) {
328 printf("the fileterjson exit\n");
329 free(tmp_filter_list->next);
330 tmp_filter_list->next = NULL;
331 break;
332 }
333 memset(_filter_list, 0, sizeof(struct filter_list_t));
334 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
335 char* str = json_object_get_string(fileter_listjson);
336 if (str) {
337 _filter_list->priority = str[0];
338 printf("fileter_listjson: %c\n", _filter_list->priority);
339 }
340
341 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
342
343 str = json_object_get_string(fileter_listjson);
344 if (str) {
345 _filter_list->tag = strdup(str);
346 printf("fileter_listjson: %s\n", _filter_list->tag);
347 }
348 else
349 {
350 _filter_list->tag = "\0";
351 }
352 //json_object_put(fileter_listjson);
353 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
354 if (NULL == _filter_list->next) {
355 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
356 break;
357 }
358 tmp_filter_list = _filter_list;
359 _filter_list = _filter_list->next;
360 _filter_list->next = NULL;
361 }
362 /***释放json对象***/
363 json_object_put(jsonobj);
364
365 tmp_filter_list = filter_log;
366 filter_log = filter_list_head;
367
368 while(tmp_filter_list != NULL) {
369 _filter_list = tmp_filter_list;
370 free(tmp_filter_list);
371 tmp_filter_list = _filter_list->next;
372 }
373
374 return 0;
375}
376
377
378int wait_update_log_level()
379{
380 int i = 0;
381 char recvBuff[100];
382 int serverFd,clientFd,addrLen;
383 struct sockaddr_un serverAddr,clientAddr;
384
385 pthread_detach(pthread_self());
386 printf("MBTK: in wait_update_log_level\n");
387
388 memset(&serverAddr,0,sizeof(serverAddr));
389 serverAddr.sun_family = AF_UNIX;
390 sprintf(serverAddr.sun_path,"%s","/var/log_server.socket");
391
392 unlink("/var/log_server.socket"); /* in case it already exists */
393
394 if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
395 {
396 printf("err -1\n");
397 return -1;
398 }
399
400 if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
401 {
402 printf("err -2\n");
403 close(serverFd);
404 return -2;
405 }
406
407 if(listen(serverFd,10) < 0)
408 {
409 printf("err -3\n");
410 return -3;
411 }
412
413 while(1)
414 {
415 addrLen = sizeof(clientAddr);
416 memset(&clientAddr,0,sizeof(clientAddr));
417 memset(&recvBuff,0,100);
418
419 if((clientFd = accept(serverFd,(struct sockaddr*)&clientAddr,&addrLen)) < 0)
420 {
421 printf("err -4\n");
422 continue;
423 }
424 printf("MBTK: wait recv\n");
425 if(recv(clientFd,recvBuff,100,0) < 0)
426 {
427 printf("err -5");
428 close(clientFd);
429 continue;
430 }
431 if(strncmp(recvBuff, "update", strlen("update")) == 0)
432 {
433 lynq_update_log_level();
434 }
435
436 close(clientFd);
437 }
438 close(serverFd);
439
440 return 0;
441}
442
443int syslog_pthread_create()
444{
445 int ret;
446
447 ret = pthread_create(&attr, NULL, wait_update_log_level, NULL);
448
449 if (ret < 0)
450 {
451 printf("MBTK:pthread create fail");
452 return -1;
453 }
454
455 return -1;
456}
457
liubin281ac462023-07-19 14:22:54 +0800458void* syslog_main(void* argv)
459{
460 static struct ubus_request req;
461 struct ubus_context *ctx;
462 uint32_t id;
463 const char *ubus_socket = NULL;
464 int ch, ret, lines = 0;
465 static struct blob_buf b;
xf.li44e08692024-01-30 01:54:44 -0800466 int tries = 60;
xf.li50b8baf2024-04-14 19:51:18 -0700467 void* tret;
468
liubin281ac462023-07-19 14:22:54 +0800469 log_config_entry *config = (log_config_entry *)argv;
470
471 pthread_detach(pthread_self());
472
473 if (NULL == argv)
474 return NULL;
475
476 signal(SIGPIPE, SIG_IGN);
477 uloop_init();
478
xf.li4bd9ee42024-04-13 01:31:44 -0700479
480 syslog_pthread_create();
xf.li44e08692024-01-30 01:54:44 -0800481 //log_file = config->out_path;
482 memset(log_file, 0, sizeof(log_file));
483 memset(log_ip, 0, sizeof(log_ip));
484 memset(log_port, 0, sizeof(log_port));
485 memset(log_prefix, 0, sizeof(log_prefix));
486 memset(pid_file, 0, sizeof(pid_file));
487 memset(hostname, 0, sizeof(hostname));
488
489 if(config->out_path != NULL)
490 {
491 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
492 }
493
liubin281ac462023-07-19 14:22:54 +0800494 memset(&file_list, 0, sizeof(struct file_list_t));
495 file_list.total = config->rotate_file_count;
496 if(config->rotate_file_size)
497 log_size = config->rotate_file_size;
498 if(config->ip)
499 {
500 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
xf.li44e08692024-01-30 01:54:44 -0800501 //log_ip = config->ip;
502 strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
503 //log_port = config->port;
504 if(config->port != NULL)
505 {
506 strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
507 }
liubin281ac462023-07-19 14:22:54 +0800508 }
509 filter_log = config->filter_list;
510 // Follow log messages
511 log_follow = 1;
512 ctx = ubus_connect(ubus_socket);
513 if (!ctx) {
514 fprintf(stderr, "Failed to connect to ubus\n");
515 return -1;
516 }
517 ubus_add_uloop(ctx);
518
519 printf("syslog log start...\n");
520 /* ugly ugly ugly ... we need a real reconnect logic */
521 do {
522 ret = ubus_lookup_id(ctx, "log", &id);
523 if (ret) {
524 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
525 sleep(1);
526 continue;
527 }
liubin281ac462023-07-19 14:22:54 +0800528 blob_buf_init(&b, 0);
529 if (lines)
530 blobmsg_add_u32(&b, "lines", lines);
531 else if (log_follow)
532 blobmsg_add_u32(&b, "lines", 0);
533 if (log_follow) {
xf.li44e08692024-01-30 01:54:44 -0800534 if (strlen(pid_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800535 FILE *fp = fopen(pid_file, "w+");
536 if (fp) {
537 fprintf(fp, "%d", getpid());
538 fclose(fp);
539 }
540 }
541 }
542
xf.li44e08692024-01-30 01:54:44 -0800543 if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
liubin281ac462023-07-19 14:22:54 +0800544 openlog("logread", LOG_PID, LOG_DAEMON);
545 log_type = LOG_NET;
546 sender.cb = log_handle_fd;
547 retry.cb = log_handle_reconnect;
548 uloop_timeout_set(&retry, 1000);
xf.li44e08692024-01-30 01:54:44 -0800549 } else if (strlen(log_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800550 log_type = LOG_FILE;
551 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
xf.li44e08692024-01-30 01:54:44 -0800552 sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
liubin281ac462023-07-19 14:22:54 +0800553 sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
554 if (sender.fd < 0) {
555 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
556 exit(-1);
557 }
558 } else {
559 sender.fd = STDOUT_FILENO;
560 }
561
562 ubus_invoke_async(ctx, id, "read", b.head, &req);
563 req.fd_cb = logread_fd_cb;
564 req.complete_cb = logread_complete_cb;
565 ubus_complete_request_async(ctx, &req);
b.liu9a306862024-03-06 16:49:40 +0800566
liubin281ac462023-07-19 14:22:54 +0800567 uloop_run();
568 ubus_free(ctx);
569 uloop_done();
570
571 } while (ret && tries--);
xf.li4bd9ee42024-04-13 01:31:44 -0700572
573 if (attr) {
574 if (pthread_join(attr, &tret) != 0) {
575 printf("MBTK:Join thread: %d error!\n", attr);
576 exit(1);
577 }
578 }
liubin281ac462023-07-19 14:22:54 +0800579
580 pthread_exit(NULL);
581 return NULL;
582}