2022-05-10 06:43:27 +00:00
|
|
|
|
//
|
|
|
|
|
// Created by xajhu on 2021/7/5 0005.
|
|
|
|
|
//
|
|
|
|
|
#include <uv.h>
|
2022-10-28 08:46:55 +00:00
|
|
|
|
#include <unistd.h>
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
|
|
#include "init.h"
|
|
|
|
|
#include "config.h"
|
|
|
|
|
#include "misc.h"
|
|
|
|
|
#include "user_errno.h"
|
|
|
|
|
#include "task_manager.h"
|
|
|
|
|
#include "uthash/utstring.h"
|
|
|
|
|
#include "banner.h"
|
|
|
|
|
#include "inet_misc.h"
|
|
|
|
|
#include "crypto.h"
|
|
|
|
|
#include "hardware.h"
|
|
|
|
|
#include "msg_queue.h"
|
2022-11-07 06:22:12 +00:00
|
|
|
|
#include "http_svr.h"
|
|
|
|
|
#include "haywire.h"
|
2022-12-14 02:16:10 +00:00
|
|
|
|
#include "lib_config.h"
|
2022-12-23 06:04:51 +00:00
|
|
|
|
#include "prj_config.h"
|
2023-02-06 07:10:02 +00:00
|
|
|
|
#include "zlog_module.h"
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
2022-11-07 06:22:12 +00:00
|
|
|
|
#define DEFAULT_CONFIG_FILE ("vcpe.cfg")
|
2022-05-10 06:43:27 +00:00
|
|
|
|
#define DEFAULT_CONFIG_DIR ("config")
|
|
|
|
|
|
2022-10-28 08:46:55 +00:00
|
|
|
|
static pid_t g_pid;
|
2022-12-03 08:46:52 +00:00
|
|
|
|
static int g_isInited = FALSE;
|
2022-10-28 08:46:55 +00:00
|
|
|
|
|
|
|
|
|
static void catch_system_interupt(int UNUSED(sig_num)) {
|
|
|
|
|
if (g_pid == uv_os_getpid()) {
|
2022-11-08 03:00:38 +00:00
|
|
|
|
printf("\n");
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(warn, ZLOG_MOD_INIT, "System close, clearing system resources..........\n\n");
|
2022-11-07 06:22:12 +00:00
|
|
|
|
task_manager_exit();
|
2022-10-28 08:46:55 +00:00
|
|
|
|
sleep(1);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-10 06:43:27 +00:00
|
|
|
|
int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pKey, int logLevel) {
|
|
|
|
|
int ret;
|
|
|
|
|
UT_string *pPath;
|
|
|
|
|
char bufCfgFile[MAX_PATH];
|
|
|
|
|
char bufCfgDir[MAX_PATH];
|
|
|
|
|
|
2022-10-28 08:46:55 +00:00
|
|
|
|
g_pid = uv_os_getpid();
|
|
|
|
|
|
|
|
|
|
signal(SIGINT, catch_system_interupt);
|
|
|
|
|
signal(SIGABRT, catch_system_interupt);
|
|
|
|
|
signal(SIGTERM, catch_system_interupt);
|
|
|
|
|
signal(SIGQUIT, catch_system_interupt);
|
|
|
|
|
signal(SIGTSTP, catch_system_interupt);
|
|
|
|
|
signal(SIGHUP, catch_system_interupt);
|
2023-01-28 02:20:27 +00:00
|
|
|
|
signal(SIGPIPE, SIG_IGN);
|
2022-10-28 08:46:55 +00:00
|
|
|
|
|
2022-05-10 06:43:27 +00:00
|
|
|
|
// 初始化 libuv loop
|
|
|
|
|
get_task_manager();
|
|
|
|
|
|
|
|
|
|
memset(bufCfgDir, 0, MAX_PATH);
|
|
|
|
|
memset(bufCfgFile, 0, MAX_PATH);
|
|
|
|
|
|
|
|
|
|
if (pCfgDirectory == NULL) {
|
|
|
|
|
sprintf(bufCfgDir, "%s/%s", get_cur_process_dir(), DEFAULT_CONFIG_DIR);
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(bufCfgDir, pCfgDirectory);
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 01:55:36 +00:00
|
|
|
|
if (pAppCfgFile == NULL || strlen(pAppCfgFile) == 0) {
|
2022-05-10 06:43:27 +00:00
|
|
|
|
const char *pTmp = strdup(bufCfgDir);
|
|
|
|
|
sprintf(bufCfgFile, "%s/%s", pTmp, DEFAULT_CONFIG_FILE);
|
|
|
|
|
free((void *)pTmp);
|
|
|
|
|
} else {
|
|
|
|
|
strcpy(bufCfgFile, pAppCfgFile);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 初始化zlog库,系统集成日志功能
|
|
|
|
|
utstring_new(pPath);
|
|
|
|
|
utstring_printf(pPath, "%s/%s", bufCfgDir, "zlog.conf");
|
|
|
|
|
|
2023-02-06 02:13:49 +00:00
|
|
|
|
if (file_exists(utstring_body(pPath))) {
|
|
|
|
|
if ((ret = dzlog_init(utstring_body(pPath), get_cur_process_name())) != ERR_SUCCESS) {
|
|
|
|
|
printf("Zlog configure file [%s] init result: %d+++++\n", utstring_body(pPath), ret);
|
|
|
|
|
zlog_profile();
|
|
|
|
|
return -ERR_ZLOG_INIT;
|
|
|
|
|
} else {
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(info, ZLOG_MOD_INIT, "Zlog used configure file [%s]\n", utstring_body(pPath));
|
2023-02-06 02:13:49 +00:00
|
|
|
|
}
|
2022-05-10 06:43:27 +00:00
|
|
|
|
} else {
|
2023-02-06 02:13:49 +00:00
|
|
|
|
printf("Zlog configure file [%s] not found, Zlog system not work+++++\n", utstring_body(pPath));
|
2022-05-10 06:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-21 07:48:57 +00:00
|
|
|
|
zlog_level_switch(zlog_get_category(get_cur_process_name()),
|
|
|
|
|
logLevel > ZLOG_LEVEL_DEBUG ? logLevel : ZLOG_LEVEL_DEBUG);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
|
|
utstring_free(pPath);
|
|
|
|
|
|
|
|
|
|
// 处置化配置文件库,系统集成配置文件支持功能
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(info, ZLOG_MOD_INIT, "System used configure file [%s]\n", bufCfgFile);
|
2022-06-01 03:35:42 +00:00
|
|
|
|
if ((ret = init_config_system(bufCfgFile, pKey)) != ERR_SUCCESS) {
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(error, ZLOG_MOD_INIT, "Load system configuration error: %d\n", ret);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
return -ERR_CONFIG_INIT;
|
|
|
|
|
}
|
|
|
|
|
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(info,
|
|
|
|
|
ZLOG_MOD_INIT,
|
|
|
|
|
"%s library version %s information: %s (Build: %s %s GCC Ver:%s) With %lu(bits) OS\n",
|
|
|
|
|
VCPE_LIB_NAME,
|
|
|
|
|
VCPE_LIB_VER,
|
|
|
|
|
VCPE_GIT_VERSION,
|
|
|
|
|
__DATE__,
|
|
|
|
|
__TIME__,
|
|
|
|
|
__VERSION__,
|
|
|
|
|
sizeof(int *) * 8);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
|
|
|
|
|
if (cfg_get_banner_enable()) {
|
|
|
|
|
banner_show();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
evp_system_init();
|
|
|
|
|
inet_api_init();
|
|
|
|
|
|
|
|
|
|
if (cfg_get_hardware_watch_enable()) {
|
|
|
|
|
init_hardware();
|
|
|
|
|
}
|
|
|
|
|
|
2022-06-01 03:35:42 +00:00
|
|
|
|
if ((ret = mq_init()) != ERR_SUCCESS) {
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
|
2022-05-10 06:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-06-13 10:49:12 +00:00
|
|
|
|
if ((ret = mq_data_init()) != ERR_SUCCESS) {
|
2023-02-06 07:10:02 +00:00
|
|
|
|
LOG_MSG(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
|
2022-06-10 10:18:37 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-11-07 06:22:12 +00:00
|
|
|
|
http_svr_init();
|
|
|
|
|
|
2022-12-03 08:46:52 +00:00
|
|
|
|
g_isInited = TRUE;
|
|
|
|
|
|
2022-06-01 03:35:42 +00:00
|
|
|
|
return ERR_SUCCESS;
|
2022-05-10 06:43:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void user_uninit() {
|
2022-12-03 08:46:52 +00:00
|
|
|
|
if (g_isInited) {
|
|
|
|
|
task_manager_exit();
|
|
|
|
|
free_http_server();
|
|
|
|
|
mq_uninit();
|
|
|
|
|
zlog_fini();
|
|
|
|
|
uninit_config_system();
|
|
|
|
|
uv_loop_close(get_task_manager());
|
|
|
|
|
}
|
2022-05-10 06:43:27 +00:00
|
|
|
|
}
|