]> git.proxmox.com Git - mirror_frr.git/commitdiff
lib: mgmtd: add manual vty server start option and use it
authorChristian Hopps <chopps@labn.net>
Fri, 19 May 2023 08:25:57 +0000 (04:25 -0400)
committerChristian Hopps <chopps@labn.net>
Tue, 30 May 2023 06:10:20 +0000 (02:10 -0400)
Signed-off-by: Christian Hopps <chopps@labn.net>
lib/libfrr.c
lib/libfrr.h
lib/vty.c
lib/vty.h
mgmtd/mgmt_main.c
tests/helpers/c/main.c

index e8900572694ae813393339160a9a05f228449ae9..33237df5fca3a28b3f3bc6ba138d4f7c254407a7 100644 (file)
@@ -1036,7 +1036,7 @@ void frr_config_fork(void)
        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
         * (not currently set anywhere) */
@@ -1058,7 +1058,15 @@ static void frr_vty_serv(void)
                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)
@@ -1155,7 +1163,8 @@ void frr_run(struct event_loop *master)
 {
        char instanceinfo[64] = "";
 
-       frr_vty_serv();
+       if (!(di->flags & FRR_MANUAL_VTY_START))
+               frr_vty_serv_start();
 
        if (di->instance)
                snprintf(instanceinfo, sizeof(instanceinfo), "instance %u ",
index c05bc01e4ff22637a01c48e30332eb2aaeafae32..b260a54dfeb1e2e8896129583b9aa1a96093b5ab 100644 (file)
@@ -39,6 +39,11 @@ extern "C" {
  * Does nothing if -d isn't used.
  */
 #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);
 struct log_arg {
@@ -150,6 +155,8 @@ extern void frr_config_fork(void);
 
 extern void frr_run(struct event_loop *master);
 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,
                             const char *path);
index e763379efe08b1b93fbea50b4a3c85b7a16b0d7e..1e17f18a2332b967ca216dd1175e25c1ec1c01c7 100644 (file)
--- a/lib/vty.c
+++ b/lib/vty.c
@@ -2374,7 +2374,7 @@ static void vtysh_write(struct event *thread)
 #endif /* VTYSH */
 
 /* 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)
@@ -2385,6 +2385,20 @@ void vty_serv_sock(const char *addr, unsigned short port, const char *path)
 #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)
 {
        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;
 
        /* Start or stop listening for vty connections */
-       if (connected) {
-               zlog_info("mgmtd: starting vty servers");
+       if (connected)
                frr_vty_serv_start();
-       } else {
-               zlog_info("mgmtd: stopping vty servers");
+       else
                frr_vty_serv_stop();
-       }
 }
 
 /*
@@ -3849,7 +3860,6 @@ void vty_init(struct event_loop *master_thread, bool do_command_logging)
 void vty_terminate(void)
 {
        struct vty *vty;
-       struct vty_serv *vtyserv;
 
        if (mgmt_lib_hndl) {
                mgmt_fe_client_lib_destroy();
@@ -3875,12 +3885,5 @@ void vty_terminate(void)
        vtys_fini(vtysh_sessions);
        vtys_init(vtysh_sessions);
 
-       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);
+       vty_serv_stop();
 }
index d26531f781ca3f536a9b161dcdc468c03dca59ac..28f27d0d4777e60b208420f5586f5030e32ba9db 100644 (file)
--- a/lib/vty.h
+++ b/lib/vty.h
@@ -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_finish(struct vty *vty, struct nb_config *config);
 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 char *vty_get_cwd(void);
 extern void vty_update_xpath(const char *oldpath, const char *newpath);
index a705990cacd74855f533432b9324885aa5eb3e41..39362fa74a835a593b9727c79e933d9987d6c466 100644 (file)
@@ -216,7 +216,10 @@ FRR_DAEMON_INFO(mgmtd, MGMTD, .vty_port = MGMTD_VTY_PORT,
                .signals = mgmt_signals, .n_signals = array_size(mgmt_signals),
 
                .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 ""
 
index cd2b5665e2e1ee7b459897647f48e9ac7087f6bf..8af53a2ea40bde45ca6df1cc0c0417f760407a69 100644 (file)
@@ -152,7 +152,7 @@ int main(int argc, char **argv)
        }
 
        /* 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*/
        if (!config_file)