blob: a4fb7f34d0f7bd518e70324b4702fd829a961bd1 [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>
15
b.liub3b923a2024-06-06 15:15:49 +080016#include "json-c/json.h"
17#include "json-c/printbuf.h"
liubin281ac462023-07-19 14:22:54 +080018#include "log_config.h"
19
b.liu5fa2fb52024-06-20 18:26:34 +080020#include "mbtk_type.h"
xf.li43643772024-03-04 19:39:53 -080021//#define DEBUG 1
liubin281ac462023-07-19 14:22:54 +080022
l.yang250444d2024-11-12 00:35:10 -080023#include <signal.h>
24
liubin281ac462023-07-19 14:22:54 +080025#ifdef DEBUG
26#define mbtk_log(...) printf(__VA_ARGS__)
27#else
28#define mbtk_log(...)
29#endif
30
l.yang250444d2024-11-12 00:35:10 -080031
32#define MAX_BUFFER_SIZE (8*1024)
33
34int tmp_syslog_fd = -1;
35int tmp_rd_fd = -1;
36
37char syslog_buff[MAX_BUFFER_SIZE] = {0};
38char radiolog_buff[MAX_BUFFER_SIZE] = {0};
39
40
41static void signal_handler(int signum)
42{
43 printf("Received SIGTERM signal, fflush buffer");
44 if(fcntl(tmp_syslog_fd, F_GETFL) != -1 && strlen(syslog_buff) > 0)
45 {
46 if(write(tmp_syslog_fd, syslog_buff, strlen(syslog_buff)) < 0)
47 {
48 perror("write error");
49 }
50 memset(syslog_buff,0,MAX_BUFFER_SIZE);
51 }
52
53 if(fcntl(tmp_rd_fd, F_GETFL) != -1 && strlen(radiolog_buff) > 0)
54 {
55 if(write(tmp_rd_fd, radiolog_buff, strlen(radiolog_buff)) < 0)
56 {
57 perror("write error");
58 }
59 memset(radiolog_buff,0,MAX_BUFFER_SIZE);
60 }
61
62}
63
liubin281ac462023-07-19 14:22:54 +080064static void handler_free(log_config_entry* listdata)
65{
66 int i, n;
67 struct filter_list_t* _filter_list = NULL;
68 log_config_entry* entry;
69
70 for (i = 0; i < 5; i++) {
71 entry = listdata + i;
72 if (entry->name) {
73 free(entry->name);
74 if (entry->out_path) {
75 free(entry->out_path);
76 }
77 if (entry->ip) {
78 free(entry->ip);
79 }
80 _filter_list = entry->filter_list;
81 for (n = 0; n < 10; n++) {
b.liu9e8584b2024-11-06 19:21:28 +080082 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list);
liubin281ac462023-07-19 14:22:54 +080083 if (_filter_list) {
b.liu9e8584b2024-11-06 19:21:28 +080084 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list->next);
liubin281ac462023-07-19 14:22:54 +080085 if (_filter_list->tag) {
86 free(_filter_list->tag);
87 }
88 free(_filter_list);
89 _filter_list = _filter_list->next;
90 } else {
91 break;
92 }
93 }
94 } else {
95 break;
96 }
97 }
98}
99
100int parse_config(log_config_entry* listdata)
101{
102 json_object* jsonobj = NULL;
103 json_object* tmpjson = NULL;
104 json_object* datajson = NULL;
105 json_object* listjson = NULL;
106 json_object* fileterjson = NULL;
107 json_object* fileter_listjson = NULL;
108 log_config_entry* entry;
xf.li43643772024-03-04 19:39:53 -0800109 int i, n, ret, array_length;
b.liu0f7ffad2024-11-06 19:57:27 +0800110 char* cmdval = NULL;
liubin281ac462023-07-19 14:22:54 +0800111
xf.li44e08692024-01-30 01:54:44 -0800112 printf("MBTK_LOGD: in parse_config\n");
liubin281ac462023-07-19 14:22:54 +0800113 jsonobj = json_object_from_file(LOG_CONFIG_PATH);
114 if (NULL == jsonobj) {
115 printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
116 return -1;
117 }
118 ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson);
119 if (!ret) {
120 printf("get jsondata error ...\n");
121 }
122
123 if (NULL == tmpjson) {
124 printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
125 }
126 /**获取total***/
b.liu0f7ffad2024-11-06 19:57:27 +0800127 cmdval = (char*)json_object_get_string(tmpjson);
liubin281ac462023-07-19 14:22:54 +0800128 printf("logd enable : %s\n", cmdval);
129 json_object_put(tmpjson);
xf.li44e08692024-01-30 01:54:44 -0800130
liubin281ac462023-07-19 14:22:54 +0800131 /***获取data***/
132 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
xf.li44e08692024-01-30 01:54:44 -0800133
liubin281ac462023-07-19 14:22:54 +0800134 for (i = 0 ; i < 5; i++) {
135 struct filter_list_t* _filter_list = NULL;
136 struct filter_list_t* tmp_filter_list = NULL;
137 datajson = json_object_array_get_idx(tmpjson, i);
138 if (NULL == datajson) {
139 mbtk_log("the datajson exit\n");
140 break;
141 }
142 entry = listdata + i;
143 json_object_object_get_ex(datajson, "enable", &listjson);
144 entry->enable = json_object_get_int(listjson);
145 mbtk_log("enable: %d\n", entry->enable);
146
147 json_object_object_get_ex(datajson, "name", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800148 entry->name = (char*)json_object_get_string(listjson);
b.liu9e8584b2024-11-06 19:21:28 +0800149 printf("cmdval: %s\n", entry->name);
liubin281ac462023-07-19 14:22:54 +0800150
151 json_object_object_get_ex(datajson, "log_file", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800152 entry->out_path = (char*)json_object_get_string(listjson);
liubin281ac462023-07-19 14:22:54 +0800153 mbtk_log("cmdval: %s\n", entry->out_path);
154
155 json_object_object_get_ex(datajson, "log_stream", &listjson);
156 if (listjson) {
b.liu0f7ffad2024-11-06 19:57:27 +0800157 entry->ip = (char*)json_object_get_string(listjson);
xf.li44e08692024-01-30 01:54:44 -0800158
liubin281ac462023-07-19 14:22:54 +0800159 cmdval = strstr(entry->ip, ":");
160 if (cmdval) {
161 entry->port = &cmdval[1];
162 cmdval[0] = 0;
163 mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port);
164 } else {
165 printf("Can't find port!!\n");
166 free(entry->ip);
167 entry->ip = NULL;
168 }
169 }
170 json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
171 entry->rotate_file_size = json_object_get_int(listjson) * 1024;
172 mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size);
173
174 json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
175 entry->rotate_file_count = json_object_get_int(listjson);
176 mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count);
177
178 json_object_object_get_ex(datajson, "filter_list", &listjson);
179 if (NULL == listjson) {
180 printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
181 json_object_put(listjson);
182 continue;
183 }
184 entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
185 _filter_list = entry->filter_list;
186
xf.li43643772024-03-04 19:39:53 -0800187 array_length = json_object_array_length(listjson);
188 for (n = 0 ; n <= array_length; n++) {
liubin281ac462023-07-19 14:22:54 +0800189 fileterjson = json_object_array_get_idx(listjson, n);
190 if (NULL == fileterjson) {
191 mbtk_log("the fileterjson exit\n");
192 free(tmp_filter_list->next);
193 tmp_filter_list->next = NULL;
194 break;
195 }
196 memset(_filter_list, 0, sizeof(struct filter_list_t));
197 json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800198 const char* str = json_object_get_string(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800199 if (str) {
200 _filter_list->priority = str[0];
201 mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
202 }
203 json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
204 // if (NULL == fileter_listjson) {
205 // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
206 // }
207 str = json_object_get_string(fileter_listjson);
208 if (str) {
209 _filter_list->tag = strdup(str);
210 mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
211 }
xf.li43643772024-03-04 19:39:53 -0800212 else
213 {
214 _filter_list->tag = "\0";
215 }
xf.li44e08692024-01-30 01:54:44 -0800216
217 //json_object_put(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800218 _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
219 if (NULL == _filter_list->next) {
220 printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
221 break;
222 }
223 tmp_filter_list = _filter_list;
224 _filter_list = _filter_list->next;
225 }
liubin281ac462023-07-19 14:22:54 +0800226 }
liubin281ac462023-07-19 14:22:54 +0800227
228 /***释放json对象***/
229 json_object_put(jsonobj);
xf.li44e08692024-01-30 01:54:44 -0800230 printf("MBTK_LOGD: parse_config end\n");
liubin281ac462023-07-19 14:22:54 +0800231 return 0;
232}
233
234#define LOGD_PID "/var/run/mbtk_logd.pid"
b.liu0f7ffad2024-11-06 19:57:27 +0800235#if 0
liubin281ac462023-07-19 14:22:54 +0800236static int save_pid(void)
237{
238 pid_t process_id;
239 int fd, ret;
240 char buf[12] = {0};
241
242 process_id = getpid();
243 if(access(LOGD_PID, F_OK) == 0)
244 {
245 printf("mbtk_logd 进程已经存在\n");
246 return -1;
247 }
248 fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600);
249 if (fd < 0) {
250 fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno));
251 return -2;
252 }
253 snprintf(buf, sizeof(buf), "%d\n", process_id);
254 ret = write(fd, buf, strlen(buf));
255 close(fd);
256
257 if(ret > 0)
258 return 0;
259 else
260 return -2;
261}
b.liu0f7ffad2024-11-06 19:57:27 +0800262#endif
xf.li43643772024-03-04 19:39:53 -0800263
liubin281ac462023-07-19 14:22:54 +0800264int main(int argc, char* argv[])
265{
266 log_config_entry listdata[5];
267 pthread_t pid[5] = {0};
268 int i, ret;
269 void* tret;
l.yang250444d2024-11-12 00:35:10 -0800270 //struct filter_list_t* _filter_list = NULL;
xf.li44e08692024-01-30 01:54:44 -0800271
l.yang250444d2024-11-12 00:35:10 -0800272 signal(SIGTERM, signal_handler);
b.liubb590492024-06-13 16:42:08 +0800273#ifdef MBTK_DUMP_SUPPORT
274 mbtk_debug_open(NULL, TRUE);
275#endif
276
xf.li44e08692024-01-30 01:54:44 -0800277 int lock_file = open("/tmp/mbtk_logd.lock", O_CREAT|O_RDWR, 0666);
278 int rc = flock(lock_file,LOCK_EX|LOCK_NB);
279 if(rc) {
280 if(EWOULDBLOCK == errno) {
281 printf("Error: cannot restart the mbtk_logd repeatedly\n");
282 exit(0);
283 }
284 }
liubin281ac462023-07-19 14:22:54 +0800285
286 memset(listdata, 0, sizeof(log_config_entry) * 5);
liubin281ac462023-07-19 14:22:54 +0800287 ret = parse_config(listdata);
288 if (ret) {
289 return -1;
290 }
liubin281ac462023-07-19 14:22:54 +0800291
292 printf("logd %s start !\n", __FUNCTION__);
293
294 for (i = 0; i < 5; ++i) {
295 if (NULL == listdata[i].name) {
296 break;
297 }
298
299 if (0 == listdata[i].enable) {
300 printf("%s log disabled !\n", listdata[i].name);
301 continue;
302 }
303
304 if (0 == memcmp(listdata[i].name, "radio", 5)) {
305 ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]);
306 if (ret != 0) {
307 fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
308 }
309 } else if (0 == memcmp(listdata[i].name, "syslog", 6)) {
310 ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]);
311 if (ret != 0) {
312 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
313 }
314 } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) {
315 ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]);
316 if (ret != 0) {
317 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
318 }
xf.li44e08692024-01-30 01:54:44 -0800319 } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 8)) {
liubin281ac462023-07-19 14:22:54 +0800320 ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]);
321 if (ret != 0) {
322 fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
323 }
324 }
325 }
326
b.liu5fa2fb52024-06-20 18:26:34 +0800327
liubin281ac462023-07-19 14:22:54 +0800328 for (i = 0; i < 5; ++i) {
329 if (NULL == listdata[i].name) {
330 break;
331 }
332 if (pid[i]) {
333 if (pthread_join(pid[i], &tret) != 0) {
b.liu0f7ffad2024-11-06 19:57:27 +0800334 printf("Join thread %d : %ld error!\n", i, pid[i]);
liubin281ac462023-07-19 14:22:54 +0800335 exit(1);
336 }
337 }
338 }
339 handler_free(listdata);
340
341 return 0;
342}