blob: db30d57c7e4b4c13d72c57a226aee8f33380a942 [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>
b.liub3b923a2024-06-06 15:15:49 +080028#include "json-c/json.h"
29#include "json-c/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':
l.yang8ee686d2024-10-14 19:07:51 -0700118 return LOG_CRIT;
liubin281ac462023-07-19 14:22:54 +0800119 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
l.yangb7972c72024-10-15 22:34:51 -0700180 //
181 //sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
182
183
184 snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
xf.li127b6fa2024-04-28 01:48:45 -0700185 if(access(tmp_buf, W_OK) != 0)
186 {
187 sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
188 }
189
190 if (sender.fd < 0)
191 {
192 return 0;
193 }
liubin281ac462023-07-19 14:22:54 +0800194
195 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
196 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
197 return 1;
198
199 if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
200 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
201 if (sender.fd < 0) {
202 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
203 exit(-1);
204 }
205 }
206
207 m = blobmsg_get_string(tb[LOG_MSG]);
208 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
209 c = ctime(&t);
210 p = blobmsg_get_u32(tb[LOG_PRIO]);
211 c[strlen(c) - 1] = '\0';
212 str = blobmsg_format_json(msg, true);
213
214 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
215 {
216 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800217 return 0;
218 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800219 }
220 if (log_type == LOG_NET) {
221 int err;
222
223 snprintf(buf, sizeof(buf), "<%u>", p);
224 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800225 if (strlen(hostname) > 0) {
liubin281ac462023-07-19 14:22:54 +0800226 strncat(buf, hostname, sizeof(buf));
227 strncat(buf, " ", sizeof(buf));
228 }
xf.li44e08692024-01-30 01:54:44 -0800229 if (strlen(log_prefix) > 0) {
liubin281ac462023-07-19 14:22:54 +0800230 strncat(buf, log_prefix, sizeof(buf));
231 strncat(buf, ": ", sizeof(buf));
232 }
233 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
234 strncat(buf, "kernel: ", sizeof(buf));
235 strncat(buf, m, sizeof(buf));
236 if (log_udp)
237 err = write(sender.fd, buf, strlen(buf));
238 else
239 err = send(sender.fd, buf, strlen(buf), 0);
240
241 if (err < 0) {
242 syslog(0, "failed to send log data to %s:%s via %s\n",
243 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
244 uloop_fd_delete(&sender);
245 close(sender.fd);
246 sender.fd = -1;
247 uloop_timeout_set(&retry, 1000);
248 }
249 } else {
250 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
251 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
252 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
253 write(sender.fd, buf, strlen(buf));
254 }
255
256 free(str);
257 if (log_type == LOG_FILE)
258 fsync(sender.fd);
259
260 return 0;
261}
262
263static void logread_fd_data_cb(struct ustream *s, int bytes)
264{
265 while (true) {
266 int len;
267 struct blob_attr *a;
268
269 a = (void*) ustream_get_read_buf(s, &len);
270 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
271 break;
272 log_notify(a);
273 ustream_consume(s, blob_len(a) + sizeof(*a));
274 }
275 if (!log_follow)
276 uloop_end();
277}
278
279static void logread_fd_cb(struct ubus_request *req, int fd)
280{
281 static struct ustream_fd test_fd;
282
283 test_fd.stream.notify_read = logread_fd_data_cb;
284 ustream_fd_init(&test_fd, fd);
285}
286
287static void logread_complete_cb(struct ubus_request *req, int ret)
288{
289}
290
xf.li4bd9ee42024-04-13 01:31:44 -0700291int lynq_update_log_level()
292{
293 json_object* jsonobj = NULL;
294 json_object* tmpjson = NULL;
295 json_object* datajson = NULL;
296 json_object* listjson = NULL;
297 json_object* fileterjson = NULL;
298 json_object* fileter_listjson = NULL;
299 struct filter_list_t* filter_list_head = NULL;
300 struct filter_list_t* tmp_filter_list = NULL;
301 struct filter_list_t* _filter_list = NULL;
302
303 int n;
304 int array_length;
305 char* tmp_string = NULL;
306
307 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
308 if (NULL == jsonobj) {
309 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
310 return -1;
311 }
312 /***获取data***/
313 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
314 datajson = json_object_array_get_idx(tmpjson, 0);//syslog index is 0
315 if (NULL == datajson) {
316 json_object_put(jsonobj);
317 return -1;
318 }
319 json_object_object_get_ex(datajson, "filter_list", &listjson);
320 if (NULL == listjson) {
321 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
322 json_object_put(listjson);
323 return -1;
324 }
325 filter_list_head = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
326 _filter_list = filter_list_head;
327
328 array_length = json_object_array_length(listjson);
329 for (n = 0 ; n < array_length; n++) {
330 fileterjson = json_object_array_get_idx(listjson, n);
331 if (NULL == fileterjson) {
332 printf("the fileterjson exit\n");
333 free(tmp_filter_list->next);
334 tmp_filter_list->next = NULL;
335 break;
336 }
337 memset(_filter_list, 0, sizeof(struct filter_list_t));
338 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
339 char* str = json_object_get_string(fileter_listjson);
340 if (str) {
341 _filter_list->priority = str[0];
342 printf("fileter_listjson: %c\n", _filter_list->priority);
343 }
344
345 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
346
347 str = json_object_get_string(fileter_listjson);
348 if (str) {
349 _filter_list->tag = strdup(str);
350 printf("fileter_listjson: %s\n", _filter_list->tag);
351 }
352 else
353 {
354 _filter_list->tag = "\0";
355 }
356 //json_object_put(fileter_listjson);
357 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
358 if (NULL == _filter_list->next) {
359 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
360 break;
361 }
362 tmp_filter_list = _filter_list;
363 _filter_list = _filter_list->next;
364 _filter_list->next = NULL;
365 }
366 /***释放json对象***/
367 json_object_put(jsonobj);
368
369 tmp_filter_list = filter_log;
370 filter_log = filter_list_head;
371
372 while(tmp_filter_list != NULL) {
373 _filter_list = tmp_filter_list;
374 free(tmp_filter_list);
375 tmp_filter_list = _filter_list->next;
376 }
377
378 return 0;
379}
380
381
382int wait_update_log_level()
383{
384 int i = 0;
385 char recvBuff[100];
386 int serverFd,clientFd,addrLen;
387 struct sockaddr_un serverAddr,clientAddr;
388
389 pthread_detach(pthread_self());
390 printf("MBTK: in wait_update_log_level\n");
391
392 memset(&serverAddr,0,sizeof(serverAddr));
393 serverAddr.sun_family = AF_UNIX;
394 sprintf(serverAddr.sun_path,"%s","/var/log_server.socket");
395
396 unlink("/var/log_server.socket"); /* in case it already exists */
397
398 if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
399 {
400 printf("err -1\n");
401 return -1;
402 }
403
404 if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
405 {
406 printf("err -2\n");
407 close(serverFd);
408 return -2;
409 }
410
411 if(listen(serverFd,10) < 0)
412 {
413 printf("err -3\n");
414 return -3;
415 }
416
417 while(1)
418 {
419 addrLen = sizeof(clientAddr);
420 memset(&clientAddr,0,sizeof(clientAddr));
421 memset(&recvBuff,0,100);
422
423 if((clientFd = accept(serverFd,(struct sockaddr*)&clientAddr,&addrLen)) < 0)
424 {
425 printf("err -4\n");
426 continue;
427 }
428 printf("MBTK: wait recv\n");
429 if(recv(clientFd,recvBuff,100,0) < 0)
430 {
431 printf("err -5");
432 close(clientFd);
433 continue;
434 }
435 if(strncmp(recvBuff, "update", strlen("update")) == 0)
436 {
437 lynq_update_log_level();
438 }
439
440 close(clientFd);
441 }
442 close(serverFd);
443
444 return 0;
445}
446
447int syslog_pthread_create()
448{
449 int ret;
450
451 ret = pthread_create(&attr, NULL, wait_update_log_level, NULL);
452
453 if (ret < 0)
454 {
455 printf("MBTK:pthread create fail");
456 return -1;
457 }
458
459 return -1;
460}
461
liubin281ac462023-07-19 14:22:54 +0800462void* syslog_main(void* argv)
463{
464 static struct ubus_request req;
465 struct ubus_context *ctx;
466 uint32_t id;
467 const char *ubus_socket = NULL;
468 int ch, ret, lines = 0;
469 static struct blob_buf b;
xf.li44e08692024-01-30 01:54:44 -0800470 int tries = 60;
xf.li50b8baf2024-04-14 19:51:18 -0700471 void* tret;
472
liubin281ac462023-07-19 14:22:54 +0800473 log_config_entry *config = (log_config_entry *)argv;
474
475 pthread_detach(pthread_self());
476
477 if (NULL == argv)
478 return NULL;
479
480 signal(SIGPIPE, SIG_IGN);
481 uloop_init();
482
xf.li4bd9ee42024-04-13 01:31:44 -0700483
484 syslog_pthread_create();
xf.li44e08692024-01-30 01:54:44 -0800485 //log_file = config->out_path;
486 memset(log_file, 0, sizeof(log_file));
487 memset(log_ip, 0, sizeof(log_ip));
488 memset(log_port, 0, sizeof(log_port));
489 memset(log_prefix, 0, sizeof(log_prefix));
490 memset(pid_file, 0, sizeof(pid_file));
491 memset(hostname, 0, sizeof(hostname));
492
493 if(config->out_path != NULL)
494 {
495 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
496 }
497
liubin281ac462023-07-19 14:22:54 +0800498 memset(&file_list, 0, sizeof(struct file_list_t));
499 file_list.total = config->rotate_file_count;
500 if(config->rotate_file_size)
501 log_size = config->rotate_file_size;
502 if(config->ip)
503 {
504 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
xf.li44e08692024-01-30 01:54:44 -0800505 //log_ip = config->ip;
506 strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
507 //log_port = config->port;
508 if(config->port != NULL)
509 {
510 strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
511 }
liubin281ac462023-07-19 14:22:54 +0800512 }
513 filter_log = config->filter_list;
514 // Follow log messages
515 log_follow = 1;
516 ctx = ubus_connect(ubus_socket);
517 if (!ctx) {
518 fprintf(stderr, "Failed to connect to ubus\n");
519 return -1;
520 }
521 ubus_add_uloop(ctx);
522
523 printf("syslog log start...\n");
524 /* ugly ugly ugly ... we need a real reconnect logic */
525 do {
526 ret = ubus_lookup_id(ctx, "log", &id);
527 if (ret) {
528 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
529 sleep(1);
530 continue;
531 }
liubin281ac462023-07-19 14:22:54 +0800532 blob_buf_init(&b, 0);
533 if (lines)
534 blobmsg_add_u32(&b, "lines", lines);
535 else if (log_follow)
536 blobmsg_add_u32(&b, "lines", 0);
537 if (log_follow) {
xf.li44e08692024-01-30 01:54:44 -0800538 if (strlen(pid_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800539 FILE *fp = fopen(pid_file, "w+");
540 if (fp) {
541 fprintf(fp, "%d", getpid());
542 fclose(fp);
543 }
544 }
545 }
546
xf.li44e08692024-01-30 01:54:44 -0800547 if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
liubin281ac462023-07-19 14:22:54 +0800548 openlog("logread", LOG_PID, LOG_DAEMON);
549 log_type = LOG_NET;
550 sender.cb = log_handle_fd;
551 retry.cb = log_handle_reconnect;
552 uloop_timeout_set(&retry, 1000);
xf.li44e08692024-01-30 01:54:44 -0800553 } else if (strlen(log_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800554 log_type = LOG_FILE;
555 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
l.yangb7972c72024-10-15 22:34:51 -0700556
557 //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
558
559 //直接将log文件存储在不会掉电丢失的路径
560 snprintf(tmp_log,sizeof(tmp_log), "%s",log_file);
liubin281ac462023-07-19 14:22:54 +0800561 sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
562 if (sender.fd < 0) {
563 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
564 exit(-1);
565 }
566 } else {
567 sender.fd = STDOUT_FILENO;
568 }
569
570 ubus_invoke_async(ctx, id, "read", b.head, &req);
571 req.fd_cb = logread_fd_cb;
572 req.complete_cb = logread_complete_cb;
573 ubus_complete_request_async(ctx, &req);
b.liu9a306862024-03-06 16:49:40 +0800574
liubin281ac462023-07-19 14:22:54 +0800575 uloop_run();
576 ubus_free(ctx);
577 uloop_done();
578
579 } while (ret && tries--);
xf.li4bd9ee42024-04-13 01:31:44 -0700580
581 if (attr) {
582 if (pthread_join(attr, &tret) != 0) {
583 printf("MBTK:Join thread: %d error!\n", attr);
584 exit(1);
585 }
586 }
liubin281ac462023-07-19 14:22:54 +0800587
588 pthread_exit(NULL);
589 return NULL;
590}