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