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