diff --git a/CMakeLists.txt b/CMakeLists.txt index f9ce289..c4c0eb5 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,8 +17,6 @@ INCLUDE(system_libs) SET(VCPE_VERSION "1.0.1") PROJECT(vCPE VERSION "${VCPE_VERSION}") - - CMAKE_DEPENDENT_OPTION(USED_HTTP_SVR "Build-in http(s) server support" ON "USED_OPENDHCPD OR USED_OPENDHCPDDNS" OFF) # 数据库开关 diff --git a/config/zlog.conf b/config/zlog.conf index 6376df4..842435a 100644 --- a/config/zlog.conf +++ b/config/zlog.conf @@ -9,7 +9,7 @@ buffer max = 2MB rotate lock file = self default format = "%d(%F %T.%l) %-6V (%c:%f:%L) %m%n" -file perms = 600 +file perms = 644 fsync period = 1K [levels] @@ -22,8 +22,8 @@ normal = "[%d(%F %T).%ms][%-6V][%c][%f:%L] %m" [rules] *.* >stdout; normal -*.INFO "./log/%c.log", \ - 1MB * 12 ~ "%E(HOME)/log/%c.%D(%F) #2r #3s.log"; \ +*.INFO "/var/log/vcpe/%M(vni)_vcpe.log", \ + 1MB * 12 ~ "/var/log/vcpe/%M(vni)_vcpe_.%D(%F) #2r #3s.log"; \ normal diff --git a/depend/third_libs.cmake b/depend/third_libs.cmake index 1573bce..284cf67 100644 --- a/depend/third_libs.cmake +++ b/depend/third_libs.cmake @@ -1,6 +1,19 @@ CMAKE_MINIMUM_REQUIRED(VERSION 3.14 FATAL_ERROR) 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(LIBCONFIG QUIET libconfig) FIND_LIBRARY(LIBZLOG zlog PATHS "/usr/local/lib") diff --git a/srcs/CMakeLists.txt b/srcs/CMakeLists.txt index e718093..c337c5b 100644 --- a/srcs/CMakeLists.txt +++ b/srcs/CMakeLists.txt @@ -6,19 +6,6 @@ ENDIF () 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}) INCLUDE_DIRECTORIES(include ./ ./include ./libs/include ./lwip/src/include ./lwip/src/arch_linux/include diff --git a/srcs/libs/crypto/symmetric.c b/srcs/libs/crypto/symmetric.c index d18fb62..5d713ae 100644 --- a/srcs/libs/crypto/symmetric.c +++ b/srcs/libs/crypto/symmetric.c @@ -68,7 +68,7 @@ static int sha1prng_for_aes_key(const char *pKey, unsigned char *pShaPrng16) { case AES128_ECB_PKCS7PADDING_SHA1PRNG: \ if (sha1prng_for_aes_key(pKey, keyBuf) != ERR_SUCCESS) { \ EVP_CIPHER_CTX_cleanup(pCtx); \ - return -ERR_AES128_KEYGEN; \ + return -ERR_AES_KEYGEN; \ } \ pCipher = EVP_aes_128_ecb(); \ break; \ @@ -165,8 +165,8 @@ int symmetric_decrypto(AES_TYPE algorithmType, return -ERR_EVP_UPDATE; } - *pOutBuf = pAesBuf; - pAesBuf += enBytes; + *pOutBuf = pAesBuf; + pAesBuf += enBytes; *pOutSize += enBytes; if (EVP_DecryptFinal_ex(pCtx, pAesBuf, &enBytes) == 0) { @@ -239,8 +239,8 @@ int symmetric_encrypto(AES_TYPE algorithmType, return -ERR_EVP_UPDATE; } - *pOutBuf = pAesBuf; - pAesBuf += enBytes; + *pOutBuf = pAesBuf; + pAesBuf += enBytes; *pOutSize += enBytes; if (EVP_EncryptFinal_ex(pCtx, pAesBuf, &enBytes) == 0) { diff --git a/srcs/libs/include/misc.h b/srcs/libs/include/misc.h index de22005..0477ea5 100644 --- a/srcs/libs/include/misc.h +++ b/srcs/libs/include/misc.h @@ -61,6 +61,8 @@ int get_nic_info(const char *pName, unsigned char *pMac); int str_to_ipaddr(const char *pIp, unsigned int *ipAddr); unsigned long long get_current_time_ms(); +int process_lock_pidfile(char *pFilePath); +void process_unlock_pidfile(); #ifdef __cplusplus } #endif diff --git a/srcs/libs/include/user_errno.h b/srcs/libs/include/user_errno.h index 9648ffd..ebaf615 100644 --- a/srcs/libs/include/user_errno.h +++ b/srcs/libs/include/user_errno.h @@ -8,8 +8,6 @@ extern "C" { #endif -#define USED_USER_ERRNO - #define DEF_ERR_CODE(ERR_CODE) \ ERR_CODE(ERR_SUCCESS, 0, "成功") \ ERR_CODE(ERR_INPUT_PARAMS, 1, "输入参数错误") \ @@ -21,48 +19,49 @@ extern "C" { ERR_CODE(ERR_OPEN_FILE, 7, "打开文件失败") \ ERR_CODE(ERR_READ_FILE, 8, "读取文件失败") \ ERR_CODE(ERR_FILE_NOT_EXISTS, 9, "文件不存在") \ - ERR_CODE(ERR_GET_FILE_SIZE, 10, "获取文件大小失败") \ - ERR_CODE(ERR_COPY_FILE, 11, "复制文件失败") \ - ERR_CODE(ERR_MALLOC_MEMORY, 12, "分配内存失败") \ - ERR_CODE(ERR_EVP_KEY_SIZE, 13, "秘钥大小不正确") \ - ERR_CODE(ERR_UNSUP_EVP_TYPE, 14, "不支持的加解密算法") \ - ERR_CODE(ERR_EVP_INIT_KEY, 15, "初始化秘钥失败") \ - ERR_CODE(ERR_EVP_UPDATE, 16, "加解密数据失败") \ - ERR_CODE(ERR_EVP_FINALE, 17, "错误的加解密结果") \ - ERR_CODE(ERR_EVP_CREATE_CTX, 18, "初始化加解密失败") \ - ERR_CODE(ERR_AES128_KEYGEN, 19, "AES128秘钥失败") \ - ERR_CODE(ERR_EVP_ENCRYPTION, 20, "加密失败") \ - ERR_CODE(ERR_EVP_DECRYPTION, 21, "解密失败") \ - ERR_CODE(ERR_CONFIG_INIT, 22, "配置文件初始化失败") \ - ERR_CODE(ERR_UNCOMPATIBILITY_TYPE, 23, "未兼容的类型") \ - ERR_CODE(ERR_STRING_TO_NUMBER, 24, "字符串转数字失败") \ - ERR_CODE(ERR_UNKNOWN_CFG_ID, 25, "未识别的配置项") \ - ERR_CODE(ERR_ZLOG_INIT, 26, "日志系统初始化失败") \ - ERR_CODE(ERR_SYS_GET_CPU_INFO, 27, "获取CPU信息失败") \ - ERR_CODE(ERR_SYS_NOT_FOUND_CPU, 28, "找不到CPU信息") \ - ERR_CODE(ERR_SYS_DISK_GET_INFO, 29, "获取磁盘空间占用信息失败") \ - ERR_CODE(ERR_SYS_IPMI_UNSUP, 30, "服务器IPMI接口不支持") \ - ERR_CODE(ERR_SYS_SENSOR_GET_INFO, 31, "获取传感器信息失败") \ - ERR_CODE(ERR_DB_CONNECT, 32, "数据库连接失败") \ - ERR_CODE(ERR_MQ_CREATE_MQ, 33, "创建消息队列失败") \ - ERR_CODE(ERR_MQ_CREATE_REP, 34, "创建REP消息队列失败") \ - ERR_CODE(ERR_MQ_BIND_SOCKET, 35, "消息队列BIND Socket失败") \ - ERR_CODE(ERR_MQ_CONN_SERVER, 36, "消息队列连接服务器失败") \ - ERR_CODE(ERR_MQ_SEND_MSG, 37, "消息队列发送消息失败") \ - ERR_CODE(ERR_JSON_CREAT_OBJ, 38, "创建JSON对象失败") \ - ERR_CODE(ERR_JSON_PRASE_OBJ, 39, "解析JSON对象失败") \ - ERR_CODE(ERR_JSON_VALID_SCH, 40, "JSON数据验证失败") \ - ERR_CODE(ERR_CREATE_NETIF, 41, "创建网络接口失败") \ - ERR_CODE(ERR_CREATE_PPPOE_NETIF, 42, "创建PPPoE网络接口失败") \ - ERR_CODE(ERR_CREATE_PPP_SESSION, 43, "创建PPP连接失败") \ - ERR_CODE(ERR_MISC_GET_IPADDR, 44, "获取网卡IP地址失败") \ - ERR_CODE(ERR_MISC_GET_NETMASK, 45, "获取网卡子网掩码失败") \ - ERR_CODE(ERR_MISC_GET_GATEWAY, 46, "获取网卡网关地址失败") \ - ERR_CODE(ERR_MISC_GET_MACADDR, 47, "获取网卡MAC地址失败") \ - ERR_CODE(ERR_MENU_EXIT, 48, "菜单执行完后自动退出") \ - ERR_CODE(ERR_HTTP_UNSUP_METHOD, 49, "不支持的 HTTP 请求方法") \ - ERR_CODE(ERR_HTTP_UNSUP_PAGE, 50, "找不到 HTTP 服务") \ - ERR_CODE(ERR_PROTO_DECODE, 51, "HTTP 协议解析失败") + ERR_CODE(ERR_FILE_LOCKED, 10, "文件被锁定") \ + ERR_CODE(ERR_GET_FILE_SIZE, 11, "获取文件大小失败") \ + ERR_CODE(ERR_COPY_FILE, 12, "复制文件失败") \ + ERR_CODE(ERR_MALLOC_MEMORY, 13, "分配内存失败") \ + ERR_CODE(ERR_EVP_KEY_SIZE, 14, "秘钥大小不正确") \ + ERR_CODE(ERR_UNSUP_EVP_TYPE, 15, "不支持的加解密算法") \ + ERR_CODE(ERR_EVP_INIT_KEY, 16, "初始化秘钥失败") \ + ERR_CODE(ERR_EVP_UPDATE, 17, "加解密数据失败") \ + ERR_CODE(ERR_EVP_FINALE, 18, "错误的加解密结果") \ + ERR_CODE(ERR_EVP_CREATE_CTX, 19, "初始化加解密失败") \ + ERR_CODE(ERR_AES_KEYGEN, 20, "AES秘钥失败") \ + ERR_CODE(ERR_EVP_ENCRYPTION, 21, "加密失败") \ + ERR_CODE(ERR_EVP_DECRYPTION, 22, "解密失败") \ + ERR_CODE(ERR_CONFIG_INIT, 23, "配置文件初始化失败") \ + ERR_CODE(ERR_UNCOMPATIBILITY_TYPE, 24, "未兼容的类型") \ + ERR_CODE(ERR_STRING_TO_NUMBER, 25, "字符串转数字失败") \ + ERR_CODE(ERR_UNKNOWN_CFG_ID, 26, "未识别的配置项") \ + ERR_CODE(ERR_ZLOG_INIT, 27, "日志系统初始化失败") \ + ERR_CODE(ERR_SYS_GET_CPU_INFO, 28, "获取CPU信息失败") \ + ERR_CODE(ERR_SYS_NOT_FOUND_CPU, 29, "找不到CPU信息") \ + ERR_CODE(ERR_SYS_DISK_GET_INFO, 30, "获取磁盘空间占用信息失败") \ + ERR_CODE(ERR_SYS_IPMI_UNSUP, 31, "服务器IPMI接口不支持") \ + ERR_CODE(ERR_SYS_SENSOR_GET_INFO, 32, "获取传感器信息失败") \ + ERR_CODE(ERR_DB_CONNECT, 33, "数据库连接失败") \ + ERR_CODE(ERR_MQ_CREATE_MQ, 34, "创建消息队列失败") \ + ERR_CODE(ERR_MQ_CREATE_REP, 35, "创建REP消息队列失败") \ + ERR_CODE(ERR_MQ_BIND_SOCKET, 36, "消息队列BIND Socket失败") \ + ERR_CODE(ERR_MQ_CONN_SERVER, 37, "消息队列连接服务器失败") \ + ERR_CODE(ERR_MQ_SEND_MSG, 38, "消息队列发送消息失败") \ + ERR_CODE(ERR_JSON_CREAT_OBJ, 39, "创建JSON对象失败") \ + ERR_CODE(ERR_JSON_PRASE_OBJ, 40, "解析JSON对象失败") \ + ERR_CODE(ERR_JSON_VALID_SCH, 41, "JSON数据验证失败") \ + ERR_CODE(ERR_CREATE_NETIF, 42, "创建网络接口失败") \ + ERR_CODE(ERR_CREATE_PPPOE_NETIF, 43, "创建PPPoE网络接口失败") \ + ERR_CODE(ERR_CREATE_PPP_SESSION, 44, "创建PPP连接失败") \ + ERR_CODE(ERR_MISC_GET_IPADDR, 45, "获取网卡IP地址失败") \ + ERR_CODE(ERR_MISC_GET_NETMASK, 46, "获取网卡子网掩码失败") \ + ERR_CODE(ERR_MISC_GET_GATEWAY, 47, "获取网卡网关地址失败") \ + ERR_CODE(ERR_MISC_GET_MACADDR, 48, "获取网卡MAC地址失败") \ + ERR_CODE(ERR_MENU_EXIT, 49, "菜单执行完后自动退出") \ + ERR_CODE(ERR_HTTP_UNSUP_METHOD, 50, "不支持的 HTTP 请求方法") \ + ERR_CODE(ERR_HTTP_UNSUP_PAGE, 51, "找不到 HTTP 服务") \ + ERR_CODE(ERR_PROTO_DECODE, 52, "HTTP 协议解析失败") #define GENERATE_ENUM(ENUM, no, x) ENUM, diff --git a/srcs/libs/include/zlog_module.h b/srcs/libs/include/zlog_module.h index 90d6fcd..27c7ff8 100644 --- a/srcs/libs/include/zlog_module.h +++ b/srcs/libs/include/zlog_module.h @@ -65,6 +65,9 @@ typedef enum { #define LOG_MOD(level, mod, format, ...) \ do { \ 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__); \ } \ } while (0) @@ -72,12 +75,16 @@ typedef enum { #define LOG_MOD_HEX(level, mod, format, ...) \ do { \ 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__); \ } \ } while (0) zlog_category_t *zlog_get_mod_cat(ZLOG_MOD_NAME logMod); int zlog_verify_level(int level, ZLOG_MOD_NAME logMod); +const char *zlog_vni_tag_get(); #ifdef __cplusplus } #endif diff --git a/srcs/libs/init/init_runtime.c b/srcs/libs/init/init_runtime.c index 7eac072..19660fe 100644 --- a/srcs/libs/init/init_runtime.c +++ b/srcs/libs/init/init_runtime.c @@ -40,6 +40,7 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK UT_string *pPath; char bufCfgFile[MAX_PATH]; char bufCfgDir[MAX_PATH]; + char pidfile[MAX_PATH]; 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); +#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()) { banner_show(); } @@ -148,5 +160,6 @@ void user_uninit() { zlog_fini(); uninit_config_system(); uv_loop_close(get_task_manager()); + process_unlock_pidfile(); } } diff --git a/srcs/libs/misc/unique_instance.c b/srcs/libs/misc/unique_instance.c new file mode 100644 index 0000000..6ca88b8 --- /dev/null +++ b/srcs/libs/misc/unique_instance.c @@ -0,0 +1,34 @@ +// +// Created by xajhuang on 2023/3/2. +// +#include +#include +#include +#include +#include +#include +#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); +} \ No newline at end of file diff --git a/srcs/libs/zlog_module/zlog_module.c b/srcs/libs/zlog_module/zlog_module.c index b739cb2..647d921 100644 --- a/srcs/libs/zlog_module/zlog_module.c +++ b/srcs/libs/zlog_module/zlog_module.c @@ -4,6 +4,7 @@ #include #include "zlog_module.h" #include "misc.h" +#include "config.h" #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"} }; +#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) { if (logMod >= ARRAY_SIZE(g_zlogModule) || logMod < 0) { logMod = ARRAY_SIZE(g_zlogModule) - 1; diff --git a/unit_test/CMakeLists.txt b/unit_test/CMakeLists.txt index 43f1b8e..35b590e 100644 --- a/unit_test/CMakeLists.txt +++ b/unit_test/CMakeLists.txt @@ -8,7 +8,7 @@ ADD_DEFINITIONS(-DPROJECT_NAME="${PROJECT_NAME}") ADD_EXECUTABLE(${PROJECT_NAME} ${UTEST_SRC}) -TARGET_LINK_LIBRARIES(${PROJECT_NAME} common) +TARGET_LINK_LIBRARIES(${PROJECT_NAME} common ${COMMON_LIBS}) #INCLUDE(CTest) #INCLUDE(doctest)