From d5a79879850ba357ee3e918fb1af53348b29ec7c Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 30 Jun 2021 06:35:06 +0000 Subject: [PATCH 1/6] get pid from socket Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/hilog_printf.cpp | 1 - frameworks/native/include/hilog_input_socket_server.h | 3 ++- services/hilogd/include/log_collector.h | 2 +- services/hilogd/log_collector.cpp | 4 +++- 4 files changed, 6 insertions(+), 4 deletions(-) diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index 81e291f..c3f1e2e 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -247,7 +247,6 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int int logLen = strnlen(buf, MAX_LOG_LEN - 1); header.type = type; header.level = level; - header.pid = getpid(); header.tid = syscall(SYS_gettid); header.domain = domain; diff --git a/frameworks/native/include/hilog_input_socket_server.h b/frameworks/native/include/hilog_input_socket_server.h index a63d419..ea45b4d 100644 --- a/frameworks/native/include/hilog_input_socket_server.h +++ b/frameworks/native/include/hilog_input_socket_server.h @@ -18,10 +18,11 @@ #include "dgram_socket_server.h" + namespace OHOS { namespace HiviewDFX { #define MAX_SOCKET_PACKET_LEN 4096 - +#define __RECV_MSG_WITH_UCRED_ class HilogInputSocketServer : public DgramSocketServer { public: #ifndef __RECV_MSG_WITH_UCRED_ diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 08d8e09..c89e236 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -26,7 +26,7 @@ public: LogCollector(HilogBuffer* buffer); void operator ()(); static size_t InsertLogToBuffer(const HilogMsg& msg); - static int onDataRecv(char *data, unsigned int dataLen); + static int onDataRecv(ucred cred, char *data, unsigned int dataLen); ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 2429001..a4dd2d1 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -26,11 +26,13 @@ namespace OHOS { namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; -int LogCollector::onDataRecv(char *data, unsigned int dataLen) +int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) { HilogMsg *msg = (HilogMsg *)data; + msg->pid = cred.pid; /* Domain flow control */ int ret = FlowCtrlDomain(msg); + if (ret < 0) { return 0; } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ -- Gitee From 9a2f3abf45eac6585cee37165454e95213df7317 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 30 Jun 2021 07:53:09 +0000 Subject: [PATCH 2/6] socket Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_collector.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index a4dd2d1..1003d74 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -29,10 +29,10 @@ HilogBuffer* LogCollector::hilogBuffer = nullptr; int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) { HilogMsg *msg = (HilogMsg *)data; - msg->pid = cred.pid; + /* Domain flow control */ int ret = FlowCtrlDomain(msg); - + msg->pid = cred.pid; if (ret < 0) { return 0; } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ -- Gitee From f032907a11d7178d78d7a695a04dca10f2936d9a Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 30 Jun 2021 08:01:54 +0000 Subject: [PATCH 3/6] get pid location Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_collector.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 1003d74..58bba87 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -29,10 +29,9 @@ HilogBuffer* LogCollector::hilogBuffer = nullptr; int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) { HilogMsg *msg = (HilogMsg *)data; - + msg->pid = cred.pid; /* Domain flow control */ int ret = FlowCtrlDomain(msg); - msg->pid = cred.pid; if (ret < 0) { return 0; } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ -- Gitee From 4e84665fcf27f981a4ba74bba7deb214021ff192 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 30 Jun 2021 12:58:27 +0000 Subject: [PATCH 4/6] delete unused code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/log_buffer.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/services/hilogd/log_buffer.cpp b/services/hilogd/log_buffer.cpp index fadacda..81c8280 100644 --- a/services/hilogd/log_buffer.cpp +++ b/services/hilogd/log_buffer.cpp @@ -55,7 +55,6 @@ HilogBuffer::~HilogBuffer() {} size_t HilogBuffer::Insert(const HilogMsg& msg) { - printf("insert: %s\n", CONTENT_PTR((&msg))); size_t eleSize = CONTENT_LEN((&msg)); /* include '\0' */ if (unlikely(msg.tag_len > MAX_TAG_LEN || msg.tag_len == 0 || eleSize > MAX_LOG_LEN || eleSize <= 0)) { -- Gitee From d088199502689a3d179643d7e71452e91d2a2e86 Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Wed, 30 Jun 2021 13:49:44 +0000 Subject: [PATCH 5/6] add two onDataRecv Signed-off-by: aajwy <13051180828@163.com> --- frameworks/native/BUILD.gn | 7 ++-- frameworks/native/hilog_printf.cpp | 3 ++ .../include/hilog_input_socket_server.h | 2 +- services/hilogd/BUILD.gn | 4 +- services/hilogd/include/log_collector.h | 4 ++ services/hilogd/log_collector.cpp | 40 +++++++++++++++++++ 6 files changed, 54 insertions(+), 6 deletions(-) diff --git a/frameworks/native/BUILD.gn b/frameworks/native/BUILD.gn index a889292..04fb382 100644 --- a/frameworks/native/BUILD.gn +++ b/frameworks/native/BUILD.gn @@ -38,8 +38,9 @@ ohos_source_set("libhilog_source") { ] defines = [ "HILOG_DEFAULT_PRIVACY=$ohos_hilog_default_privacy" ] - + defines += [ "__RECV_MSG_WITH_UCRED_" ] public_configs = [ ":libhilog_config" ] + configs = [ ":libhilog_config" ] deps = [ "//base/hiviewdfx/hilog/frameworks/native:libhilogutil", @@ -70,7 +71,7 @@ ohos_shared_library("libhilogutil") { ] public_configs = [ ":libhilogutil_config" ] configs = [ ":libhilogutil_config" ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter", "//utils/native/base:utilsecurec_shared", @@ -96,7 +97,7 @@ ohos_shared_library("libhilogutil_native") { ] public_configs = [ ":libhilogutil_config" ] configs = [ ":libhilogutil_config" ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] deps = [ "//base/hiviewdfx/hilog/adapter:libhilog_os_adapter_native", "//utils/native/base:utilsecurec_shared", diff --git a/frameworks/native/hilog_printf.cpp b/frameworks/native/hilog_printf.cpp index c3f1e2e..611aeea 100644 --- a/frameworks/native/hilog_printf.cpp +++ b/frameworks/native/hilog_printf.cpp @@ -248,6 +248,9 @@ int HiLogPrintArgs(const LogType type, const LogLevel level, const unsigned int header.type = type; header.level = level; header.tid = syscall(SYS_gettid); +#ifndef __RECV_MSG_WITH_UCRED_ + header.pid = getpid(); +#endif header.domain = domain; /* flow control */ diff --git a/frameworks/native/include/hilog_input_socket_server.h b/frameworks/native/include/hilog_input_socket_server.h index ea45b4d..3961fa9 100644 --- a/frameworks/native/include/hilog_input_socket_server.h +++ b/frameworks/native/include/hilog_input_socket_server.h @@ -22,7 +22,7 @@ namespace OHOS { namespace HiviewDFX { #define MAX_SOCKET_PACKET_LEN 4096 -#define __RECV_MSG_WITH_UCRED_ + class HilogInputSocketServer : public DgramSocketServer { public: #ifndef __RECV_MSG_WITH_UCRED_ diff --git a/services/hilogd/BUILD.gn b/services/hilogd/BUILD.gn index a0e4359..8adfb2f 100644 --- a/services/hilogd/BUILD.gn +++ b/services/hilogd/BUILD.gn @@ -33,7 +33,7 @@ ohos_executable("hilogd") { "log_reader.cpp", "main.cpp", ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] configs = [ ":hilogd_config" ] deps = [ @@ -65,7 +65,7 @@ ohos_executable("hilogd_native") { "log_reader.cpp", "main.cpp", ] - + defines = [ "__RECV_MSG_WITH_UCRED_" ] configs = [ ":hilogd_config" ] deps = [ diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index c89e236..3415180 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -26,7 +26,11 @@ public: LogCollector(HilogBuffer* buffer); void operator ()(); static size_t InsertLogToBuffer(const HilogMsg& msg); +#ifndef __RECV_MSG_WITH_UCRED_ + static int onDataRecv(char *data, unsigned int dataLen); +#else static int onDataRecv(ucred cred, char *data, unsigned int dataLen); +#endif ~LogCollector() = default; private: static HilogBuffer* hilogBuffer; diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index 58bba87..f790f3d 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -26,6 +26,45 @@ namespace OHOS { namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; + +#ifndef __RECV_MSG_WITH_UCRED_ +int LogCollector::onDataRecv(char *data, unsigned int dataLen) +{ + HilogMsg *msg = (HilogMsg *)data; + /* Domain flow control */ + int ret = FlowCtrlDomain(msg); + if (ret < 0) { + return 0; + } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ + string dropLog = to_string(ret) + " line(s) dropped!"; + char tag[] = "LOGLIMITD"; + int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; + HilogMsg *dropMsg = (HilogMsg*)malloc(len); + if (dropMsg != nullptr) { + dropMsg->len = len; + dropMsg->version = msg->version; + dropMsg->type = msg->type; + dropMsg->level = msg->level; + dropMsg->tag_len = sizeof(tag) + 1; + dropMsg->tv_sec = msg->tv_sec; + dropMsg->tv_nsec = msg->tv_nsec; + dropMsg->pid = msg->pid; + dropMsg->tid = msg->tid; + dropMsg->domain = msg->domain; + if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { + } + if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, + dropLog.c_str(), dropLog.size() + 1)) { + } + InsertLogToBuffer(*dropMsg); + free(dropMsg); + } + } + + InsertLogToBuffer(*msg); + return 0; +} +#else int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) { HilogMsg *msg = (HilogMsg *)data; @@ -63,6 +102,7 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) InsertLogToBuffer(*msg); return 0; } +#endif LogCollector::LogCollector(HilogBuffer* buffer) { -- Gitee From 123c7e154f0c98ca18fc8e923d5955917ce9cd5f Mon Sep 17 00:00:00 2001 From: aajwy <13051180828@163.com> Date: Fri, 2 Jul 2021 07:19:24 +0000 Subject: [PATCH 6/6] remove duplicated code Signed-off-by: aajwy <13051180828@163.com> --- services/hilogd/include/log_collector.h | 1 + services/hilogd/log_collector.cpp | 82 ++++++++++--------------- 2 files changed, 33 insertions(+), 50 deletions(-) diff --git a/services/hilogd/include/log_collector.h b/services/hilogd/include/log_collector.h index 3415180..3109a7d 100644 --- a/services/hilogd/include/log_collector.h +++ b/services/hilogd/include/log_collector.h @@ -25,6 +25,7 @@ class LogCollector { public: LogCollector(HilogBuffer* buffer); void operator ()(); + static int FlowCtrlDataRecv(HilogMsg *msg, int ret); static size_t InsertLogToBuffer(const HilogMsg& msg); #ifndef __RECV_MSG_WITH_UCRED_ static int onDataRecv(char *data, unsigned int dataLen); diff --git a/services/hilogd/log_collector.cpp b/services/hilogd/log_collector.cpp index f790f3d..313b2e5 100644 --- a/services/hilogd/log_collector.cpp +++ b/services/hilogd/log_collector.cpp @@ -27,6 +27,34 @@ namespace HiviewDFX { using namespace std; HilogBuffer* LogCollector::hilogBuffer = nullptr; +int LogCollector::FlowCtrlDataRecv(HilogMsg *msg, int ret) +{ + string dropLog = to_string(ret) + " line(s) dropped!"; + char tag[] = "LOGLIMITD"; + int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; + HilogMsg *dropMsg = (HilogMsg*)malloc(len); + if (dropMsg != nullptr) { + dropMsg->len = len; + dropMsg->version = msg->version; + dropMsg->type = msg->type; + dropMsg->level = msg->level; + dropMsg->tag_len = sizeof(tag) + 1; + dropMsg->tv_sec = msg->tv_sec; + dropMsg->tv_nsec = msg->tv_nsec; + dropMsg->pid = msg->pid; + dropMsg->tid = msg->tid; + dropMsg->domain = msg->domain; + if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { + } + if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, + dropLog.c_str(), dropLog.size() + 1)) { + } + InsertLogToBuffer(*dropMsg); + free(dropMsg); + } + return 0; +} + #ifndef __RECV_MSG_WITH_UCRED_ int LogCollector::onDataRecv(char *data, unsigned int dataLen) { @@ -36,32 +64,9 @@ int LogCollector::onDataRecv(char *data, unsigned int dataLen) if (ret < 0) { return 0; } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ - string dropLog = to_string(ret) + " line(s) dropped!"; - char tag[] = "LOGLIMITD"; - int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; - HilogMsg *dropMsg = (HilogMsg*)malloc(len); - if (dropMsg != nullptr) { - dropMsg->len = len; - dropMsg->version = msg->version; - dropMsg->type = msg->type; - dropMsg->level = msg->level; - dropMsg->tag_len = sizeof(tag) + 1; - dropMsg->tv_sec = msg->tv_sec; - dropMsg->tv_nsec = msg->tv_nsec; - dropMsg->pid = msg->pid; - dropMsg->tid = msg->tid; - dropMsg->domain = msg->domain; - if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { - } - if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, - dropLog.c_str(), dropLog.size() + 1)) { - } - InsertLogToBuffer(*dropMsg); - free(dropMsg); - } + FlowCtrlDataRecv(msg, ret); + InsertLogToBuffer(*msg); } - - InsertLogToBuffer(*msg); return 0; } #else @@ -74,32 +79,9 @@ int LogCollector::onDataRecv(ucred cred, char *data, unsigned int dataLen) if (ret < 0) { return 0; } else if (ret > 0) { /* if >0 !Need print how many lines was dopped */ - string dropLog = to_string(ret) + " line(s) dropped!"; - char tag[] = "LOGLIMITD"; - int len = sizeof(HilogMsg) + sizeof(tag) + 1 + dropLog.size() + 1; - HilogMsg *dropMsg = (HilogMsg*)malloc(len); - if (dropMsg != nullptr) { - dropMsg->len = len; - dropMsg->version = msg->version; - dropMsg->type = msg->type; - dropMsg->level = msg->level; - dropMsg->tag_len = sizeof(tag) + 1; - dropMsg->tv_sec = msg->tv_sec; - dropMsg->tv_nsec = msg->tv_nsec; - dropMsg->pid = msg->pid; - dropMsg->tid = msg->tid; - dropMsg->domain = msg->domain; - if (memcpy_s(dropMsg->tag, len - sizeof(HilogMsg), tag, sizeof(tag) + 1)) { - } - if (memcpy_s(dropMsg->tag + sizeof(tag) + 1, len - sizeof(HilogMsg) - sizeof(tag) - 1, - dropLog.c_str(), dropLog.size() + 1)) { - } - InsertLogToBuffer(*dropMsg); - free(dropMsg); - } + FlowCtrlDataRecv(msg, ret); + InsertLogToBuffer(*msg); } - - InsertLogToBuffer(*msg); return 0; } #endif -- Gitee