vcpe/srcs/libs/init/init_runtime.c

169 lines
4.8 KiB
C
Raw Normal View History

2022-05-10 06:43:27 +00:00
//
// Created by xajhu on 2021/7/5 0005.
//
#include <uv.h>
#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"
#include "http_svr.h"
2022-12-14 02:16:10 +00:00
#include "lib_config.h"
#include "prj_config.h"
2023-02-06 07:10:02 +00:00
#include "zlog_module.h"
2022-05-10 06:43:27 +00:00
#define DEFAULT_CONFIG_FILE ("vcpe.cfg")
2022-05-10 06:43:27 +00:00
#define DEFAULT_CONFIG_DIR ("config")
static pid_t g_pid;
static int g_isInited = FALSE;
static void catch_system_interupt(int UNUSED(sig_num)) {
if (g_pid == uv_os_getpid()) {
printf("\n");
LOG_MOD(warn, ZLOG_MOD_INIT, "System close, clearing system resources..........\n\n");
task_manager_exit();
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];
char pidfile[MAX_PATH];
2022-05-10 06:43:27 +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);
signal(SIGPIPE, SIG_IGN);
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);
}
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");
if (file_exists(utstring_body(pPath))) {
if ((ret = zlog_init(utstring_body(pPath))) != ERR_SUCCESS) {
printf("Zlog configure file [%s] init result: %d+++++\n", utstring_body(pPath), ret);
zlog_profile();
return -ERR_ZLOG_INIT;
} else {
LOG_MOD(info, ZLOG_MOD_INIT, "Zlog used configure file [%s]\n", utstring_body(pPath));
dzlog_init(utstring_body(pPath), get_cur_process_name());
// 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
} else {
printf("Zlog configure file [%s] not found, Zlog system not work+++++\n", utstring_body(pPath));
2022-05-10 06:43:27 +00:00
}
utstring_free(pPath);
// 处置化配置文件库,系统集成配置文件支持功能
LOG_MOD(info, ZLOG_MOD_INIT, "System used configure file [%s]\n", bufCfgFile);
if ((ret = init_config_system(bufCfgFile, pKey)) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Load system configuration error: %d\n", ret);
2022-05-10 06:43:27 +00:00
return -ERR_CONFIG_INIT;
}
LOG_MOD(info,
2023-02-06 07:10:02 +00:00
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
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);
}
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();
}
#ifdef ZEROMQ_ON
if ((ret = mq_init()) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
2022-05-10 06:43:27 +00:00
}
if ((ret = mq_data_init()) != ERR_SUCCESS) {
LOG_MOD(error, ZLOG_MOD_INIT, "Message queue init error: %d\n", ret);
}
#endif
#ifdef HTTPSERVER_ON
http_svr_init();
#endif
g_isInited = TRUE;
return ERR_SUCCESS;
2022-05-10 06:43:27 +00:00
}
void user_uninit() {
if (g_isInited) {
task_manager_exit();
#ifdef HTTPSERVER_ON
2023-02-21 07:17:49 +00:00
http_svr_uinit();
#endif
#ifdef ZEROMQ_ON
mq_uninit();
#endif
zlog_fini();
uninit_config_system();
uv_loop_close(get_task_manager());
process_unlock_pidfile();
}
2022-05-10 06:43:27 +00:00
}