blob: bfc70ba253ffcb46999b19a2bd23c99955edea27 [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>
b.liu0f7ffad2024-11-06 19:57:27 +080025#include <stdlib.h>
26#include <pthread.h>
liubin281ac462023-07-19 14:22:54 +080027
28#define SYSLOG_NAMES
29#include <syslog.h>
b.liub3b923a2024-06-06 15:15:49 +080030#include "json-c/json.h"
31#include "json-c/printbuf.h"
liubin281ac462023-07-19 14:22:54 +080032
33#include <libubox/ustream.h>
34#include <libubox/blobmsg_json.h>
35#include <libubox/usock.h>
36#include <libubox/uloop.h>
37#include "libubus.h"
b.liu63a4a322024-03-07 19:00:53 +080038#include "syslog.h"
liubin281ac462023-07-19 14:22:54 +080039#include "log_config.h"
b.liu0f7ffad2024-11-06 19:57:27 +080040
41#include "mbtk_utils.h"
b.liu9a306862024-03-06 16:49:40 +080042//#include "lynq/liblog.h"
liubin281ac462023-07-19 14:22:54 +080043
b.liu63a4a322024-03-07 19:00:53 +080044enum {
45 SOURCE_KLOG = 0,
46 SOURCE_SYSLOG = 1,
47 SOURCE_INTERNAL = 2,
48 SOURCE_ANY = 0xff,
49};
50
xf.li44e08692024-01-30 01:54:44 -080051#define LOG_CONFIG_LEN 50
liubin281ac462023-07-19 14:22:54 +080052enum {
53 LOG_STDOUT,
54 LOG_FILE,
55 LOG_NET,
56};
57
58enum {
59 LOG_MSG,
60 LOG_ID,
61 LOG_PRIO,
62 LOG_SOURCE,
63 LOG_TIME,
64 __LOG_MAX
65};
b.liu0f7ffad2024-11-06 19:57:27 +080066
l.yang67782e62024-11-05 01:28:10 -080067#define SYSLOG_BUFF_SIZE (4*1024)
68#define MAX_BUFFER_SIZE (8*1024)
69
liubin281ac462023-07-19 14:22:54 +080070
71static const struct blobmsg_policy log_policy[] = {
72 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
73 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
74 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
75 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
76 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
77};
78
79static struct uloop_timeout retry;
80static struct uloop_fd sender;
xf.li44e08692024-01-30 01:54:44 -080081//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
82static 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 +080083static int log_type = LOG_STDOUT;
84static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
85static struct file_list_t file_list;
86static struct filter_list_t *filter_log = NULL;
b.liu0f7ffad2024-11-06 19:57:27 +080087static char tmp_log[100] = {0};
xf.li4bd9ee42024-04-13 01:31:44 -070088pthread_t attr = -1;
liubin281ac462023-07-19 14:22:54 +080089
90static const char* getcodetext(int value, CODE *codetable) {
91 CODE *i;
92
93 if (value >= 0)
94 for (i = codetable; i->c_val != -1; i++)
95 if (i->c_val == value)
96 return (i->c_name);
97 return "<unknown>";
98};
99
100static void log_handle_reconnect(struct uloop_timeout *timeout)
101{
102 sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
103 if (sender.fd < 0) {
104 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
105 uloop_timeout_set(&retry, 1000);
106 } else {
107 uloop_fd_add(&sender, ULOOP_READ);
108 syslog(0, "Logread connected to %s:%s\n", log_ip, log_port);
109 }
110}
111
112static void log_handle_fd(struct uloop_fd *u, unsigned int events)
113{
114 if (u->eof) {
115 uloop_fd_delete(u);
116 close(sender.fd);
117 sender.fd = -1;
118 uloop_timeout_set(&retry, 1000);
119 }
120}
121
122static int filter_char_to_pri(char c)
123{
124 switch (c) {
125 case 'v':
l.yang8ee686d2024-10-14 19:07:51 -0700126 return LOG_CRIT;
liubin281ac462023-07-19 14:22:54 +0800127 case 'd':
128 return LOG_DEBUG;
129 case 'i':
130 return LOG_INFO;
131 case 'w':
132 return LOG_WARNING;
133 case 'e':
134 return LOG_ERR;
135 case 'f':
136 return LOG_ALERT;
137 case '*':
138 default:
139 return 8;
140 }
141}
142
143static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
144{
145 struct filter_list_t *_filter = filter;
xf.li4bd9ee42024-04-13 01:31:44 -0700146 struct filter_list_t *_filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800147
148 while(_filter)
149 {
150 int p = filter_char_to_pri(_filter->priority);
xf.li43643772024-03-04 19:39:53 -0800151 int len = strlen(_filter->tag);
152 if(len > 0)
liubin281ac462023-07-19 14:22:54 +0800153 {
xf.li43643772024-03-04 19:39:53 -0800154 if(0 == memcmp(_filter->tag, tag, len))
155 {
156 if((pri < p) || (pri == p))
157 {
158 return 0;
159 }
160 else
161 {
162 return -1;
163 }
164 }
liubin281ac462023-07-19 14:22:54 +0800165 }else{ // have no tag
xf.li4bd9ee42024-04-13 01:31:44 -0700166 _filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800167 }
168 _filter = _filter->next;
169 }
xf.li4bd9ee42024-04-13 01:31:44 -0700170 //common tag
171 int p = filter_char_to_pri(_filter_common->priority);
172 if(pri > p)
173 return -1;
liubin281ac462023-07-19 14:22:54 +0800174
xf.li4bd9ee42024-04-13 01:31:44 -0700175 return 0;
liubin281ac462023-07-19 14:22:54 +0800176}
177static int log_notify(struct blob_attr *msg)
178{
xf.li01e39822024-10-30 16:00:32 +0800179 int index = 0;
180 int len = 0;
liubin281ac462023-07-19 14:22:54 +0800181 struct blob_attr *tb[__LOG_MAX];
182 struct stat s;
b.liu0f7ffad2024-11-06 19:57:27 +0800183 char buf[1024 * 2] = {'\0'};
184 char tmp_buf[1024] = {0};
liubin281ac462023-07-19 14:22:54 +0800185 uint32_t p;
186 char *str;
187 time_t t;
188 char *c, *m;
189
l.yang67782e62024-11-05 01:28:10 -0800190 static char buffer[MAX_BUFFER_SIZE] = {0};
191 static int buffer_index = 0;
b.liu0f7ffad2024-11-06 19:57:27 +0800192
l.yangb7972c72024-10-15 22:34:51 -0700193 //
194 //sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
195
b.liu0f7ffad2024-11-06 19:57:27 +0800196
l.yangb7972c72024-10-15 22:34:51 -0700197 snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
xf.li127b6fa2024-04-28 01:48:45 -0700198 if(access(tmp_buf, W_OK) != 0)
199 {
200 sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
l.yang67782e62024-11-05 01:28:10 -0800201 if(sender.fd < 0)
202 {
203 perror("Failed to open file ");
204 return -1;
205 }
xf.li127b6fa2024-04-28 01:48:45 -0700206 }
b.liu0f7ffad2024-11-06 19:57:27 +0800207
208
209
liubin281ac462023-07-19 14:22:54 +0800210 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
211 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
212 return 1;
213
l.yang67782e62024-11-05 01:28:10 -0800214 if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size))
215 {
216 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
b.liu0f7ffad2024-11-06 19:57:27 +0800217 if (sender.fd < 0)
l.yang67782e62024-11-05 01:28:10 -0800218 {
219 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
220 exit(-1);
221 }
222 }
liubin281ac462023-07-19 14:22:54 +0800223
224 m = blobmsg_get_string(tb[LOG_MSG]);
225 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
226 c = ctime(&t);
227 p = blobmsg_get_u32(tb[LOG_PRIO]);
228 c[strlen(c) - 1] = '\0';
229 str = blobmsg_format_json(msg, true);
230
231 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
232 {
233 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800234 return 0;
235 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800236 }
b.liu0f7ffad2024-11-06 19:57:27 +0800237 if (log_type == LOG_NET)
l.yang67782e62024-11-05 01:28:10 -0800238 {
liubin281ac462023-07-19 14:22:54 +0800239 int err;
240
241 snprintf(buf, sizeof(buf), "<%u>", p);
242 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800243 if (strlen(hostname) > 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800244 strncat(buf, hostname, strlen(hostname));
245 strncat(buf, " ", 1);
liubin281ac462023-07-19 14:22:54 +0800246 }
xf.li44e08692024-01-30 01:54:44 -0800247 if (strlen(log_prefix) > 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800248 strncat(buf, log_prefix, strlen(log_prefix));
249 strncat(buf, ": ", 2);
liubin281ac462023-07-19 14:22:54 +0800250 }
251 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
b.liu0f7ffad2024-11-06 19:57:27 +0800252 strncat(buf, "kernel: ", strlen("kernel: "));
253 strncat(buf, m, strlen(m));
liubin281ac462023-07-19 14:22:54 +0800254 if (log_udp)
255 err = write(sender.fd, buf, strlen(buf));
256 else
257 err = send(sender.fd, buf, strlen(buf), 0);
258
259 if (err < 0) {
260 syslog(0, "failed to send log data to %s:%s via %s\n",
261 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
262 uloop_fd_delete(&sender);
263 close(sender.fd);
264 sender.fd = -1;
265 uloop_timeout_set(&retry, 1000);
266 }
b.liu0f7ffad2024-11-06 19:57:27 +0800267 }
268 else
l.yang67782e62024-11-05 01:28:10 -0800269 {
liubin281ac462023-07-19 14:22:54 +0800270 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
271 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
272 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
xf.li01e39822024-10-30 16:00:32 +0800273
274 len = strlen(buf);
275 if(access("/etc/syslog_encrypt_flag", F_OK) == 0)
276 {
277 for(index = 0; index < len; index++)
278 {
279 buf[index] ^= 1;
280 }
281 }
l.yang67782e62024-11-05 01:28:10 -0800282
b.liu0f7ffad2024-11-06 19:57:27 +0800283
284
l.yang67782e62024-11-05 01:28:10 -0800285 if (buffer_index >= SYSLOG_BUFF_SIZE)
286 {
287 // Flush the buffer if it's full
b.liu0f7ffad2024-11-06 19:57:27 +0800288 if (write(sender.fd, buffer, buffer_index) < 0)
l.yang67782e62024-11-05 01:28:10 -0800289 {
290 perror("write error");
291 return -1;
292 }
293 buffer_index = 0; // Reset buffer index after flushing
294 }
b.liu0f7ffad2024-11-06 19:57:27 +0800295
l.yang67782e62024-11-05 01:28:10 -0800296 if(len < SYSLOG_BUFF_SIZE)
297 {
b.liu0f7ffad2024-11-06 19:57:27 +0800298 //copy buf to buffer
l.yang67782e62024-11-05 01:28:10 -0800299 memcpy(buffer + buffer_index, buf, len);
300 buffer_index += len;
301 }
302 else
303 {
b.liu0f7ffad2024-11-06 19:57:27 +0800304 mbtk_write(sender.fd, buf, len);
l.yang67782e62024-11-05 01:28:10 -0800305 }
b.liu0f7ffad2024-11-06 19:57:27 +0800306
307
liubin281ac462023-07-19 14:22:54 +0800308 }
309
310 free(str);
l.yang67782e62024-11-05 01:28:10 -0800311 //if (log_type == LOG_FILE)
312 //fsync(sender.fd);
liubin281ac462023-07-19 14:22:54 +0800313
314 return 0;
315}
316
317static void logread_fd_data_cb(struct ustream *s, int bytes)
318{
319 while (true) {
320 int len;
321 struct blob_attr *a;
322
323 a = (void*) ustream_get_read_buf(s, &len);
324 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
325 break;
326 log_notify(a);
327 ustream_consume(s, blob_len(a) + sizeof(*a));
328 }
329 if (!log_follow)
330 uloop_end();
331}
332
333static void logread_fd_cb(struct ubus_request *req, int fd)
334{
335 static struct ustream_fd test_fd;
336
337 test_fd.stream.notify_read = logread_fd_data_cb;
338 ustream_fd_init(&test_fd, fd);
339}
340
341static void logread_complete_cb(struct ubus_request *req, int ret)
342{
343}
344
xf.li4bd9ee42024-04-13 01:31:44 -0700345int lynq_update_log_level()
346{
347 json_object* jsonobj = NULL;
348 json_object* tmpjson = NULL;
349 json_object* datajson = NULL;
350 json_object* listjson = NULL;
351 json_object* fileterjson = NULL;
352 json_object* fileter_listjson = NULL;
353 struct filter_list_t* filter_list_head = NULL;
354 struct filter_list_t* tmp_filter_list = NULL;
355 struct filter_list_t* _filter_list = NULL;
356
357 int n;
358 int array_length;
b.liu0f7ffad2024-11-06 19:57:27 +0800359// char* tmp_string = NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700360
361 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
362 if (NULL == jsonobj) {
363 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
364 return -1;
365 }
366 /***获取data***/
367 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
368 datajson = json_object_array_get_idx(tmpjson, 0);//syslog index is 0
369 if (NULL == datajson) {
370 json_object_put(jsonobj);
371 return -1;
372 }
373 json_object_object_get_ex(datajson, "filter_list", &listjson);
374 if (NULL == listjson) {
375 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
376 json_object_put(listjson);
377 return -1;
378 }
379 filter_list_head = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
380 _filter_list = filter_list_head;
381
382 array_length = json_object_array_length(listjson);
383 for (n = 0 ; n < array_length; n++) {
384 fileterjson = json_object_array_get_idx(listjson, n);
385 if (NULL == fileterjson) {
386 printf("the fileterjson exit\n");
387 free(tmp_filter_list->next);
388 tmp_filter_list->next = NULL;
389 break;
390 }
391 memset(_filter_list, 0, sizeof(struct filter_list_t));
392 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800393 const char* str = json_object_get_string(fileter_listjson);
xf.li4bd9ee42024-04-13 01:31:44 -0700394 if (str) {
395 _filter_list->priority = str[0];
396 printf("fileter_listjson: %c\n", _filter_list->priority);
397 }
398
399 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800400
xf.li4bd9ee42024-04-13 01:31:44 -0700401 str = json_object_get_string(fileter_listjson);
402 if (str) {
403 _filter_list->tag = strdup(str);
404 printf("fileter_listjson: %s\n", _filter_list->tag);
405 }
406 else
407 {
408 _filter_list->tag = "\0";
409 }
410 //json_object_put(fileter_listjson);
411 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
412 if (NULL == _filter_list->next) {
413 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
414 break;
415 }
416 tmp_filter_list = _filter_list;
417 _filter_list = _filter_list->next;
418 _filter_list->next = NULL;
419 }
420 /***释放json对象***/
421 json_object_put(jsonobj);
422
423 tmp_filter_list = filter_log;
424 filter_log = filter_list_head;
425
426 while(tmp_filter_list != NULL) {
427 _filter_list = tmp_filter_list;
428 free(tmp_filter_list);
429 tmp_filter_list = _filter_list->next;
430 }
b.liu0f7ffad2024-11-06 19:57:27 +0800431
xf.li4bd9ee42024-04-13 01:31:44 -0700432 return 0;
433}
434
435
b.liu0f7ffad2024-11-06 19:57:27 +0800436void* wait_update_log_level(void *arg)
xf.li4bd9ee42024-04-13 01:31:44 -0700437{
b.liu0f7ffad2024-11-06 19:57:27 +0800438// int i = 0;
xf.li4bd9ee42024-04-13 01:31:44 -0700439 char recvBuff[100];
b.liu0f7ffad2024-11-06 19:57:27 +0800440 int serverFd,clientFd;
441 socklen_t addrLen;
xf.li4bd9ee42024-04-13 01:31:44 -0700442 struct sockaddr_un serverAddr,clientAddr;
443
444 pthread_detach(pthread_self());
445 printf("MBTK: in wait_update_log_level\n");
446
447 memset(&serverAddr,0,sizeof(serverAddr));
448 serverAddr.sun_family = AF_UNIX;
449 sprintf(serverAddr.sun_path,"%s","/var/log_server.socket");
450
451 unlink("/var/log_server.socket"); /* in case it already exists */
452
453 if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
454 {
455 printf("err -1\n");
b.liu0f7ffad2024-11-06 19:57:27 +0800456 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700457 }
458
459 if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
460 {
461 printf("err -2\n");
462 close(serverFd);
b.liu0f7ffad2024-11-06 19:57:27 +0800463 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700464 }
465
466 if(listen(serverFd,10) < 0)
467 {
468 printf("err -3\n");
b.liu0f7ffad2024-11-06 19:57:27 +0800469 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700470 }
471
472 while(1)
473 {
474 addrLen = sizeof(clientAddr);
475 memset(&clientAddr,0,sizeof(clientAddr));
476 memset(&recvBuff,0,100);
477
478 if((clientFd = accept(serverFd,(struct sockaddr*)&clientAddr,&addrLen)) < 0)
479 {
480 printf("err -4\n");
481 continue;
482 }
483 printf("MBTK: wait recv\n");
484 if(recv(clientFd,recvBuff,100,0) < 0)
485 {
486 printf("err -5");
487 close(clientFd);
488 continue;
489 }
490 if(strncmp(recvBuff, "update", strlen("update")) == 0)
491 {
492 lynq_update_log_level();
493 }
494
495 close(clientFd);
496 }
497 close(serverFd);
498
b.liu0f7ffad2024-11-06 19:57:27 +0800499 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700500}
501
502int syslog_pthread_create()
503{
504 int ret;
b.liu0f7ffad2024-11-06 19:57:27 +0800505
xf.li4bd9ee42024-04-13 01:31:44 -0700506 ret = pthread_create(&attr, NULL, wait_update_log_level, NULL);
507
508 if (ret < 0)
509 {
510 printf("MBTK:pthread create fail");
511 return -1;
512 }
513
514 return -1;
515}
516
liubin281ac462023-07-19 14:22:54 +0800517void* syslog_main(void* argv)
518{
519 static struct ubus_request req;
520 struct ubus_context *ctx;
521 uint32_t id;
522 const char *ubus_socket = NULL;
b.liu0f7ffad2024-11-06 19:57:27 +0800523 int ret, lines = 0;
liubin281ac462023-07-19 14:22:54 +0800524 static struct blob_buf b;
xf.li44e08692024-01-30 01:54:44 -0800525 int tries = 60;
xf.li50b8baf2024-04-14 19:51:18 -0700526 void* tret;
527
liubin281ac462023-07-19 14:22:54 +0800528 log_config_entry *config = (log_config_entry *)argv;
529
530 pthread_detach(pthread_self());
531
b.liu0f7ffad2024-11-06 19:57:27 +0800532// if (NULL == argv)
533// return NULL;
liubin281ac462023-07-19 14:22:54 +0800534
535 signal(SIGPIPE, SIG_IGN);
536 uloop_init();
537
xf.li4bd9ee42024-04-13 01:31:44 -0700538
539 syslog_pthread_create();
xf.li44e08692024-01-30 01:54:44 -0800540 //log_file = config->out_path;
541 memset(log_file, 0, sizeof(log_file));
542 memset(log_ip, 0, sizeof(log_ip));
543 memset(log_port, 0, sizeof(log_port));
544 memset(log_prefix, 0, sizeof(log_prefix));
545 memset(pid_file, 0, sizeof(pid_file));
546 memset(hostname, 0, sizeof(hostname));
547
548 if(config->out_path != NULL)
549 {
550 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
551 }
552
liubin281ac462023-07-19 14:22:54 +0800553 memset(&file_list, 0, sizeof(struct file_list_t));
554 file_list.total = config->rotate_file_count;
555 if(config->rotate_file_size)
556 log_size = config->rotate_file_size;
557 if(config->ip)
558 {
559 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
xf.li44e08692024-01-30 01:54:44 -0800560 //log_ip = config->ip;
561 strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
562 //log_port = config->port;
563 if(config->port != NULL)
564 {
565 strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
566 }
liubin281ac462023-07-19 14:22:54 +0800567 }
568 filter_log = config->filter_list;
569 // Follow log messages
570 log_follow = 1;
571 ctx = ubus_connect(ubus_socket);
572 if (!ctx) {
573 fprintf(stderr, "Failed to connect to ubus\n");
b.liu0f7ffad2024-11-06 19:57:27 +0800574 return NULL;
liubin281ac462023-07-19 14:22:54 +0800575 }
576 ubus_add_uloop(ctx);
577
578 printf("syslog log start...\n");
579 /* ugly ugly ugly ... we need a real reconnect logic */
580 do {
581 ret = ubus_lookup_id(ctx, "log", &id);
582 if (ret) {
583 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
584 sleep(1);
585 continue;
586 }
liubin281ac462023-07-19 14:22:54 +0800587 blob_buf_init(&b, 0);
588 if (lines)
589 blobmsg_add_u32(&b, "lines", lines);
590 else if (log_follow)
591 blobmsg_add_u32(&b, "lines", 0);
592 if (log_follow) {
xf.li44e08692024-01-30 01:54:44 -0800593 if (strlen(pid_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800594 FILE *fp = fopen(pid_file, "w+");
595 if (fp) {
596 fprintf(fp, "%d", getpid());
597 fclose(fp);
598 }
599 }
600 }
601
xf.li44e08692024-01-30 01:54:44 -0800602 if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
liubin281ac462023-07-19 14:22:54 +0800603 openlog("logread", LOG_PID, LOG_DAEMON);
604 log_type = LOG_NET;
605 sender.cb = log_handle_fd;
606 retry.cb = log_handle_reconnect;
607 uloop_timeout_set(&retry, 1000);
xf.li44e08692024-01-30 01:54:44 -0800608 } else if (strlen(log_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800609 log_type = LOG_FILE;
610 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
b.liu0f7ffad2024-11-06 19:57:27 +0800611
l.yangb7972c72024-10-15 22:34:51 -0700612 //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
b.liu0f7ffad2024-11-06 19:57:27 +0800613
l.yangb7972c72024-10-15 22:34:51 -0700614 //直接将log文件存储在不会掉电丢失的路径
615 snprintf(tmp_log,sizeof(tmp_log), "%s",log_file);
b.liu0f7ffad2024-11-06 19:57:27 +0800616
liubin281ac462023-07-19 14:22:54 +0800617 sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
618 if (sender.fd < 0) {
619 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
620 exit(-1);
621 }
622 } else {
623 sender.fd = STDOUT_FILENO;
624 }
liubin281ac462023-07-19 14:22:54 +0800625 ubus_invoke_async(ctx, id, "read", b.head, &req);
626 req.fd_cb = logread_fd_cb;
627 req.complete_cb = logread_complete_cb;
628 ubus_complete_request_async(ctx, &req);
b.liu9a306862024-03-06 16:49:40 +0800629
liubin281ac462023-07-19 14:22:54 +0800630 uloop_run();
631 ubus_free(ctx);
632 uloop_done();
633
634 } while (ret && tries--);
b.liu0f7ffad2024-11-06 19:57:27 +0800635
xf.li4bd9ee42024-04-13 01:31:44 -0700636 if (attr) {
637 if (pthread_join(attr, &tret) != 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800638 printf("MBTK:Join thread: %ld error!\n", attr);
xf.li4bd9ee42024-04-13 01:31:44 -0700639 exit(1);
640 }
641 }
liubin281ac462023-07-19 14:22:54 +0800642
643 pthread_exit(NULL);
644 return NULL;
645}