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