blob: 183bee829b3b99a327bb41c2dbf355f7edb8a31c [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
l.yang250444d2024-11-12 00:35:10 -080070extern int tmp_syslog_fd;
l.yangfa05af32024-11-27 00:00:56 -080071//extern char syslog_buff[MAX_BUFFER_SIZE];
72
73extern char *sys_globalPtr;
74extern int sys_len;
l.yangd2c71bc2024-12-16 19:24:24 -080075extern int emmc_pro;
liubin281ac462023-07-19 14:22:54 +080076
77static const struct blobmsg_policy log_policy[] = {
78 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
79 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
80 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
81 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
82 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
83};
84
85static struct uloop_timeout retry;
86static struct uloop_fd sender;
xf.li44e08692024-01-30 01:54:44 -080087//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
88static 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 +080089static int log_type = LOG_STDOUT;
90static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
91static struct file_list_t file_list;
92static struct filter_list_t *filter_log = NULL;
b.liu0f7ffad2024-11-06 19:57:27 +080093static char tmp_log[100] = {0};
b.liudeb8e422024-12-14 17:36:56 +080094pthread_t attr;
liubin281ac462023-07-19 14:22:54 +080095
96static const char* getcodetext(int value, CODE *codetable) {
97 CODE *i;
98
99 if (value >= 0)
100 for (i = codetable; i->c_val != -1; i++)
101 if (i->c_val == value)
102 return (i->c_name);
103 return "<unknown>";
104};
105
106static void log_handle_reconnect(struct uloop_timeout *timeout)
107{
108 sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
109 if (sender.fd < 0) {
110 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
111 uloop_timeout_set(&retry, 1000);
112 } else {
113 uloop_fd_add(&sender, ULOOP_READ);
114 syslog(0, "Logread connected to %s:%s\n", log_ip, log_port);
115 }
116}
117
118static void log_handle_fd(struct uloop_fd *u, unsigned int events)
119{
120 if (u->eof) {
121 uloop_fd_delete(u);
122 close(sender.fd);
123 sender.fd = -1;
124 uloop_timeout_set(&retry, 1000);
125 }
126}
127
128static int filter_char_to_pri(char c)
129{
130 switch (c) {
131 case 'v':
l.yang8ee686d2024-10-14 19:07:51 -0700132 return LOG_CRIT;
liubin281ac462023-07-19 14:22:54 +0800133 case 'd':
134 return LOG_DEBUG;
135 case 'i':
136 return LOG_INFO;
137 case 'w':
138 return LOG_WARNING;
139 case 'e':
140 return LOG_ERR;
141 case 'f':
142 return LOG_ALERT;
143 case '*':
144 default:
145 return 8;
146 }
147}
148
149static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
150{
151 struct filter_list_t *_filter = filter;
xf.li4bd9ee42024-04-13 01:31:44 -0700152 struct filter_list_t *_filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800153
154 while(_filter)
155 {
156 int p = filter_char_to_pri(_filter->priority);
xf.li43643772024-03-04 19:39:53 -0800157 int len = strlen(_filter->tag);
158 if(len > 0)
liubin281ac462023-07-19 14:22:54 +0800159 {
xf.li43643772024-03-04 19:39:53 -0800160 if(0 == memcmp(_filter->tag, tag, len))
161 {
162 if((pri < p) || (pri == p))
163 {
164 return 0;
165 }
166 else
167 {
168 return -1;
169 }
170 }
liubin281ac462023-07-19 14:22:54 +0800171 }else{ // have no tag
xf.li4bd9ee42024-04-13 01:31:44 -0700172 _filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800173 }
174 _filter = _filter->next;
175 }
xf.li4bd9ee42024-04-13 01:31:44 -0700176 //common tag
177 int p = filter_char_to_pri(_filter_common->priority);
178 if(pri > p)
179 return -1;
liubin281ac462023-07-19 14:22:54 +0800180
xf.li4bd9ee42024-04-13 01:31:44 -0700181 return 0;
liubin281ac462023-07-19 14:22:54 +0800182}
183static int log_notify(struct blob_attr *msg)
184{
xf.li01e39822024-10-30 16:00:32 +0800185 int index = 0;
186 int len = 0;
liubin281ac462023-07-19 14:22:54 +0800187 struct blob_attr *tb[__LOG_MAX];
188 struct stat s;
b.liu0f7ffad2024-11-06 19:57:27 +0800189 char buf[1024 * 2] = {'\0'};
190 char tmp_buf[1024] = {0};
liubin281ac462023-07-19 14:22:54 +0800191 uint32_t p;
192 char *str;
193 time_t t;
194 char *c, *m;
195
l.yang67782e62024-11-05 01:28:10 -0800196 static char buffer[MAX_BUFFER_SIZE] = {0};
197 static int buffer_index = 0;
l.yangfa05af32024-11-27 00:00:56 -0800198
199 sys_globalPtr = buffer;
200
l.yangb7972c72024-10-15 22:34:51 -0700201 //
202 //sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
203
b.liu0f7ffad2024-11-06 19:57:27 +0800204
l.yangb7972c72024-10-15 22:34:51 -0700205 snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
l.yang250444d2024-11-12 00:35:10 -0800206
207 if(fcntl(sender.fd, F_GETFL) == -1 || access(tmp_buf, W_OK) != 0)
xf.li127b6fa2024-04-28 01:48:45 -0700208 {
209 sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
l.yang67782e62024-11-05 01:28:10 -0800210 if(sender.fd < 0)
211 {
212 perror("Failed to open file ");
213 return -1;
214 }
l.yang250444d2024-11-12 00:35:10 -0800215 tmp_syslog_fd = sender.fd;
xf.li127b6fa2024-04-28 01:48:45 -0700216 }
b.liu0f7ffad2024-11-06 19:57:27 +0800217
218
219
liubin281ac462023-07-19 14:22:54 +0800220 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
221 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
222 return 1;
223
l.yang250444d2024-11-12 00:35:10 -0800224 if ((log_type == LOG_FILE) && log_size && (!stat(log_file, &s)) && (s.st_size > log_size))
l.yang67782e62024-11-05 01:28:10 -0800225 {
226 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
b.liu0f7ffad2024-11-06 19:57:27 +0800227 if (sender.fd < 0)
l.yang67782e62024-11-05 01:28:10 -0800228 {
l.yang250444d2024-11-12 00:35:10 -0800229 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
l.yang67782e62024-11-05 01:28:10 -0800230 exit(-1);
231 }
l.yang250444d2024-11-12 00:35:10 -0800232 tmp_syslog_fd = sender.fd;
l.yang67782e62024-11-05 01:28:10 -0800233 }
liubin281ac462023-07-19 14:22:54 +0800234
235 m = blobmsg_get_string(tb[LOG_MSG]);
236 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
237 c = ctime(&t);
238 p = blobmsg_get_u32(tb[LOG_PRIO]);
239 c[strlen(c) - 1] = '\0';
240 str = blobmsg_format_json(msg, true);
241
242 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
243 {
244 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800245 return 0;
246 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800247 }
b.liu0f7ffad2024-11-06 19:57:27 +0800248 if (log_type == LOG_NET)
l.yang67782e62024-11-05 01:28:10 -0800249 {
liubin281ac462023-07-19 14:22:54 +0800250 int err;
251
252 snprintf(buf, sizeof(buf), "<%u>", p);
253 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800254 if (strlen(hostname) > 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800255 strncat(buf, hostname, strlen(hostname));
256 strncat(buf, " ", 1);
liubin281ac462023-07-19 14:22:54 +0800257 }
xf.li44e08692024-01-30 01:54:44 -0800258 if (strlen(log_prefix) > 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800259 strncat(buf, log_prefix, strlen(log_prefix));
260 strncat(buf, ": ", 2);
liubin281ac462023-07-19 14:22:54 +0800261 }
262 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
b.liu0f7ffad2024-11-06 19:57:27 +0800263 strncat(buf, "kernel: ", strlen("kernel: "));
264 strncat(buf, m, strlen(m));
liubin281ac462023-07-19 14:22:54 +0800265 if (log_udp)
266 err = write(sender.fd, buf, strlen(buf));
267 else
268 err = send(sender.fd, buf, strlen(buf), 0);
269
270 if (err < 0) {
271 syslog(0, "failed to send log data to %s:%s via %s\n",
272 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
273 uloop_fd_delete(&sender);
274 close(sender.fd);
275 sender.fd = -1;
276 uloop_timeout_set(&retry, 1000);
277 }
b.liu0f7ffad2024-11-06 19:57:27 +0800278 }
279 else
l.yang67782e62024-11-05 01:28:10 -0800280 {
liubin281ac462023-07-19 14:22:54 +0800281 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
282 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
283 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
xf.li01e39822024-10-30 16:00:32 +0800284
l.yang7ec80982024-12-15 23:34:35 -0800285 if(access("/etc/syslog_close", F_OK) == 0)
286 {
287 //not kernel log
288 if(strstr(buf,"kernel") == NULL)
289 {
290 return 0;
291 }
292
293 //is kernel log but close
294 if(strstr(buf,"kernel") != NULL && access("/etc/kernel_log_close", F_OK) == 0)
295 {
296 return 0;
297 }
298
299
300 }
301 else
302 {
303 //is kernel log but close
304 if(strstr(buf,"kernel") != NULL && access("/etc/kernel_log_close", F_OK) == 0)
305 {
306 return 0;
307 }
308 }
309
310 len = strlen(buf);
311 if(access("/etc/syslog_encrypt_flag", F_OK) == 0)
312 {
313 for(index = 0; index < len; index++)
314 {
315 buf[index] ^= 1;
316 }
317 }
318
l.yangd2c71bc2024-12-16 19:24:24 -0800319 if(emmc_pro)
l.yang67782e62024-11-05 01:28:10 -0800320 {
l.yangd2c71bc2024-12-16 19:24:24 -0800321 if(buffer_index + len >= SYSLOG_BUFF_SIZE && buffer_index < SYSLOG_BUFF_SIZE)
322 {
323 memcpy(buffer + buffer_index, buf, len);
324 buffer_index += len;
325 if (write(sender.fd, buffer, buffer_index) < 0)
326 {
327 perror("write error");
328 close(sender.fd);
329 return -1;
330 }
331 buffer_index = 0;
332 }
333 else
334 {
335 memcpy(buffer + buffer_index, buf, len);
336 buffer_index += len;
337 sys_len = buffer_index;
338 //memcpy(syslog_buff,buffer,buffer_index);
339 }
340 }
341 else
342 {
343
344 if(write(sender.fd, buf, len) < 0)
l.yang250444d2024-11-12 00:35:10 -0800345 {
346 perror("write error");
347 close(sender.fd);
348 return -1;
349 }
l.yang67782e62024-11-05 01:28:10 -0800350 }
b.liu0f7ffad2024-11-06 19:57:27 +0800351
352
liubin281ac462023-07-19 14:22:54 +0800353 }
354
355 free(str);
l.yang67782e62024-11-05 01:28:10 -0800356 //if (log_type == LOG_FILE)
357 //fsync(sender.fd);
liubin281ac462023-07-19 14:22:54 +0800358
359 return 0;
360}
361
362static void logread_fd_data_cb(struct ustream *s, int bytes)
363{
364 while (true) {
365 int len;
366 struct blob_attr *a;
367
368 a = (void*) ustream_get_read_buf(s, &len);
369 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
370 break;
371 log_notify(a);
372 ustream_consume(s, blob_len(a) + sizeof(*a));
373 }
374 if (!log_follow)
375 uloop_end();
376}
377
378static void logread_fd_cb(struct ubus_request *req, int fd)
379{
380 static struct ustream_fd test_fd;
381
382 test_fd.stream.notify_read = logread_fd_data_cb;
383 ustream_fd_init(&test_fd, fd);
384}
385
386static void logread_complete_cb(struct ubus_request *req, int ret)
387{
388}
389
xf.li4bd9ee42024-04-13 01:31:44 -0700390int lynq_update_log_level()
391{
392 json_object* jsonobj = NULL;
393 json_object* tmpjson = NULL;
394 json_object* datajson = NULL;
395 json_object* listjson = NULL;
396 json_object* fileterjson = NULL;
397 json_object* fileter_listjson = NULL;
398 struct filter_list_t* filter_list_head = NULL;
399 struct filter_list_t* tmp_filter_list = NULL;
400 struct filter_list_t* _filter_list = NULL;
401
402 int n;
403 int array_length;
b.liu0f7ffad2024-11-06 19:57:27 +0800404// char* tmp_string = NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700405
406 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
407 if (NULL == jsonobj) {
408 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
409 return -1;
410 }
411 /***获取data***/
412 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
413 datajson = json_object_array_get_idx(tmpjson, 0);//syslog index is 0
414 if (NULL == datajson) {
415 json_object_put(jsonobj);
416 return -1;
417 }
418 json_object_object_get_ex(datajson, "filter_list", &listjson);
419 if (NULL == listjson) {
420 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
421 json_object_put(listjson);
422 return -1;
423 }
424 filter_list_head = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
425 _filter_list = filter_list_head;
426
427 array_length = json_object_array_length(listjson);
428 for (n = 0 ; n < array_length; n++) {
429 fileterjson = json_object_array_get_idx(listjson, n);
430 if (NULL == fileterjson) {
431 printf("the fileterjson exit\n");
432 free(tmp_filter_list->next);
433 tmp_filter_list->next = NULL;
434 break;
435 }
436 memset(_filter_list, 0, sizeof(struct filter_list_t));
437 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800438 const char* str = json_object_get_string(fileter_listjson);
xf.li4bd9ee42024-04-13 01:31:44 -0700439 if (str) {
440 _filter_list->priority = str[0];
441 printf("fileter_listjson: %c\n", _filter_list->priority);
442 }
443
444 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800445
xf.li4bd9ee42024-04-13 01:31:44 -0700446 str = json_object_get_string(fileter_listjson);
447 if (str) {
448 _filter_list->tag = strdup(str);
449 printf("fileter_listjson: %s\n", _filter_list->tag);
450 }
451 else
452 {
453 _filter_list->tag = "\0";
454 }
455 //json_object_put(fileter_listjson);
456 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
457 if (NULL == _filter_list->next) {
458 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
459 break;
460 }
461 tmp_filter_list = _filter_list;
462 _filter_list = _filter_list->next;
463 _filter_list->next = NULL;
464 }
465 /***释放json对象***/
466 json_object_put(jsonobj);
467
468 tmp_filter_list = filter_log;
469 filter_log = filter_list_head;
470
471 while(tmp_filter_list != NULL) {
472 _filter_list = tmp_filter_list;
b.liudeb8e422024-12-14 17:36:56 +0800473
474 struct filter_list_t* list_ptr = tmp_filter_list;
xf.li4bd9ee42024-04-13 01:31:44 -0700475 tmp_filter_list = _filter_list->next;
b.liudeb8e422024-12-14 17:36:56 +0800476 free(list_ptr);
xf.li4bd9ee42024-04-13 01:31:44 -0700477 }
b.liu0f7ffad2024-11-06 19:57:27 +0800478
xf.li4bd9ee42024-04-13 01:31:44 -0700479 return 0;
480}
481
482
b.liu0f7ffad2024-11-06 19:57:27 +0800483void* wait_update_log_level(void *arg)
xf.li4bd9ee42024-04-13 01:31:44 -0700484{
b.liu0f7ffad2024-11-06 19:57:27 +0800485// int i = 0;
xf.li4bd9ee42024-04-13 01:31:44 -0700486 char recvBuff[100];
b.liu0f7ffad2024-11-06 19:57:27 +0800487 int serverFd,clientFd;
488 socklen_t addrLen;
xf.li4bd9ee42024-04-13 01:31:44 -0700489 struct sockaddr_un serverAddr,clientAddr;
490
491 pthread_detach(pthread_self());
492 printf("MBTK: in wait_update_log_level\n");
493
494 memset(&serverAddr,0,sizeof(serverAddr));
495 serverAddr.sun_family = AF_UNIX;
496 sprintf(serverAddr.sun_path,"%s","/var/log_server.socket");
497
498 unlink("/var/log_server.socket"); /* in case it already exists */
499
500 if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
501 {
502 printf("err -1\n");
b.liu0f7ffad2024-11-06 19:57:27 +0800503 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700504 }
505
506 if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
507 {
508 printf("err -2\n");
509 close(serverFd);
b.liu0f7ffad2024-11-06 19:57:27 +0800510 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700511 }
512
513 if(listen(serverFd,10) < 0)
514 {
515 printf("err -3\n");
b.liu0f7ffad2024-11-06 19:57:27 +0800516 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700517 }
518
519 while(1)
520 {
521 addrLen = sizeof(clientAddr);
522 memset(&clientAddr,0,sizeof(clientAddr));
523 memset(&recvBuff,0,100);
524
525 if((clientFd = accept(serverFd,(struct sockaddr*)&clientAddr,&addrLen)) < 0)
526 {
527 printf("err -4\n");
528 continue;
529 }
530 printf("MBTK: wait recv\n");
531 if(recv(clientFd,recvBuff,100,0) < 0)
532 {
533 printf("err -5");
534 close(clientFd);
535 continue;
536 }
537 if(strncmp(recvBuff, "update", strlen("update")) == 0)
538 {
539 lynq_update_log_level();
540 }
541
542 close(clientFd);
543 }
544 close(serverFd);
545
b.liu0f7ffad2024-11-06 19:57:27 +0800546 return NULL;
xf.li4bd9ee42024-04-13 01:31:44 -0700547}
548
549int syslog_pthread_create()
550{
551 int ret;
b.liu0f7ffad2024-11-06 19:57:27 +0800552
xf.li4bd9ee42024-04-13 01:31:44 -0700553 ret = pthread_create(&attr, NULL, wait_update_log_level, NULL);
554
555 if (ret < 0)
556 {
557 printf("MBTK:pthread create fail");
558 return -1;
559 }
560
561 return -1;
562}
563
liubin281ac462023-07-19 14:22:54 +0800564void* syslog_main(void* argv)
565{
566 static struct ubus_request req;
567 struct ubus_context *ctx;
568 uint32_t id;
569 const char *ubus_socket = NULL;
b.liu0f7ffad2024-11-06 19:57:27 +0800570 int ret, lines = 0;
liubin281ac462023-07-19 14:22:54 +0800571 static struct blob_buf b;
xf.li44e08692024-01-30 01:54:44 -0800572 int tries = 60;
xf.li50b8baf2024-04-14 19:51:18 -0700573 void* tret;
574
liubin281ac462023-07-19 14:22:54 +0800575 log_config_entry *config = (log_config_entry *)argv;
576
577 pthread_detach(pthread_self());
578
b.liu0f7ffad2024-11-06 19:57:27 +0800579// if (NULL == argv)
580// return NULL;
liubin281ac462023-07-19 14:22:54 +0800581
582 signal(SIGPIPE, SIG_IGN);
583 uloop_init();
584
xf.li4bd9ee42024-04-13 01:31:44 -0700585
586 syslog_pthread_create();
xf.li44e08692024-01-30 01:54:44 -0800587 //log_file = config->out_path;
588 memset(log_file, 0, sizeof(log_file));
589 memset(log_ip, 0, sizeof(log_ip));
590 memset(log_port, 0, sizeof(log_port));
591 memset(log_prefix, 0, sizeof(log_prefix));
592 memset(pid_file, 0, sizeof(pid_file));
593 memset(hostname, 0, sizeof(hostname));
594
595 if(config->out_path != NULL)
596 {
597 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
598 }
599
liubin281ac462023-07-19 14:22:54 +0800600 memset(&file_list, 0, sizeof(struct file_list_t));
601 file_list.total = config->rotate_file_count;
602 if(config->rotate_file_size)
603 log_size = config->rotate_file_size;
604 if(config->ip)
605 {
606 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
xf.li44e08692024-01-30 01:54:44 -0800607 //log_ip = config->ip;
608 strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
609 //log_port = config->port;
610 if(config->port != NULL)
611 {
612 strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
613 }
liubin281ac462023-07-19 14:22:54 +0800614 }
615 filter_log = config->filter_list;
616 // Follow log messages
617 log_follow = 1;
618 ctx = ubus_connect(ubus_socket);
619 if (!ctx) {
620 fprintf(stderr, "Failed to connect to ubus\n");
b.liu0f7ffad2024-11-06 19:57:27 +0800621 return NULL;
liubin281ac462023-07-19 14:22:54 +0800622 }
623 ubus_add_uloop(ctx);
624
625 printf("syslog log start...\n");
626 /* ugly ugly ugly ... we need a real reconnect logic */
627 do {
628 ret = ubus_lookup_id(ctx, "log", &id);
629 if (ret) {
630 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
631 sleep(1);
632 continue;
633 }
liubin281ac462023-07-19 14:22:54 +0800634 blob_buf_init(&b, 0);
635 if (lines)
636 blobmsg_add_u32(&b, "lines", lines);
637 else if (log_follow)
638 blobmsg_add_u32(&b, "lines", 0);
639 if (log_follow) {
xf.li44e08692024-01-30 01:54:44 -0800640 if (strlen(pid_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800641 FILE *fp = fopen(pid_file, "w+");
642 if (fp) {
643 fprintf(fp, "%d", getpid());
644 fclose(fp);
645 }
646 }
647 }
648
xf.li44e08692024-01-30 01:54:44 -0800649 if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
liubin281ac462023-07-19 14:22:54 +0800650 openlog("logread", LOG_PID, LOG_DAEMON);
651 log_type = LOG_NET;
652 sender.cb = log_handle_fd;
653 retry.cb = log_handle_reconnect;
654 uloop_timeout_set(&retry, 1000);
xf.li44e08692024-01-30 01:54:44 -0800655 } else if (strlen(log_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800656 log_type = LOG_FILE;
657 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
b.liu0f7ffad2024-11-06 19:57:27 +0800658
l.yangb7972c72024-10-15 22:34:51 -0700659 //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
b.liu0f7ffad2024-11-06 19:57:27 +0800660
l.yangb7972c72024-10-15 22:34:51 -0700661 //直接将log文件存储在不会掉电丢失的路径
662 snprintf(tmp_log,sizeof(tmp_log), "%s",log_file);
b.liu0f7ffad2024-11-06 19:57:27 +0800663
liubin281ac462023-07-19 14:22:54 +0800664 sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
665 if (sender.fd < 0) {
666 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
667 exit(-1);
668 }
l.yang250444d2024-11-12 00:35:10 -0800669 tmp_syslog_fd = sender.fd;
liubin281ac462023-07-19 14:22:54 +0800670 } else {
671 sender.fd = STDOUT_FILENO;
672 }
liubin281ac462023-07-19 14:22:54 +0800673 ubus_invoke_async(ctx, id, "read", b.head, &req);
674 req.fd_cb = logread_fd_cb;
675 req.complete_cb = logread_complete_cb;
676 ubus_complete_request_async(ctx, &req);
b.liu9a306862024-03-06 16:49:40 +0800677
liubin281ac462023-07-19 14:22:54 +0800678 uloop_run();
679 ubus_free(ctx);
680 uloop_done();
681
682 } while (ret && tries--);
b.liu0f7ffad2024-11-06 19:57:27 +0800683
xf.li4bd9ee42024-04-13 01:31:44 -0700684 if (attr) {
685 if (pthread_join(attr, &tret) != 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800686 printf("MBTK:Join thread: %ld error!\n", attr);
xf.li4bd9ee42024-04-13 01:31:44 -0700687 exit(1);
688 }
689 }
liubin281ac462023-07-19 14:22:54 +0800690
691 pthread_exit(NULL);
692 return NULL;
693}