OCT 1. 增加进程唯一运行检测功能

This commit is contained in:
huangxin 2023-03-03 15:08:35 +08:00
parent 26359affa0
commit 2c9a9eeaf8
5 changed files with 99 additions and 44 deletions

View File

@ -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

View File

@ -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,

View File

@ -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

View File

@ -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();
}
}

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);
}