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