blob: ee3d49e03b5034c1d649e39fe061a00b069bef3c [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.yang250444d2024-11-12 00:35:10 -080034
35#define MAX_BUFFER_SIZE (8*1024)
36
37int tmp_syslog_fd = -1;
38int tmp_rd_fd = -1;
39
40char syslog_buff[MAX_BUFFER_SIZE] = {0};
41char radiolog_buff[MAX_BUFFER_SIZE] = {0};
42
43
44static 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.liu3fb0ffe2024-11-12 19:24:43 +080049 if(write(tmp_syslog_fd, syslog_buff, strlen(syslog_buff)) < 0)
l.yang250444d2024-11-12 00:35:10 -080050 {
51 perror("write error");
52 }
53 memset(syslog_buff,0,MAX_BUFFER_SIZE);
54 }
b.liu3fb0ffe2024-11-12 19:24:43 +080055
l.yang250444d2024-11-12 00:35:10 -080056 if(fcntl(tmp_rd_fd, F_GETFL) != -1 && strlen(radiolog_buff) > 0)
57 {
b.liu3fb0ffe2024-11-12 19:24:43 +080058 if(write(tmp_rd_fd, radiolog_buff, strlen(radiolog_buff)) < 0)
l.yang250444d2024-11-12 00:35:10 -080059 {
60 perror("write error");
61 }
62 memset(radiolog_buff,0,MAX_BUFFER_SIZE);
63 }
b.liu3fb0ffe2024-11-12 19:24:43 +080064
l.yang250444d2024-11-12 00:35:10 -080065}
66
liubin281ac462023-07-19 14:22:54 +080067static 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.liu9e8584b2024-11-06 19:21:28 +080085 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list);
liubin281ac462023-07-19 14:22:54 +080086 if (_filter_list) {
b.liu9e8584b2024-11-06 19:21:28 +080087 printf("%s %d: malloc %p!\n", __FUNCTION__, __LINE__, _filter_list->next);
liubin281ac462023-07-19 14:22:54 +080088 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
103int 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.li43643772024-03-04 19:39:53 -0800112 int i, n, ret, array_length;
b.liu0f7ffad2024-11-06 19:57:27 +0800113 char* cmdval = NULL;
liubin281ac462023-07-19 14:22:54 +0800114
xf.li44e08692024-01-30 01:54:44 -0800115 printf("MBTK_LOGD: in parse_config\n");
liubin281ac462023-07-19 14:22:54 +0800116 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.liu0f7ffad2024-11-06 19:57:27 +0800130 cmdval = (char*)json_object_get_string(tmpjson);
liubin281ac462023-07-19 14:22:54 +0800131 printf("logd enable : %s\n", cmdval);
132 json_object_put(tmpjson);
xf.li44e08692024-01-30 01:54:44 -0800133
liubin281ac462023-07-19 14:22:54 +0800134 /***获取data***/
135 json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
xf.li44e08692024-01-30 01:54:44 -0800136
liubin281ac462023-07-19 14:22:54 +0800137 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.liu0f7ffad2024-11-06 19:57:27 +0800151 entry->name = (char*)json_object_get_string(listjson);
b.liu9e8584b2024-11-06 19:21:28 +0800152 printf("cmdval: %s\n", entry->name);
liubin281ac462023-07-19 14:22:54 +0800153
154 json_object_object_get_ex(datajson, "log_file", &listjson);
b.liu0f7ffad2024-11-06 19:57:27 +0800155 entry->out_path = (char*)json_object_get_string(listjson);
liubin281ac462023-07-19 14:22:54 +0800156 mbtk_log("cmdval: %s\n", entry->out_path);
157
158 json_object_object_get_ex(datajson, "log_stream", &listjson);
159 if (listjson) {
b.liu0f7ffad2024-11-06 19:57:27 +0800160 entry->ip = (char*)json_object_get_string(listjson);
xf.li44e08692024-01-30 01:54:44 -0800161
liubin281ac462023-07-19 14:22:54 +0800162 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.li43643772024-03-04 19:39:53 -0800190 array_length = json_object_array_length(listjson);
191 for (n = 0 ; n <= array_length; n++) {
liubin281ac462023-07-19 14:22:54 +0800192 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.liu0f7ffad2024-11-06 19:57:27 +0800201 const char* str = json_object_get_string(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800202 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.li43643772024-03-04 19:39:53 -0800215 else
216 {
217 _filter_list->tag = "\0";
218 }
xf.li44e08692024-01-30 01:54:44 -0800219
220 //json_object_put(fileter_listjson);
liubin281ac462023-07-19 14:22:54 +0800221 _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 }
liubin281ac462023-07-19 14:22:54 +0800229 }
liubin281ac462023-07-19 14:22:54 +0800230
231 /***释放json对象***/
232 json_object_put(jsonobj);
xf.li44e08692024-01-30 01:54:44 -0800233 printf("MBTK_LOGD: parse_config end\n");
liubin281ac462023-07-19 14:22:54 +0800234 return 0;
235}
236
237#define LOGD_PID "/var/run/mbtk_logd.pid"
b.liu0f7ffad2024-11-06 19:57:27 +0800238#if 0
liubin281ac462023-07-19 14:22:54 +0800239static 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.liu0f7ffad2024-11-06 19:57:27 +0800265#endif
xf.li43643772024-03-04 19:39:53 -0800266
liubin281ac462023-07-19 14:22:54 +0800267int 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.yang250444d2024-11-12 00:35:10 -0800273 //struct filter_list_t* _filter_list = NULL;
xf.li44e08692024-01-30 01:54:44 -0800274
l.yang250444d2024-11-12 00:35:10 -0800275 signal(SIGTERM, signal_handler);
b.liubb590492024-06-13 16:42:08 +0800276#ifdef MBTK_DUMP_SUPPORT
277 mbtk_debug_open(NULL, TRUE);
278#endif
279
xf.li44e08692024-01-30 01:54:44 -0800280 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 }
liubin281ac462023-07-19 14:22:54 +0800288
289 memset(listdata, 0, sizeof(log_config_entry) * 5);
liubin281ac462023-07-19 14:22:54 +0800290 ret = parse_config(listdata);
291 if (ret) {
292 return -1;
293 }
liubin281ac462023-07-19 14:22:54 +0800294
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.li44e08692024-01-30 01:54:44 -0800322 } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 8)) {
liubin281ac462023-07-19 14:22:54 +0800323 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.liu5fa2fb52024-06-20 18:26:34 +0800330
liubin281ac462023-07-19 14:22:54 +0800331 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.liu0f7ffad2024-11-06 19:57:27 +0800337 printf("Join thread %d : %ld error!\n", i, pid[i]);
liubin281ac462023-07-19 14:22:54 +0800338 exit(1);
339 }
340 }
341 }
342 handler_free(listdata);
343
344 return 0;
345}