lib: mgmtd: add manual vty server start option and use it

Signed-off-by: Christian Hopps <chopps@labn.net>
This commit is contained in:
Christian Hopps 2023-05-19 04:25:57 -04:00
parent e13a5c4165
commit 5ba5613077
6 changed files with 44 additions and 21 deletions

View File

@ -1036,7 +1036,7 @@ void frr_config_fork(void)
zlog_tls_buffer_init(); zlog_tls_buffer_init();
} }
static void frr_vty_serv(void) void frr_vty_serv_start(void)
{ {
/* allow explicit override of vty_path in the future /* allow explicit override of vty_path in the future
* (not currently set anywhere) */ * (not currently set anywhere) */
@ -1058,7 +1058,15 @@ static void frr_vty_serv(void)
di->vty_path = vtypath_default; di->vty_path = vtypath_default;
} }
vty_serv_sock(di->vty_addr, di->vty_port, di->vty_path); vty_serv_start(di->vty_addr, di->vty_port, di->vty_path);
}
void frr_vty_serv_stop(void)
{
vty_serv_stop();
if (di->vty_path)
unlink(di->vty_path);
} }
static void frr_check_detach(void) static void frr_check_detach(void)
@ -1155,7 +1163,8 @@ void frr_run(struct event_loop *master)
{ {
char instanceinfo[64] = ""; char instanceinfo[64] = "";
frr_vty_serv(); if (!(di->flags & FRR_MANUAL_VTY_START))
frr_vty_serv_start();
if (di->instance) if (di->instance)
snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ", snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ",

View File

@ -39,6 +39,11 @@ extern "C" {
* Does nothing if -d isn't used. * Does nothing if -d isn't used.
*/ */
#define FRR_DETACH_LATER (1 << 6) #define FRR_DETACH_LATER (1 << 6)
/* If FRR_MANUAL_VTY_START is used, frr_run() will not automatically start
* listening on for vty connection (either TCP or Unix socket based). The daemon
* is responsible for calling frr_vty_serv() itself.
*/
#define FRR_MANUAL_VTY_START (1 << 7)
PREDECL_DLIST(log_args); PREDECL_DLIST(log_args);
struct log_arg { struct log_arg {
@ -150,6 +155,8 @@ extern void frr_config_fork(void);
extern void frr_run(struct event_loop *master); extern void frr_run(struct event_loop *master);
extern void frr_detach(void); extern void frr_detach(void);
extern void frr_vty_serv_start(void);
extern void frr_vty_serv_stop(void);
extern bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len, extern bool frr_zclient_addr(struct sockaddr_storage *sa, socklen_t *sa_len,
const char *path); const char *path);

View File

@ -2374,7 +2374,7 @@ static void vtysh_write(struct event *thread)
#endif /* VTYSH */ #endif /* VTYSH */
/* Determine address family to bind. */ /* Determine address family to bind. */
void vty_serv_sock(const char *addr, unsigned short port, const char *path) void vty_serv_start(const char *addr, unsigned short port, const char *path)
{ {
/* If port is set to 0, do not listen on TCP/IP at all! */ /* If port is set to 0, do not listen on TCP/IP at all! */
if (port) if (port)
@ -2385,6 +2385,20 @@ void vty_serv_sock(const char *addr, unsigned short port, const char *path)
#endif /* VTYSH */ #endif /* VTYSH */
} }
void vty_serv_stop(void)
{
struct vty_serv *vtyserv;
while ((vtyserv = vtyservs_pop(vty_servs))) {
EVENT_OFF(vtyserv->t_accept);
close(vtyserv->sock);
XFREE(MTYPE_VTY_SERV, vtyserv);
}
vtyservs_fini(vty_servs);
vtyservs_init(vty_servs);
}
static void vty_error_delete(void *arg) static void vty_error_delete(void *arg)
{ {
struct vty_error *ve = arg; struct vty_error *ve = arg;
@ -3393,13 +3407,10 @@ static void vty_mgmt_server_connected(uintptr_t lib_hndl, uintptr_t usr_data,
mgmt_fe_connected = connected; mgmt_fe_connected = connected;
/* Start or stop listening for vty connections */ /* Start or stop listening for vty connections */
if (connected) { if (connected)
zlog_info("mgmtd: starting vty servers");
frr_vty_serv_start(); frr_vty_serv_start();
} else { else
zlog_info("mgmtd: stopping vty servers");
frr_vty_serv_stop(); frr_vty_serv_stop();
}
} }
/* /*
@ -3849,7 +3860,6 @@ void vty_init(struct event_loop *master_thread, bool do_command_logging)
void vty_terminate(void) void vty_terminate(void)
{ {
struct vty *vty; struct vty *vty;
struct vty_serv *vtyserv;
if (mgmt_lib_hndl) { if (mgmt_lib_hndl) {
mgmt_fe_client_lib_destroy(); mgmt_fe_client_lib_destroy();
@ -3875,12 +3885,5 @@ void vty_terminate(void)
vtys_fini(vtysh_sessions); vtys_fini(vtysh_sessions);
vtys_init(vtysh_sessions); vtys_init(vtysh_sessions);
while ((vtyserv = vtyservs_pop(vty_servs))) { vty_serv_stop();
EVENT_OFF(vtyserv->t_accept);
close(vtyserv->sock);
XFREE(MTYPE_VTY_SERV, vtyserv);
}
vtyservs_fini(vty_servs);
vtyservs_init(vty_servs);
} }

View File

@ -385,7 +385,8 @@ extern bool vty_read_config(struct nb_config *config, const char *config_file,
extern void vty_read_file(struct nb_config *config, FILE *confp); extern void vty_read_file(struct nb_config *config, FILE *confp);
extern void vty_read_file_finish(struct vty *vty, struct nb_config *config); extern void vty_read_file_finish(struct vty *vty, struct nb_config *config);
extern void vty_time_print(struct vty *, int); extern void vty_time_print(struct vty *, int);
extern void vty_serv_sock(const char *, unsigned short, const char *); extern void vty_serv_start(const char *, unsigned short, const char *);
extern void vty_serv_stop(void);
extern void vty_close(struct vty *); extern void vty_close(struct vty *);
extern char *vty_get_cwd(void); extern char *vty_get_cwd(void);
extern void vty_update_xpath(const char *oldpath, const char *newpath); extern void vty_update_xpath(const char *oldpath, const char *newpath);

View File

@ -216,7 +216,10 @@ FRR_DAEMON_INFO(mgmtd, MGMTD, .vty_port = MGMTD_VTY_PORT,
.signals = mgmt_signals, .n_signals = array_size(mgmt_signals), .signals = mgmt_signals, .n_signals = array_size(mgmt_signals),
.privs = &mgmt_privs, .yang_modules = mgmt_yang_modules, .privs = &mgmt_privs, .yang_modules = mgmt_yang_modules,
.n_yang_modules = array_size(mgmt_yang_modules)); .n_yang_modules = array_size(mgmt_yang_modules),
/* avoid libfrr trying to read our config file for us */
.flags = FRR_MANUAL_VTY_START);
#define DEPRECATED_OPTIONS "" #define DEPRECATED_OPTIONS ""

View File

@ -152,7 +152,7 @@ int main(int argc, char **argv)
} }
/* Create VTY socket */ /* Create VTY socket */
vty_serv_sock(vty_addr, vty_port, "/tmp/.heavy.sock"); vty_serv_start(vty_addr, vty_port, "/tmp/.heavy.sock");
/* Configuration file read*/ /* Configuration file read*/
if (!config_file) if (!config_file)