blob: 142ce138e762ede580badc132c1561795645ba4e [file] [log] [blame]
liubin281ac462023-07-19 14:22:54 +08001#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.li44e08692024-01-30 01:54:44 -080010#include <sys/file.h>
liubin281ac462023-07-19 14:22:54 +080011#include <sys/types.h>
12#include <sys/stat.h>
b.liu0f7ffad2024-11-06 19:57:27 +080013#include <string.h>
14#include <pthread.h>
b.liu3fb0ffe2024-11-12 19:24:43 +080015#include <signal.h>
b.liu0f7ffad2024-11-06 19:57:27 +080016
b.liub3b923a2024-06-06 15:15:49 +080017#include "json-c/json.h"
18#include "json-c/printbuf.h"
liubin281ac462023-07-19 14:22:54 +080019#include "log_config.h"
20
b.liu5fa2fb52024-06-20 18:26:34 +080021#include "mbtk_type.h"
xf.li43643772024-03-04 19:39:53 -080022//#define DEBUG 1
liubin281ac462023-07-19 14:22:54 +080023
b.liu3fb0ffe2024-11-12 19:24:43 +080024#ifdef MBTK_DUMP_SUPPORT
25void mbtk_debug_open(const char *log_file, bool thread_support);
26#endif
l.yang250444d2024-11-12 00:35:10 -080027
liubin281ac462023-07-19 14:22:54 +080028#ifdef DEBUG
29#define mbtk_log(...) printf(__VA_ARGS__)
30#else
31#define mbtk_log(...)
32#endif
33
l.yangd2c71bc2024-12-16 19:24:24 -080034int emmc_pro = -1;
l.yang250444d2024-11-12 00:35:10 -080035
36#define MAX_BUFFER_SIZE (8*1024)
l.yange4285f22024-11-13 00:30:41 -080037#define MAX_SLEEP_TIME 2147483647
l.yang250444d2024-11-12 00:35:10 -080038
39int tmp_syslog_fd = -1;
40int tmp_rd_fd = -1;
41
l.yangfa05af32024-11-27 00:00:56 -080042//char syslog_buff[MAX_BUFFER_SIZE] = {0};
43//char radiolog_buff[MAX_BUFFER_SIZE] = {0};
l.yang250444d2024-11-12 00:35:10 -080044
l.yangfa05af32024-11-27 00:00:56 -080045char *radio_globalPtr = NULL;
46char *sys_globalPtr = NULL;
47
48int radio_len = 0;
49int sys_len = 0;
l.yang250444d2024-11-12 00:35:10 -080050
51static void signal_handler(int signum)
52{
l.yangd2c71bc2024-12-16 19:24:24 -080053 printf("Received SIGTERM signal, fflush buffer\n");
l.yangfa05af32024-11-27 00:00:56 -080054 if(fcntl(tmp_syslog_fd, F_GETFL) != -1 && sys_globalPtr !=NULL )
l.yang250444d2024-11-12 00:35:10 -080055 {
l.yangfa05af32024-11-27 00:00:56 -080056 if(write(tmp_syslog_fd, sys_globalPtr, sys_len) < 0)
l.yang250444d2024-11-12 00:35:10 -080057 {
58 perror("write error");
59 }
l.yangfa05af32024-11-27 00:00:56 -080060 // memset(syslog_buff,0,MAX_BUFFER_SIZE);
61 memset(sys_globalPtr, 0, MAX_BUFFER_SIZE);
62 sys_globalPtr =NULL;
l.yang250444d2024-11-12 00:35:10 -080063 }
b.liu3fb0ffe2024-11-12 19:24:43 +080064
l.yangfa05af32024-11-27 00:00:56 -080065 if(fcntl(tmp_rd_fd, F_GETFL) != -1 && radio_globalPtr !=NULL)
l.yang250444d2024-11-12 00:35:10 -080066 {
l.yangfa05af32024-11-27 00:00:56 -080067 if(write(tmp_rd_fd, radio_globalPtr, radio_len) < 0)
l.yang250444d2024-11-12 00:35:10 -080068 {
69 perror("write error");
70 }
l.yangfa05af32024-11-27 00:00:56 -080071
72 //memset(radiolog_buff,0,MAX_BUFFER_SIZE);
73 memset(radio_globalPtr, 0, MAX_BUFFER_SIZE);
74 radio_globalPtr =NULL;
75
l.yang250444d2024-11-12 00:35:10 -080076 }
b.liu3fb0ffe2024-11-12 19:24:43 +080077
l.yang250444d2024-11-12 00:35:10 -080078}
79
liubin281ac462023-07-19 14:22:54 +080080static void handler_free(log_config_entry* listdata)
81{
82 int i, n;
83 struct filter_list_t* _filter_list = NULL;
84 log_config_entry* entry;
85
86 for (i = 0; i < 5; i++) {
87 entry = listdata + i;
88 if (entry->name) {
l.yange4285f22024-11-13 00:30:41 -080089 //free(entry->name);
liubin281ac462023-07-19 14:22:54 +080090 if (entry->out_path) {
l.yange4285f22024-11-13 00:30:41 -080091 //free(entry->out_path);
liubin281ac462023-07-19 14:22:54 +080092 }
93 if (entry->ip) {
94 free(entry->ip);
95 }
96 _filter_list = entry->filter_list;
97 for (n = 0; n < 10; n++) {
b.liu9e8584b2024-11-06 19:21:28 +080098 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list);
liubin281ac462023-07-19 14:22:54 +080099 if (_filter_list) {
b.liu9e8584b2024-11-06 19:21:28 +0800100 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list->next);
l.yange4285f22024-11-13 00:30:41 -0800101 if (_filter_list->tag && _filter_list->tag[0] != '\0') {
liubin281ac462023-07-19 14:22:54 +0800102 free(_filter_list->tag);
103 }
b.liudeb8e422024-12-14 17:36:56 +0800104 struct filter_list_t* tmp_filter_list = _filter_list;
liubin281ac462023-07-19 14:22:54 +0800105 _filter_list = _filter_list->next;
b.liudeb8e422024-12-14 17:36:56 +0800106 free(tmp_filter_list);
liubin281ac462023-07-19 14:22:54 +0800107 } else {
108 break;
109 }
110 }
111 } else {
112 break;
113 }
114 }
115}
116
117int parse_config(log_config_entry* listdata)
118{
119 json_object* jsonobj = NULL;
120 json_object* tmpjson = NULL;
121 json_object* datajson = NULL;
122 json_object* listjson = NULL;
123 json_object* fileterjson = NULL;
124 json_object* fileter_listjson = NULL;
125 log_config_entry* entry;
xf.li43643772024-03-04 19:39:53 -0800126 int i, n, ret, array_length;
b.liu0f7ffad2024-11-06 19:57:27 +0800127 char* cmdval = NULL;
liubin281ac462023-07-19 14:22:54 +0800128
xf.li44e08692024-01-30 01:54:44 -0800129 printf("MBTK_LOGD: in parse_config\n");
liubin281ac462023-07-19 14:22:54 +0800130 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
131 if (NULL == jsonobj) {
132 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
133 return -1;
134 }
135 ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson);
136 if (!ret) {
137 printf("get jsondata error ...\n");
138 }
139
140 if (NULL == tmpjson) {
141 printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
142 }
143 /**获取total***/
b.liu0f7ffad2024-11-06 19:57:27 +0800144 cmdval = (char*)json_object_get_string(tmpjson);
liubin281ac462023-07-19 14:22:54 +0800145 printf("logd enable : %s\n", cmdval);
146 json_object_put(tmpjson);
xf.li44e08692024-01-30 01:54:44 -0800147
liubin281ac462023-07-19 14:22:54 +0800148 /***获取data***/
149 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
xf.li44e08692024-01-30 01:54:44 -0800150
liubin281ac462023-07-19 14:22:54 +0800151 for (i = 0 ; i < 5; i++) {
152 struct filter_list_t* _filter_list = NULL;
153 struct filter_list_t* tmp_filter_list = NULL;
154 datajson = json_object_array_get_idx(tmpjson, i);
155 if (NULL == datajson) {
156 mbtk_log("the datajson exit\n");
157 break;
158 }
159 entry = listdata + i;
160 json_object_object_get_ex(datajson, "enable", &listjson);
161 entry->enable = json_object_get_int(listjson);
162 mbtk_log("enable: %d\n", entry->enable);
l.yangd2c71bc2024-12-16 19:24:24 -0800163
liubin281ac462023-07-19 14:22:54 +0800164 json_object_object_get_ex(datajson, "name", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800165 entry->name = (char*)json_object_get_string(listjson);
b.liu9e8584b2024-11-06 19:21:28 +0800166 printf("cmdval: %s\n", entry->name);
liubin281ac462023-07-19 14:22:54 +0800167
168 json_object_object_get_ex(datajson, "log_file", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800169 entry->out_path = (char*)json_object_get_string(listjson);
liubin281ac462023-07-19 14:22:54 +0800170 mbtk_log("cmdval: %s\n", entry->out_path);
171
l.yangd2c71bc2024-12-16 19:24:24 -0800172 json_object_object_get_ex(datajson, "emmc_protect", &listjson);
173 emmc_pro = json_object_get_int(listjson);
174 printf("emmc_pro enable: %d\n", emmc_pro);
175
liubin281ac462023-07-19 14:22:54 +0800176 json_object_object_get_ex(datajson, "log_stream", &listjson);
177 if (listjson) {
b.liu0f7ffad2024-11-06 19:57:27 +0800178 entry->ip = (char*)json_object_get_string(listjson);
xf.li44e08692024-01-30 01:54:44 -0800179
liubin281ac462023-07-19 14:22:54 +0800180 cmdval = strstr(entry->ip, ":");
181 if (cmdval) {
182 entry->port = &cmdval[1];
183 cmdval[0] = 0;
184 mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port);
185 } else {
186 printf("Can't find port!!\n");
187 free(entry->ip);
188 entry->ip = NULL;
189 }
190 }
191 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
192 entry->rotate_file_size = json_object_get_int(listjson) * 1024;
193 mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size);
194
195 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
196 entry->rotate_file_count = json_object_get_int(listjson);
197 mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count);
198
199 json_object_object_get_ex(datajson, "filter_list", &listjson);
200 if (NULL == listjson) {
201 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
202 json_object_put(listjson);
203 continue;
204 }
205 entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
206 _filter_list = entry->filter_list;
207
xf.li43643772024-03-04 19:39:53 -0800208 array_length = json_object_array_length(listjson);
209 for (n = 0 ; n <= array_length; n++) {
liubin281ac462023-07-19 14:22:54 +0800210 fileterjson = json_object_array_get_idx(listjson, n);
211 if (NULL == fileterjson) {
212 mbtk_log("the fileterjson exit\n");
213 free(tmp_filter_list->next);
214 tmp_filter_list->next = NULL;
215 break;
216 }
217 memset(_filter_list, 0, sizeof(struct filter_list_t));
218 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800219 const char* str = json_object_get_string(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800220 if (str) {
221 _filter_list->priority = str[0];
222 mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
223 }
224 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
225 // if (NULL == fileter_listjson) {
226 // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
227 // }
228 str = json_object_get_string(fileter_listjson);
229 if (str) {
230 _filter_list->tag = strdup(str);
231 mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
232 }
xf.li43643772024-03-04 19:39:53 -0800233 else
234 {
235 _filter_list->tag = "\0";
236 }
xf.li44e08692024-01-30 01:54:44 -0800237
238 //json_object_put(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800239 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
240 if (NULL == _filter_list->next) {
241 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
242 break;
243 }
244 tmp_filter_list = _filter_list;
245 _filter_list = _filter_list->next;
246 }
liubin281ac462023-07-19 14:22:54 +0800247 }
liubin281ac462023-07-19 14:22:54 +0800248
249 /***释放json对象***/
250 json_object_put(jsonobj);
xf.li44e08692024-01-30 01:54:44 -0800251 printf("MBTK_LOGD: parse_config end\n");
liubin281ac462023-07-19 14:22:54 +0800252 return 0;
253}
254
255#define LOGD_PID "/var/run/mbtk_logd.pid"
b.liu0f7ffad2024-11-06 19:57:27 +0800256#if 0
liubin281ac462023-07-19 14:22:54 +0800257static int save_pid(void)
258{
259 pid_t process_id;
260 int fd, ret;
261 char buf[12] = {0};
262
263 process_id = getpid();
264 if(access(LOGD_PID, F_OK) == 0)
265 {
266 printf("mbtk_logd 进程已经存在\n");
267 return -1;
268 }
269 fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600);
270 if (fd < 0) {
271 fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno));
272 return -2;
273 }
274 snprintf(buf, sizeof(buf), "%d\n", process_id);
275 ret = write(fd, buf, strlen(buf));
276 close(fd);
277
278 if(ret > 0)
279 return 0;
280 else
281 return -2;
282}
b.liu0f7ffad2024-11-06 19:57:27 +0800283#endif
xf.li43643772024-03-04 19:39:53 -0800284
liubin281ac462023-07-19 14:22:54 +0800285int main(int argc, char* argv[])
286{
287 log_config_entry listdata[5];
288 pthread_t pid[5] = {0};
289 int i, ret;
290 void* tret;
l.yang250444d2024-11-12 00:35:10 -0800291 //struct filter_list_t* _filter_list = NULL;
xf.li44e08692024-01-30 01:54:44 -0800292
l.yang250444d2024-11-12 00:35:10 -0800293 signal(SIGTERM, signal_handler);
b.liubb590492024-06-13 16:42:08 +0800294#ifdef MBTK_DUMP_SUPPORT
295 mbtk_debug_open(NULL, TRUE);
296#endif
297
xf.li44e08692024-01-30 01:54:44 -0800298 int lock_file = open("/tmp/mbtk_logd.lock", O_CREAT|O_RDWR, 0666);
299 int rc = flock(lock_file,LOCK_EX|LOCK_NB);
300 if(rc) {
301 if(EWOULDBLOCK == errno) {
302 printf("Error: cannot restart the mbtk_logd repeatedly\n");
303 exit(0);
304 }
305 }
liubin281ac462023-07-19 14:22:54 +0800306
307 memset(listdata, 0, sizeof(log_config_entry) * 5);
liubin281ac462023-07-19 14:22:54 +0800308 ret = parse_config(listdata);
309 if (ret) {
310 return -1;
311 }
liubin281ac462023-07-19 14:22:54 +0800312
313 printf("logd %s start !\n", __FUNCTION__);
314
315 for (i = 0; i < 5; ++i) {
316 if (NULL == listdata[i].name) {
317 break;
318 }
319
320 if (0 == listdata[i].enable) {
321 printf("%s log disabled !\n", listdata[i].name);
322 continue;
323 }
324
325 if (0 == memcmp(listdata[i].name, "radio", 5)) {
326 ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]);
327 if (ret != 0) {
328 fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
329 }
330 } else if (0 == memcmp(listdata[i].name, "syslog", 6)) {
331 ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]);
332 if (ret != 0) {
333 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
334 }
335 } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) {
336 ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]);
337 if (ret != 0) {
338 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
339 }
xf.li44e08692024-01-30 01:54:44 -0800340 } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 8)) {
liubin281ac462023-07-19 14:22:54 +0800341 ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]);
342 if (ret != 0) {
343 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
344 }
345 }
346 }
347
b.liu5fa2fb52024-06-20 18:26:34 +0800348
liubin281ac462023-07-19 14:22:54 +0800349 for (i = 0; i < 5; ++i) {
350 if (NULL == listdata[i].name) {
351 break;
352 }
353 if (pid[i]) {
354 if (pthread_join(pid[i], &tret) != 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800355 printf("Join thread %d : %ld error!\n", i, pid[i]);
liubin281ac462023-07-19 14:22:54 +0800356 exit(1);
357 }
358 }
359 }
360 handler_free(listdata);
l.yange4285f22024-11-13 00:30:41 -0800361 sleep(MAX_SLEEP_TIME);
liubin281ac462023-07-19 14:22:54 +0800362 return 0;
363}