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