blob: 1d6da4bbdc542f157fc290c907495efba8a9d4e9 [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>
23#include <sys/socket.h>
24
25#define SYSLOG_NAMES
26#include <syslog.h>
27
28#include <libubox/ustream.h>
29#include <libubox/blobmsg_json.h>
30#include <libubox/usock.h>
31#include <libubox/uloop.h>
32#include "libubus.h"
b.liu63a4a322024-03-07 19:00:53 +080033#include "syslog.h"
liubin281ac462023-07-19 14:22:54 +080034#include "log_config.h"
b.liu9a306862024-03-06 16:49:40 +080035//#include "lynq/liblog.h"
liubin281ac462023-07-19 14:22:54 +080036
b.liu63a4a322024-03-07 19:00:53 +080037enum {
38 SOURCE_KLOG = 0,
39 SOURCE_SYSLOG = 1,
40 SOURCE_INTERNAL = 2,
41 SOURCE_ANY = 0xff,
42};
43
xf.li44e08692024-01-30 01:54:44 -080044#define LOG_CONFIG_LEN 50
liubin281ac462023-07-19 14:22:54 +080045enum {
46 LOG_STDOUT,
47 LOG_FILE,
48 LOG_NET,
49};
50
51enum {
52 LOG_MSG,
53 LOG_ID,
54 LOG_PRIO,
55 LOG_SOURCE,
56 LOG_TIME,
57 __LOG_MAX
58};
59
60static const struct blobmsg_policy log_policy[] = {
61 [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING },
62 [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 },
63 [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
64 [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 },
65 [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 },
66};
67
68static struct uloop_timeout retry;
69static struct uloop_fd sender;
xf.li44e08692024-01-30 01:54:44 -080070//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
71static 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 +080072static int log_type = LOG_STDOUT;
73static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
74static struct file_list_t file_list;
75static struct filter_list_t *filter_log = NULL;
76static char tmp_log[48] = {0};
77
78static const char* getcodetext(int value, CODE *codetable) {
79 CODE *i;
80
81 if (value >= 0)
82 for (i = codetable; i->c_val != -1; i++)
83 if (i->c_val == value)
84 return (i->c_name);
85 return "<unknown>";
86};
87
88static void log_handle_reconnect(struct uloop_timeout *timeout)
89{
90 sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port);
91 if (sender.fd < 0) {
92 fprintf(stderr, "failed to connect: %s\n", strerror(errno));
93 uloop_timeout_set(&retry, 1000);
94 } else {
95 uloop_fd_add(&sender, ULOOP_READ);
96 syslog(0, "Logread connected to %s:%s\n", log_ip, log_port);
97 }
98}
99
100static void log_handle_fd(struct uloop_fd *u, unsigned int events)
101{
102 if (u->eof) {
103 uloop_fd_delete(u);
104 close(sender.fd);
105 sender.fd = -1;
106 uloop_timeout_set(&retry, 1000);
107 }
108}
109
110static int filter_char_to_pri(char c)
111{
112 switch (c) {
113 case 'v':
114 return 8;
115 case 'd':
116 return LOG_DEBUG;
117 case 'i':
118 return LOG_INFO;
119 case 'w':
120 return LOG_WARNING;
121 case 'e':
122 return LOG_ERR;
123 case 'f':
124 return LOG_ALERT;
125 case '*':
126 default:
127 return 8;
128 }
129}
130
131static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter)
132{
133 struct filter_list_t *_filter = filter;
134
135 while(_filter)
136 {
137 int p = filter_char_to_pri(_filter->priority);
xf.li43643772024-03-04 19:39:53 -0800138 int len = strlen(_filter->tag);
139 if(len > 0)
liubin281ac462023-07-19 14:22:54 +0800140 {
xf.li43643772024-03-04 19:39:53 -0800141 if(0 == memcmp(_filter->tag, tag, len))
142 {
143 if((pri < p) || (pri == p))
144 {
145 return 0;
146 }
147 else
148 {
149 return -1;
150 }
151 }
liubin281ac462023-07-19 14:22:54 +0800152 }else{ // have no tag
153 if(pri > p)
154 return -1;
155 else
156 return 0;
157 }
158 _filter = _filter->next;
159 }
160
161 return -1;
162}
163static int log_notify(struct blob_attr *msg)
164{
165 struct blob_attr *tb[__LOG_MAX];
166 struct stat s;
xf.li43643772024-03-04 19:39:53 -0800167 char buf[512] = {'\0'};
liubin281ac462023-07-19 14:22:54 +0800168 uint32_t p;
169 char *str;
170 time_t t;
171 char *c, *m;
172
173 if (sender.fd < 0)
174 return 0;
175
176 blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
177 if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
178 return 1;
179
180 if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
181 sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
182 if (sender.fd < 0) {
183 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
184 exit(-1);
185 }
186 }
187
188 m = blobmsg_get_string(tb[LOG_MSG]);
189 t = blobmsg_get_u64(tb[LOG_TIME]) / 1000;
190 c = ctime(&t);
191 p = blobmsg_get_u32(tb[LOG_PRIO]);
192 c[strlen(c) - 1] = '\0';
193 str = blobmsg_format_json(msg, true);
194
195 if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log))
196 {
197 // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m);
xf.li43643772024-03-04 19:39:53 -0800198 return 0;
199 //exit(-1);
liubin281ac462023-07-19 14:22:54 +0800200 }
201 if (log_type == LOG_NET) {
202 int err;
203
204 snprintf(buf, sizeof(buf), "<%u>", p);
205 strncat(buf, c + 4, 16);
xf.li44e08692024-01-30 01:54:44 -0800206 if (strlen(hostname) > 0) {
liubin281ac462023-07-19 14:22:54 +0800207 strncat(buf, hostname, sizeof(buf));
208 strncat(buf, " ", sizeof(buf));
209 }
xf.li44e08692024-01-30 01:54:44 -0800210 if (strlen(log_prefix) > 0) {
liubin281ac462023-07-19 14:22:54 +0800211 strncat(buf, log_prefix, sizeof(buf));
212 strncat(buf, ": ", sizeof(buf));
213 }
214 if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
215 strncat(buf, "kernel: ", sizeof(buf));
216 strncat(buf, m, sizeof(buf));
217 if (log_udp)
218 err = write(sender.fd, buf, strlen(buf));
219 else
220 err = send(sender.fd, buf, strlen(buf), 0);
221
222 if (err < 0) {
223 syslog(0, "failed to send log data to %s:%s via %s\n",
224 log_ip, log_port, (log_udp) ? ("udp") : ("tcp"));
225 uloop_fd_delete(&sender);
226 close(sender.fd);
227 sender.fd = -1;
228 uloop_timeout_set(&retry, 1000);
229 }
230 } else {
231 snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
232 c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
233 (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m);
234 write(sender.fd, buf, strlen(buf));
235 }
236
237 free(str);
238 if (log_type == LOG_FILE)
239 fsync(sender.fd);
240
241 return 0;
242}
243
244static void logread_fd_data_cb(struct ustream *s, int bytes)
245{
246 while (true) {
247 int len;
248 struct blob_attr *a;
249
250 a = (void*) ustream_get_read_buf(s, &len);
251 if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a))
252 break;
253 log_notify(a);
254 ustream_consume(s, blob_len(a) + sizeof(*a));
255 }
256 if (!log_follow)
257 uloop_end();
258}
259
260static void logread_fd_cb(struct ubus_request *req, int fd)
261{
262 static struct ustream_fd test_fd;
263
264 test_fd.stream.notify_read = logread_fd_data_cb;
265 ustream_fd_init(&test_fd, fd);
266}
267
268static void logread_complete_cb(struct ubus_request *req, int ret)
269{
270}
271
272void* syslog_main(void* argv)
273{
274 static struct ubus_request req;
275 struct ubus_context *ctx;
276 uint32_t id;
277 const char *ubus_socket = NULL;
278 int ch, ret, lines = 0;
279 static struct blob_buf b;
xf.li44e08692024-01-30 01:54:44 -0800280 int tries = 60;
liubin281ac462023-07-19 14:22:54 +0800281 log_config_entry *config = (log_config_entry *)argv;
282
283 pthread_detach(pthread_self());
284
285 if (NULL == argv)
286 return NULL;
287
288 signal(SIGPIPE, SIG_IGN);
289 uloop_init();
290
xf.li44e08692024-01-30 01:54:44 -0800291 //log_file = config->out_path;
292 memset(log_file, 0, sizeof(log_file));
293 memset(log_ip, 0, sizeof(log_ip));
294 memset(log_port, 0, sizeof(log_port));
295 memset(log_prefix, 0, sizeof(log_prefix));
296 memset(pid_file, 0, sizeof(pid_file));
297 memset(hostname, 0, sizeof(hostname));
298
299 if(config->out_path != NULL)
300 {
301 strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
302 }
303
liubin281ac462023-07-19 14:22:54 +0800304 memset(&file_list, 0, sizeof(struct file_list_t));
305 file_list.total = config->rotate_file_count;
306 if(config->rotate_file_size)
307 log_size = config->rotate_file_size;
308 if(config->ip)
309 {
310 printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
xf.li44e08692024-01-30 01:54:44 -0800311 //log_ip = config->ip;
312 strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
313 //log_port = config->port;
314 if(config->port != NULL)
315 {
316 strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
317 }
liubin281ac462023-07-19 14:22:54 +0800318 }
319 filter_log = config->filter_list;
320 // Follow log messages
321 log_follow = 1;
322 ctx = ubus_connect(ubus_socket);
323 if (!ctx) {
324 fprintf(stderr, "Failed to connect to ubus\n");
325 return -1;
326 }
327 ubus_add_uloop(ctx);
328
329 printf("syslog log start...\n");
330 /* ugly ugly ugly ... we need a real reconnect logic */
331 do {
332 ret = ubus_lookup_id(ctx, "log", &id);
333 if (ret) {
334 fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret));
335 sleep(1);
336 continue;
337 }
liubin281ac462023-07-19 14:22:54 +0800338 blob_buf_init(&b, 0);
339 if (lines)
340 blobmsg_add_u32(&b, "lines", lines);
341 else if (log_follow)
342 blobmsg_add_u32(&b, "lines", 0);
343 if (log_follow) {
xf.li44e08692024-01-30 01:54:44 -0800344 if (strlen(pid_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800345 FILE *fp = fopen(pid_file, "w+");
346 if (fp) {
347 fprintf(fp, "%d", getpid());
348 fclose(fp);
349 }
350 }
351 }
352
xf.li44e08692024-01-30 01:54:44 -0800353 if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
liubin281ac462023-07-19 14:22:54 +0800354 openlog("logread", LOG_PID, LOG_DAEMON);
355 log_type = LOG_NET;
356 sender.cb = log_handle_fd;
357 retry.cb = log_handle_reconnect;
358 uloop_timeout_set(&retry, 1000);
xf.li44e08692024-01-30 01:54:44 -0800359 } else if (strlen(log_file) > 0) {
liubin281ac462023-07-19 14:22:54 +0800360 log_type = LOG_FILE;
361 // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
xf.li44e08692024-01-30 01:54:44 -0800362 sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
liubin281ac462023-07-19 14:22:54 +0800363 sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
364 if (sender.fd < 0) {
365 fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
366 exit(-1);
367 }
368 } else {
369 sender.fd = STDOUT_FILENO;
370 }
371
372 ubus_invoke_async(ctx, id, "read", b.head, &req);
373 req.fd_cb = logread_fd_cb;
374 req.complete_cb = logread_complete_cb;
375 ubus_complete_request_async(ctx, &req);
b.liu9a306862024-03-06 16:49:40 +0800376
liubin281ac462023-07-19 14:22:54 +0800377 uloop_run();
378 ubus_free(ctx);
379 uloop_done();
380
381 } while (ret && tries--);
382
383 pthread_exit(NULL);
384 return NULL;
385}