From 9dc9d645aaf0c75c3da4ce726dd9a55e73d37dfc Mon Sep 17 00:00:00 2001 From: huangxin Date: Mon, 7 Nov 2022 14:22:12 +0800 Subject: [PATCH] =?UTF-8?q?OCT=201.=20=E6=9B=B4=E6=96=B0=E6=8E=A7=E5=88=B6?= =?UTF-8?q?=E5=8F=B0=E5=91=BD=E4=BB=A4=E6=8E=A5=E5=8F=A3=EF=BC=8C=E6=94=AF?= =?UTF-8?q?=E6=8C=81dhcp=E6=9C=8D=E5=8A=A1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- srcs/libs/cmdline/cmd_menu.c | 83 +++++++++++++++++++++++++++++++- srcs/libs/include/task_manager.h | 6 ++- srcs/libs/init/init.c | 10 ++-- srcs/libs/task/task_manager.c | 50 +++++++++++++++++-- srcs/vcpe_main.c | 8 ++- 5 files changed, 147 insertions(+), 10 deletions(-) diff --git a/srcs/libs/cmdline/cmd_menu.c b/srcs/libs/cmdline/cmd_menu.c index 6d8226b..f5a0f17 100644 --- a/srcs/libs/cmdline/cmd_menu.c +++ b/srcs/libs/cmdline/cmd_menu.c @@ -19,6 +19,7 @@ #include "config.h" #include "s2j/s2j.h" #include "msg_queue.h" +#include "../../open_dhcp/dhcpd.h" #define REG_ICASE (ARG_REX_ICASE) @@ -358,6 +359,72 @@ static int on_cmd6(void *pTbl[], const char *pName, void *pInfo) { return ERR_MENU_EXIT; } +typedef struct { + int mode; + const char *pIni; + const char *pStatus; + const char *pIfName; +} DHCPD_SETUP, *PDHCPD_SETUP; + +static void dhcpd_task(void *pArg) { + PDHCPD_SETUP p = (PDHCPD_SETUP)pArg; + + if (p) { + dhcpd_main(p->mode, p->pIni, p->pStatus, p->pIfName); + + if (p->pIni) { + free((void *)p->pIni); + } + + if (p->pStatus) { + free((void *)p->pStatus); + } + + if (p->pIfName) { + free((void *)p->pIfName); + } + + free(p); + } +} + +static int on_cmd7(void *pTbl[], const char *pName, void *pInfo) { + int ret; + PDHCPD_SETUP p; + static uv_thread_t uvThread; + PARG_TBL_INFO pArg = (PARG_TBL_INFO)pInfo; + int mode = ((arg_lit_t *)pTbl[1])->count; + const char *pCfgFile = ((arg_file_t *)pTbl[2])->filename[0]; + const char *pCfgDir = ((arg_file_t *)pTbl[3])->filename[0]; + const char *piniFile = ((arg_file_t *)pTbl[4])->filename[0]; + const char *pstatFile = ((arg_file_t *)pTbl[5])->filename[0]; + const char *pIfName = ((arg_str_t *)pTbl[6])->sval[0]; + const char *pKey = ((arg_str_t *)pTbl[7])->sval[0]; + int help = get_help_cmd(pTbl, pArg)->count; + + if (pArg->pHelp && help > 0) { + pArg->pHelp(pArg->argTbl, pName, pTbl); + return ERR_MENU_EXIT; + } + + if ((ret = user_init(pCfgFile, pCfgDir, pKey, 0)) != ERR_SUCCESS) { + printf("System init error: %d\n", ret); + return ret; + } + + p = (PDHCPD_SETUP)malloc(sizeof(DHCPD_SETUP)); + + if (p) { + p->mode = mode; + p->pIni = piniFile ? strdup(piniFile) : NULL; + p->pStatus = pstatFile ? strdup(pstatFile) : NULL; + p->pIfName = pIfName ? strdup(pIfName) : NULL; + uv_thread_create(&uvThread, dhcpd_task, p); + } + + return ERR_SUCCESS; +} + static int on_cmd_(void *pTbl[], const char *pName, void *pInfo) { PARG_TBL_INFO pArg = (PARG_TBL_INFO)pInfo; int help = get_help_cmd(pTbl, pArg)->count; @@ -418,7 +485,7 @@ int menu_run(int argc, char **argv) { int errCode2 = 0; arg_rex_t *cmd3 = arg_rex1(NULL, NULL, "base64", NULL, REG_ICASE, NULL); - arg_lit_t *operate3 = arg_litn("d", "operate", 0, ARG_MAX_FLAG, "\tBase64 decode, otherwise mean base64 encode"); + arg_lit_t *operate3 = arg_litn("o", "operate", 0, ARG_MAX_FLAG, "\tBase64 decode, otherwise mean base64 encode"); arg_str_t *value3 = arg_str0(NULL, NULL, "", NULL); arg_lit_t *helpCmd3 = arg_lit0("h", "help", NULL); arg_end_t *end3 = arg_end(20); @@ -478,6 +545,19 @@ int menu_run(int argc, char **argv) { void *pArgTbl6[] = {cmd6, keyGen, helpCmd6, end6}; int errCode6 = 0; + arg_rex_t *cmd7 = arg_rex1(NULL, NULL, "dhcpd", NULL, REG_ICASE, NULL); + arg_lit_t *operate7 = arg_lit0("m", "mode", "\tService works on verbatim mode."); + arg_file_t *cfgfile7 = arg_file1("c", "config", "", "\tSpecify the current application configuration file path"); + arg_file_t *cfgdir7 = arg_file1("d", "directory", "", "\tSpecify the configuration directory path"); + arg_str_t *key7 = arg_str0("k", "key", "Key", "\tSpecify the configuration file cryptographic key."); + arg_file_t *inifile = arg_filen("i", "ini", "", 0, 1, "\tSpecify the dhcpd service configuration file path"); + arg_file_t *statfile = arg_filen("s", "state", "", 0, 1, "\tSpecify the dhcpd service state cache file path"); + arg_str_t *ifname = arg_str1("n", "nic", "", "\tSpecify the dhcpd service network interface name."); + arg_lit_t *helpCmd7 = arg_lit0("h", "help", NULL); + arg_end_t *end7 = arg_end(20); + void *pArgTbl7[] = {cmd7, operate7, cfgfile7, cfgdir7, inifile, statfile, ifname, key7, helpCmd7, end7}; + int errCode7 = 0; + /***************************************************************** * 系统帮助参数定义 ******************************************************************/ @@ -489,6 +569,7 @@ int menu_run(int argc, char **argv) { ARG_TBL_INFO argTblInfo[] = { {pArgTbl1, ARRAY_SIZE(pArgTbl1), &errCode1, on_cmd1, cmdn_help, "\r \033[1;4;36mstart\033[0m: \033[1mRun agent service normally.\033[0m\n"}, + {pArgTbl7, ARRAY_SIZE(pArgTbl7), &errCode7, on_cmd7, cmdn_help, "\n \033[1;4;36mdhcpd\033[0m: \033[1mRun DHCP service normally.\033[0m\n"}, {pArgTbl2, ARRAY_SIZE(pArgTbl2), &errCode2, on_cmd2, cmd2_help, "\n \033[1;4;36mif\033[0m: \033[1mRun agent interface tools.\033[0m\n"}, {pArgTbl3, ARRAY_SIZE(pArgTbl3), &errCode3, on_cmd3, cmdn_help, "\n \033[1;4;36mbase64\033[0m: \033[1mString base64 encode/decode.\033[0m\n"}, {pArgTbl4, ARRAY_SIZE(pArgTbl4), &errCode4, on_cmd4, cmdn_help, "\n \033[1;4;36mhash\033[0m: \033[1mCalculate file's hash value.\033[0m\n"}, diff --git a/srcs/libs/include/task_manager.h b/srcs/libs/include/task_manager.h index cc6965d..7f2c71d 100644 --- a/srcs/libs/include/task_manager.h +++ b/srcs/libs/include/task_manager.h @@ -12,9 +12,13 @@ extern "C" { #define LIBUV_CURRENT_TIME_S() (uv_hrtime() / 1000000000) -void task_manager_run(); +typedef void (*system_exit_cb)(void *); + +void task_manager_run(void); uv_loop_t *get_task_manager(void); void task_manager_exit(); +int is_system_cleanup(); +int task_add_exit_event_handler(system_exit_cb cb, void *userdata); #ifdef __cplusplus } diff --git a/srcs/libs/init/init.c b/srcs/libs/init/init.c index 0f52bd0..f4dbe09 100644 --- a/srcs/libs/init/init.c +++ b/srcs/libs/init/init.c @@ -16,8 +16,10 @@ #include "crypto.h" #include "hardware.h" #include "msg_queue.h" +#include "http_svr.h" +#include "haywire.h" -#define DEFAULT_CONFIG_FILE ("agent.cfg") +#define DEFAULT_CONFIG_FILE ("vcpe.cfg") #define DEFAULT_CONFIG_DIR ("config") static pid_t g_pid; @@ -25,9 +27,8 @@ static pid_t g_pid; static void catch_system_interupt(int UNUSED(sig_num)) { if (g_pid == uv_os_getpid()) { printf("\nSystem close, clearing system resources..........\n"); - user_uninit(); + task_manager_exit(); sleep(1); - exit(0); } } @@ -115,11 +116,14 @@ int user_init(const char *pAppCfgFile, const char *pCfgDirectory, const char *pK dzlog_error("Message queue init error: %d\n", ret); } + http_svr_init(); + return ERR_SUCCESS; } void user_uninit() { task_manager_exit(); + free_http_server(); zlog_fini(); mq_uninit(); uv_loop_close(get_task_manager()); diff --git a/srcs/libs/task/task_manager.c b/srcs/libs/task/task_manager.c index 52c3bd0..be7b626 100644 --- a/srcs/libs/task/task_manager.c +++ b/srcs/libs/task/task_manager.c @@ -2,11 +2,22 @@ // Created by xajhu on 2021/7/1 0001. // +#include #include "task_manager.h" #include "misc.h" +#include "uthash/utlist.h" +#include "user_errno.h" -static uv_loop_t *g_pMainTaskLoop = NULL; -static int g_taskManagerExit = FALSE; +typedef struct SYSTEM_EXIT_CONTENT { + system_exit_cb cb; + void *pUserData; + struct SYSTEM_EXIT_CONTENT *next, *prev; +} SYSTEM_EXIT_CONTENT, *PSYSTEM_EXIT_CONTENT; + +static uv_loop_t *g_pMainTaskLoop = NULL; +static int g_taskManagerExit = FALSE; +static int g_isSystemExit = FALSE; +static PSYSTEM_EXIT_CONTENT g_sysExitCtx = NULL; uv_loop_t *get_task_manager(void) { if (g_pMainTaskLoop == NULL) { @@ -20,12 +31,35 @@ void task_manager_exit() { g_taskManagerExit = TRUE; } +int is_system_cleanup() { + return g_isSystemExit; +} + static int is_task_exit() { return (g_taskManagerExit == TRUE); } +int task_add_exit_event_handler(system_exit_cb cb, void *userdata) { + PSYSTEM_EXIT_CONTENT pCtx; + if (cb != NULL) { + pCtx = (PSYSTEM_EXIT_CONTENT)malloc(sizeof(SYSTEM_EXIT_CONTENT)); + + if (pCtx) { + pCtx->cb = cb; + pCtx->pUserData = userdata; + LL_APPEND(g_sysExitCtx, pCtx); + return ERR_SUCCESS; + } + + return -ERR_MALLOC_MEMORY; + } + + return ERR_SUCCESS; +} + void task_manager_run() { - int more; + int more; + PSYSTEM_EXIT_CONTENT pCtx, pTmp; if (g_pMainTaskLoop == NULL) { g_pMainTaskLoop = uv_default_loop(); @@ -43,5 +77,13 @@ void task_manager_run() { } } - printf("exit..............\n"); + LL_FOREACH_SAFE(g_sysExitCtx, pCtx, pTmp) { + pCtx->cb(pCtx->pUserData); + LL_DELETE(g_sysExitCtx, pCtx); + free(pCtx); + } + + g_isSystemExit = TRUE; + + printf("main task exit..............\n"); } \ No newline at end of file diff --git a/srcs/vcpe_main.c b/srcs/vcpe_main.c index 6948dbf..12b0592 100644 --- a/srcs/vcpe_main.c +++ b/srcs/vcpe_main.c @@ -4,6 +4,7 @@ #include #include #include +#include #include "cmdline.h" #include "task_manager.h" #include "init.h" @@ -13,7 +14,6 @@ #endif #ifdef OPENDHCPD_ON -#include "open_dhcp/dhcpd.h" #include "user_errno.h" #endif @@ -46,6 +46,7 @@ static void lwip_init_env() { int main(int argc, char **argv) { int ret; + #ifdef OPENDHCPDDNS_ON return dual_server_main(argc, argv); #elif OPENDHCPD_ON @@ -74,6 +75,11 @@ int main(int argc, char **argv) { #endif task_manager_run(); + + while(!is_system_cleanup()) { + sleep(1); + } + user_uninit(); return 0;