Merge remote-tracking branch 'origin/v1.2.0_dev' into v1.2.0_dev

This commit is contained in:
dongwenze 2023-03-03 16:38:16 +08:00
commit 9a0f34b168
12 changed files with 138 additions and 68 deletions

View File

@ -17,8 +17,6 @@ INCLUDE(system_libs)
SET(VCPE_VERSION "1.0.1") SET(VCPE_VERSION "1.0.1")
PROJECT(vCPE VERSION "${VCPE_VERSION}") PROJECT(vCPE VERSION "${VCPE_VERSION}")
CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF) CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF)
# #

View File

@ -9,7 +9,7 @@ buffer max = 2MB
rotate lock file = self rotate lock file = self
default format = "%d(%F %T.%l) %-6V (%c:%f:%L) %m%n" default format = "%d(%F %T.%l) %-6V (%c:%f:%L) %m%n"
file perms = 600 file perms = 644
fsync period = 1K fsync period = 1K
[levels] [levels]
@ -22,8 +22,8 @@ normal = "[%d(%F %T).%ms][%-6V][%c][%f:%L] %m"
[rules] [rules]
*.* >stdout; normal *.* >stdout; normal
*.INFO "./log/%c.log", \ *.INFO "/var/log/vcpe/%M(vni)_vcpe.log", \
1MB * 12 ~ "%E(HOME)/log/%c.%D(%F) #2r #3s.log"; \ 1MB * 12 ~ "/var/log/vcpe/%M(vni)_vcpe_.%D(%F) #2r #3s.log"; \
normal normal

View File

@ -1,6 +1,19 @@
CMAKE_MINIMUM_REQUIRED(VERSION 3.14 FATAL_ERROR) CMAKE_MINIMUM_REQUIRED(VERSION 3.14 FATAL_ERROR)
INCLUDE(FetchContent) INCLUDE(FetchContent)
PKG_SEARCH_MODULE(LIBCURL QUIET libcurl)
PKG_SEARCH_MODULE(LIBSSL QUIET libssl)
PKG_SEARCH_MODULE(LIBCRYPTO QUIET libcrypto)
PKG_SEARCH_MODULE(LIBZMQ QUIET libzmq)
IF ((NOT LIBZMQ_FOUND) OR (NOT LIBCRYPTO_FOUND) OR (NOT LIBSSL_FOUND) OR (NOT LIBCURL_FOUND))
LINUX_INSTALL_SYSTEM_PACKAGE()
ENDIF ()
LIST(APPEND COMMON_LIBS "${LIBCURL_LDFLAGS} ${LIBSSL_LDFLAGS} ${LIBCRYPTO_LDFLAGS}")
LIST(APPEND COMMON_LIBS "${LIBZMQ_LDFLAGS}")
LIST(APPEND COMMON_LIBS "-lm -lpthread")
PKG_SEARCH_MODULE(LIBUV QUIET libuv) PKG_SEARCH_MODULE(LIBUV QUIET libuv)
PKG_SEARCH_MODULE(LIBCONFIG QUIET libconfig) PKG_SEARCH_MODULE(LIBCONFIG QUIET libconfig)
FIND_LIBRARY(LIBZLOG zlog PATHS "/usr/local/lib") FIND_LIBRARY(LIBZLOG zlog PATHS "/usr/local/lib")

View File

@ -6,19 +6,6 @@ ENDIF ()
PROJECT(${PROJECT_TARGET}) PROJECT(${PROJECT_TARGET})
PKG_SEARCH_MODULE(LIBCURL QUIET libcurl)
PKG_SEARCH_MODULE(LIBSSL QUIET libssl)
PKG_SEARCH_MODULE(LIBCRYPTO QUIET libcrypto)
PKG_SEARCH_MODULE(LIBZMQ QUIET libzmq)
IF ((NOT LIBZMQ_FOUND) OR (NOT LIBCRYPTO_FOUND) OR (NOT LIBSSL_FOUND) OR (NOT LIBCURL_FOUND))
LINUX_INSTALL_SYSTEM_PACKAGE()
ENDIF ()
LIST(APPEND COMMON_LIBS "${LIBCURL_LDFLAGS} ${LIBSSL_LDFLAGS} ${LIBCRYPTO_LDFLAGS}")
LIST(APPEND COMMON_LIBS "${LIBZMQ_LDFLAGS}")
LIST(APPEND COMMON_LIBS "-lm -lpthread")
ADD_DEFINITIONS(${COMMON_DEFINE}) ADD_DEFINITIONS(${COMMON_DEFINE})
INCLUDE_DIRECTORIES(include ./ ./include ./libs/include ./lwip/src/include ./lwip/src/arch_linux/include INCLUDE_DIRECTORIES(include ./ ./include ./libs/include ./lwip/src/include ./lwip/src/arch_linux/include

View File

@ -68,7 +68,7 @@ static int sha1prng_for_aes_key(const char *pKey, unsigned char *pShaPrng16) {
case AES128_ECB_PKCS7PADDING_SHA1PRNG: \ case AES128_ECB_PKCS7PADDING_SHA1PRNG: \
if (sha1prng_for_aes_key(pKey, keyBuf) != ERR_SUCCESS) { \ if (sha1prng_for_aes_key(pKey, keyBuf) != ERR_SUCCESS) { \
EVP_CIPHER_CTX_cleanup(pCtx); \ EVP_CIPHER_CTX_cleanup(pCtx); \
return -ERR_AES128_KEYGEN; \ return -ERR_AES_KEYGEN; \
} \ } \
pCipher = EVP_aes_128_ecb(); \ pCipher = EVP_aes_128_ecb(); \
break; \ break; \

View File

@ -61,6 +61,8 @@ int get_nic_info(const char *pName,
unsigned char *pMac); unsigned char *pMac);
int str_to_ipaddr(const char *pIp, unsigned int *ipAddr); int str_to_ipaddr(const char *pIp, unsigned int *ipAddr);
unsigned long long get_current_time_ms(); unsigned long long get_current_time_ms();
int process_lock_pidfile(char *pFilePath);
void process_unlock_pidfile();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -8,8 +8,6 @@
extern "C" { extern "C" {
#endif #endif
#define USED_USER_ERRNO
#define DEF_ERR_CODE(ERR_CODE) \ #define DEF_ERR_CODE(ERR_CODE) \
ERR_CODE(ERR_SUCCESS, 0, "成功") \ ERR_CODE(ERR_SUCCESS, 0, "成功") \
ERR_CODE(ERR_INPUT_PARAMS, 1, "输入参数错误") \ ERR_CODE(ERR_INPUT_PARAMS, 1, "输入参数错误") \
@ -21,48 +19,49 @@ extern "C" {
ERR_CODE(ERR_OPEN_FILE, 7, "打开文件失败") \ ERR_CODE(ERR_OPEN_FILE, 7, "打开文件失败") \
ERR_CODE(ERR_READ_FILE, 8, "读取文件失败") \ ERR_CODE(ERR_READ_FILE, 8, "读取文件失败") \
ERR_CODE(ERR_FILE_NOT_EXISTS, 9, "文件不存在") \ ERR_CODE(ERR_FILE_NOT_EXISTS, 9, "文件不存在") \
ERR_CODE(ERR_GET_FILE_SIZE, 10, "获取文件大小失败") \ ERR_CODE(ERR_FILE_LOCKED, 10, "文件被锁定") \
ERR_CODE(ERR_COPY_FILE, 11, "复制文件失败") \ ERR_CODE(ERR_GET_FILE_SIZE, 11, "获取文件大小失败") \
ERR_CODE(ERR_MALLOC_MEMORY, 12, "分配内存失败") \ ERR_CODE(ERR_COPY_FILE, 12, "复制文件失败") \
ERR_CODE(ERR_EVP_KEY_SIZE, 13, "秘钥大小不正确") \ ERR_CODE(ERR_MALLOC_MEMORY, 13, "分配内存失败") \
ERR_CODE(ERR_UNSUP_EVP_TYPE, 14, "不支持的加解密算法") \ ERR_CODE(ERR_EVP_KEY_SIZE, 14, "秘钥大小不正确") \
ERR_CODE(ERR_EVP_INIT_KEY, 15, "初始化秘钥失败") \ ERR_CODE(ERR_UNSUP_EVP_TYPE, 15, "不支持的加解密算法") \
ERR_CODE(ERR_EVP_UPDATE, 16, "加解密数据失败") \ ERR_CODE(ERR_EVP_INIT_KEY, 16, "初始化秘钥失败") \
ERR_CODE(ERR_EVP_FINALE, 17, "错误的加解密结果") \ ERR_CODE(ERR_EVP_UPDATE, 17, "加解密数据失败") \
ERR_CODE(ERR_EVP_CREATE_CTX, 18, "初始化加解密失败") \ ERR_CODE(ERR_EVP_FINALE, 18, "错误的加解密结果") \
ERR_CODE(ERR_AES128_KEYGEN, 19, "AES128秘钥失败") \ ERR_CODE(ERR_EVP_CREATE_CTX, 19, "初始化加解密失败") \
ERR_CODE(ERR_EVP_ENCRYPTION, 20, "加密失败") \ ERR_CODE(ERR_AES_KEYGEN, 20, "AES秘钥失败") \
ERR_CODE(ERR_EVP_DECRYPTION, 21, "解密失败") \ ERR_CODE(ERR_EVP_ENCRYPTION, 21, "加密失败") \
ERR_CODE(ERR_CONFIG_INIT, 22, "配置文件初始化失败") \ ERR_CODE(ERR_EVP_DECRYPTION, 22, "解密失败") \
ERR_CODE(ERR_UNCOMPATIBILITY_TYPE, 23, "未兼容的类型") \ ERR_CODE(ERR_CONFIG_INIT, 23, "配置文件初始化失败") \
ERR_CODE(ERR_STRING_TO_NUMBER, 24, "字符串转数字失败") \ ERR_CODE(ERR_UNCOMPATIBILITY_TYPE, 24, "未兼容的类型") \
ERR_CODE(ERR_UNKNOWN_CFG_ID, 25, "未识别的配置项") \ ERR_CODE(ERR_STRING_TO_NUMBER, 25, "字符串转数字失败") \
ERR_CODE(ERR_ZLOG_INIT, 26, "日志系统初始化失败") \ ERR_CODE(ERR_UNKNOWN_CFG_ID, 26, "未识别的配置项") \
ERR_CODE(ERR_SYS_GET_CPU_INFO, 27, "获取CPU信息失败") \ ERR_CODE(ERR_ZLOG_INIT, 27, "日志系统初始化失败") \
ERR_CODE(ERR_SYS_NOT_FOUND_CPU, 28, "找不到CPU信息") \ ERR_CODE(ERR_SYS_GET_CPU_INFO, 28, "获取CPU信息失败") \
ERR_CODE(ERR_SYS_DISK_GET_INFO, 29, "获取磁盘空间占用信息失败") \ ERR_CODE(ERR_SYS_NOT_FOUND_CPU, 29, "找不到CPU信息") \
ERR_CODE(ERR_SYS_IPMI_UNSUP, 30, "服务器IPMI接口不支持") \ ERR_CODE(ERR_SYS_DISK_GET_INFO, 30, "获取磁盘空间占用信息失败") \
ERR_CODE(ERR_SYS_SENSOR_GET_INFO, 31, "获取传感器信息失败") \ ERR_CODE(ERR_SYS_IPMI_UNSUP, 31, "服务器IPMI接口不支持") \
ERR_CODE(ERR_DB_CONNECT, 32, "数据库连接失败") \ ERR_CODE(ERR_SYS_SENSOR_GET_INFO, 32, "获取传感器信息失败") \
ERR_CODE(ERR_MQ_CREATE_MQ, 33, "创建消息队列失败") \ ERR_CODE(ERR_DB_CONNECT, 33, "数据库连接失败") \
ERR_CODE(ERR_MQ_CREATE_REP, 34, "创建REP消息队列失败") \ ERR_CODE(ERR_MQ_CREATE_MQ, 34, "创建消息队列失败") \
ERR_CODE(ERR_MQ_BIND_SOCKET, 35, "消息队列BIND Socket失败") \ ERR_CODE(ERR_MQ_CREATE_REP, 35, "创建REP消息队列失败") \
ERR_CODE(ERR_MQ_CONN_SERVER, 36, "消息队列连接服务器失败") \ ERR_CODE(ERR_MQ_BIND_SOCKET, 36, "消息队列BIND Socket失败") \
ERR_CODE(ERR_MQ_SEND_MSG, 37, "消息队列发送消息失败") \ ERR_CODE(ERR_MQ_CONN_SERVER, 37, "消息队列连接服务器失败") \
ERR_CODE(ERR_JSON_CREAT_OBJ, 38, "创建JSON对象失败") \ ERR_CODE(ERR_MQ_SEND_MSG, 38, "消息队列发送消息失败") \
ERR_CODE(ERR_JSON_PRASE_OBJ, 39, "解析JSON对象失败") \ ERR_CODE(ERR_JSON_CREAT_OBJ, 39, "创建JSON对象失败") \
ERR_CODE(ERR_JSON_VALID_SCH, 40, "JSON数据验证失败") \ ERR_CODE(ERR_JSON_PRASE_OBJ, 40, "解析JSON对象失败") \
ERR_CODE(ERR_CREATE_NETIF, 41, "创建网络接口失败") \ ERR_CODE(ERR_JSON_VALID_SCH, 41, "JSON数据验证失败") \
ERR_CODE(ERR_CREATE_PPPOE_NETIF, 42, "创建PPPoE网络接口失败") \ ERR_CODE(ERR_CREATE_NETIF, 42, "创建网络接口失败") \
ERR_CODE(ERR_CREATE_PPP_SESSION, 43, "创建PPP连接失败") \ ERR_CODE(ERR_CREATE_PPPOE_NETIF, 43, "创建PPPoE网络接口失败") \
ERR_CODE(ERR_MISC_GET_IPADDR, 44, "获取网卡IP地址失败") \ ERR_CODE(ERR_CREATE_PPP_SESSION, 44, "创建PPP连接失败") \
ERR_CODE(ERR_MISC_GET_NETMASK, 45, "获取网卡子网掩码失败") \ ERR_CODE(ERR_MISC_GET_IPADDR, 45, "获取网卡IP地址失败") \
ERR_CODE(ERR_MISC_GET_GATEWAY, 46, "获取网卡网关地址失败") \ ERR_CODE(ERR_MISC_GET_NETMASK, 46, "获取网卡子网掩码失败") \
ERR_CODE(ERR_MISC_GET_MACADDR, 47, "获取网卡MAC地址失败") \ ERR_CODE(ERR_MISC_GET_GATEWAY, 47, "获取网卡网关地址失败") \
ERR_CODE(ERR_MENU_EXIT, 48, "菜单执行完后自动退出") \ ERR_CODE(ERR_MISC_GET_MACADDR, 48, "获取网卡MAC地址失败") \
ERR_CODE(ERR_HTTP_UNSUP_METHOD, 49, "不支持的 HTTP 请求方法") \ ERR_CODE(ERR_MENU_EXIT, 49, "菜单执行完后自动退出") \
ERR_CODE(ERR_HTTP_UNSUP_PAGE, 50, "找不到 HTTP 服务") \ ERR_CODE(ERR_HTTP_UNSUP_METHOD, 50, "不支持的 HTTP 请求方法") \
ERR_CODE(ERR_PROTO_DECODE, 51, "HTTP 协议解析失败") ERR_CODE(ERR_HTTP_UNSUP_PAGE, 51, "找不到 HTTP 服务") \
ERR_CODE(ERR_PROTO_DECODE, 52, "HTTP 协议解析失败")
#define GENERATE_ENUM(ENUM, no, x) ENUM, #define GENERATE_ENUM(ENUM, no, x) ENUM,

View File

@ -65,6 +65,9 @@ typedef enum {
#define LOG_MOD(level, mod, format, ...) \ #define LOG_MOD(level, mod, format, ...) \
do { \ do { \
if (zlog_verify_level(level, mod)) { \ if (zlog_verify_level(level, mod)) { \
if (zlog_vni_tag_get()) { \
zlog_put_mdc("vni", zlog_vni_tag_get()); \
} \
zlog_##level(zlog_get_mod_cat((mod)), format, ##__VA_ARGS__); \ zlog_##level(zlog_get_mod_cat((mod)), format, ##__VA_ARGS__); \
} \ } \
} while (0) } while (0)
@ -72,12 +75,16 @@ typedef enum {
#define LOG_MOD_HEX(level, mod, format, ...) \ #define LOG_MOD_HEX(level, mod, format, ...) \
do { \ do { \
if (zlog_verify_level(level, mod)) { \ if (zlog_verify_level(level, mod)) { \
if (zlog_vni_tag_get()) { \
zlog_put_mdc("vni", zlog_vni_tag_get()); \
} \
hzlog_##level(zlog_get_mod_cat((mod)), format, ##__VA_ARGS__); \ hzlog_##level(zlog_get_mod_cat((mod)), format, ##__VA_ARGS__); \
} \ } \
} while (0) } while (0)
zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod); zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod);
int zlog_verify_level(int level, ZLOG_MOD_NAME logMod); int zlog_verify_level(int level, ZLOG_MOD_NAME logMod);
const char *zlog_vni_tag_get();
#ifdef __cplusplus #ifdef __cplusplus
} }
#endif #endif

View File

@ -40,6 +40,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
UT_string *pPath; UT_string *pPath;
char bufCfgFile[MAX_PATH]; char bufCfgFile[MAX_PATH];
char bufCfgDir[MAX_PATH]; char bufCfgDir[MAX_PATH];
char pidfile[MAX_PATH];
g_pid = uv_os_getpid(); g_pid = uv_os_getpid();
@ -112,6 +113,17 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK
LOG_MOD(info, ZLOG_MOD_INIT, "Application build configure: [%s]\n", VCPE_BUILD_CONFIG); LOG_MOD(info, ZLOG_MOD_INIT, "Application build configure: [%s]\n", VCPE_BUILD_CONFIG);
#ifdef USERVNI_ON
sprintf(pidfile, "/tmp/%d_vcpe.pid", cfg_get_user_vni_id());
#else
sprintf(pidfile, "/tmp/vcpe.pid");
#endif
if (process_lock_pidfile(pidfile) == -ERR_FILE_LOCKED) {
LOG_MOD(error, ZLOG_MOD_INIT, "!!!Another same process is running!!!, system exit......\n");
exit(-ERR_FILE_LOCKED);
}
if (cfg_get_banner_enable()) { if (cfg_get_banner_enable()) {
banner_show(); banner_show();
} }
@ -148,5 +160,6 @@ void user_uninit() {
zlog_fini(); zlog_fini();
uninit_config_system(); uninit_config_system();
uv_loop_close(get_task_manager()); uv_loop_close(get_task_manager());
process_unlock_pidfile();
} }
} }

View File

@ -0,0 +1,34 @@
//
// Created by xajhuang on 2023/3/2.
//
#include <fcntl.h>
#include <sys/file.h>
#include <errno.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include "user_errno.h"
#include "misc.h"
static int g_lockfd = -1;
static char g_pidPath[MAX_PATH] = {0};
int process_lock_pidfile(char *pFilePath) {
if (pFilePath == NULL || strlen(pFilePath) == 0) {
return ERR_SUCCESS;
}
strcpy(g_pidPath, pFilePath);
g_lockfd = open(pFilePath, O_CREAT | O_RDWR, 0666); // open file
int rc = flock(g_lockfd, LOCK_EX | LOCK_NB); // lock access to open file
if (rc && EWOULDBLOCK == errno) { // check lock success
return -ERR_FILE_LOCKED; // another instance is running
}
return ERR_SUCCESS;
}
void process_unlock_pidfile() {
close(g_lockfd);
unlink(g_pidPath);
}

View File

@ -4,6 +4,7 @@
#include <string.h> #include <string.h>
#include "zlog_module.h" #include "zlog_module.h"
#include "misc.h" #include "misc.h"
#include "config.h"
#define MAX_ZLOG_MOD_LEN (8) #define MAX_ZLOG_MOD_LEN (8)
@ -20,6 +21,22 @@ static ZLOG_MODULE g_zlogModule[] = {
DEF_ZLOG_MOD(GENERATE_ENUM_ARRAY) {-1, ZLOG_LEVEL_FATAL, NULL, "UNKNOWN"} DEF_ZLOG_MOD(GENERATE_ENUM_ARRAY) {-1, ZLOG_LEVEL_FATAL, NULL, "UNKNOWN"}
}; };
#ifdef USERVNI_ON
static char g_vni_str[64] = {0};
const char *zlog_vni_tag_get() {
if (strlen(g_vni_str) == 0) {
sprintf(g_vni_str, "%u", cfg_get_user_vni_id());
}
return (const char *)g_vni_str;
}
#else
const char *zlog_vni_tag_get() {
return "main";
}
#endif
zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod) { zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod) {
if (logMod >= ARRAY_SIZE(g_zlogModule) || logMod < 0) { if (logMod >= ARRAY_SIZE(g_zlogModule) || logMod < 0) {
logMod = ARRAY_SIZE(g_zlogModule) - 1; logMod = ARRAY_SIZE(g_zlogModule) - 1;

View File

@ -8,7 +8,7 @@ ADD_DEFINITIONS(-DPROJECT_NAME="${PROJECT_NAME}")
ADD_EXECUTABLE(${PROJECT_NAME} ${UTEST_SRC}) ADD_EXECUTABLE(${PROJECT_NAME} ${UTEST_SRC})
TARGET_LINK_LIBRARIES(${PROJECT_NAME} common) TARGET_LINK_LIBRARIES(${PROJECT_NAME} common ${COMMON_LIBS})
#INCLUDE(CTest) #INCLUDE(CTest)
#INCLUDE(doctest) #INCLUDE(doctest)