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