From dbf78092dae5717334dbb0f20fa540a47fbebb70 Mon Sep 17 00:00:00 2001 From: David Lamparter Date: Tue, 12 May 2015 21:56:18 +0200 Subject: [PATCH] lib/vty: add vty_stdio at-close hook This is intended to be used for either "exit on close", "fork on close" or "reopen vty on close" functionality for the stdio vty. Which of these options to take depends on the context, the use case right now is test programs exiting on EOF. Signed-off-by: David Lamparter (cherry picked from commit 464ccf36b4aa1b942cad413ea30267b4bf9e6315) --- lib/vty.c | 8 +++++++- lib/vty.h | 2 +- 2 files changed, 8 insertions(+), 2 deletions(-) diff --git a/lib/vty.c b/lib/vty.c index d202f72fa3..65bb28bb29 100644 --- a/lib/vty.c +++ b/lib/vty.c @@ -1704,6 +1704,7 @@ vty_create (int vty_sock, union sockunion *su) /* create vty for stdio */ static struct termios stdio_orig_termios; static struct vty *stdio_vty = NULL; +static void (*stdio_vty_atclose)(void); static void vty_stdio_reset (void) @@ -1712,11 +1713,15 @@ vty_stdio_reset (void) { tcsetattr (0, TCSANOW, &stdio_orig_termios); stdio_vty = NULL; + + if (stdio_vty_atclose) + stdio_vty_atclose (); + stdio_vty_atclose = NULL; } } struct vty * -vty_stdio (void) +vty_stdio (void (*atclose)()) { struct vty *vty; struct termios termios; @@ -1726,6 +1731,7 @@ vty_stdio (void) return NULL; vty = stdio_vty = vty_new_init (0); + stdio_vty_atclose = atclose; vty->wfd = 1; /* always have stdio vty in a known _unchangeable_ state, don't want config diff --git a/lib/vty.h b/lib/vty.h index a248680dbc..81251e07bb 100644 --- a/lib/vty.h +++ b/lib/vty.h @@ -273,7 +273,7 @@ extern void vty_init_vtysh (void); extern void vty_terminate (void); extern void vty_reset (void); extern struct vty *vty_new (void); -extern struct vty *vty_stdio (void); +extern struct vty *vty_stdio (void (*atclose)(void)); extern int vty_out (struct vty *, const char *, ...) PRINTF_ATTRIBUTE(2, 3); extern void vty_read_config (char *, char *); extern void vty_time_print (struct vty *, int); -- 2.39.5