add T108 mbtk_logd and liblog

Change-Id: Ib486a63a423e22ca07b3568a1b3c856b23e63ea3
diff --git a/mbtk/mbtk_logd/common.c b/mbtk/mbtk_logd/common.c
index 1e293b6..5b2de92 100755
--- a/mbtk/mbtk_logd/common.c
+++ b/mbtk/mbtk_logd/common.c
@@ -226,6 +226,7 @@
             sprintf(old, "%s.%02d", old, num);
         }
     }
+    printf("MBTK_LOGD: tmp_buf is %s, old is %s\n", tmp_buf, old);
     if (movefile(tmp_buf, old) != 0){
         printf("%s %d: Renamed error ,%s to %s.\n", __FUNCTION__, __LINE__, tmp_buf, old);
     }
diff --git a/mbtk/mbtk_logd/main.c b/mbtk/mbtk_logd/main.c
index 25dbdb1..e47bdca 100755
--- a/mbtk/mbtk_logd/main.c
+++ b/mbtk/mbtk_logd/main.c
@@ -7,6 +7,7 @@
 #include <time.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <sys/file.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 #include "json/json.h"
@@ -69,6 +70,7 @@
     int i, n, ret;
     char* cmdval = NULL;
 
+    printf("MBTK_LOGD: in parse_config\n");
     jsonobj = json_object_from_file(LOG_CONFIG_PATH);
     if (NULL == jsonobj) {
         printf("Can't open config file: %s\n", LOG_CONFIG_PATH);
@@ -86,8 +88,10 @@
     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;
@@ -102,16 +106,17 @@
         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);
+        entry->name = json_object_get_string(listjson);
+        printf("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));
+        entry->out_path = 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));
+            entry->ip = json_object_get_string(listjson);
+
             cmdval = strstr(entry->ip, ":");
             if (cmdval) {
                 entry->port = &cmdval[1];
@@ -164,7 +169,8 @@
                 _filter_list->tag = strdup(str);
                 mbtk_log("fileter_listjson: %s\n", _filter_list->tag);
             }
-            json_object_put(fileter_listjson);
+
+            //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__);
@@ -173,13 +179,11 @@
             tmp_filter_list = _filter_list;
             _filter_list = _filter_list->next;
         }
-        json_object_put(listjson);
     }
-    json_object_put(tmpjson);
 
     /***释放json对象***/
     json_object_put(jsonobj);
-
+    printf("MBTK_LOGD: parse_config end\n");
     return 0;
 }
 
@@ -218,18 +222,22 @@
     pthread_t pid[5] = {0};
     int i, ret;
     void* tret;
+    struct filter_list_t* _filter_list = NULL;
+
+    int lock_file = open("/tmp/mbtk_logd.lock", O_CREAT|O_RDWR, 0666);
+    int rc = flock(lock_file,LOCK_EX|LOCK_NB);
+    if(rc) {
+        if(EWOULDBLOCK == errno) {
+            printf("Error: cannot restart the mbtk_logd repeatedly\n");
+            exit(0);
+        }
+    }
 
     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__);
 
@@ -258,7 +266,7 @@
             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)) {
+        } else if (0 == memcmp(listdata[i].name, "/dev/ttyS0", 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__);
@@ -266,6 +274,7 @@
         }
     }
 
+    
     for (i = 0; i < 5; ++i) {
         if (NULL == listdata[i].name) {
             break;
diff --git a/mbtk/mbtk_logd/syslog_read.c b/mbtk/mbtk_logd/syslog_read.c
index 3cae9e9..c52a6ee 100755
--- a/mbtk/mbtk_logd/syslog_read.c
+++ b/mbtk/mbtk_logd/syslog_read.c
@@ -33,6 +33,7 @@
 #include "syslog.h"
 #include "log_config.h"
 
+#define LOG_CONFIG_LEN 50
 enum {
 	LOG_STDOUT,
 	LOG_FILE,
@@ -58,7 +59,8 @@
 
 static struct uloop_timeout retry;
 static struct uloop_fd sender;
-static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
+//static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
+static char log_file[LOG_CONFIG_LEN], log_ip[LOG_CONFIG_LEN], log_port[LOG_CONFIG_LEN], log_prefix[LOG_CONFIG_LEN], pid_file[LOG_CONFIG_LEN], hostname[LOG_CONFIG_LEN];
 static int log_type = LOG_STDOUT;
 static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
 static struct file_list_t file_list;
@@ -184,11 +186,11 @@
 
 		snprintf(buf, sizeof(buf), "<%u>", p);
 		strncat(buf, c + 4, 16);
-		if (hostname) {
+		if (strlen(hostname) > 0) {
 			strncat(buf, hostname, sizeof(buf));
 			strncat(buf, " ", sizeof(buf));
 		}
-		if (log_prefix) {
+		if (strlen(log_prefix) > 0) {
 			strncat(buf, log_prefix, sizeof(buf));
 			strncat(buf, ": ", sizeof(buf));
 		}
@@ -258,7 +260,7 @@
 	const char *ubus_socket = NULL;
 	int ch, ret, lines = 0;
 	static struct blob_buf b;
-	int tries = 5;
+	int tries = 60;
     log_config_entry *config = (log_config_entry *)argv;
 
     pthread_detach(pthread_self());
@@ -269,7 +271,19 @@
 	signal(SIGPIPE, SIG_IGN);
 	uloop_init();
 
-    log_file = config->out_path;
+    //log_file = config->out_path;
+	memset(log_file, 0, sizeof(log_file));
+	memset(log_ip, 0, sizeof(log_ip));
+	memset(log_port, 0, sizeof(log_port));
+	memset(log_prefix, 0, sizeof(log_prefix));
+	memset(pid_file, 0, sizeof(pid_file));
+	memset(hostname, 0, sizeof(hostname));
+
+	if(config->out_path != NULL)
+	{
+		strncpy(log_file, config->out_path, LOG_CONFIG_LEN - 1);
+	}
+
     memset(&file_list, 0, sizeof(struct file_list_t));
     file_list.total = config->rotate_file_count;
     if(config->rotate_file_size)
@@ -277,8 +291,13 @@
     if(config->ip)
     {
         printf("%s %d : %s:%s\n", __FUNCTION__, __LINE__, config->ip, config->port);
-        log_ip = config->ip;
-        log_port = config->port;
+        //log_ip = config->ip;
+        strncpy(log_ip, config->ip, LOG_CONFIG_LEN - 1);
+        //log_port = config->port;
+        if(config->port != NULL)
+        {
+            strncpy(log_port, config->port, LOG_CONFIG_LEN - 1);
+        }
     }
     filter_log = config->filter_list;
     // Follow log messages
@@ -299,14 +318,13 @@
 			sleep(1);
 			continue;
 		}
-
 		blob_buf_init(&b, 0);
 		if (lines)
 			blobmsg_add_u32(&b, "lines", lines);
 		else if (log_follow)
 			blobmsg_add_u32(&b, "lines", 0);
 		if (log_follow) {
-			if (pid_file) {
+			if (strlen(pid_file) > 0) {
 				FILE *fp = fopen(pid_file, "w+");
 				if (fp) {
 					fprintf(fp, "%d", getpid());
@@ -315,16 +333,16 @@
 			}
 		}
 
-		if (log_ip && log_port) {
+		if (strlen(log_ip) > 0 && strlen(log_port) > 0) {
 			openlog("logread", LOG_PID, LOG_DAEMON);
 			log_type = LOG_NET;
 			sender.cb = log_handle_fd;
 			retry.cb = log_handle_reconnect;
 			uloop_timeout_set(&retry, 1000);
-		} else if (log_file) {
+		} else if (strlen(log_file) > 0) {
 			log_type = LOG_FILE;
             // 先将文件保存到 /tmp/log/ 目录下,后面到达 rotate_file_size 后,转移到out_path
-            sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
+			sprintf(tmp_log, "/tmp/log%s", strstr_tail(log_file, "/"));
 			sender.fd = open(tmp_log, O_CREAT | O_WRONLY| O_APPEND, 0600);
 			if (sender.fd < 0) {
 				fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
@@ -338,7 +356,7 @@
 		req.fd_cb = logread_fd_cb;
 		req.complete_cb = logread_complete_cb;
 		ubus_complete_request_async(ctx, &req);
-
+		
 		uloop_run();
 		ubus_free(ctx);
 		uloop_done();