diff --git a/lib/command.c b/lib/command.c index a62bc48ac507..ecc082eaf624 100644 --- a/lib/command.c +++ b/lib/command.c @@ -1741,7 +1741,7 @@ static int file_write_config(struct vty *vty) vty_time_print(file_vty, 1); vty_out(file_vty, "!\n"); vty_write_config(file_vty); - vty_close(file_vty); + vty_close(file_vty, false); if (stat(config_file, &conf_stat) >= 0) { if (unlink(config_file_sav) != 0) diff --git a/lib/northbound_cli.c b/lib/northbound_cli.c index b498b9d9301e..527453bef15b 100644 --- a/lib/northbound_cli.c +++ b/lib/northbound_cli.c @@ -750,7 +750,7 @@ static int nb_write_config(struct nb_config *config, enum nb_cfg_format format, if (config) ret = nb_cli_show_config(file_vty, config, format, translator, false); - vty_close(file_vty); + vty_close(file_vty, false); return ret; } diff --git a/lib/vty.c b/lib/vty.c index cec8dd64b860..ba5bc78e4eff 100644 --- a/lib/vty.c +++ b/lib/vty.c @@ -196,7 +196,7 @@ void vty_mgmt_resume_response(struct vty *vty, int ret) } if (vty->status == VTY_CLOSE) - vty_close(vty); + vty_close(vty, false); else if (vty->type != VTY_FILE) vty_event(VTYSH_READ, vty); else @@ -1611,7 +1611,7 @@ static void vty_read(struct event *thread) /* Check status. */ if (vty->status == VTY_CLOSE) - vty_close(vty); + vty_close(vty, false); else { vty_event(VTY_WRITE, vty); vty_event(VTY_READ, vty); @@ -1648,11 +1648,11 @@ static void vty_flush(struct event *thread) vty->fd, vty->wfd); buffer_reset(vty->lbuf); buffer_reset(vty->obuf); - vty_close(vty); + vty_close(vty, false); return; case BUFFER_EMPTY: if (vty->status == VTY_CLOSE) - vty_close(vty); + vty_close(vty, false); else { vty->status = VTY_NORMAL; if (vty->lines == 0) @@ -1757,7 +1757,7 @@ static struct vty *vty_create(int vty_sock, union sockunion *su) if (host.password == NULL && host.password_encrypt == NULL) { vty_out(vty, "Vty password is not set.\n"); vty->status = VTY_CLOSE; - vty_close(vty); + vty_close(vty, false); return NULL; } } @@ -1853,7 +1853,7 @@ void vty_stdio_close(void) { if (!stdio_vty) return; - vty_close(stdio_vty); + vty_close(stdio_vty, false); } struct vty *vty_stdio(void (*atclose)(int isexit)) @@ -2208,7 +2208,7 @@ static int vtysh_flush(struct vty *vty) __func__, vty->fd); buffer_reset(vty->lbuf); buffer_reset(vty->obuf); - vty_close(vty); + vty_close(vty, false); return -1; case BUFFER_EMPTY: break; @@ -2286,7 +2286,7 @@ bool mgmt_vty_read_configs(void) vty->pending_allowed = false; if (!count) - vty_close(vty); + vty_close(vty, false); else vty_read_file_finish(vty, NULL); @@ -2334,7 +2334,7 @@ static void vtysh_read(struct event *thread) } buffer_reset(vty->lbuf); buffer_reset(vty->obuf); - vty_close(vty); + vty_close(vty, false); #ifdef VTYSH_DEBUG printf("close vtysh\n"); #endif /* VTYSH_DEBUG */ @@ -2422,7 +2422,7 @@ static void vtysh_read(struct event *thread) } if (vty->status == VTY_CLOSE) - vty_close(vty); + vty_close(vty, false); else vty_event(VTYSH_READ, vty); } @@ -2473,7 +2473,7 @@ static void vty_error_delete(void *arg) will be careful not to access the vty afterwards (since it has now been freed). This is safest from top-level functions (called directly by the thread dispatcher). */ -void vty_close(struct vty *vty) +void vty_close(struct vty *vty, bool shutdown) { int i; bool was_stdio = false; @@ -2489,7 +2489,8 @@ void vty_close(struct vty *vty) "vty closed, uncommitted config will be lost."); /* Drop out of configure / transaction if needed. */ - vty_config_exit(vty); + if (!shutdown) + vty_config_exit(vty); if (mgmt_fe_client && vty->mgmt_session_id) { debug_fe_client("closing vty session"); @@ -2574,7 +2575,7 @@ static void vty_timeout(struct event *thread) /* Close connection. */ vty->status = VTY_CLOSE; - vty_close(vty); + vty_close(vty, false); } /* Read up configuration file from file_name. */ @@ -2678,7 +2679,7 @@ void vty_read_file_finish(struct vty *vty, struct nb_config *config) __func__, nb_err_name(ret), errmsg); } - vty_close(vty); + vty_close(vty, false); } static FILE *vty_use_backup_config(const char *fullpath) @@ -3407,7 +3408,7 @@ void vty_reset(void) buffer_reset(vty->lbuf); buffer_reset(vty->obuf); vty->status = VTY_CLOSE; - vty_close(vty); + vty_close(vty, false); } vty_timeout_val = VTY_TIMEOUT_DEFAULT; @@ -3519,7 +3520,7 @@ static void vty_mgmt_session_notify(struct mgmt_fe_client *client, vty->mgmt_session_id = 0; /* We may come here by way of vty_close() and short-circuits */ if (vty->status != VTY_CLOSE) - vty_close(vty); + vty_close(vty, false); } } @@ -4187,7 +4188,7 @@ void vty_terminate(void) buffer_reset(vty->lbuf); buffer_reset(vty->obuf); vty->status = VTY_CLOSE; - vty_close(vty); + vty_close(vty, true); } vtys_fini(vtysh_sessions); diff --git a/lib/vty.h b/lib/vty.h index f185e710ed88..75088b5d64c7 100644 --- a/lib/vty.h +++ b/lib/vty.h @@ -393,7 +393,7 @@ 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_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 *vty, bool shutdown); extern char *vty_get_cwd(void); extern void vty_update_xpath(const char *oldpath, const char *newpath); extern int vty_config_enter(struct vty *vty, bool private_config, diff --git a/tests/bgpd/test_peer_attr.c b/tests/bgpd/test_peer_attr.c index 12c2f1103abe..2f7cd0c08a23 100644 --- a/tests/bgpd/test_peer_attr.c +++ b/tests/bgpd/test_peer_attr.c @@ -929,7 +929,7 @@ static void test_finish(struct test *test) /* Cleanup allocated memory. */ if (test->vty) { - vty_close(test->vty); + vty_close(test->vty, false); test->vty = NULL; } if (test->log) diff --git a/tests/lib/cli/test_commands.c b/tests/lib/cli/test_commands.c index ea84120fc1ea..a6ed4b73d909 100644 --- a/tests/lib/cli/test_commands.c +++ b/tests/lib/cli/test_commands.c @@ -394,7 +394,7 @@ int main(int argc, char **argv) } fprintf(stderr, "\nDone.\n"); - vty_close(vty); + vty_close(vty, false); prng_free(prng); test_terminate(); return 0; diff --git a/tests/lib/test_grpc.cpp b/tests/lib/test_grpc.cpp index 202313603d0e..db9007885fe1 100644 --- a/tests/lib/test_grpc.cpp +++ b/tests/lib/test_grpc.cpp @@ -146,7 +146,7 @@ static void static_startup(void) static void static_shutdown(void) { hook_call(test_grpc_fini); - vty_close(vty); + vty_close(vty, true); vrf_terminate(); vty_terminate(); cmd_terminate(); diff --git a/vtysh/vtysh.c b/vtysh/vtysh.c index 6fcd188cc5ae..6618360e464f 100644 --- a/vtysh/vtysh.c +++ b/vtysh/vtysh.c @@ -801,28 +801,28 @@ int vtysh_mark_file(const char *filename) fprintf(stderr, "line %d: Warning...: %s\n", lineno, vty->buf); fclose(confp); - vty_close(vty); + vty_close(vty, false); XFREE(MTYPE_VTYSH_CMD, vty_buf_copy); return ret; case CMD_ERR_AMBIGUOUS: fprintf(stderr, "line %d: %% Ambiguous command: %s\n", lineno, vty->buf); fclose(confp); - vty_close(vty); + vty_close(vty, false); XFREE(MTYPE_VTYSH_CMD, vty_buf_copy); return CMD_ERR_AMBIGUOUS; case CMD_ERR_NO_MATCH: fprintf(stderr, "line %d: %% Unknown command: %s\n", lineno, vty->buf); fclose(confp); - vty_close(vty); + vty_close(vty, false); XFREE(MTYPE_VTYSH_CMD, vty_buf_copy); return CMD_ERR_NO_MATCH; case CMD_ERR_INCOMPLETE: fprintf(stderr, "line %d: %% Command incomplete: %s\n", lineno, vty->buf); fclose(confp); - vty_close(vty); + vty_close(vty, false); XFREE(MTYPE_VTYSH_CMD, vty_buf_copy); return CMD_ERR_INCOMPLETE; case CMD_SUCCESS: @@ -848,7 +848,7 @@ int vtysh_mark_file(const char *filename) } /* This is the end */ vty_out(vty, "\nend\n"); - vty_close(vty); + vty_close(vty, false); XFREE(MTYPE_VTYSH_CMD, vty_buf_copy); if (confp != stdin) diff --git a/vtysh/vtysh_config.c b/vtysh/vtysh_config.c index 8a5c5237ba29..71ff07b7535a 100644 --- a/vtysh/vtysh_config.c +++ b/vtysh/vtysh_config.c @@ -645,7 +645,7 @@ static int vtysh_read_file(FILE *confp, bool dry_run) vtysh_execute_no_pager("end"); vtysh_execute_no_pager("disable"); - vty_close(vty); + vty_close(vty, false); return (ret); }