Fix mbtk_logd warning.
Change-Id: I3df4bbbfb0e8d865fd23e8beb0752b1226410cc9
diff --git a/mbtk/mbtk_logd/common.c b/mbtk/mbtk_logd/common.c
index 50177f1..000e680 100755
--- a/mbtk/mbtk_logd/common.c
+++ b/mbtk/mbtk_logd/common.c
@@ -82,7 +82,7 @@
while (pdst >= dst ) //当pdst大于dst则表明dst这个字符串还没有找完
{
- if (tmp=(char *)strstr(pdst, psrc = src)) //使用strstr帮助寻找,找到保存到tmp
+ if ((tmp=(char *)strstr(pdst, psrc = src))) //使用strstr帮助寻找,找到保存到tmp
return tmp;
pdst--;
@@ -190,12 +190,12 @@
*/
int get_rotate_file(int fd, char *base_file, struct file_list_t *_file_list)
{
- char old_name[32] = {0};
+ char old_name[32] = {0};
char new_name[32] = {0};
-
+
int rotate_file_count_max = 0;
-
+
if(_file_list->total == -1 || _file_list->total == 0 || _file_list->total > ROTATE_FILE_COUNT_MAX)
{
rotate_file_count_max = ROTATE_FILE_COUNT_MAX;
@@ -204,18 +204,18 @@
{
rotate_file_count_max = _file_list->total;
}
-
-
+
+
snprintf(new_name, sizeof(new_name), "%s.%d", base_file, rotate_file_count_max - 1);
remove(new_name);
- for (int i = rotate_file_count_max - 1; i >= 0; i--)
+ for (int i = rotate_file_count_max - 1; i >= 0; i--)
{
- if (i == 0)
+ if (i == 0)
{
snprintf(old_name, sizeof(old_name), "%s", base_file);
- }
- else
+ }
+ else
{
snprintf(old_name, sizeof(old_name), "%s.%d", base_file, i - 1);
}
@@ -225,7 +225,7 @@
close(fd);
int fd_new = open(base_file, O_CREAT | O_WRONLY | O_APPEND, 0600);
- if (fd_new < 0)
+ if (fd_new < 0)
{
fprintf(stderr, "failed to open %s: %s\n", base_file, strerror(errno));
exit(-1);
diff --git a/mbtk/mbtk_logd/common_read.c b/mbtk/mbtk_logd/common_read.c
index 90f2f7f..68eda60 100755
--- a/mbtk/mbtk_logd/common_read.c
+++ b/mbtk/mbtk_logd/common_read.c
@@ -9,17 +9,25 @@
#include <time.h>
#include <unistd.h>
#include <errno.h>
+#include <string.h>
+#include <stdlib.h>
+#include <pthread.h>
+
#include "log_config.h"
+#include "mbtk_utils.h"
#define ALOG_DEV "/dev/log_radio"
-static const char *log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
+static char *log_file;
static int log_size = 1 * 1024 * 1024;
static log_config_entry *config = NULL;
static char tmp_log[48] = {0};
+char *strstr_tail(const char *dst, const char *src);
+int tcp_connect(char* ip, int port);
+#if 0
static int fileter_log(int pri, char *tag, struct filter_list_t *filter)
{
struct filter_list_t *_filter = filter;
@@ -46,20 +54,21 @@
return -1;
}
+#endif
int common_log_print(
int fd,
struct file_list_t *_file_list,
char *entry)
{
- char priChar;
+// char priChar;
char timeBuf[32];
char defaultBuffer[512];
size_t totalLen;
int fd_new = fd;
struct stat s;
time_t timetemp; // 定义一个时间结构体变量
- char * ret = NULL;
+ int ret = 0;
if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
fd_new = get_rotate_file(fd_new, log_file, _file_list);
diff --git a/mbtk/mbtk_logd/log_config.h b/mbtk/mbtk_logd/log_config.h
index b3ca188..c37ce24 100755
--- a/mbtk/mbtk_logd/log_config.h
+++ b/mbtk/mbtk_logd/log_config.h
@@ -21,9 +21,9 @@
typedef struct log_config_entry_t {
int enable;
- const char *name;
+ char *name;
int send_fd;
- const char *out_path;
+ char *out_path;
char *ip;
char *port;
int rotate_file_size;
diff --git a/mbtk/mbtk_logd/main.c b/mbtk/mbtk_logd/main.c
index ef750e8..5dde3b8 100755
--- a/mbtk/mbtk_logd/main.c
+++ b/mbtk/mbtk_logd/main.c
@@ -10,6 +10,9 @@
#include <sys/file.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <string.h>
+#include <pthread.h>
+
#include "json-c/json.h"
#include "json-c/printbuf.h"
#include "log_config.h"
@@ -69,7 +72,7 @@
json_object* fileter_listjson = NULL;
log_config_entry* entry;
int i, n, ret, array_length;
- const char* cmdval = NULL;
+ char* cmdval = NULL;
printf("MBTK_LOGD: in parse_config\n");
jsonobj = json_object_from_file(LOG_CONFIG_PATH);
@@ -86,7 +89,7 @@
printf("the tmpjson : [%s]\n", json_object_to_json_string(tmpjson));
}
/**获取total***/
- cmdval = json_object_get_string(tmpjson);
+ cmdval = (char*)json_object_get_string(tmpjson);
printf("logd enable : %s\n", cmdval);
json_object_put(tmpjson);
@@ -107,16 +110,16 @@
mbtk_log("enable: %d\n", entry->enable);
json_object_object_get_ex(datajson, "name", &listjson);
- entry->name = json_object_get_string(listjson);
+ entry->name = (char*)json_object_get_string(listjson);
printf("cmdval: %s\n", entry->name);
json_object_object_get_ex(datajson, "log_file", &listjson);
- entry->out_path = json_object_get_string(listjson);
+ entry->out_path = (char*)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 = json_object_get_string(listjson);
+ entry->ip = (char*)json_object_get_string(listjson);
cmdval = strstr(entry->ip, ":");
if (cmdval) {
@@ -157,7 +160,7 @@
}
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);
+ const char* str = json_object_get_string(fileter_listjson);
if (str) {
_filter_list->priority = str[0];
mbtk_log("fileter_listjson: %c\n", _filter_list->priority);
@@ -194,7 +197,7 @@
}
#define LOGD_PID "/var/run/mbtk_logd.pid"
-
+#if 0
static int save_pid(void)
{
pid_t process_id;
@@ -221,7 +224,7 @@
else
return -2;
}
-
+#endif
int main(int argc, char* argv[])
{
@@ -229,7 +232,7 @@
pthread_t pid[5] = {0};
int i, ret;
void* tret;
- struct filter_list_t* _filter_list = NULL;
+// struct filter_list_t* _filter_list = NULL;
#ifdef MBTK_DUMP_SUPPORT
mbtk_debug_open(NULL, TRUE);
@@ -292,7 +295,7 @@
}
if (pid[i]) {
if (pthread_join(pid[i], &tret) != 0) {
- printf("Join thread %d : %d error!\n", i, pid[i]);
+ printf("Join thread %d : %ld error!\n", i, pid[i]);
exit(1);
}
}
diff --git a/mbtk/mbtk_logd/socket_read.c b/mbtk/mbtk_logd/socket_read.c
index df8fe27..7b54051 100755
--- a/mbtk/mbtk_logd/socket_read.c
+++ b/mbtk/mbtk_logd/socket_read.c
@@ -10,6 +10,9 @@
#include <time.h>
#include <unistd.h>
#include <errno.h>
+#include <stdlib.h>
+#include <pthread.h>
+
#include "log_config.h"
#define _MOPEN_RILD_CONNET_NUM 5
@@ -17,12 +20,15 @@
static int client_sockfd[_MOPEN_RILD_CONNET_NUM];
-static const char* log_file, *log_ip, *log_port, *log_prefix, *pid_file, *hostname;
+static char* log_file;
static int log_size = 1 * 1024 * 1024;
static log_config_entry* config = NULL;
static char tmp_log[48] = {0};
+int tcp_connect(char* ip, int port);
+char *strstr_tail(const char *dst, const char *src);
+#if 0
static int fileter_log(int pri, char* tag, struct filter_list_t* filter)
{
struct filter_list_t* _filter = filter;
@@ -49,20 +55,21 @@
return -1;
}
+#endif
int socket_log_print(
int fd,
struct file_list_t* _file_list,
char* entry)
{
- char priChar;
+// char priChar;
// char timeBuf[32];
// time_t timetemp; // 定义一个时间结构体变量
char defaultBuffer[512];
size_t totalLen;
int fd_new = fd;
struct stat s;
- char* ret = NULL;
+ int ret = 0;
if (log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size)) {
fd_new = get_rotate_file(fd_new, log_file, _file_list);
diff --git a/mbtk/mbtk_logd/syslog_read.c b/mbtk/mbtk_logd/syslog_read.c
index 94f16a8..bfc70ba 100755
--- a/mbtk/mbtk_logd/syslog_read.c
+++ b/mbtk/mbtk_logd/syslog_read.c
@@ -22,6 +22,8 @@
#include <sys/types.h>
#include <sys/un.h>
#include <sys/socket.h>
+#include <stdlib.h>
+#include <pthread.h>
#define SYSLOG_NAMES
#include <syslog.h>
@@ -35,6 +37,8 @@
#include "libubus.h"
#include "syslog.h"
#include "log_config.h"
+
+#include "mbtk_utils.h"
//#include "lynq/liblog.h"
enum {
@@ -59,7 +63,7 @@
LOG_TIME,
__LOG_MAX
};
-
+
#define SYSLOG_BUFF_SIZE (4*1024)
#define MAX_BUFFER_SIZE (8*1024)
@@ -80,7 +84,7 @@
static int log_size = 1 * 1024 * 1024, log_udp, log_follow = 0;
static struct file_list_t file_list;
static struct filter_list_t *filter_log = NULL;
-static char tmp_log[48] = {0};
+static char tmp_log[100] = {0};
pthread_t attr = -1;
static const char* getcodetext(int value, CODE *codetable) {
@@ -176,8 +180,8 @@
int len = 0;
struct blob_attr *tb[__LOG_MAX];
struct stat s;
- char buf[512] = {'\0'};
- char tmp_buf[48] = {0};
+ char buf[1024 * 2] = {'\0'};
+ char tmp_buf[1024] = {0};
uint32_t p;
char *str;
time_t t;
@@ -185,11 +189,11 @@
static char buffer[MAX_BUFFER_SIZE] = {0};
static int buffer_index = 0;
-
+
//
//sprintf(tmp_buf, "/tmp/log%s", strstr_tail(log_file, "/"));
-
+
snprintf(tmp_buf,sizeof(tmp_buf), "%s", log_file);
if(access(tmp_buf, W_OK) != 0)
{
@@ -200,9 +204,9 @@
return -1;
}
}
-
-
-
+
+
+
blobmsg_parse(log_policy, ARRAY_SIZE(log_policy), tb, blob_data(msg), blob_len(msg));
if (!tb[LOG_ID] || !tb[LOG_PRIO] || !tb[LOG_SOURCE] || !tb[LOG_TIME] || !tb[LOG_MSG])
return 1;
@@ -210,7 +214,7 @@
if ((log_type == LOG_FILE) && log_size && (!stat(tmp_log, &s)) && (s.st_size > log_size))
{
sender.fd = get_rotate_file(sender.fd, log_file, &file_list);
- if (sender.fd < 0)
+ if (sender.fd < 0)
{
fprintf(stderr, "failed to open %s: %s\n", tmp_log, strerror(errno));
exit(-1);
@@ -230,23 +234,23 @@
return 0;
//exit(-1);
}
- if (log_type == LOG_NET)
+ if (log_type == LOG_NET)
{
int err;
snprintf(buf, sizeof(buf), "<%u>", p);
strncat(buf, c + 4, 16);
if (strlen(hostname) > 0) {
- strncat(buf, hostname, sizeof(buf));
- strncat(buf, " ", sizeof(buf));
+ strncat(buf, hostname, strlen(hostname));
+ strncat(buf, " ", 1);
}
if (strlen(log_prefix) > 0) {
- strncat(buf, log_prefix, sizeof(buf));
- strncat(buf, ": ", sizeof(buf));
+ strncat(buf, log_prefix, strlen(log_prefix));
+ strncat(buf, ": ", 2);
}
if (blobmsg_get_u32(tb[LOG_SOURCE]) == SOURCE_KLOG)
- strncat(buf, "kernel: ", sizeof(buf));
- strncat(buf, m, sizeof(buf));
+ strncat(buf, "kernel: ", strlen("kernel: "));
+ strncat(buf, m, strlen(m));
if (log_udp)
err = write(sender.fd, buf, strlen(buf));
else
@@ -260,8 +264,8 @@
sender.fd = -1;
uloop_timeout_set(&retry, 1000);
}
- }
- else
+ }
+ else
{
snprintf(buf, sizeof(buf), "%s %s.%s%s %s\n",
c, getcodetext(LOG_FAC(p) << 3, facilitynames), getcodetext(LOG_PRI(p), prioritynames),
@@ -275,32 +279,32 @@
buf[index] ^= 1;
}
}
-
-
+
+
if (buffer_index >= SYSLOG_BUFF_SIZE)
{
// Flush the buffer if it's full
- if (write(sender.fd, buffer, buffer_index) < 0)
+ if (write(sender.fd, buffer, buffer_index) < 0)
{
perror("write error");
return -1;
}
buffer_index = 0; // Reset buffer index after flushing
}
-
+
if(len < SYSLOG_BUFF_SIZE)
{
- //copy buf to buffer
+ //copy buf to buffer
memcpy(buffer + buffer_index, buf, len);
buffer_index += len;
}
else
{
- write(sender.fd, buf, len);
+ mbtk_write(sender.fd, buf, len);
}
-
-
+
+
}
free(str);
@@ -352,7 +356,7 @@
int n;
int array_length;
- char* tmp_string = NULL;
+// char* tmp_string = NULL;
jsonobj = json_object_from_file(LOG_CONFIG_PATH);
if (NULL == jsonobj) {
@@ -386,14 +390,14 @@
}
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);
+ const char* str = json_object_get_string(fileter_listjson);
if (str) {
_filter_list->priority = str[0];
printf("fileter_listjson: %c\n", _filter_list->priority);
}
json_object_object_get_ex(fileterjson, "tag", &fileter_listjson);
-
+
str = json_object_get_string(fileter_listjson);
if (str) {
_filter_list->tag = strdup(str);
@@ -424,16 +428,17 @@
free(tmp_filter_list);
tmp_filter_list = _filter_list->next;
}
-
+
return 0;
}
-int wait_update_log_level()
+void* wait_update_log_level(void *arg)
{
- int i = 0;
+// int i = 0;
char recvBuff[100];
- int serverFd,clientFd,addrLen;
+ int serverFd,clientFd;
+ socklen_t addrLen;
struct sockaddr_un serverAddr,clientAddr;
pthread_detach(pthread_self());
@@ -448,20 +453,20 @@
if ((serverFd = socket(AF_UNIX,SOCK_STREAM,0)) < 0)
{
printf("err -1\n");
- return -1;
+ return NULL;
}
if (bind(serverFd,(struct sockaddr *)&serverAddr,sizeof(serverAddr)) < 0)
{
printf("err -2\n");
close(serverFd);
- return -2;
+ return NULL;
}
if(listen(serverFd,10) < 0)
{
printf("err -3\n");
- return -3;
+ return NULL;
}
while(1)
@@ -491,13 +496,13 @@
}
close(serverFd);
- return 0;
+ return NULL;
}
int syslog_pthread_create()
{
int ret;
-
+
ret = pthread_create(&attr, NULL, wait_update_log_level, NULL);
if (ret < 0)
@@ -515,7 +520,7 @@
struct ubus_context *ctx;
uint32_t id;
const char *ubus_socket = NULL;
- int ch, ret, lines = 0;
+ int ret, lines = 0;
static struct blob_buf b;
int tries = 60;
void* tret;
@@ -524,8 +529,8 @@
pthread_detach(pthread_self());
- if (NULL == argv)
- return NULL;
+// if (NULL == argv)
+// return NULL;
signal(SIGPIPE, SIG_IGN);
uloop_init();
@@ -566,7 +571,7 @@
ctx = ubus_connect(ubus_socket);
if (!ctx) {
fprintf(stderr, "Failed to connect to ubus\n");
- return -1;
+ return NULL;
}
ubus_add_uloop(ctx);
@@ -603,12 +608,12 @@
} 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, "/"));
-
+
//直接将log文件存储在不会掉电丢失的路径
snprintf(tmp_log,sizeof(tmp_log), "%s",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));
@@ -627,10 +632,10 @@
uloop_done();
} while (ret && tries--);
-
+
if (attr) {
if (pthread_join(attr, &tret) != 0) {
- printf("MBTK:Join thread: %d error!\n", attr);
+ printf("MBTK:Join thread: %ld error!\n", attr);
exit(1);
}
}