Add toolchain and mbtk source
Change-Id: Ie12546301367ea59240bf23d5e184ad7e36e40b3
diff --git a/mbtk/mbtk_logd/main.c b/mbtk/mbtk_logd/main.c
new file mode 100755
index 0000000..25dbdb1
--- /dev/null
+++ b/mbtk/mbtk_logd/main.c
@@ -0,0 +1,283 @@
+#include <stdio.h>
+#include <fcntl.h>
+#include <time.h>
+#include <stdio.h>
+#include <unistd.h>
+#include <sys/socket.h>
+#include <time.h>
+#include <errno.h>
+#include <stdlib.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include "json/json.h"
+#include "json/printbuf.h"
+#include "log_config.h"
+
+// #define DEBUG 1
+
+#ifdef DEBUG
+#define mbtk_log(...) printf(__VA_ARGS__)
+#else
+#define mbtk_log(...)
+#endif
+
+static void handler_free(log_config_entry* listdata)
+{
+ int i, n;
+ struct filter_list_t* _filter_list = NULL;
+ log_config_entry* entry;
+
+ for (i = 0; i < 5; i++) {
+ entry = listdata + i;
+ if (entry->name) {
+ free(entry->name);
+ if (entry->out_path) {
+ free(entry->out_path);
+ }
+ if (entry->ip) {
+ free(entry->ip);
+ }
+ _filter_list = entry->filter_list;
+ for (n = 0; n < 10; n++) {
+ printf("%s %d: malloc %x!\n", __FUNCTION__, __LINE__, _filter_list);
+ if (_filter_list) {
+ printf("%s %d: malloc %x!\n", __FUNCTION__, __LINE__, _filter_list->next);
+ if (_filter_list->tag) {
+ free(_filter_list->tag);
+ }
+ free(_filter_list);
+ _filter_list = _filter_list->next;
+ } else {
+ break;
+ }
+ }
+ } else {
+ break;
+ }
+ }
+}
+
+int parse_config(log_config_entry* listdata)
+{
+ json_object* jsonobj = NULL;
+ json_object* tmpjson = NULL;
+ json_object* datajson = NULL;
+ json_object* listjson = NULL;
+ json_object* fileterjson = NULL;
+ json_object* fileter_listjson = NULL;
+ log_config_entry* entry;
+ int i, n, ret;
+ char* cmdval = NULL;
+
+ jsonobj = json_object_from_file(LOG_CONFIG_PATH);
+ if (NULL == jsonobj) {
+ printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
+ return -1;
+ }
+ ret = json_object_object_get_ex(jsonobj, "logd", &tmpjson);
+ if (!ret) {
+ printf("get jsondata error ...\n");
+ }
+
+ if (NULL == tmpjson) {
+ printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
+ }
+ /**获取total***/
+ cmdval = json_object_get_string(tmpjson);
+ printf("logd enable : %s\n", cmdval);
+ json_object_put(tmpjson);
+ /***获取data***/
+ json_object_object_get_ex(jsonobj, "buffer_list", &tmpjson);
+ for (i = 0 ; i < 5; i++) {
+ struct filter_list_t* _filter_list = NULL;
+ struct filter_list_t* tmp_filter_list = NULL;
+ datajson = json_object_array_get_idx(tmpjson, i);
+ if (NULL == datajson) {
+ mbtk_log("the datajson exit\n");
+ break;
+ }
+ entry = listdata + i;
+ json_object_object_get_ex(datajson, "enable", &listjson);
+ entry->enable = json_object_get_int(listjson);
+ mbtk_log("enable: %d\n", entry->enable);
+
+ json_object_object_get_ex(datajson, "name", &listjson);
+ entry->name = strdup(json_object_get_string(listjson));
+ mbtk_log("cmdval: %x, %s\n", entry->name, entry->name);
+
+ json_object_object_get_ex(datajson, "log_file", &listjson);
+ entry->out_path = strdup(json_object_get_string(listjson));
+ mbtk_log("cmdval: %s\n", entry->out_path);
+
+ json_object_object_get_ex(datajson, "log_stream", &listjson);
+ if (listjson) {
+ entry->ip = strdup(json_object_get_string(listjson));
+ cmdval = strstr(entry->ip, ":");
+ if (cmdval) {
+ entry->port = &cmdval[1];
+ cmdval[0] = 0;
+ mbtk_log("cmdval: %s [%s]\n", entry->ip, entry->port);
+ } else {
+ printf("Can't find port!!\n");
+ free(entry->ip);
+ entry->ip = NULL;
+ }
+ }
+ json_object_object_get_ex(datajson, "rotate_file_size", &listjson);
+ entry->rotate_file_size = json_object_get_int(listjson) * 1024;
+ mbtk_log("rotate_file_size: %d\n", entry->rotate_file_size);
+
+ json_object_object_get_ex(datajson, "rotate_file_count", &listjson);
+ entry->rotate_file_count = json_object_get_int(listjson);
+ mbtk_log("rotate_file_count: %d\n", entry->rotate_file_count);
+
+ json_object_object_get_ex(datajson, "filter_list", &listjson);
+ if (NULL == listjson) {
+ printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
+ json_object_put(listjson);
+ continue;
+ }
+ entry->filter_list = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
+ _filter_list = entry->filter_list;
+
+ for (n = 0 ; n < 5; n++) {
+ fileterjson = json_object_array_get_idx(listjson, n);
+ if (NULL == fileterjson) {
+ mbtk_log("the fileterjson exit\n");
+ free(tmp_filter_list->next);
+ tmp_filter_list->next = NULL;
+ break;
+ }
+ memset(_filter_list, 0, sizeof(struct filter_list_t));
+ json_object_object_get_ex(fileterjson, "priority", &fileter_listjson);
+ char* str = json_object_get_string(fileter_listjson);
+ if (str) {
+ _filter_list->priority = str[0];
+ mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
+ }
+ json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
+ // if (NULL == fileter_listjson) {
+ // printf("%s %d: object failure!\n", __FUNCTION__, __LINE__);
+ // }
+ str = json_object_get_string(fileter_listjson);
+ if (str) {
+ _filter_list->tag = strdup(str);
+ mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
+ }
+ json_object_put(fileter_listjson);
+ _filter_list->next = (struct filter_list_t*)malloc(sizeof(struct filter_list_t));
+ if (NULL == _filter_list->next) {
+ printf("%s %d: malloc failure!\n", __FUNCTION__, __LINE__);
+ break;
+ }
+ tmp_filter_list = _filter_list;
+ _filter_list = _filter_list->next;
+ }
+ json_object_put(listjson);
+ }
+ json_object_put(tmpjson);
+
+ /***释放json对象***/
+ json_object_put(jsonobj);
+
+ return 0;
+}
+
+#define LOGD_PID "/var/run/mbtk_logd.pid"
+
+static int save_pid(void)
+{
+ pid_t process_id;
+ int fd, ret;
+ char buf[12] = {0};
+
+ process_id = getpid();
+ if(access(LOGD_PID, F_OK) == 0)
+ {
+ printf("mbtk_logd 进程已经存在\n");
+ return -1;
+ }
+ fd = open(LOGD_PID, O_CREAT | O_WRONLY, 0600);
+ if (fd < 0) {
+ fprintf(stderr, "failed to open %s: %s\n", LOGD_PID, strerror(errno));
+ return -2;
+ }
+ snprintf(buf, sizeof(buf), "%d\n", process_id);
+ ret = write(fd, buf, strlen(buf));
+ close(fd);
+
+ if(ret > 0)
+ return 0;
+ else
+ return -2;
+}
+
+int main(int argc, char* argv[])
+{
+ log_config_entry listdata[5];
+ pthread_t pid[5] = {0};
+ int i, ret;
+ void* tret;
+
+ memset(listdata, 0, sizeof(log_config_entry) * 5);
+
+ ret = parse_config(listdata);
+ if (ret) {
+ return -1;
+ }
+ if(0 != save_pid())
+ {
+ printf("%s %d: logd exit!\n", __FUNCTION__, __LINE__);
+ return -1;
+ }
+
+ printf("logd %s start !\n", __FUNCTION__);
+
+ for (i = 0; i < 5; ++i) {
+ if (NULL == listdata[i].name) {
+ break;
+ }
+
+ if (0 == listdata[i].enable) {
+ printf("%s log disabled !\n", listdata[i].name);
+ continue;
+ }
+
+ if (0 == memcmp(listdata[i].name, "radio", 5)) {
+ ret = pthread_create(&pid[i], NULL, alog_thread, &listdata[i]);
+ if (ret != 0) {
+ fprintf(stderr, "\n%s: Failed to create pthread\n", __FUNCTION__);
+ }
+ } else if (0 == memcmp(listdata[i].name, "syslog", 6)) {
+ ret = pthread_create(&pid[i], NULL, syslog_main, &listdata[i]);
+ if (ret != 0) {
+ fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
+ }
+ } else if (0 == memcmp(listdata[i].name, "local_socket", 12)) {
+ ret = pthread_create(&pid[i], NULL, socket_log_thread, &listdata[i]);
+ if (ret != 0) {
+ fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
+ }
+ } else if (0 == memcmp(listdata[i].name, "/dev/tty", 8)) {
+ ret = pthread_create(&pid[i], NULL, common_log_thread, &listdata[i]);
+ if (ret != 0) {
+ fprintf(stderr, "\n%s %d: Failed to create pthread\n", __FUNCTION__, __LINE__);
+ }
+ }
+ }
+
+ for (i = 0; i < 5; ++i) {
+ if (NULL == listdata[i].name) {
+ break;
+ }
+ if (pid[i]) {
+ if (pthread_join(pid[i], &tret) != 0) {
+ printf("Join thread %d : %d error!\n", i, pid[i]);
+ exit(1);
+ }
+ }
+ }
+ handler_free(listdata);
+
+ return 0;
+}