liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | /* |
| 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.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 23 | #include <sys/un.h> |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 24 | #include <sys/socket.h> |
| 25 | |
| 26 | #define SYSLOG_NAMES |
| 27 | #include <syslog.h> |
b.liu | b3b923a | 2024-06-06 15:15:49 +0800 | [diff] [blame] | 28 | #include "json-c/json.h" |
| 29 | #include "json-c/printbuf.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 30 | |
| 31 | #include <libubox/ustream.h> |
| 32 | #include <libubox/blobmsg_json.h> |
| 33 | #include <libubox/usock.h> |
| 34 | #include <libubox/uloop.h> |
| 35 | #include "libubus.h" |
b.liu | 63a4a32 | 2024-03-07 19:00:53 +0800 | [diff] [blame] | 36 | #include "syslog.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 37 | #include "log_config.h" |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 38 | //#include "lynq/liblog.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 39 | |
b.liu | 63a4a32 | 2024-03-07 19:00:53 +0800 | [diff] [blame] | 40 | enum { |
| 41 | SOURCE_KLOG = 0, |
| 42 | SOURCE_SYSLOG = 1, |
| 43 | SOURCE_INTERNAL = 2, |
| 44 | SOURCE_ANY = 0xff, |
| 45 | }; |
| 46 | |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 47 | #define LOG_CONFIG_LEN 50 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 48 | enum { |
| 49 | LOG_STDOUT, |
| 50 | LOG_FILE, |
| 51 | LOG_NET, |
| 52 | }; |
| 53 | |
| 54 | enum { |
| 55 | LOG_MSG, |
| 56 | LOG_ID, |
| 57 | LOG_PRIO, |
| 58 | LOG_SOURCE, |
| 59 | LOG_TIME, |
| 60 | __LOG_MAX |
| 61 | }; |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 62 | |
| 63 | #define SYSLOG_BUFF_SIZE (4*1024) |
| 64 | #define MAX_BUFFER_SIZE (8*1024) |
| 65 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 66 | |
| 67 | static const struct blobmsg_policy log_policy[] = { |
| 68 | [LOG_MSG] = { .name = "msg", .type = BLOBMSG_TYPE_STRING }, |
| 69 | [LOG_ID] = { .name = "id", .type = BLOBMSG_TYPE_INT32 }, |
| 70 | [LOG_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 }, |
| 71 | [LOG_SOURCE] = { .name = "source", .type = BLOBMSG_TYPE_INT32 }, |
| 72 | [LOG_TIME] = { .name = "time", .type = BLOBMSG_TYPE_INT64 }, |
| 73 | }; |
| 74 | |
| 75 | static struct uloop_timeout retry; |
| 76 | static struct uloop_fd sender; |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 77 | //static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname; |
| 78 | static 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]; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 79 | static int log_type = LOG_STDOUT; |
| 80 | static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0; |
| 81 | static struct file_list_t file_list; |
| 82 | static struct filter_list_t *filter_log = NULL; |
| 83 | static char tmp_log[48] = {0}; |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 84 | pthread_t attr = -1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 85 | |
| 86 | static const char* getcodetext(int value, CODE *codetable) { |
| 87 | CODE *i; |
| 88 | |
| 89 | if (value >= 0) |
| 90 | for (i = codetable; i->c_val != -1; i++) |
| 91 | if (i->c_val == value) |
| 92 | return (i->c_name); |
| 93 | return "<unknown>"; |
| 94 | }; |
| 95 | |
| 96 | static void log_handle_reconnect(struct uloop_timeout *timeout) |
| 97 | { |
| 98 | sender.fd = usock((log_udp) ? (USOCK_UDP) : (USOCK_TCP), log_ip, log_port); |
| 99 | if (sender.fd < 0) { |
| 100 | fprintf(stderr, "failed to connect: %s\n", strerror(errno)); |
| 101 | uloop_timeout_set(&retry, 1000); |
| 102 | } else { |
| 103 | uloop_fd_add(&sender, ULOOP_READ); |
| 104 | syslog(0, "Logread connected to %s:%s\n", log_ip, log_port); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | static void log_handle_fd(struct uloop_fd *u, unsigned int events) |
| 109 | { |
| 110 | if (u->eof) { |
| 111 | uloop_fd_delete(u); |
| 112 | close(sender.fd); |
| 113 | sender.fd = -1; |
| 114 | uloop_timeout_set(&retry, 1000); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | static int filter_char_to_pri(char c) |
| 119 | { |
| 120 | switch (c) { |
| 121 | case 'v': |
l.yang | 8ee686d | 2024-10-14 19:07:51 -0700 | [diff] [blame] | 122 | return LOG_CRIT; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 123 | case 'd': |
| 124 | return LOG_DEBUG; |
| 125 | case 'i': |
| 126 | return LOG_INFO; |
| 127 | case 'w': |
| 128 | return LOG_WARNING; |
| 129 | case 'e': |
| 130 | return LOG_ERR; |
| 131 | case 'f': |
| 132 | return LOG_ALERT; |
| 133 | case '*': |
| 134 | default: |
| 135 | return 8; |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | static int syslog_fileter_log(int pri, char *tag, struct filter_list_t *filter) |
| 140 | { |
| 141 | struct filter_list_t *_filter = filter; |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 142 | struct filter_list_t *_filter_common = _filter; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 143 | |
| 144 | while(_filter) |
| 145 | { |
| 146 | int p = filter_char_to_pri(_filter->priority); |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 147 | int len = strlen(_filter->tag); |
| 148 | if(len > 0) |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 149 | { |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 150 | if(0 == memcmp(_filter->tag, tag, len)) |
| 151 | { |
| 152 | if((pri < p) || (pri == p)) |
| 153 | { |
| 154 | return 0; |
| 155 | } |
| 156 | else |
| 157 | { |
| 158 | return -1; |
| 159 | } |
| 160 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 161 | }else{ // have no tag |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 162 | _filter_common = _filter; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 163 | } |
| 164 | _filter = _filter->next; |
| 165 | } |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 166 | //common tag |
| 167 | int p = filter_char_to_pri(_filter_common->priority); |
| 168 | if(pri > p) |
| 169 | return -1; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 170 | |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 171 | return 0; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 172 | } |
| 173 | static int log_notify(struct blob_attr *msg) |
| 174 | { |
xf.li | 01e3982 | 2024-10-30 16:00:32 +0800 | [diff] [blame] | 175 | int index = 0; |
| 176 | int len = 0; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 177 | struct blob_attr *tb[__LOG_MAX]; |
| 178 | struct stat s; |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 179 | char buf[512] = {'\0'}; |
xf.li | 127b6fa | 2024-04-28 01:48:45 -0700 | [diff] [blame] | 180 | char tmp_buf[48] = {0}; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 181 | uint32_t p; |
| 182 | char *str; |
| 183 | time_t t; |
| 184 | char *c, *m; |
| 185 | |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 186 | static char buffer[MAX_BUFFER_SIZE] = {0}; |
| 187 | static int buffer_index = 0; |
| 188 | |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 189 | // |
| 190 | //sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/")); |
| 191 | |
| 192 | |
| 193 | snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file); |
xf.li | 127b6fa | 2024-04-28 01:48:45 -0700 | [diff] [blame] | 194 | if(access(tmp_buf, W_OK) != 0) |
| 195 | { |
| 196 | sender.fd = open(tmp_buf, O_CREAT | O_WRONLY | O_APPEND, 0600); |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 197 | if(sender.fd < 0) |
| 198 | { |
| 199 | perror("Failed to open file "); |
| 200 | return -1; |
| 201 | } |
xf.li | 127b6fa | 2024-04-28 01:48:45 -0700 | [diff] [blame] | 202 | } |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 203 | |
| 204 | |
| 205 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 206 | blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg)); |
| 207 | if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG]) |
| 208 | return 1; |
| 209 | |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 210 | if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) |
| 211 | { |
| 212 | sender.fd = get_rotate_file(sender.fd, log_file, &file_list); |
| 213 | if (sender.fd < 0) |
| 214 | { |
| 215 | fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno)); |
| 216 | exit(-1); |
| 217 | } |
| 218 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 219 | |
| 220 | m = blobmsg_get_string(tb[LOG_MSG]); |
| 221 | t = blobmsg_get_u64(tb[LOG_TIME]) / 1000; |
| 222 | c = ctime(&t); |
| 223 | p = blobmsg_get_u32(tb[LOG_PRIO]); |
| 224 | c[strlen(c) - 1] = '\0'; |
| 225 | str = blobmsg_format_json(msg, true); |
| 226 | |
| 227 | if(filter_log && syslog_fileter_log(LOG_PRI(p), m, filter_log)) |
| 228 | { |
| 229 | // printf("%s %d: fileter pri:%d tag:%s!\n", __FUNCTION__, __LINE__, p, m); |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 230 | return 0; |
| 231 | //exit(-1); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 232 | } |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 233 | if (log_type == LOG_NET) |
| 234 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 235 | int err; |
| 236 | |
| 237 | snprintf(buf, sizeof(buf), "<%u>", p); |
| 238 | strncat(buf, c + 4, 16); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 239 | if (strlen(hostname) > 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 240 | strncat(buf, hostname, sizeof(buf)); |
| 241 | strncat(buf, " ", sizeof(buf)); |
| 242 | } |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 243 | if (strlen(log_prefix) > 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 244 | strncat(buf, log_prefix, sizeof(buf)); |
| 245 | strncat(buf, ": ", sizeof(buf)); |
| 246 | } |
| 247 | if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG) |
| 248 | strncat(buf, "kernel: ", sizeof(buf)); |
| 249 | strncat(buf, m, sizeof(buf)); |
| 250 | if (log_udp) |
| 251 | err = write(sender.fd, buf, strlen(buf)); |
| 252 | else |
| 253 | err = send(sender.fd, buf, strlen(buf), 0); |
| 254 | |
| 255 | if (err < 0) { |
| 256 | syslog(0, "failed to send log data to %s:%s via %s\n", |
| 257 | log_ip, log_port, (log_udp) ? ("udp") : ("tcp")); |
| 258 | uloop_fd_delete(&sender); |
| 259 | close(sender.fd); |
| 260 | sender.fd = -1; |
| 261 | uloop_timeout_set(&retry, 1000); |
| 262 | } |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 263 | } |
| 264 | else |
| 265 | { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 266 | snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n", |
| 267 | c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames), |
| 268 | (blobmsg_get_u32(tb[LOG_SOURCE])) ? ("") : (" kernel:"), m); |
xf.li | 01e3982 | 2024-10-30 16:00:32 +0800 | [diff] [blame] | 269 | |
| 270 | len = strlen(buf); |
| 271 | if(access("/etc/syslog_encrypt_flag", F_OK) == 0) |
| 272 | { |
| 273 | for(index = 0; index < len; index++) |
| 274 | { |
| 275 | buf[index] ^= 1; |
| 276 | } |
| 277 | } |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 278 | |
| 279 | |
| 280 | |
| 281 | if (buffer_index >= SYSLOG_BUFF_SIZE) |
| 282 | { |
| 283 | // Flush the buffer if it's full |
| 284 | if (write(sender.fd, buffer, buffer_index) < 0) |
| 285 | { |
| 286 | perror("write error"); |
| 287 | return -1; |
| 288 | } |
| 289 | buffer_index = 0; // Reset buffer index after flushing |
| 290 | } |
| 291 | |
| 292 | if(len < SYSLOG_BUFF_SIZE) |
| 293 | { |
| 294 | //copy buf to buffer |
| 295 | memcpy(buffer + buffer_index, buf, len); |
| 296 | buffer_index += len; |
| 297 | } |
| 298 | else |
| 299 | { |
| 300 | write(sender.fd, buf, len); |
| 301 | } |
| 302 | |
| 303 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 304 | } |
| 305 | |
| 306 | free(str); |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 307 | //if (log_type == LOG_FILE) |
| 308 | //fsync(sender.fd); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 309 | |
| 310 | return 0; |
| 311 | } |
| 312 | |
| 313 | static void logread_fd_data_cb(struct ustream *s, int bytes) |
| 314 | { |
| 315 | while (true) { |
| 316 | int len; |
| 317 | struct blob_attr *a; |
| 318 | |
| 319 | a = (void*) ustream_get_read_buf(s, &len); |
| 320 | if (len < sizeof(*a) || len < blob_len(a) + sizeof(*a)) |
| 321 | break; |
| 322 | log_notify(a); |
| 323 | ustream_consume(s, blob_len(a) + sizeof(*a)); |
| 324 | } |
| 325 | if (!log_follow) |
| 326 | uloop_end(); |
| 327 | } |
| 328 | |
| 329 | static void logread_fd_cb(struct ubus_request *req, int fd) |
| 330 | { |
| 331 | static struct ustream_fd test_fd; |
| 332 | |
| 333 | test_fd.stream.notify_read = logread_fd_data_cb; |
| 334 | ustream_fd_init(&test_fd, fd); |
| 335 | } |
| 336 | |
| 337 | static void logread_complete_cb(struct ubus_request *req, int ret) |
| 338 | { |
| 339 | } |
| 340 | |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 341 | int lynq_update_log_level() |
| 342 | { |
| 343 | json_object* jsonobj = NULL; |
| 344 | json_object* tmpjson = NULL; |
| 345 | json_object* datajson = NULL; |
| 346 | json_object* listjson = NULL; |
| 347 | json_object* fileterjson = NULL; |
| 348 | json_object* fileter_listjson = NULL; |
| 349 | struct filter_list_t* filter_list_head = NULL; |
| 350 | struct filter_list_t* tmp_filter_list = NULL; |
| 351 | struct filter_list_t* _filter_list = NULL; |
| 352 | |
| 353 | int n; |
| 354 | int array_length; |
| 355 | char* tmp_string = NULL; |
| 356 | |
| 357 | jsonobj = json_object_from_file(LOG_CONFIG_PATH); |
| 358 | if (NULL == jsonobj) { |
| 359 | printf("Can't open config file: %s\n", LOG_CONFIG_PATH); |
| 360 | return -1; |
| 361 | } |
| 362 | /***获取data***/ |
| 363 | json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson); |
| 364 | datajson = json_object_array_get_idx(tmpjson, 0);//syslog index is 0 |
| 365 | if (NULL == datajson) { |
| 366 | json_object_put(jsonobj); |
| 367 | return -1; |
| 368 | } |
| 369 | json_object_object_get_ex(datajson, "filter_list", &listjson); |
| 370 | if (NULL == listjson) { |
| 371 | printf("%s %d: object failure!\n", __FUNCTION__, __LINE__); |
| 372 | json_object_put(listjson); |
| 373 | return -1; |
| 374 | } |
| 375 | filter_list_head = (struct filter_list_t*)malloc(sizeof(struct filter_list_t)); |
| 376 | _filter_list = filter_list_head; |
| 377 | |
| 378 | array_length = json_object_array_length(listjson); |
| 379 | for (n = 0 ; n < array_length; n++) { |
| 380 | fileterjson = json_object_array_get_idx(listjson, n); |
| 381 | if (NULL == fileterjson) { |
| 382 | printf("the fileterjson exit\n"); |
| 383 | free(tmp_filter_list->next); |
| 384 | tmp_filter_list->next = NULL; |
| 385 | break; |
| 386 | } |
| 387 | memset(_filter_list, 0, sizeof(struct filter_list_t)); |
| 388 | json_object_object_get_ex(fileterjson, "priority", &fileter_listjson); |
| 389 | char* str = json_object_get_string(fileter_listjson); |
| 390 | if (str) { |
| 391 | _filter_list->priority = str[0]; |
| 392 | printf("fileter_listjson: %c\n", _filter_list->priority); |
| 393 | } |
| 394 | |
| 395 | json_object_object_get_ex(fileterjson, "tag", &fileter_listjson); |
| 396 | |
| 397 | str = json_object_get_string(fileter_listjson); |
| 398 | if (str) { |
| 399 | _filter_list->tag = strdup(str); |
| 400 | printf("fileter_listjson: %s\n", _filter_list->tag); |
| 401 | } |
| 402 | else |
| 403 | { |
| 404 | _filter_list->tag = "\0"; |
| 405 | } |
| 406 | //json_object_put(fileter_listjson); |
| 407 | _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t)); |
| 408 | if (NULL == _filter_list->next) { |
| 409 | printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__); |
| 410 | break; |
| 411 | } |
| 412 | tmp_filter_list = _filter_list; |
| 413 | _filter_list = _filter_list->next; |
| 414 | _filter_list->next = NULL; |
| 415 | } |
| 416 | /***释放json对象***/ |
| 417 | json_object_put(jsonobj); |
| 418 | |
| 419 | tmp_filter_list = filter_log; |
| 420 | filter_log = filter_list_head; |
| 421 | |
| 422 | while(tmp_filter_list != NULL) { |
| 423 | _filter_list = tmp_filter_list; |
| 424 | free(tmp_filter_list); |
| 425 | tmp_filter_list = _filter_list->next; |
| 426 | } |
| 427 | |
| 428 | return 0; |
| 429 | } |
| 430 | |
| 431 | |
| 432 | int wait_update_log_level() |
| 433 | { |
| 434 | int i = 0; |
| 435 | char recvBuff[100]; |
| 436 | int serverFd,clientFd,addrLen; |
| 437 | struct sockaddr_un serverAddr,clientAddr; |
| 438 | |
| 439 | pthread_detach(pthread_self()); |
| 440 | printf("MBTK: in wait_update_log_level\n"); |
| 441 | |
| 442 | memset(&serverAddr,0,sizeof(serverAddr)); |
| 443 | serverAddr.sun_family = AF_UNIX; |
| 444 | sprintf(serverAddr.sun_path,"%s","/var/log_server.socket"); |
| 445 | |
| 446 | unlink("/var/log_server.socket"); /* in case it already exists */ |
| 447 | |
| 448 | if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0) |
| 449 | { |
| 450 | printf("err -1\n"); |
| 451 | return -1; |
| 452 | } |
| 453 | |
| 454 | if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0) |
| 455 | { |
| 456 | printf("err -2\n"); |
| 457 | close(serverFd); |
| 458 | return -2; |
| 459 | } |
| 460 | |
| 461 | if(listen(serverFd,10) < 0) |
| 462 | { |
| 463 | printf("err -3\n"); |
| 464 | return -3; |
| 465 | } |
| 466 | |
| 467 | while(1) |
| 468 | { |
| 469 | addrLen = sizeof(clientAddr); |
| 470 | memset(&clientAddr,0,sizeof(clientAddr)); |
| 471 | memset(&recvBuff,0,100); |
| 472 | |
| 473 | if((clientFd = accept(serverFd,(struct sockaddr*)&clientAddr,&addrLen)) < 0) |
| 474 | { |
| 475 | printf("err -4\n"); |
| 476 | continue; |
| 477 | } |
| 478 | printf("MBTK: wait recv\n"); |
| 479 | if(recv(clientFd,recvBuff,100,0) < 0) |
| 480 | { |
| 481 | printf("err -5"); |
| 482 | close(clientFd); |
| 483 | continue; |
| 484 | } |
| 485 | if(strncmp(recvBuff, "update", strlen("update")) == 0) |
| 486 | { |
| 487 | lynq_update_log_level(); |
| 488 | } |
| 489 | |
| 490 | close(clientFd); |
| 491 | } |
| 492 | close(serverFd); |
| 493 | |
| 494 | return 0; |
| 495 | } |
| 496 | |
| 497 | int syslog_pthread_create() |
| 498 | { |
| 499 | int ret; |
| 500 | |
| 501 | ret = pthread_create(&attr, NULL, wait_update_log_level, NULL); |
| 502 | |
| 503 | if (ret < 0) |
| 504 | { |
| 505 | printf("MBTK:pthread create fail"); |
| 506 | return -1; |
| 507 | } |
| 508 | |
| 509 | return -1; |
| 510 | } |
| 511 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 512 | void* syslog_main(void* argv) |
| 513 | { |
| 514 | static struct ubus_request req; |
| 515 | struct ubus_context *ctx; |
| 516 | uint32_t id; |
| 517 | const char *ubus_socket = NULL; |
| 518 | int ch, ret, lines = 0; |
| 519 | static struct blob_buf b; |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 520 | int tries = 60; |
xf.li | 50b8baf | 2024-04-14 19:51:18 -0700 | [diff] [blame] | 521 | void* tret; |
| 522 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 523 | log_config_entry *config = (log_config_entry *)argv; |
| 524 | |
| 525 | pthread_detach(pthread_self()); |
| 526 | |
| 527 | if (NULL == argv) |
| 528 | return NULL; |
| 529 | |
| 530 | signal(SIGPIPE, SIG_IGN); |
| 531 | uloop_init(); |
| 532 | |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 533 | |
| 534 | syslog_pthread_create(); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 535 | //log_file = config->out_path; |
| 536 | memset(log_file, 0, sizeof(log_file)); |
| 537 | memset(log_ip, 0, sizeof(log_ip)); |
| 538 | memset(log_port, 0, sizeof(log_port)); |
| 539 | memset(log_prefix, 0, sizeof(log_prefix)); |
| 540 | memset(pid_file, 0, sizeof(pid_file)); |
| 541 | memset(hostname, 0, sizeof(hostname)); |
| 542 | |
| 543 | if(config->out_path != NULL) |
| 544 | { |
| 545 | strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1); |
| 546 | } |
| 547 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 548 | memset(&file_list, 0, sizeof(struct file_list_t)); |
| 549 | file_list.total = config->rotate_file_count; |
| 550 | if(config->rotate_file_size) |
| 551 | log_size = config->rotate_file_size; |
| 552 | if(config->ip) |
| 553 | { |
| 554 | printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 555 | //log_ip = config->ip; |
| 556 | strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1); |
| 557 | //log_port = config->port; |
| 558 | if(config->port != NULL) |
| 559 | { |
| 560 | strncpy(log_port, config->port, LOG_CONFIG_LEN - 1); |
| 561 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 562 | } |
| 563 | filter_log = config->filter_list; |
| 564 | // Follow log messages |
| 565 | log_follow = 1; |
| 566 | ctx = ubus_connect(ubus_socket); |
| 567 | if (!ctx) { |
| 568 | fprintf(stderr, "Failed to connect to ubus\n"); |
| 569 | return -1; |
| 570 | } |
| 571 | ubus_add_uloop(ctx); |
| 572 | |
| 573 | printf("syslog log start...\n"); |
| 574 | /* ugly ugly ugly ... we need a real reconnect logic */ |
| 575 | do { |
| 576 | ret = ubus_lookup_id(ctx, "log", &id); |
| 577 | if (ret) { |
| 578 | fprintf(stderr, "Failed to find log object: %s\n", ubus_strerror(ret)); |
| 579 | sleep(1); |
| 580 | continue; |
| 581 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 582 | blob_buf_init(&b, 0); |
| 583 | if (lines) |
| 584 | blobmsg_add_u32(&b, "lines", lines); |
| 585 | else if (log_follow) |
| 586 | blobmsg_add_u32(&b, "lines", 0); |
| 587 | if (log_follow) { |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 588 | if (strlen(pid_file) > 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 589 | FILE *fp = fopen(pid_file, "w+"); |
| 590 | if (fp) { |
| 591 | fprintf(fp, "%d", getpid()); |
| 592 | fclose(fp); |
| 593 | } |
| 594 | } |
| 595 | } |
| 596 | |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 597 | if (strlen(log_ip) > 0 && strlen(log_port) > 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 598 | openlog("logread", LOG_PID, LOG_DAEMON); |
| 599 | log_type = LOG_NET; |
| 600 | sender.cb = log_handle_fd; |
| 601 | retry.cb = log_handle_reconnect; |
| 602 | uloop_timeout_set(&retry, 1000); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 603 | } else if (strlen(log_file) > 0) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 604 | log_type = LOG_FILE; |
| 605 | // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path |
l.yang | b7972c7 | 2024-10-15 22:34:51 -0700 | [diff] [blame] | 606 | |
| 607 | //sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/")); |
| 608 | |
| 609 | //直接将log文件存储在不会掉电丢失的路径 |
| 610 | snprintf(tmp_log,sizeof(tmp_log), "%s",log_file); |
l.yang | 67782e6 | 2024-11-05 01:28:10 -0800 | [diff] [blame] | 611 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 612 | sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600); |
| 613 | if (sender.fd < 0) { |
| 614 | fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno)); |
| 615 | exit(-1); |
| 616 | } |
| 617 | } else { |
| 618 | sender.fd = STDOUT_FILENO; |
| 619 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 620 | ubus_invoke_async(ctx, id, "read", b.head, &req); |
| 621 | req.fd_cb = logread_fd_cb; |
| 622 | req.complete_cb = logread_complete_cb; |
| 623 | ubus_complete_request_async(ctx, &req); |
b.liu | 9a30686 | 2024-03-06 16:49:40 +0800 | [diff] [blame] | 624 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 625 | uloop_run(); |
| 626 | ubus_free(ctx); |
| 627 | uloop_done(); |
| 628 | |
| 629 | } while (ret && tries--); |
xf.li | 4bd9ee4 | 2024-04-13 01:31:44 -0700 | [diff] [blame] | 630 | |
| 631 | if (attr) { |
| 632 | if (pthread_join(attr, &tret) != 0) { |
| 633 | printf("MBTK:Join thread: %d error!\n", attr); |
| 634 | exit(1); |
| 635 | } |
| 636 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 637 | |
| 638 | pthread_exit(NULL); |
| 639 | return NULL; |
| 640 | } |