blob: 36df306f40f1b87c99e05d23f632ac2841ec2e27 [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;
71extern char syslog_buff[MAX_BUFFER_SIZE];
liubin281ac462023-07-19 14:22:54 +080072
73static const struct blobmsg_policy log_policy[] = {
74 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
75 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
76 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
77 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
78 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
79};
80
81static struct uloop_timeout retry;
82static struct uloop_fd sender;
xf.li44e08692024-01-30 01:54:44 -080083//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
84static 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 +080085static int log_type = LOG_STDOUT;
86static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
87static struct file_list_t file_list;
88static struct filter_list_t *filter_log = NULL;
b.liu0f7ffad2024-11-06 19:57:27 +080089static char tmp_log[100] = {0};
xf.li4bd9ee42024-04-13 01:31:44 -070090pthread_t attr = -1;
liubin281ac462023-07-19 14:22:54 +080091
92static const char* getcodetext(int value, CODE *codetable) {
93 CODE *i;
94
95 if (value >= 0)
96 for (i = codetable; i->c_val != -1; i++)
97 if (i->c_val == value)
98 return (i->c_name);
99 return "<unknown>";
100};
101
102static void log_handle_reconnect(struct uloop_timeout *timeout)
103{
104 sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
105 if (sender.fd < 0) {
106 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
107 uloop_timeout_set(&retry, 1000);
108 } else {
109 uloop_fd_add(&sender, ULOOP_READ);
110 syslog(0, "Logread connected to %s:%s\n", log_ip, log_port);
111 }
112}
113
114static void log_handle_fd(struct uloop_fd *u, unsigned int events)
115{
116 if (u->eof) {
117 uloop_fd_delete(u);
118 close(sender.fd);
119 sender.fd = -1;
120 uloop_timeout_set(&retry, 1000);
121 }
122}
123
124static int filter_char_to_pri(char c)
125{
126 switch (c) {
127 case 'v':
l.yang8ee686d2024-10-14 19:07:51 -0700128 return LOG_CRIT;
liubin281ac462023-07-19 14:22:54 +0800129 case 'd':
130 return LOG_DEBUG;
131 case 'i':
132 return LOG_INFO;
133 case 'w':
134 return LOG_WARNING;
135 case 'e':
136 return LOG_ERR;
137 case 'f':
138 return LOG_ALERT;
139 case '*':
140 default:
141 return 8;
142 }
143}
144
145static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
146{
147 struct filter_list_t *_filter = filter;
xf.li4bd9ee42024-04-13 01:31:44 -0700148 struct filter_list_t *_filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800149
150 while(_filter)
151 {
152 int p = filter_char_to_pri(_filter->priority);
xf.li43643772024-03-04 19:39:53 -0800153 int len = strlen(_filter->tag);
154 if(len > 0)
liubin281ac462023-07-19 14:22:54 +0800155 {
xf.li43643772024-03-04 19:39:53 -0800156 if(0 == memcmp(_filter->tag, tag, len))
157 {
158 if((pri < p) || (pri == p))
159 {
160 return 0;
161 }
162 else
163 {
164 return -1;
165 }
166 }
liubin281ac462023-07-19 14:22:54 +0800167 }else{ // have no tag
xf.li4bd9ee42024-04-13 01:31:44 -0700168 _filter_common = _filter;
liubin281ac462023-07-19 14:22:54 +0800169 }
170 _filter = _filter->next;
171 }
xf.li4bd9ee42024-04-13 01:31:44 -0700172 //common tag
173 int p = filter_char_to_pri(_filter_common->priority);
174 if(pri > p)
175 return -1;
liubin281ac462023-07-19 14:22:54 +0800176
xf.li4bd9ee42024-04-13 01:31:44 -0700177 return 0;
liubin281ac462023-07-19 14:22:54 +0800178}
179static int log_notify(struct blob_attr *msg)
180{
xf.li01e39822024-10-30 16:00:32 +0800181 int index = 0;
182 int len = 0;
liubin281ac462023-07-19 14:22:54 +0800183 struct blob_attr *tb[__LOG_MAX];
184 struct stat s;
b.liu0f7ffad2024-11-06 19:57:27 +0800185 char buf[1024 * 2] = {'\0'};
186 char tmp_buf[1024] = {0};
liubin281ac462023-07-19 14:22:54 +0800187 uint32_t p;
188 char *str;
189 time_t t;
190 char *c, *m;
191
l.yang67782e62024-11-05 01:28:10 -0800192 static char buffer[MAX_BUFFER_SIZE] = {0};
193 static int buffer_index = 0;
b.liu0f7ffad2024-11-06 19:57:27 +0800194
l.yangb7972c72024-10-15 22:34:51 -0700195 //
196 //sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
197
b.liu0f7ffad2024-11-06 19:57:27 +0800198
l.yangb7972c72024-10-15 22:34:51 -0700199 snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
l.yang250444d2024-11-12 00:35:10 -0800200
201 if(fcntl(sender.fd, F_GETFL) == -1 || access(tmp_buf, W_OK) != 0)
xf.li127b6fa2024-04-28 01:48:45 -0700202 {
203 sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600);
l.yang67782e62024-11-05 01:28:10 -0800204 if(sender.fd < 0)
205 {
206 perror("Failed to open file ");
207 return -1;
208 }
l.yang250444d2024-11-12 00:35:10 -0800209 tmp_syslog_fd = sender.fd;
xf.li127b6fa2024-04-28 01:48:45 -0700210 }
b.liu0f7ffad2024-11-06 19:57:27 +0800211
212
213
liubin281ac462023-07-19 14:22:54 +0800214 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
215 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
216 return 1;
217
l.yang250444d2024-11-12 00:35:10 -0800218 if ((log_type == LOG_FILE) && log_size && (!stat(log_file, &s)) && (s.st_size > log_size))
l.yang67782e62024-11-05 01:28:10 -0800219 {
220 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
b.liu0f7ffad2024-11-06 19:57:27 +0800221 if (sender.fd < 0)
l.yang67782e62024-11-05 01:28:10 -0800222 {
l.yang250444d2024-11-12 00:35:10 -0800223 fprintf(stderr, "failed to open %s: %s\n", log_file, strerror(errno));
l.yang67782e62024-11-05 01:28:10 -0800224 exit(-1);
225 }
l.yang250444d2024-11-12 00:35:10 -0800226 tmp_syslog_fd = sender.fd;
l.yang67782e62024-11-05 01:28:10 -0800227 }
liubin281ac462023-07-19 14:22:54 +0800228
229 m = blobmsg_get_string(tb[LOG_MSG]);
230 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
231 c = ctime(&t);
232 p = blobmsg_get_u32(tb[LOG_PRIO]);
233 c[strlen(c) - 1] = '\0';
234 str = blobmsg_format_json(msg, true);
235
236 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
237 {
238 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800239 return 0;
240 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800241 }
b.liu0f7ffad2024-11-06 19:57:27 +0800242 if (log_type == LOG_NET)
l.yang67782e62024-11-05 01:28:10 -0800243 {
liubin281ac462023-07-19 14:22:54 +0800244 int err;
245
246 snprintf(buf, sizeof(buf), "<%u>", p);
247 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800248 if (strlen(hostname) > 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800249 strncat(buf, hostname, strlen(hostname));
250 strncat(buf, " ", 1);
liubin281ac462023-07-19 14:22:54 +0800251 }
xf.li44e08692024-01-30 01:54:44 -0800252 if (strlen(log_prefix) > 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800253 strncat(buf, log_prefix, strlen(log_prefix));
254 strncat(buf, ": ", 2);
liubin281ac462023-07-19 14:22:54 +0800255 }
256 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
b.liu0f7ffad2024-11-06 19:57:27 +0800257 strncat(buf, "kernel: ", strlen("kernel: "));
258 strncat(buf, m, strlen(m));
liubin281ac462023-07-19 14:22:54 +0800259 if (log_udp)
260 err = write(sender.fd, buf, strlen(buf));
261 else
262 err = send(sender.fd, buf, strlen(buf), 0);
263
264 if (err < 0) {
265 syslog(0, "failed to send log data to %s:%s via %s\n",
266 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
267 uloop_fd_delete(&sender);
268 close(sender.fd);
269 sender.fd = -1;
270 uloop_timeout_set(&retry, 1000);
271 }
b.liu0f7ffad2024-11-06 19:57:27 +0800272 }
273 else
l.yang67782e62024-11-05 01:28:10 -0800274 {
liubin281ac462023-07-19 14:22:54 +0800275 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
276 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
277 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
xf.li01e39822024-10-30 16:00:32 +0800278
279 len = strlen(buf);
280 if(access("/etc/syslog_encrypt_flag", F_OK) == 0)
281 {
282 for(index = 0; index < len; index++)
283 {
284 buf[index] ^= 1;
285 }
286 }
l.yang250444d2024-11-12 00:35:10 -0800287
288 if(buffer_index + len >= SYSLOG_BUFF_SIZE && buffer_index < SYSLOG_BUFF_SIZE)
l.yang67782e62024-11-05 01:28:10 -0800289 {
l.yang67782e62024-11-05 01:28:10 -0800290 memcpy(buffer + buffer_index, buf, len);
291 buffer_index += len;
l.yang250444d2024-11-12 00:35:10 -0800292 if (write(sender.fd, buffer, buffer_index) < 0)
293 {
294 perror("write error");
295 close(sender.fd);
296 return -1;
297 }
298 buffer_index = 0;
l.yang67782e62024-11-05 01:28:10 -0800299 }
300 else
301 {
l.yang250444d2024-11-12 00:35:10 -0800302 memcpy(buffer + buffer_index, buf, len);
303 buffer_index += len;
304 memcpy(syslog_buff,buffer,buffer_index);
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 }
l.yang250444d2024-11-12 00:35:10 -0800622 tmp_syslog_fd = sender.fd;
liubin281ac462023-07-19 14:22:54 +0800623 } else {
624 sender.fd = STDOUT_FILENO;
625 }
liubin281ac462023-07-19 14:22:54 +0800626 ubus_invoke_async(ctx, id, "read", b.head, &req);
627 req.fd_cb = logread_fd_cb;
628 req.complete_cb = logread_complete_cb;
629 ubus_complete_request_async(ctx, &req);
b.liu9a306862024-03-06 16:49:40 +0800630
liubin281ac462023-07-19 14:22:54 +0800631 uloop_run();
632 ubus_free(ctx);
633 uloop_done();
634
635 } while (ret && tries--);
b.liu0f7ffad2024-11-06 19:57:27 +0800636
xf.li4bd9ee42024-04-13 01:31:44 -0700637 if (attr) {
638 if (pthread_join(attr, &tret) != 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800639 printf("MBTK:Join thread: %ld error!\n", attr);
xf.li4bd9ee42024-04-13 01:31:44 -0700640 exit(1);
641 }
642 }
liubin281ac462023-07-19 14:22:54 +0800643
644 pthread_exit(NULL);
645 return NULL;
646}