OCT 1. 更新控制台命令接口,支持dhcp服务

This commit is contained in:
huangxin 2022-11-07 14:22:12 +08:00
parent 78d4550ec4
commit 9dc9d645aa
5 changed files with 147 additions and 10 deletions

View File

@ -19,6 +19,7 @@
#include "config.h" #include "config.h"
#include "s2j/s2j.h" #include "s2j/s2j.h"
#include "msg_queue.h" #include "msg_queue.h"
#include "../../open_dhcp/dhcpd.h"
#define REG_ICASE (ARG_REX_ICASE) #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; 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) { static int on_cmd_(void *pTbl[], const char *pName, void *pInfo) {
PARG_TBL_INFO pArg = (PARG_TBL_INFO)pInfo; PARG_TBL_INFO pArg = (PARG_TBL_INFO)pInfo;
int help = get_help_cmd(pTbl, pArg)->count; int help = get_help_cmd(pTbl, pArg)->count;
@ -418,7 +485,7 @@ int menu_run(int argc, char **argv) {
int errCode2 = 0; int errCode2 = 0;
arg_rex_t *cmd3 = arg_rex1(NULL, NULL, "base64", NULL, REG_ICASE, NULL); 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, "<base64 string>", NULL); arg_str_t *value3 = arg_str0(NULL, NULL, "<base64 string>", NULL);
arg_lit_t *helpCmd3 = arg_lit0("h", "help", NULL); arg_lit_t *helpCmd3 = arg_lit0("h", "help", NULL);
arg_end_t *end3 = arg_end(20); arg_end_t *end3 = arg_end(20);
@ -478,6 +545,19 @@ int menu_run(int argc, char **argv) {
void *pArgTbl6[] = {cmd6, keyGen, helpCmd6, end6}; void *pArgTbl6[] = {cmd6, keyGen, helpCmd6, end6};
int errCode6 = 0; 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", "<file>", "\tSpecify the current application configuration file path");
arg_file_t *cfgdir7 = arg_file1("d", "directory", "<dir>", "\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", "<file>", 0, 1, "\tSpecify the dhcpd service configuration file path");
arg_file_t *statfile = arg_filen("s", "state", "<file>", 0, 1, "\tSpecify the dhcpd service state cache file path");
arg_str_t *ifname = arg_str1("n", "nic", "<ifname>", "\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[] = { 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"}, {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"}, {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"}, {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"}, {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"},

View File

@ -12,9 +12,13 @@ extern "C" {
#define LIBUV_CURRENT_TIME_S() (uv_hrtime() / 1000000000) #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); uv_loop_t *get_task_manager(void);
void task_manager_exit(); void task_manager_exit();
int is_system_cleanup();
int task_add_exit_event_handler(system_exit_cb cb, void *userdata);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -16,8 +16,10 @@
#include "crypto.h" #include "crypto.h"
#include "hardware.h" #include "hardware.h"
#include "msg_queue.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") #define DEFAULT_CONFIG_DIR ("config")
static pid_t g_pid; static pid_t g_pid;
@ -25,9 +27,8 @@ static pid_t g_pid;
static void catch_system_interupt(int UNUSED(sig_num)) { static void catch_system_interupt(int UNUSED(sig_num)) {
if (g_pid == uv_os_getpid()) { if (g_pid == uv_os_getpid()) {
printf("\nSystem close, clearing system resources..........\n"); printf("\nSystem close, clearing system resources..........\n");
user_uninit(); task_manager_exit();
sleep(1); 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); dzlog_error("Message queue init error: %d\n", ret);
} }
http_svr_init();
return ERR_SUCCESS; return ERR_SUCCESS;
} }
void user_uninit() { void user_uninit() {
task_manager_exit(); task_manager_exit();
free_http_server();
zlog_fini(); zlog_fini();
mq_uninit(); mq_uninit();
uv_loop_close(get_task_manager()); uv_loop_close(get_task_manager());

View File

@ -2,11 +2,22 @@
// Created by xajhu on 2021/7/1 0001. // Created by xajhu on 2021/7/1 0001.
// //
#include <stdlib.h>
#include "task_manager.h" #include "task_manager.h"
#include "misc.h" #include "misc.h"
#include "uthash/utlist.h"
#include "user_errno.h"
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 uv_loop_t *g_pMainTaskLoop = NULL;
static int g_taskManagerExit = FALSE; static int g_taskManagerExit = FALSE;
static int g_isSystemExit = FALSE;
static PSYSTEM_EXIT_CONTENT g_sysExitCtx = NULL;
uv_loop_t *get_task_manager(void) { uv_loop_t *get_task_manager(void) {
if (g_pMainTaskLoop == NULL) { if (g_pMainTaskLoop == NULL) {
@ -20,12 +31,35 @@ void task_manager_exit() {
g_taskManagerExit = TRUE; g_taskManagerExit = TRUE;
} }
int is_system_cleanup() {
return g_isSystemExit;
}
static int is_task_exit() { static int is_task_exit() {
return (g_taskManagerExit == TRUE); 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() { void task_manager_run() {
int more; int more;
PSYSTEM_EXIT_CONTENT pCtx, pTmp;
if (g_pMainTaskLoop == NULL) { if (g_pMainTaskLoop == NULL) {
g_pMainTaskLoop = uv_default_loop(); 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");
} }

View File

@ -4,6 +4,7 @@
#include <uv.h> #include <uv.h>
#include <zlog.h> #include <zlog.h>
#include <stdlib.h> #include <stdlib.h>
#include <unistd.h>
#include "cmdline.h" #include "cmdline.h"
#include "task_manager.h" #include "task_manager.h"
#include "init.h" #include "init.h"
@ -13,7 +14,6 @@
#endif #endif
#ifdef OPENDHCPD_ON #ifdef OPENDHCPD_ON
#include "open_dhcp/dhcpd.h"
#include "user_errno.h" #include "user_errno.h"
#endif #endif
@ -46,6 +46,7 @@ static void lwip_init_env() {
int main(int argc, char **argv) { int main(int argc, char **argv) {
int ret; int ret;
#ifdef OPENDHCPDDNS_ON #ifdef OPENDHCPDDNS_ON
return dual_server_main(argc, argv); return dual_server_main(argc, argv);
#elif OPENDHCPD_ON #elif OPENDHCPD_ON
@ -74,6 +75,11 @@ int main(int argc, char **argv) {
#endif #endif
task_manager_run(); task_manager_run();
while(!is_system_cleanup()) {
sleep(1);
}
user_uninit(); user_uninit();
return 0; return 0;