liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 1 | #include <stdio.h> |
| 2 | #include <fcntl.h> |
| 3 | #include <time.h> |
| 4 | #include <stdio.h> |
| 5 | #include <unistd.h> |
| 6 | #include <sys/socket.h> |
| 7 | #include <time.h> |
| 8 | #include <errno.h> |
| 9 | #include <stdlib.h> |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 10 | #include <sys/file.h> |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 11 | #include <sys/types.h> |
| 12 | #include <sys/stat.h> |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 13 | #include <string.h> |
| 14 | #include <pthread.h> |
b.liu | 3fb0ffe | 2024-11-12 19:24:43 +0800 | [diff] [blame^] | 15 | #include <signal.h> |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 16 | |
b.liu | b3b923a | 2024-06-06 15:15:49 +0800 | [diff] [blame] | 17 | #include "json-c/json.h" |
| 18 | #include "json-c/printbuf.h" |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 19 | #include "log_config.h" |
| 20 | |
b.liu | 5fa2fb5 | 2024-06-20 18:26:34 +0800 | [diff] [blame] | 21 | #include "mbtk_type.h" |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 22 | //#define DEBUG 1 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 23 | |
b.liu | 3fb0ffe | 2024-11-12 19:24:43 +0800 | [diff] [blame^] | 24 | #ifdef MBTK_DUMP_SUPPORT |
| 25 | void mbtk_debug_open(const char *log_file, bool thread_support); |
| 26 | #endif |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 27 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 28 | #ifdef DEBUG |
| 29 | #define mbtk_log(...) printf(__VA_ARGS__) |
| 30 | #else |
| 31 | #define mbtk_log(...) |
| 32 | #endif |
| 33 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 34 | |
| 35 | #define MAX_BUFFER_SIZE (8*1024) |
| 36 | |
| 37 | int tmp_syslog_fd = -1; |
| 38 | int tmp_rd_fd = -1; |
| 39 | |
| 40 | char syslog_buff[MAX_BUFFER_SIZE] = {0}; |
| 41 | char radiolog_buff[MAX_BUFFER_SIZE] = {0}; |
| 42 | |
| 43 | |
| 44 | static void signal_handler(int signum) |
| 45 | { |
| 46 | printf("Received SIGTERM signal, fflush buffer"); |
| 47 | if(fcntl(tmp_syslog_fd, F_GETFL) != -1 && strlen(syslog_buff) > 0) |
| 48 | { |
b.liu | 3fb0ffe | 2024-11-12 19:24:43 +0800 | [diff] [blame^] | 49 | if(write(tmp_syslog_fd, syslog_buff, strlen(syslog_buff)) < 0) |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 50 | { |
| 51 | perror("write error"); |
| 52 | } |
| 53 | memset(syslog_buff,0,MAX_BUFFER_SIZE); |
| 54 | } |
b.liu | 3fb0ffe | 2024-11-12 19:24:43 +0800 | [diff] [blame^] | 55 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 56 | if(fcntl(tmp_rd_fd, F_GETFL) != -1 && strlen(radiolog_buff) > 0) |
| 57 | { |
b.liu | 3fb0ffe | 2024-11-12 19:24:43 +0800 | [diff] [blame^] | 58 | if(write(tmp_rd_fd, radiolog_buff, strlen(radiolog_buff)) < 0) |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 59 | { |
| 60 | perror("write error"); |
| 61 | } |
| 62 | memset(radiolog_buff,0,MAX_BUFFER_SIZE); |
| 63 | } |
b.liu | 3fb0ffe | 2024-11-12 19:24:43 +0800 | [diff] [blame^] | 64 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 65 | } |
| 66 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 67 | static void handler_free(log_config_entry* listdata) |
| 68 | { |
| 69 | int i, n; |
| 70 | struct filter_list_t* _filter_list = NULL; |
| 71 | log_config_entry* entry; |
| 72 | |
| 73 | for (i = 0; i < 5; i++) { |
| 74 | entry = listdata + i; |
| 75 | if (entry->name) { |
| 76 | free(entry->name); |
| 77 | if (entry->out_path) { |
| 78 | free(entry->out_path); |
| 79 | } |
| 80 | if (entry->ip) { |
| 81 | free(entry->ip); |
| 82 | } |
| 83 | _filter_list = entry->filter_list; |
| 84 | for (n = 0; n < 10; n++) { |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 85 | printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 86 | if (_filter_list) { |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 87 | printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list->next); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 88 | if (_filter_list->tag) { |
| 89 | free(_filter_list->tag); |
| 90 | } |
| 91 | free(_filter_list); |
| 92 | _filter_list = _filter_list->next; |
| 93 | } else { |
| 94 | break; |
| 95 | } |
| 96 | } |
| 97 | } else { |
| 98 | break; |
| 99 | } |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | int parse_config(log_config_entry* listdata) |
| 104 | { |
| 105 | json_object* jsonobj = NULL; |
| 106 | json_object* tmpjson = NULL; |
| 107 | json_object* datajson = NULL; |
| 108 | json_object* listjson = NULL; |
| 109 | json_object* fileterjson = NULL; |
| 110 | json_object* fileter_listjson = NULL; |
| 111 | log_config_entry* entry; |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 112 | int i, n, ret, array_length; |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 113 | char* cmdval = NULL; |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 114 | |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 115 | printf("MBTK_LOGD: in parse_config\n"); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 116 | jsonobj = json_object_from_file(LOG_CONFIG_PATH); |
| 117 | if (NULL == jsonobj) { |
| 118 | printf("Can't open config file: %s\n", LOG_CONFIG_PATH); |
| 119 | return -1; |
| 120 | } |
| 121 | ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson); |
| 122 | if (!ret) { |
| 123 | printf("get jsondata error ...\n"); |
| 124 | } |
| 125 | |
| 126 | if (NULL == tmpjson) { |
| 127 | printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson)); |
| 128 | } |
| 129 | /**获取total***/ |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 130 | cmdval = (char*)json_object_get_string(tmpjson); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 131 | printf("logd enable : %s\n", cmdval); |
| 132 | json_object_put(tmpjson); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 133 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 134 | /***获取data***/ |
| 135 | json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 136 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 137 | for (i = 0 ; i < 5; i++) { |
| 138 | struct filter_list_t* _filter_list = NULL; |
| 139 | struct filter_list_t* tmp_filter_list = NULL; |
| 140 | datajson = json_object_array_get_idx(tmpjson, i); |
| 141 | if (NULL == datajson) { |
| 142 | mbtk_log("the datajson exit\n"); |
| 143 | break; |
| 144 | } |
| 145 | entry = listdata + i; |
| 146 | json_object_object_get_ex(datajson, "enable", &listjson); |
| 147 | entry->enable = json_object_get_int(listjson); |
| 148 | mbtk_log("enable: %d\n", entry->enable); |
| 149 | |
| 150 | json_object_object_get_ex(datajson, "name", &listjson); |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 151 | entry->name = (char*)json_object_get_string(listjson); |
b.liu | 9e8584b | 2024-11-06 19:21:28 +0800 | [diff] [blame] | 152 | printf("cmdval: %s\n", entry->name); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 153 | |
| 154 | json_object_object_get_ex(datajson, "log_file", &listjson); |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 155 | entry->out_path = (char*)json_object_get_string(listjson); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 156 | mbtk_log("cmdval: %s\n", entry->out_path); |
| 157 | |
| 158 | json_object_object_get_ex(datajson, "log_stream", &listjson); |
| 159 | if (listjson) { |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 160 | entry->ip = (char*)json_object_get_string(listjson); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 161 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 162 | cmdval = strstr(entry->ip, ":"); |
| 163 | if (cmdval) { |
| 164 | entry->port = &cmdval[1]; |
| 165 | cmdval[0] = 0; |
| 166 | mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port); |
| 167 | } else { |
| 168 | printf("Can't find port!!\n"); |
| 169 | free(entry->ip); |
| 170 | entry->ip = NULL; |
| 171 | } |
| 172 | } |
| 173 | json_object_object_get_ex(datajson, "rotate_file_size", &listjson); |
| 174 | entry->rotate_file_size = json_object_get_int(listjson) * 1024; |
| 175 | mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size); |
| 176 | |
| 177 | json_object_object_get_ex(datajson, "rotate_file_count", &listjson); |
| 178 | entry->rotate_file_count = json_object_get_int(listjson); |
| 179 | mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count); |
| 180 | |
| 181 | json_object_object_get_ex(datajson, "filter_list", &listjson); |
| 182 | if (NULL == listjson) { |
| 183 | printf("%s %d: object failure!\n", __FUNCTION__, __LINE__); |
| 184 | json_object_put(listjson); |
| 185 | continue; |
| 186 | } |
| 187 | entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t)); |
| 188 | _filter_list = entry->filter_list; |
| 189 | |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 190 | array_length = json_object_array_length(listjson); |
| 191 | for (n = 0 ; n <= array_length; n++) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 192 | fileterjson = json_object_array_get_idx(listjson, n); |
| 193 | if (NULL == fileterjson) { |
| 194 | mbtk_log("the fileterjson exit\n"); |
| 195 | free(tmp_filter_list->next); |
| 196 | tmp_filter_list->next = NULL; |
| 197 | break; |
| 198 | } |
| 199 | memset(_filter_list, 0, sizeof(struct filter_list_t)); |
| 200 | json_object_object_get_ex(fileterjson, "priority", &fileter_listjson); |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 201 | const char* str = json_object_get_string(fileter_listjson); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 202 | if (str) { |
| 203 | _filter_list->priority = str[0]; |
| 204 | mbtk_log("fileter_listjson: %c\n", _filter_list->priority); |
| 205 | } |
| 206 | json_object_object_get_ex(fileterjson, "tag", &fileter_listjson); |
| 207 | // if (NULL == fileter_listjson) { |
| 208 | // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__); |
| 209 | // } |
| 210 | str = json_object_get_string(fileter_listjson); |
| 211 | if (str) { |
| 212 | _filter_list->tag = strdup(str); |
| 213 | mbtk_log("fileter_listjson: %s\n", _filter_list->tag); |
| 214 | } |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 215 | else |
| 216 | { |
| 217 | _filter_list->tag = "\0"; |
| 218 | } |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 219 | |
| 220 | //json_object_put(fileter_listjson); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 221 | _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t)); |
| 222 | if (NULL == _filter_list->next) { |
| 223 | printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__); |
| 224 | break; |
| 225 | } |
| 226 | tmp_filter_list = _filter_list; |
| 227 | _filter_list = _filter_list->next; |
| 228 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 229 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 230 | |
| 231 | /***释放json对象***/ |
| 232 | json_object_put(jsonobj); |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 233 | printf("MBTK_LOGD: parse_config end\n"); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | #define LOGD_PID "/var/run/mbtk_logd.pid" |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 238 | #if 0 |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 239 | static int save_pid(void) |
| 240 | { |
| 241 | pid_t process_id; |
| 242 | int fd, ret; |
| 243 | char buf[12] = {0}; |
| 244 | |
| 245 | process_id = getpid(); |
| 246 | if(access(LOGD_PID, F_OK) == 0) |
| 247 | { |
| 248 | printf("mbtk_logd 进程已经存在\n"); |
| 249 | return -1; |
| 250 | } |
| 251 | fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600); |
| 252 | if (fd < 0) { |
| 253 | fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno)); |
| 254 | return -2; |
| 255 | } |
| 256 | snprintf(buf, sizeof(buf), "%d\n", process_id); |
| 257 | ret = write(fd, buf, strlen(buf)); |
| 258 | close(fd); |
| 259 | |
| 260 | if(ret > 0) |
| 261 | return 0; |
| 262 | else |
| 263 | return -2; |
| 264 | } |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 265 | #endif |
xf.li | 4364377 | 2024-03-04 19:39:53 -0800 | [diff] [blame] | 266 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 267 | int main(int argc, char* argv[]) |
| 268 | { |
| 269 | log_config_entry listdata[5]; |
| 270 | pthread_t pid[5] = {0}; |
| 271 | int i, ret; |
| 272 | void* tret; |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 273 | //struct filter_list_t* _filter_list = NULL; |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 274 | |
l.yang | 250444d | 2024-11-12 00:35:10 -0800 | [diff] [blame] | 275 | signal(SIGTERM, signal_handler); |
b.liu | bb59049 | 2024-06-13 16:42:08 +0800 | [diff] [blame] | 276 | #ifdef MBTK_DUMP_SUPPORT |
| 277 | mbtk_debug_open(NULL, TRUE); |
| 278 | #endif |
| 279 | |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 280 | int lock_file = open("/tmp/mbtk_logd.lock", O_CREAT|O_RDWR, 0666); |
| 281 | int rc = flock(lock_file,LOCK_EX|LOCK_NB); |
| 282 | if(rc) { |
| 283 | if(EWOULDBLOCK == errno) { |
| 284 | printf("Error: cannot restart the mbtk_logd repeatedly\n"); |
| 285 | exit(0); |
| 286 | } |
| 287 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 288 | |
| 289 | memset(listdata, 0, sizeof(log_config_entry) * 5); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 290 | ret = parse_config(listdata); |
| 291 | if (ret) { |
| 292 | return -1; |
| 293 | } |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 294 | |
| 295 | printf("logd %s start !\n", __FUNCTION__); |
| 296 | |
| 297 | for (i = 0; i < 5; ++i) { |
| 298 | if (NULL == listdata[i].name) { |
| 299 | break; |
| 300 | } |
| 301 | |
| 302 | if (0 == listdata[i].enable) { |
| 303 | printf("%s log disabled !\n", listdata[i].name); |
| 304 | continue; |
| 305 | } |
| 306 | |
| 307 | if (0 == memcmp(listdata[i].name, "radio", 5)) { |
| 308 | ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]); |
| 309 | if (ret != 0) { |
| 310 | fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__); |
| 311 | } |
| 312 | } else if (0 == memcmp(listdata[i].name, "syslog", 6)) { |
| 313 | ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]); |
| 314 | if (ret != 0) { |
| 315 | fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__); |
| 316 | } |
| 317 | } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) { |
| 318 | ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]); |
| 319 | if (ret != 0) { |
| 320 | fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__); |
| 321 | } |
xf.li | 44e0869 | 2024-01-30 01:54:44 -0800 | [diff] [blame] | 322 | } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 8)) { |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 323 | ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]); |
| 324 | if (ret != 0) { |
| 325 | fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__); |
| 326 | } |
| 327 | } |
| 328 | } |
| 329 | |
b.liu | 5fa2fb5 | 2024-06-20 18:26:34 +0800 | [diff] [blame] | 330 | |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 331 | for (i = 0; i < 5; ++i) { |
| 332 | if (NULL == listdata[i].name) { |
| 333 | break; |
| 334 | } |
| 335 | if (pid[i]) { |
| 336 | if (pthread_join(pid[i], &tret) != 0) { |
b.liu | 0f7ffad | 2024-11-06 19:57:27 +0800 | [diff] [blame] | 337 | printf("Join thread %d : %ld error!\n", i, pid[i]); |
liubin | 281ac46 | 2023-07-19 14:22:54 +0800 | [diff] [blame] | 338 | exit(1); |
| 339 | } |
| 340 | } |
| 341 | } |
| 342 | handler_free(listdata); |
| 343 | |
| 344 | return 0; |
| 345 | } |