Merge branch 'openkylin/yangtze' into debian/unstable

This commit is contained in:
xibowen 2023-12-13 15:03:25 +08:00
commit c7fbc58946
10 changed files with 492 additions and 21 deletions

View File

@ -0,0 +1,5 @@
Name: libkysdk-diagnostics
Description: kysdk base layer diagnostics component
Version: 2.0.0
Cflags: -I/usr/include/kysdk/kysdk-base/
Libs: -L/usr/lib/kysdk/kysdk-base/ -lkydiagnostics -Wl,-rpath=/usr/lib/kysdk/kysdk-base/

View File

@ -12,5 +12,5 @@ add_subdirectory(log)
# add_subdirectory(thread)
add_subdirectory(timer)
add_subdirectory(utils)
# add_subdirectory(diagnostics)
add_subdirectory(diagnostics)
add_subdirectory(gsettings)

View File

@ -0,0 +1,33 @@
cmake_minimum_required(VERSION 3.5)
project(kydiagnostics LANGUAGES CXX)
set(DIAGNOSTICS_TOP_DIR ${CMAKE_CURRENT_LIST_DIR})
add_library(${PROJECT_NAME} SHARED)
set_target_properties(kydiagnostics PROPERTIES VERSION 2.0.0 SOVERSION 1)
target_compile_options(${PROJECT_NAME} PRIVATE -Wall -g)
target_compile_features(${PROJECT_NAME} PRIVATE cxx_std_11)
find_package(PkgConfig REQUIRED)
pkg_check_modules(OPENSSL openssl)
target_include_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${OPENSSL_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${OPENSSL_LIBRARIES})
pkg_check_modules(DBUS-1 dbus-1)
target_include_directories(${PROJECT_NAME} PRIVATE ${DBUS-1_INCLUDE_DIRS})
target_link_directories(${PROJECT_NAME} PRIVATE ${DBUS-1_LIBRARY_DIRS})
target_link_libraries(${PROJECT_NAME} PRIVATE ${DBUS-1_LIBRARIES})
set(SRCS
"${DIAGNOSTICS_TOP_DIR}/libkydiagnostics.cpp"
"${DIAGNOSTICS_TOP_DIR}/buriedpoint.cpp")
target_include_directories(${PROJECT_NAME} PRIVATE ${DIAGNOSTICS_TOP_DIR})
target_sources(${PROJECT_NAME} PRIVATE ${SRCS})
install(TARGETS ${PROJECT_NAME} DESTINATION lib/kysdk/kysdk-base)

View File

@ -0,0 +1,270 @@
#include <unistd.h>
#include <sys/stat.h>
#include <time.h>
#include <sys/time.h>
#include <iostream>
#include <fstream>
#include <dbus-1.0/dbus/dbus.h>
#include "openssl/rsa.h"
#include "openssl/pem.h"
#include "openssl/err.h"
#include "openssl/evp.h"
#include "openssl/sha.h"
#include "openssl/bio.h"
#include "openssl/buffer.h"
#include "openssl/hmac.h"
#include "nlohmann/json.hpp"
#include "buriedpoint.h"
namespace kdk
{
namespace
{
constexpr char pkgInfoKeyPackageName[] = "packageName";
constexpr char pkgInfoKeyMessageType[] = "messageType";
constexpr char pkgInfoKeyTid[] = "tid";
constexpr char dbusServerName[] = "com.kylin.daq";
constexpr char dbusObjectName[] = "/com/kylin/daq";
constexpr char dbusInterfaceName[] = "com.kylin.daq.interface";
constexpr char dbusMehtodName[] = "UploadMessage";
const std::string configPath = getenv("HOME") + std::string("/") + ".config/buriedpoint/uploadmessage.conf";
}
BuriedPoint::BuriedPoint() = default;
BuriedPoint::~BuriedPoint() = default;
bool BuriedPoint::uploadMessage(std::string packageName , std::string messageType , std::map<std::string , std::string> data)
{
if (!checkDir()) {
std::cout << "kdk : Failed to create configuration directory !";
return false;
}
/* 从配置文件中获取 tid */
std::string tid = readTid();
/* 生成 package info */
nlohmann::json pkgInfoObj;
pkgInfoObj[pkgInfoKeyPackageName] = packageName;
pkgInfoObj[pkgInfoKeyMessageType] = messageType;
pkgInfoObj[pkgInfoKeyTid] = tid;
std::string pkgInfo = pkgInfoObj.dump();
/* 获取上传数据 */
std::string uploadData = getUploadData(data);
/* 调用 d-bus */
if (!callDbus(pkgInfo , uploadData , "")) {
std::cout << "kdk : buried point d-bus call fail !" << std::endl;
return false;
}
return true;
}
/* 配置文件存放路径 ~/.config/buriedpoint/ */
bool BuriedPoint::checkDir(void)
{
std::string homePath = std::string(getenv("HOME"));
std::string subPath = homePath + "/.config";
if (access(subPath.c_str() , F_OK)) {
if (mkdir(subPath.c_str() , 0775)) {
return false;
}
}
std::string destPath = subPath + "/buriedpoint";
if (access(destPath.c_str() , F_OK)) {
if (mkdir(destPath.c_str() , 0775)) {
return false;
}
}
return true;
}
std::string BuriedPoint::getUploadData(std::map<std::string , std::string> &data)
{
nlohmann::json jsonData;
std::map<std::string , std::string>::iterator it = data.begin();
while(it != data.end()) {
jsonData[it->first] = it->second;
it++;
}
jsonData["createTimeStamp"] = getCurrentTime();
return jsonData.dump();
}
std::string BuriedPoint::getCurrentTime(void)
{
struct timeval tp;
gettimeofday(&tp , NULL);
int msec = tp.tv_usec / 1000;
struct tm t;
localtime_r(&tp.tv_sec , &t);
char buf[128] = {0};
strftime(buf , sizeof(buf) , "%Y-%m-%d %H:%M:%S" , &t);
char currTime[512] = {0};
snprintf(currTime , sizeof(currTime) , "%s.%03d" , buf , msec);
return std::string(currTime);
}
bool BuriedPoint::callDbus(const std::string &pkgInfo , const std::string &uploadData , const std::string &uploadDataSha256)
{
DBusConnection *conn;
DBusError error;
dbus_error_init(&error);
conn = dbus_bus_get(DBUS_BUS_SYSTEM , &error);
if (dbus_error_is_set(&error)) {
std::cout << "d-bus connect fail !" << std::endl;
return false;
}
if (conn == NULL) {
return -1;
}
DBusMessage *sendMsg = NULL;
DBusPendingCall *sendMsgPending = NULL;
DBusMessage *replyMsg = NULL;
sendMsg = dbus_message_new_method_call(dbusServerName , dbusObjectName , dbusInterfaceName , dbusMehtodName);
const char *tmpPkgInfo = pkgInfo.c_str();
const char *tmpUploadData = uploadData.c_str();
const char *tmpUploadDataSha256 = uploadDataSha256.c_str();
if (!dbus_message_append_args(sendMsg , DBUS_TYPE_STRING , &tmpPkgInfo , DBUS_TYPE_STRING , &tmpUploadData , DBUS_TYPE_STRING , &tmpUploadDataSha256 , DBUS_TYPE_INVALID)) {
std::cout << "kdk : d-bus append args fail !" << std::endl;
return false;
}
if (!dbus_connection_send_with_reply(conn , sendMsg , &sendMsgPending , -1)) {
std::cout << "kdk : d-bus send message fail !" << std::endl;
return false;
}
if (sendMsgPending == NULL) {
std::cout << "kdk : d-bus pending message is NULL !" << std::endl;
return false;
}
dbus_connection_flush(conn);
if (sendMsg) {
dbus_message_unref(sendMsg);
}
dbus_pending_call_block(sendMsgPending);
replyMsg = dbus_pending_call_steal_reply(sendMsgPending);
if (replyMsg == NULL) {
std::cout << "d-bus get reply message fail !" << std::endl;
return false;
}
if (sendMsgPending) {
dbus_pending_call_unref(sendMsgPending);
}
DBusMessageIter args;
int retState = -1;
char *nowTid = NULL;
if (!dbus_message_iter_init(replyMsg , &args)) {
dbus_message_unref(replyMsg);
std::cout << "kdk : d-bus init reply message fail !";
return false;
} else {
dbus_message_iter_get_basic(&args , &retState);
}
if (dbus_message_iter_has_next(&args)) {
if (!dbus_message_iter_next(&args)) {
dbus_message_unref(replyMsg);
std::cout << "kdk : d-bus next reply message fail !";
return false;
} else {
dbus_message_iter_get_basic(&args , &nowTid);
}
}
/* 处理 dbus 返回值 */
bool retvalue = false;
switch (retState) {
case returnState::OK:
retvalue = true;
break;
case returnState::InvalidTid:
if (nowTid != NULL) {
if (!writeTid(nowTid)) {
std::cout << "kdk : tid write fail !" << std::endl;
}
}
retvalue = true;
break;
default:
std::cout << "kdk : dbus return error ! return state " << retState << std::endl;
break;
}
if (replyMsg) {
dbus_message_unref(replyMsg);
}
return retvalue;
}
/* 配置文件存放路径 ~/.config/buriedpoint/uploadmessage.conf */
std::string BuriedPoint::readTid(void)
{
std::string ret("");
std::ifstream ifs;
ifs.open(configPath , std::ios::in);
if (ifs.is_open()) {
std::getline(ifs , ret);
} else {
return "";
}
ifs.close();
size_t found = ret.find('=');
if (found == std::string::npos) {
return "";
}
return ret.substr(found + 1);
}
bool BuriedPoint::writeTid(std::string tid)
{
const std::string dest = "tid=" + tid;
std::ofstream ofs;
ofs.open(configPath , std::ios::out | std::ios::trunc);
if (ofs.is_open()) {
ofs << dest << std::endl;
} else {
std::cout << "kdk : open uploadmessage file fail !" << std::endl;
return false;
}
ofs.close();
return true;
}
}

View File

@ -0,0 +1,48 @@
#ifndef BURIEDPOINT_H_
#define BURIEDPOINT_H_
#include <string>
#include <map>
namespace kdk
{
class BuriedPoint
{
public:
BuriedPoint();
~BuriedPoint();
/**
* @brief
*
* @param packageName :
* @param messageType :
* @param data :
*
* @retval true :
* @retval false :
*/
bool uploadMessage(std::string packageName , std::string messageType , std::map<std::string , std::string> data);
private:
enum returnState {
OK = 0, /* 存储成功 */
InvalidArgumentFormat = 1, /* 参数格式错误 */
InvalidTid = 2, /* tid异常 , 但消息存储成功 */
InvalidUploadedMessageSha256 = 3, /* shan256异常 */
InvalidUploadedMessageSha256Decryption = 4, /* sha256解密异常 */
InvalidCreateTimeStamp = 5 /* 时间字段异常 */
};
bool checkDir(void);
std::string getUploadData(std::map<std::string , std::string> &data);
std::string getCurrentTime(void);
bool callDbus(const std::string &pkgInfo , const std::string &uploadData , const std::string &uploadMessageSha256);
std::string readTid(void);
bool writeTid(std::string tid);
};
}
#endif

View File

@ -29,11 +29,21 @@
#include <stdlib.h>
#include <stdio.h>
#include "dbus/dbus.h"
#include <sys/wait.h>
#include <errno.h>
#include <limits.h>
#define PATHSIZE 1024
KLogger* logger;
const char* stringLevel[8] = {"EMERG", "ALERT", "CRIT", "ERROR", "WARNING", "NOTICE", "INFO", "DEBUG"};
const char* stringLType[LTENUMMAX] = {"user." , "local3." , "syslog."};
static int verify_file(char *pFileName)
{
return 1;
}
static int _call_method(const char *path)
{
DBusError err;
@ -133,13 +143,44 @@ static int _dir_exist(const char *dpath)
static int _create_dir(const char *dpath)
{
#ifdef __linux__
char *command = malloc(strlen(dpath) + 10);
if (!command)
return -1;
sprintf(command, "mkdir -p %s", dpath);
int ret = system(command);
free(command);
return ret;
// char *command = malloc(strlen(dpath) + 10);
// if (!command)
// return -1;
// sprintf(command, "mkdir -p %s", dpath);
// int ret = system(command);
// free(command);
// return ret;
pid_t pid=-1;
int status=-1;
char **env=NULL;
pid = fork();
if (pid == (pid_t) 0)
{
char* args[] = {"mkdir -p", dpath, NULL};
/* Child side. */
(void)execve("/usr/bin/mkdir", args, env);
_exit(127);
}
else if(pid<(pid_t)0)
{
/* The fork failed. */
status = -1;
}
else
{
/* Parent side. */
int n;
do
{
n = waitpid (pid, &status, 0);
}
while (n == -1 && errno == EINTR);
if (n != pid)
status = -1;
}
return status;
#else
return 1;
#endif
@ -264,12 +305,19 @@ int initKLogger(int cid)
strcpy(logger->rootPath, dpath);
else
{
char canonical_filename[PATH_MAX] = "\0";
memset(canonical_filename,0,PATH_MAX);
char *hpath = getenv("HOME");
if (!hpath || strcmp(hpath, "/root") == 0)
realpath(hpath, canonical_filename);
if(!verify_file(canonical_filename))
{
return -1;
}
if (!canonical_filename || strcmp(canonical_filename, "/root") == 0)
strcpy(logger->rootPath, "/var/log");
else
{
strcpy(logger->rootPath, hpath);
strncpy(logger->rootPath, canonical_filename, PATHSIZE);
strcat(logger->rootPath, "/.log");
if (!_dir_exist(logger->rootPath))
{
@ -294,7 +342,7 @@ int initKLogger(int cid)
snprintf(logger->logfileName.commonlogfileName , KLOG_MAXPATHLEN , "%s.log" , logger->processName);
}
klog_rotate_init(cid, logger->processName, logger->rootPath);
// klog_rotate_init(cid, logger->processName, logger->rootPath);
}
else //使用指定的specName作为日志名称
{
@ -319,7 +367,7 @@ int initKLogger(int cid)
{
snprintf(logger->logfileName.commonlogfileName , KLOG_MAXPATHLEN , "%s.log" , fName);
}
klog_rotate_init(cid, logger->specName, logger->rootPath);
// klog_rotate_init(cid, logger->specName, logger->rootPath);
}
if (logger->levelBasedStorage)
@ -343,7 +391,14 @@ int initKLogger(int cid)
{
char logPath[(KLOG_MAXPATHLEN << 1) + 1];
snprintf(logPath, KLOG_MAXPATHLEN << 1, "%s/%s", logger->rootPath, logger->logfileName.commonlogfileName);
logger->fp.commonfp = fopen(logPath , "at");
char canonical_filename[PATH_MAX] = "\0";
memset(canonical_filename,0,PATH_MAX);
realpath(logPath, canonical_filename);
if(!verify_file(canonical_filename))
{
return -1;
}
logger->fp.commonfp = fopen(canonical_filename , "at");
if (!logger->fp.commonfp)
{
printf("无法打开日志文件%s%s\n" ,logPath, strerror(errno));
@ -391,7 +446,14 @@ int setRootDir(const char *dpath)
char logPath[KLOG_MAXPATHLEN * 2];
fclose(logger->fp.commonfp);
sprintf(logPath, "%s/%s", logger->rootPath, logger->logfileName.commonlogfileName);
logger->fp.commonfp = fopen(logPath, "at");
char canonical_filename[PATH_MAX] = "\0";
memset(canonical_filename,0,PATH_MAX);
int ret = realpath(logPath, canonical_filename);
if(!verify_file(canonical_filename))
{
return -1;
}
logger->fp.commonfp = fopen(canonical_filename, "at");
if (!logger->fp.commonfp)
{
printf("无法打开日志文件%s%s\n", logPath, strerror(errno));

View File

@ -28,6 +28,11 @@
#include <string.h>
#include <pthread.h>
#define FILENAMESIZE 512
#define FUNCSIZE 128
#define LINESIZE 10
#define BUFFERSIZE 1398 //2048-512-128-10
PrintFormat klog_printformat;
extern const char* stringLevel[8];
@ -138,7 +143,8 @@ int formatMessage(int lvl , const char *filename , const char *func , int linenu
pos += 1;
if (klog_printformat.vis_filename)
{
memcpy(pos , filename , strlen(filename) * sizeof(char));
size_t len = strlen(filename) * sizeof(char);
memcpy(pos , filename , FILENAMESIZE > len ? len : FILENAMESIZE);
pos += strlen(filename);
}
if (klog_printformat.vis_funcline)
@ -148,19 +154,22 @@ int formatMessage(int lvl , const char *filename , const char *func , int linenu
strcpy(pos , ":");
pos += 1;
}
memcpy(pos , func , strlen(func) * sizeof(char));
size_t len = strlen(func) * sizeof(char);
memcpy(pos , func , FUNCSIZE > len ? len : FUNCSIZE);
pos += strlen(func);
char line[10] = {0};
snprintf(line , 9 , "-%d" , linenum);
memcpy(pos , line , strlen(line) * sizeof(char));
len = strlen(line) * sizeof(char);
memcpy(pos , line , LINESIZE > len ? len : LINESIZE);
pos += strlen(line);
}
strcpy(pos , "] ");
pos += 2;
}
size_t remainMsgSize = KLOG_MAXMSGSIZE - strlen(buffer);
// size_t remainMsgSize = KLOG_MAXMSGSIZE - strlen(buffer);
// size_t remainMsgSize = KLOG_MAXMSGSIZE - ((pos - buffer) / sizeof(char));
size_t rawMsgSize = strlen(message) * sizeof(char);
memcpy(pos , message , rawMsgSize > remainMsgSize ? remainMsgSize : rawMsgSize);
memcpy(pos , message , BUFFERSIZE > rawMsgSize ? rawMsgSize : BUFFERSIZE);
memcpy(result , buffer , resultSize * sizeof(char));
return 0;
}

View File

@ -26,6 +26,12 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>
static int verify_file(char *pFileName)
{
return !strncmp(pFileName, "/etc", strlen("/etc"));
}
int klog_rotate_init(int cid, const char *name, const char *rootpath)
{
@ -37,7 +43,14 @@ int klog_rotate_init(int cid, const char *name, const char *rootpath)
char tmp[1025];
snprintf(tmp, 1024, "/etc/kysdk/kysdk-base/logrotate.d/%s", name);
FILE *fp = fopen(tmp, "wt+");
char canonical_filename[PATH_MAX] = "\0";
memset(canonical_filename,0,PATH_MAX);
realpath(tmp, canonical_filename);
if(!verify_file(canonical_filename))
{
return -1;
}
FILE *fp = fopen(canonical_filename, "wt+");
if (!fp)
return -1;

View File

@ -24,6 +24,12 @@
#include <errno.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
static int verify_file(char *pFileName)
{
return 1;
}
int writeFile(int lvl , const char *message , unsigned int len)
{
@ -68,7 +74,14 @@ int writeFile(int lvl , const char *message , unsigned int len)
{
if (!logger->fp.commonfp)
{
logger->fp.commonfp = fopen(logger->logfileName.commonlogfileName , "at");
char canonical_filename[PATH_MAX] = "\0";
memset(canonical_filename,0,PATH_MAX);
realpath(logger->logfileName.commonlogfileName, canonical_filename);
if(!verify_file(canonical_filename))
{
return errno;
}
logger->fp.commonfp = fopen(canonical_filename, "at");
if (!logger->fp.commonfp)
{
printf("无法打开日志文件:%s\n" , strerror(errno));

View File

@ -360,6 +360,24 @@ int kdkVolumeBaseNumericalConvert(double origin_numerical, KDKVolumeBaseType ori
case KDK_EXABYTE:
strcpy(unit, "EB");
break;
case KDK_KILO:
strcpy(unit, "K");
break;
case KDK_MEGA:
strcpy(unit, "M");
break;
case KDK_GIGA:
strcpy(unit, "G");
break;
case KDK_TERA:
strcpy(unit, "T");
break;
case KDK_PETA:
strcpy(unit, "P");
break;
case KDK_EXA:
strcpy(unit, "E");
break;
default:
strcpy(unit, "B");
break;