summaryrefslogtreecommitdiffstats
path: root/lib/vty_io.c
diff options
context:
space:
mode:
Diffstat (limited to 'lib/vty_io.c')
-rw-r--r--lib/vty_io.c3351
1 files changed, 1249 insertions, 2102 deletions
diff --git a/lib/vty_io.c b/lib/vty_io.c
index 2eadc2d1..96df3850 100644
--- a/lib/vty_io.c
+++ b/lib/vty_io.c
@@ -1,4 +1,4 @@
-/* VTY IO Functions
+/* VTY IO Functions -- top level of VTY IO hierarchy
* Copyright (C) 1997, 98 Kunihiro Ishiguro
*
* Revisions: Copyright (C) 2010 Chris Hall (GMCH), Highwayman
@@ -20,14 +20,19 @@
* Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
* 02111-1307, USA.
*/
-
-#include "zebra.h"
+#include "misc.h"
#include "vty.h"
#include "vty_io.h"
+#include "vty_io_term.h"
+#include "vty_io_file.h"
#include "vty_cli.h"
+#include "vty_command.h"
#include "qstring.h"
#include "keystroke.h"
+#include "list_util.h"
+#include "command_parse.h"
+#include "command_execute.h"
#include "memory.h"
@@ -40,148 +45,37 @@
#include <arpa/telnet.h>
#include <sys/un.h> /* for VTYSH */
#include <sys/socket.h>
+#include <wait.h>
#define VTYSH_DEBUG 0
/*==============================================================================
- * VTY Command Output -- base functions
- *
- * During command processing the output sent here is held until the command
- * completes.
+ * Basic output to VTY.
*/
-static int uty_config_write(vty_io vio, bool all) ;
-
/*------------------------------------------------------------------------------
- * VTY output function -- cf fprintf
+ * VTY output -- cf fprintf ! Same as vty_out, less the VTY_LOCK().
*
- * Returns: >= 0 => OK
- * < 0 => failed (see errno)
- */
-extern int
-uty_out (struct vty *vty, const char *format, ...)
-{
- int result;
- VTY_ASSERT_LOCKED() ;
- va_list args;
- va_start (args, format);
- result = uty_vout(vty, format, args);
- va_end (args);
- return result;
-}
-
-/*------------------------------------------------------------------------------
- * VTY output function -- cf vfprintf
+ * This is for command output, which may later be suppressed
*
* Returns: >= 0 => OK
* < 0 => failed (see errno)
- *
- * NB: for VTY_TERM and for VTY_SHELL_SERV -- this is command output:
- *
- * * MAY NOT do any command output if !cmd_enabled
- *
- * * first, the life of a vty is not guaranteed unless cmd_in_progress,
- * so should not attempt to use a vty anywhere other than command
- * execution.
- *
- * * second, cmd_out_enabled is false most of the time, and is only
- * set true when a command completes, and it is time to write away
- * the results.
- *
- * * all output is placed in the vio->cmd_obuf. When the command completes,
- * the contents of the cmd_obuf will be written away -- subject to line
- * control.
- *
- * * output is discarded if the vty is no longer write_open
*/
extern int
-uty_vout(struct vty *vty, const char *format, va_list args)
+uty_out(vty_io vio, const char *format, ...)
{
- vty_io vio ;
- int ret ;
+ int ret ;
+ va_list args ;
VTY_ASSERT_LOCKED() ;
- vio = vty->vio ;
-
- switch (vio->type)
- {
- case VTY_STDOUT:
- case VTY_SHELL:
- ret = vprintf (format, args) ;
- break ;
-
- case VTY_STDERR:
- ret = vfprintf (stderr, format, args) ;
- break ;
-
- case VTY_CONFIG_WRITE:
- ret = vio_fifo_vprintf(&vio->cmd_obuf, format, args) ;
- if ((ret > 0) && vio_fifo_full_lump(&vio->cmd_obuf))
- ret = uty_config_write(vio, false) ;
- break ;
-
- case VTY_TERM:
- case VTY_SHELL_SERV:
- assert(vio->cmd_in_progress) ;
-
- if (!vio->sock.write_open)
- return 0 ; /* discard output if not open ! */
-
- /* fall through.... */
-
- case VTY_CONFIG_READ:
- ret = vio_fifo_vprintf(&vio->cmd_obuf, format, args) ;
- break ;
-
- default:
- zabort("impossible VTY type") ;
- } ;
+ va_start (args, format) ;
+ ret = uty_vprintf(vio, format, args) ;
+ va_end (args) ;
return ret ;
} ;
-/*------------------------------------------------------------------------------
- * Clear the contents of the command output FIFO etc.
- *
- * NB: does not change any of the cli_blocked/cmd_in_progress/cli_wait_more/etc
- * flags -- competent parties must deal with those
- */
-extern void
-uty_out_clear(vty_io vio)
-{
- VTY_ASSERT_LOCKED() ;
-
- vio_fifo_clear(&vio->cmd_obuf) ;
-
- if (vio->cmd_lc != NULL)
- vio_lc_clear(vio->cmd_lc) ;
-} ;
-
-/*------------------------------------------------------------------------------
- * Flush the contents of the command output FIFO to the given file.
- *
- * Takes no notice of any errors !
- */
-extern void
-uty_out_fflush(vty_io vio, FILE* file)
-{
- char* src ;
- size_t have ;
-
- VTY_ASSERT_LOCKED() ;
-
- fflush(file) ;
-
- while ((src = vio_fifo_get_lump(&vio->cmd_obuf, &have)) != NULL)
- {
- fwrite(src, 1, have, file) ;
- vio_fifo_got_upto(&vio->cmd_obuf, src + have) ;
- } ;
-
- fflush(file) ;
-} ;
-
/*==============================================================================
* The watch dog.
*
@@ -193,2552 +87,1805 @@ uty_out_fflush(vty_io vio, FILE* file)
* * the death watch list
*/
-enum { vty_watch_dog_interval = 5 } ;
+/* Watch-dog timer. */
+static vio_timer_t vty_watch_dog ;
-static void vty_watch_dog_qnexus(qtimer qtr, void* timer_info, qtime_t when) ;
-static int vty_watch_dog_thread(struct thread *thread) ;
+static vty_timer_time uty_watch_dog_bark(vio_timer timer, void* info) ;
+static void uty_death_watch_scan(bool final) ;
-static void uty_watch_dog_bark(void) ;
-static bool uty_death_watch_scan(void) ;
+static vty_io uty_dispose(vty_io vio) ;
/*------------------------------------------------------------------------------
- * Start watch dog -- the first time a VTY is created.
+ * Initialise watch dog -- at start-up time.
*/
extern void
-uty_watch_dog_start()
+uty_watch_dog_init(void)
{
- if (vty_cli_nexus)
- vty_watch_dog.qnexus = qtimer_init_new(NULL, vty_cli_nexus->pile,
- NULL, NULL) ;
+ vio_timer_init_new(vty_watch_dog, NULL, NULL) ; /* empty */
+} ;
- uty_watch_dog_bark() ; /* start up by barking the first time */
-}
+/*------------------------------------------------------------------------------
+ * Start watch dog -- before a VTY is created.
+ */
+extern void
+uty_watch_dog_start(void)
+{
+ vio_timer_init_new(vty_watch_dog, uty_watch_dog_bark, NULL) ;
+ vio_timer_set(vty_watch_dog, VTY_WATCH_DOG_INTERVAL) ;
+} ;
/*------------------------------------------------------------------------------
* Stop watch dog timer -- at close down.
*
* Final run along the death-watch
- *
*/
extern void
uty_watch_dog_stop(void)
{
- if (vty_watch_dog.anon != NULL)
- {
- if (vty_cli_nexus)
- qtimer_free(vty_watch_dog.qnexus) ;
- else
- thread_cancel(vty_watch_dog.thread) ;
- } ;
-
- uty_death_watch_scan() ; /* scan the death-watch list */
+ vio_timer_reset(vty_watch_dog, keep_it) ;
+ uty_death_watch_scan(true) ; /* scan the death-watch list */
}
/*------------------------------------------------------------------------------
- * qnexus watch dog action
- */
-static void
-vty_watch_dog_qnexus(qtimer qtr, void* timer_info, qtime_t when)
-{
- VTY_LOCK() ;
-
- uty_watch_dog_bark() ;
-
- VTY_UNLOCK() ;
-} ;
-
-/*------------------------------------------------------------------------------
- * thread watch dog action
+ * Watch dog vio_timer action
*/
-static int
-vty_watch_dog_thread(struct thread *thread)
+static vty_timer_time
+uty_watch_dog_bark(vio_timer timer, void* info)
{
- VTY_LOCK() ;
+ cmd_host_name(true) ; /* check for host name change */
- vty_watch_dog.thread = NULL ;
- uty_watch_dog_bark() ;
+ uty_death_watch_scan(false) ; /* scan the death-watch list */
- VTY_UNLOCK() ;
- return 0 ;
+ return VTY_WATCH_DOG_INTERVAL ;
} ;
/*------------------------------------------------------------------------------
- * Watch dog action
+ * Process the death watch list -- anything on the list can be disposed of.
+ *
+ * At curtains...
*/
static void
-uty_watch_dog_bark(void)
+uty_death_watch_scan(bool final)
{
- uty_check_host_name() ; /* check for host name change */
+ VTY_ASSERT_CLI_THREAD() ;
- uty_death_watch_scan() ; /* scan the death-watch list */
+ /* Dispose of anything on the death watch list. */
- /* Set timer to go off again later */
- if (vty_cli_nexus)
- qtimer_set(vty_watch_dog.qnexus,
- qt_add_monotonic(QTIME(vty_watch_dog_interval)),
- vty_watch_dog_qnexus) ;
- else
+ while (vio_death_watch != NULL)
{
- if (vty_watch_dog.thread != NULL)
- thread_cancel (vty_watch_dog.thread);
- vty_watch_dog.thread = thread_add_timer (vty_master,
- vty_watch_dog_thread, NULL, vty_watch_dog_interval) ;
- } ;
-} ;
-
-/*------------------------------------------------------------------------------
- * Scan the death watch list.
- *
- * A vty may finally be freed if it is closed and there is no command in
- * progress.
- */
-static bool
-uty_death_watch_scan(void)
-{
- vty_io vio ;
- vty_io next ;
+ vty vty ;
+ vty_io vio ;
- next = vio_death_watch ;
- while (next != NULL)
- {
- vio = next ;
- next = sdl_next(vio, vio_list) ;
+ vio = vio_death_watch ;
+ vio_death_watch = sdl_next(vio, vio_list) ; /* take off death watch */
- if (vio->closed && !vio->cmd_in_progress)
- {
- uty_close(vio) ; /* closes again to ensure that all buffers
- are released. */
+ vty = vio->vty ;
- sdl_del(vio_death_watch, vio, vio_list) ;
+ vty->vio = uty_dispose(vty->vio) ;
+ assert(vty->exec == NULL) ;
- XFREE(MTYPE_VTY, vio->vty) ;
- XFREE(MTYPE_VTY, vio) ;
- } ;
+ XFREE(MTYPE_VTY, vty) ;
} ;
-
- return (vio_death_watch == NULL) ;
} ;
/*==============================================================================
* Prototypes.
*/
-static void uty_sock_init_new(vio_sock sock, int fd, void* info) ;
-static void uty_sock_half_close(vio_sock sock) ;
-static void uty_sock_close(vio_sock sock) ;
-
-static void vty_read_qnexus (qps_file qf, void* file_info) ;
-static void vty_write_qnexus (qps_file qf, void* file_info) ;
-static void vty_timer_qnexus (qtimer qtr, void* timer_info, qtime_t when) ;
-
-static int vty_read_thread (struct thread *thread) ;
-static int vty_write_thread (struct thread *thread) ;
-static int vty_timer_thread (struct thread *thread) ;
-
-static void vtysh_read_qnexus (qps_file qf, void* file_info) ;
-static int vtysh_read_thread (struct thread *thread) ;
-
-static enum vty_readiness uty_write(vty_io vio) ;
+static void uty_vout_close_reason(vio_vf vf, const char* reason) ;
+static cmd_return_code_t uty_vf_read_close(vio_vf vf, bool final) ;
+static cmd_return_code_t uty_vf_write_close(vio_vf vf, bool final) ;
+static vio_vf uty_vf_free(vio_vf vf) ;
/*==============================================================================
* Creation and destruction of VTY objects
*/
/*------------------------------------------------------------------------------
- * Allocate new vty struct
+ * Allocate new vty structure, including empty vty_io and empty execution
+ * structures.
+ *
+ * Caller must complete the initialisation of the vty_io, which means:
*
- * Allocates and initialises basic vty and vty_io structures, setting the
- * given type.
+ * * constructing a suitable vio_vf and doing uty_vin_push() to set the
+ * vin_base.
*
- * Note that where is not setting up a vty_sock, this *may* be called from
- * any thread.
+ * All vty_io MUST have a vin_base, even if it is /dev/null.
*
- * NB: may not create a VTY_CONFIG_WRITE type vty directly
+ * * constructing a suitable vio_vf and doing uty_vout_push() to set the
+ * vout_base and the vio->obuf.
*
- * see: vty_open_config_write() and vty_close_config_write()
+ * All vty_io MUST have a vout_base, even if it is /dev/null.
*
- * NB: the sock_fd *must* be valid for VTY_TERM and VTY_SHELL_SERV.
- * (So MUST be in the CLI thread to set those up !)
+ * * setting vio->cli, if required
*
- * the sock_fd is ignored for everything else.
+ * * etc.
*
- * Returns: new vty
+ * No "exec" is allocated. That is done when the command loop is entered.
*/
-extern struct vty *
-uty_new(enum vty_type type, int sock_fd)
+extern vty
+uty_new(vty_type_t type, node_type_t node)
{
- struct vty *vty ;
- struct vty_io* vio ;
+ vty vty ;
+ vty_io vio ;
VTY_ASSERT_LOCKED() ;
- /* If this is a VTY_TERM or a VTY_SHELL, place */
- switch (type)
- {
- case VTY_TERM: /* Require fd -- Telnet session */
- case VTY_SHELL_SERV: /* Require fd -- Unix socket */
- assert(sock_fd >= 0) ;
- break ;
-
- case VTY_CONFIG_WRITE:
- zabort("may not make a new VTY_CONFIG_WRITE VTY") ;
- break ;
-
- case VTY_CONFIG_READ:
- case VTY_STDOUT:
- case VTY_STDERR:
- case VTY_SHELL:
- sock_fd = -1 ; /* No fd -- output to stdout/stderr */
- break ;
-
- default:
- zabort("unknown VTY type") ;
- } ;
-
/* Basic allocation */
- vty = XCALLOC (MTYPE_VTY, sizeof (struct vty));
- vio = XCALLOC (MTYPE_VTY, sizeof (struct vty_io)) ;
-
- vty->vio = vio ;
- vio->vty = vty ;
- /* Zeroising the vty_io structure has set:
- *
- * name = NULL -- no name, yet
- *
- * vio_list both pointers NULL
- * mon_list both pointers NULL
- *
- * half_closed = 0 -- NOT half closed (important !)
- * closed = 0 -- NOT closed (important !)
- * close_reason = NULL -- no reason, yet
+ vty = XCALLOC(MTYPE_VTY, sizeof(struct vty)) ;
+ /* Zeroising the vty structure has set:
*
- * real_type = 0 -- not material
- * file_fd = 0 -- not material
- * file_error = 0 -- not material
+ * type = X -- set to actual type, below
*
- * key_stream = NULL -- no key stream (always empty, at EOF)
+ * node = X -- set to actual node, below
*
- * cli_drawn = 0 -- not drawn
- * cli_dirty = 0 -- not dirty
- * cli_prompt_len = 0 )
- * cli_extra_len = 0 ) not material
- * cli_echo_suppress = 0 )
+ * index = NULL -- nothing, yet
+ * index_sub = NULL -- nothing, yet
*
- * cli_prompt_node = 0 -- not material
- * cli_prompt_set = 0 -- so prompt needs to be constructed
+ * config = false -- not owner of configuration symbol of power
+ * config_brand = 0 -- none, yet
*
- * cli_blocked = 0 -- not blocked
- * cmd_in_progress = 0 -- no command in progress
- * cmd_out_enabled = 0 -- command output is disabled
- * cli_wait_more = 0 -- not waiting for response to "--more--"
+ * exec = NULL -- execution state set up when required
+ * vio = X -- set below
+ */
+ confirm(NULL_NODE == 0) ;
+ confirm(QSTRING_INIT_ALL_ZEROS) ;
+
+ vty->type = type ;
+ vty->node = node ;
+
+ vio = XCALLOC(MTYPE_VTY, sizeof(struct vty_io)) ;
+
+ /* Zeroising the vty_io structure has set:
*
- * cli_more_enabled = 0 -- not enabled for "--more--"
+ * vty = X -- set to point to parent vty, below
*
- * cmd_out_done = 0 -- not material
+ * name = NULL -- no name, yet TODO ???
*
- * cli_do = 0 == cli_do_nothing
+ * vin = NULL -- empty input stack
+ * vin_base = NULL -- empty input stack
+ * vin_depth = 0 -- no stacked vin's, yet
*
- * cmd_lc = NULL -- no line control
+ * real_depth = 0 -- nothing stacked, yet
*
- * fail = 0 -- no login failures yet
+ * vout = NULL -- empty output stack
+ * vout_base = NULL -- empty output stack
+ * vout_depth = 0 -- no stacked vout's, yet
*
- * hist = empty vector
- * hp = 0 -- at the beginning
- * hindex = 0 -- the beginning
+ * depth_mark = 0 -- no stacked vin/vout, yet
*
- * width = 0 -- unknown console width
- * height = 0 -- unknown console height
+ * err_hard = false -- no error at all, yet
+ * ebuf = NULL -- no error at all, yet
*
- * lines = 0 -- no limit
- * lines_set = 0 -- no explicit setting
+ * vio_list = NULLs -- not on the vio_list, yet
+ * mon_list = NULLs -- not on the monitors list
*
- * monitor = 0 -- not a monitor
- * monitor_busy = 0 -- not a busy monitor
+ * blocking = X -- set below: false unless VTY_CONFIG_READ
*
- * config = 0 -- not holder of "config" mode
- */
- confirm(cli_do_nothing == 0) ;
- confirm(AUTH_NODE == 0) ; /* default node type */
-
- vio->type = type ;
-
- /* Zeroising the vty structure has set:
+ * state = vc_null -- not started vty command loop
*
- * node = 0 TODO: something better for node value ????
- * buf = NULL -- no command line, yet
- * parsed = NULL -- no parsed command, yet
- * lineno = 0 -- nothing read, yet
- * index = NULL -- nothing, yet
- * index_sub = NULL -- nothing, yet
- */
- if (type == VTY_TERM)
- vty->newline = "\n" ; /* line control looks after "\r\n" */
- else
- vty->newline = "\n" ;
-
- /* Initialise the vio_sock, */
- uty_sock_init_new(&vio->sock, sock_fd, vio) ;
-
- /* Make sure all buffers etc. are initialised clean and empty.
+ * close_reason = NULL -- none set
*
- * Note that no buffers are actually allocated at this stage.
+ * obuf = NULL -- no output buffer, yet
*/
- qs_init_new(&vio->cli_prompt_for_node, 0) ;
+ confirm(vc_null == 0) ;
- qs_init_new(&vio->cl, 0) ;
- qs_init_new(&vio->clx, 0) ;
-
- vio_fifo_init_new(&vio->cli_obuf, 2 * 1024) ; /* allocate in 2K lumps */
+ vty->vio = vio ;
+ vio->vty = vty ;
- vio_fifo_init_new(&vio->cmd_obuf, 8 * 1024) ;
+ vio->blocking = (type == VTY_CONFIG_READ) ;
/* Place on list of known vio/vty */
- sdl_push(vio_list_base, vio, vio_list) ;
+ sdl_push(vio_live_list, vio, vio_list) ;
return vty;
} ;
/*------------------------------------------------------------------------------
- * Create new vty of type VTY_TERM -- ie attached to a telnet session.
+ * Add a new vf to the vio->vin stack, and set read stuff.
+ *
+ * Sets the vf->vin_type and set vf->read_open.
+ *
+ * Initialises an input buffer if required, and sets line_complete and
+ * line_step so that first attempt to fetch a line will give line 1.
*
- * Returns: new vty
+ * Sets the read ready action and the read timer timeout action.
+ *
+ * NB: is usually called from the cli thread, but may be called from the cmd
+ * thread for vf which is blocking !
+ *
+ * NB: a uty_cmd_prepare() is required before command processing can continue.
*/
-static struct vty *
-uty_new_term(int sock_fd, union sockunion *su)
+extern void
+uty_vin_push(vty_io vio, vio_vf vf, vio_in_type_t type,
+ vio_vfd_action* read_action,
+ vio_timer_action* read_timer_action,
+ usize ibuf_size)
{
- struct vty *vty ;
- vty_io vio ;
- enum vty_readiness ready ;
-
- VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
+ vf->vin_type = type ;
+ vf->vin_state = vf_open ;
- /* Allocate new vty structure and set up default values. */
- vty = uty_new (VTY_TERM, sock_fd) ;
- vio = vty->vio ;
+ assert(type != VIN_NONE) ;
- /* Allocate and initialise a keystroke stream TODO: CSI ?? */
- vio->key_stream = keystroke_stream_new('\0', uty_cli_iac_callback, vio) ;
-
- /* Set the socket action functions */
- if (vty_cli_nexus)
- {
- vio->sock.action.read.qnexus = vty_read_qnexus ;
- vio->sock.action.write.qnexus = vty_write_qnexus ;
- vio->sock.action.timer.qnexus = vty_timer_qnexus ;
- }
- else
+ if ((type < VIN_SPECIALS) && (!vf->blocking))
{
- vio->sock.action.read.thread = vty_read_thread ;
- vio->sock.action.write.thread = vty_write_thread ;
- vio->sock.action.timer.thread = vty_timer_thread ;
+ vio_vfd_set_read_action(vf->vfd, read_action) ;
+ vio_vfd_set_read_timeout_action(vf->vfd, read_timer_action) ;
} ;
- /* The text form of the address identifies the VTY */
- vio->name = sockunion_su2str (su, MTYPE_VTY_NAME);
+ ssl_push(vio->vin, vf, vin_next) ;
+ vio->real_depth = ++vio->vin_depth ;
- /* Set the initial node */
- if (no_password_check)
+ if (vio->vin_base == NULL)
{
- if (restricted_mode)
- vty->node = RESTRICTED_NODE;
- else if (host.advanced)
- vty->node = ENABLE_NODE;
- else
- vty->node = VIEW_NODE;
- }
- else
- vty->node = AUTH_NODE;
-
- /* Pick up current timeout setting */
- vio->sock.v_timeout = vty_timeout_val;
-
- /* Use global 'lines' setting, as default. May be -1 => unset */
- vio->lines = host.lines ;
-
- /* For VTY_TERM use vio_line_control for '\n' and "--more--" */
- vio->cmd_lc = vio_lc_init_new(NULL, 0, 0) ;
- uty_set_height(vio) ; /* set initial state */
-
- /* Initialise the CLI, ready for start-up messages etc. */
- uty_cli_init(vio) ;
+ assert(vio->vin_depth == 1) ;
+ vio->vin_base = vf ;
+ } ;
- /* Reject connection if password isn't set, and not "no password" */
- if ((host.password == NULL) && (host.password_encrypt == NULL)
- && ! no_password_check)
- {
- uty_half_close (vio, "Vty password is not set.");
- vty = NULL;
- }
- else
+ if (ibuf_size != 0)
{
- /* Say hello to the world. */
- vty_hello (vty);
-
- if (! no_password_check)
- uty_out (vty, "%sUser Access Verification%s%s", VTY_NEWLINE,
- VTY_NEWLINE, VTY_NEWLINE);
+ vf->ibuf = vio_fifo_new(ibuf_size) ;
+ vf->cl = qs_new(150) ;
+ vf->line_complete = true ;
+ vf->line_step = 1 ;
} ;
-
- /* Now start the CLI and set a suitable state of readiness */
- ready = uty_cli_start(vio) ;
- uty_sock_set_readiness(&vio->sock, ready) ;
-
- return vty;
} ;
/*------------------------------------------------------------------------------
- * Create new vty of type VTY_SHELL_SERV -- ie attached to a vtysh session.
+ * Save the given context in the current top of the vin stack.
+ *
+ * This is done when a new pipe is opened, so that:
+ *
+ * a) saves the current context in the current vin (the new pipe has
+ * not yet been pushed) so that uty_vin_pop() can restore this context
+ * after closing the then top of the stack.
+ *
+ * b) can update context for about to be run vin, eg:
+ *
+ * - dir_here -- if required
*
- * Returns: new vty
+ * - can_enable
+ *
+ * So the top of the vin stack does not contain the current context, that is
+ * in the vty->exec !
*/
-static struct vty *
-uty_new_shell_serv(int sock_fd)
+extern void
+uty_vin_new_context(vty_io vio, cmd_context context, qpath file_here)
{
- struct vty *vty ;
- vty_io vio ;
+ assert(vio->vin->context == NULL) ;
+ vio->vin->context = cmd_context_new_save(context, file_here) ;
+} ;
+/*------------------------------------------------------------------------------
+ * Push a new vf to the vio->vout stack, and set write stuff.
+ *
+ * Sets the vf->vout_type and set vf->write_open.
+ *
+ * Sets the write ready action and the write timer timeout action.
+ *
+ * Initialises an output buffer and sets an end_mark.
+ *
+ * The depth_mark is set to the current vio->depth_mark + 1. This is the
+ * vin_depth below which the vout should be closed. Before a command line
+ * is fetched (and hence after the previous command line has completed) the
+ * vout->depth_mark is checked. If it is > the current vin_depth, then
+ * the vout is closed before a command line can be fetched.
+ *
+ * NB: is usually called from the cli thread, but may be called from the cmd
+ * thread for vf which is blocking !
+ *
+ * NB: VOUT_DEV_NULL, VOUT_STDOUT and VOUT_STDERR are special.
+ *
+ * The write_action and the write_timer_action are ignored.
+ *
+ * All actual I/O to these outputs is direct, blocking and via standard
+ * I/O -- except VOUT_DEV_NULL where all I/O is discarded.
+ *
+ * NB: all outputs are set up with an obuf, so all output is collected, even
+ * if it is later to be discarded.
+ *
+ * NB: a uty_cmd_prepare() is required before command processing can continue.
+ */
+extern void
+uty_vout_push(vty_io vio, vio_vf vf, vio_out_type_t type,
+ vio_vfd_action* write_action,
+ vio_timer_action* write_timer_action,
+ usize obuf_size)
+{
VTY_ASSERT_LOCKED() ;
- /* Allocate new vty structure and set up default values. */
- vty = uty_new (VTY_SHELL_SERV, sock_fd) ;
- vio = vty->vio ;
+ vf->vout_type = type ;
+ vf->vout_state = vf_open ;
- /* Set the action functions */
- if (vty_cli_nexus)
+ assert(type != VOUT_NONE) ;
+
+ if ((type < VOUT_SPECIALS) && (!vf->blocking))
{
- vio->sock.action.read.qnexus = vtysh_read_qnexus ;
- vio->sock.action.write.qnexus = vty_write_qnexus ;
- vio->sock.action.timer.qnexus = NULL ;
- }
- else
+ vio_vfd_set_write_action(vf->vfd, write_action) ;
+ vio_vfd_set_write_timeout_action(vf->vfd, write_timer_action) ;
+ } ;
+
+ ssl_push(vio->vout, vf, vout_next) ;
+ ++vio->vout_depth ;
+
+ if (vio->vout_base == NULL)
{
- vio->sock.action.read.thread = vtysh_read_thread ;
- vio->sock.action.write.thread = vty_write_thread ;
- vio->sock.action.timer.thread = NULL ;
+ assert(vio->vout_depth == 1) ;
+ vio->vout_base = vf ;
} ;
- vty->node = VIEW_NODE;
+ vf->obuf = vio_fifo_new(obuf_size) ;
+ vio_fifo_set_end_mark(vf->obuf) ;
- /* Kick start the CLI etc. */
- uty_sock_set_readiness(&vio->sock, write_ready) ;
+ vf->depth_mark = vio->depth_mark + 1 ;
- return vty;
+ vio->obuf = vf->obuf ;
} ;
/*------------------------------------------------------------------------------
- * Set/Clear "monitor" state:
+ * Set timeout value.
*
- * set: if VTY_TERM and not already "monitor" (and write_open !)
- * clear: if is "monitor"
+ * This is only ever called when a command (eg exec-timeout) sets a new
+ * time out value -- which applies only to VIN_TERM and VTY_VTYSH.
*/
extern void
-uty_set_monitor(vty_io vio, bool on)
+uty_set_timeout(vty_io vio, vty_timer_time timeout)
{
+ vio_in_type_t vt ;
+
VTY_ASSERT_LOCKED() ;
- if (on && !vio->monitor)
- {
- if ((vio->type == VTY_TERM) && vio->sock.write_open)
- {
- vio->monitor = 1 ;
- sdl_push(vio_monitors_base, vio, mon_list) ;
- } ;
- }
- else if (!on && vio->monitor)
- {
- vio->monitor = 0 ;
- sdl_del(vio_monitors_base, vio, mon_list) ;
- }
+ vt = vio->vin_base->vin_type ;
+
+ if ((vt == VIN_TERM) || (vt == VIN_VTYSH))
+ uty_vf_set_read_timeout(vio->vin_base, timeout) ;
} ;
/*------------------------------------------------------------------------------
- * Return "name" of VTY
+ * Return "name" of VTY.
*
- * For VTY_TERM this is the IP address of the far end of the telnet connection.
+ * The name of the base vin, or (failing that) the base vout.
*/
extern const char*
uty_get_name(vty_io vio)
{
- return (vio->name != NULL) ? vio->name : "?" ;
+ const char* name ;
+
+ name = vio->vin_base->name ;
+ if (name == NULL)
+ name = vio->vout_base->name ;
+
+ return (name != NULL) ? name : "?" ;
} ;
/*------------------------------------------------------------------------------
- * Closing down VTY for reading.
+ * Close VTY -- final.
+ *
+ * Two forms: "curtains" and "not-curtains".
+ *
+ * At "curtains" the system is being terminated, and all message and event
+ * handling has stopped. Can assume that a vty is not in any command loop,
+ * so can be killed off here and now.
+ *
+ * At "not-curtains" messages and event handling is still running. It is
+ * possible that a vty is running a command in the cmd_thread, so cannot
+ * completely close things down -- must leave enough for the command loop
+ * to continue to work, up to the point that the closing of the vty is
+ * detected.
+ *
+ * For everything that is closed, uses "final" close, which stops things
+ * instantly -- will not block, or set any read/write ready, or continue the
+ * command loop, or take any notice of errors.
+ *
+ * This close is called by:
+ *
+ * * uty_reset() -- SIGHUP -- !curtains
+ *
+ * Will close "final" everything except vout_base.
*
- * For VTY_TERM (must be in CLI thread):
+ * If the command loop is not already stopped, it is signalled to stop
+ * as soon as possible. When it does it will return here via
+ * vty_cmd_loop_exit().
*
- * * shut the socket for reading
- * * discard all buffered input, setting it to "EOF"
- * * turns off any monitor status !
- * * drop down to RESTRICTED_NODE
+ * If command loop has already stopped, then will proceed to complete
+ * close -- see below.
*
- * For VTY_SHELL_SERV (must be in CLI thread):
+ * * uty_reset() -- SIGTERM -- curtains
*
- * * shut the socket for reading
- * * discard all buffered input
- * * drop down to RESTRICTED_NODE
+ * Will close "final" everything except vout_base.
*
- * In all cases:
+ * The command loop will be set stopped, and will proceed to complete
+ * close -- see below.
*
- * * place on death watch
- * * set the vty half_closed
- * * sets the reason for closing (if any given)
+ * * vty_cmd_loop_exit() -- when the command loop has stopped -- !curtains
*
- * For VTY_TERM and VTY_SHELL_SERV, when the output side has emptied out all
- * the buffers, the VTY is closed.
+ * The command loop may have stopped in response to a vc_close_trap,
+ * which means that have been here before, and the vty is pretty much
+ * closed already.
*
- * May already have set the vio->close_reason, or can set it now. (Passing a
- * NULL reason has no effect on any existing posted reason.)
+ * The command loop may have stopped:
+ *
+ * - because has reached the end of the vin_base input
+ * - vin_base has timed out
+ * - there was a command error, on non-interactive vty
+ * - there was an I/O error
+ *
+ * In all these cases, the stack will already have been closed final or
+ * otherwise, except for vout_base, and all output will have been pushed.
+ *
+ * vout_base will have been closed, but not "final", so will be sitting
+ * in vf_closing state.
+ *
+ * So the vio is ready for complete close.
+ *
+ * For complete close any remaining vf are closed final, and the close reason
+ * is output to the vout_base, if any and if possible.
+ *
+ * The vty is then closed and placed on death watch to be finally reaped.
*/
extern void
-uty_half_close (vty_io vio, const char* reason)
+uty_close(vty_io vio, const char* reason, bool curtains)
{
- VTY_ASSERT_LOCKED() ;
+ VTY_ASSERT_CAN_CLOSE(vio->vty) ;
- if (vio->half_closed)
- return ;
+ /* Stamp on any monitor output instantly. */
+ uty_set_monitor(vio, off) ;
- if (reason != NULL)
- vio->close_reason = reason ;
+ /* Save the close reason for later, unless one is already set. */
+ if ((reason != NULL) && (vio->close_reason == NULL))
+ vio->close_reason = XSTRDUP(MTYPE_TMP, reason) ;
- /* Do the file side of things
+ /* Close the command loop -- if not already stopped (or closed !)
*
- * Note that half closing the file sets a new timeout, sets read off
- * and write on.
+ * If command loop is not already stopped, the if "curtains" will stop it
+ * instantly, otherwise will signal to the command loop to close, soonest.
*/
- uty_sock_half_close(&vio->sock) ;
- uty_set_monitor(vio, 0) ;
+ uty_cmd_loop_close(vio, curtains) ;
- /* Discard everything in the keystroke stream and force it to EOF */
- if (vio->key_stream != NULL)
- keystroke_stream_set_eof(vio->key_stream) ;
-
- /* Turn off "--more--" so that all output clears without interruption.
+ /* Close all vin including the vin_base.
*
- * If is sitting on a "--more--" prompt, then exit the wait_more CLI.
+ * Note that the vin_base is closed, but is still on the vin stack.
*/
- vio->cli_more_enabled = 0 ;
+ do
+ uty_vin_pop(vio, true, NULL) ; /* final close, discard context */
+ while (vio->vin != vio->vin_base) ;
- if (vio->cli_more_wait)
- uty_cli_exit_more_wait(vio) ;
+ /* Close all the vout excluding the vout_base. */
+ while (vio->vout != vio->vout_base)
+ uty_vout_pop(vio, true) ; /* final close */
- /* If a command is not in progress, enable output, which will clear
- * the output buffer if there is anything there, plus any close reason,
- * and then close.
+ /* If command loop is still running, this is as far as can go.
*
- * If command is in progress, then this process will start when it
- * completes.
+ * The command loop will hit the vc_close_trap or execute CMD_CLOSE, and
+ * that will cause this function to be called again, in vc_stopped state.
+ *
+ * Save the close_reason for later.
*/
- if (!vio->cmd_in_progress)
- vio->cmd_out_enabled = 1 ;
-
- /* Make sure no longer holding the config symbol of power */
- uty_config_unlock(vio->vty, RESTRICTED_NODE) ;
-
- /* Log closing of VTY_TERM */
- if (vio->type == VTY_TERM)
- uzlog (NULL, LOG_INFO, "Vty connection (fd %d) close", vio->sock.fd) ;
-
- /* Move to the death watch list */
- sdl_del(vio_list_base, vio, vio_list) ;
- sdl_push(vio_death_watch, vio, vio_list) ;
+ if ((vio->state == vc_running) || (vio->state == vc_close_trap))
+ return ;
- vio->half_closed = 1 ;
-} ;
+ /* If the vout_base is not closed, try to output the close reason,
+ * if any.
+ */
+ if ((vio->close_reason != NULL) && (vio->vout_base->vout_state != vf_closed))
+ uty_vout_close_reason(vio->vout_base, vio->close_reason) ;
-/*------------------------------------------------------------------------------
- * Closing down VTY.
- *
- * Shuts down everything and discards all buffers etc. etc.
- *
- * If cmd_in_progress, cannot complete the process -- but sets the closed
- * flag.
- *
- * Can call vty_close() any number of times.
- *
- * The vty structure is placed on death watch, which will finally free the
- * structure once no longer cmd_in_progress.
- */
-extern void
-uty_close (vty_io vio)
-{
- VTY_ASSERT_LOCKED() ;
+ /* Now final close the vout_base.
+ *
+ * Note that the vout_base will be closed, but on the vout stack with an
+ * empty obuf... just in case TODO ?
+ */
+ uty_vout_pop(vio, true) ; /* final close */
- /* Empty all the output buffers */
- vio_fifo_reset_keep(&vio->cli_obuf) ;
- vio_fifo_reset_keep(&vio->cmd_obuf) ;
- vio->cmd_lc = vio_lc_reset_free(vio->cmd_lc) ;
+ assert(vio->obuf == vio->vout_base->obuf) ;
+ vio_fifo_clear(vio->obuf, true) ; /* and discard any marks */
- /* If not already closed, close. */
- if (!vio->closed)
+ /* All should now be very quiet indeed. */
+ if (vty_debug)
{
- uty_half_close(vio, NULL) ; /* place on death watch -- if not
- already done */
- if (vio->type == VTY_TERM)
- uty_cli_close(vio) ; /* tell the CLI to stop */
-
- vio->closed = 1 ; /* now closed (stop uty_write()
- from recursing) */
-
- if (vio->sock.write_open)
- uty_write(vio) ; /* last gasp attempt */
+ assert(vio->vin == vio->vin_base) ;
+ assert(vio->vin_depth == 0) ;
+ assert(vio->real_depth == 0) ;
- uty_sock_close(&vio->sock) ;
+ assert(vio->vout == vio->vout_base) ;
+ assert(vio->vout_depth == 0) ;
+ assert(vio->vin->vin_state == vf_closed) ;
+ assert(vio->vout->vout_state == vf_closed) ;
} ;
- /* Nothing more should happen, so can now release almost everything,
- * the exceptions being the things that are related to a cmd_in_progress.
- *
- * All writing to buffers is suppressed, and as the sock has been closed,
- * there will be no more read_ready or write_ready events.
- */
- if (vio->name != NULL)
- XFREE(MTYPE_VTY_NAME, vio->name) ;
-
- vio->key_stream = keystroke_stream_free(vio->key_stream) ;
-
- qs_free_body(&vio->cli_prompt_for_node) ;
- qs_free_body(&vio->cl) ;
+ /* Can dispose of these now -- leave vin/vout for final disposition */
+ vio->vty->exec = cmd_exec_free(vio->vty->exec) ;
+ vio->ebuf = vio_fifo_free(vio->ebuf) ;
- {
- qstring line ;
- while ((line = vector_ream_keep(&vio->hist)) != NULL)
- qs_reset_free(line) ;
- } ;
-
- /* The final stage cannot be completed if cmd_in_progress.
- *
- * The clx is pointed at by vty->buf -- containing the current command.
- *
- * Once everything is released, can take the vty off death watch, and
- * release the vio and the vty.
+ /* Command loop is not running, so can place on death watch for final
+ * disposition.
*/
- if (!vio->cmd_in_progress)
+ if (vio->state == vc_stopped)
{
- qs_free_body(&vio->clx) ;
- vio->vty->buf = NULL ;
- } ;
-} ;
-
-/*==============================================================================
- * For writing configuration file by command, temporarily redirect output to
- * an actual file.
- */
-
-/*------------------------------------------------------------------------------
- * Set the given fd as the VTY_FILE output.
- */
-extern void
-vty_open_config_write(struct vty* vty, int fd)
-{
- vty_io vio ;
-
- VTY_LOCK() ;
-
- vio = vty->vio ;
-
- assert((vio->type != VTY_CONFIG_WRITE) && (vio->type != VTY_NONE)) ;
+ vio->state = vc_closed ;
- vio->real_type = vio->type ;
-
- vio->type = VTY_CONFIG_WRITE ;
- vio->file_fd = fd ;
- vio->file_error = 0 ;
+ sdl_del(vio_live_list, vio, vio_list) ;
+ sdl_push(vio_death_watch, vio, vio_list) ;
+ } ;
- VTY_UNLOCK() ;
+ assert(vio->state == vc_closed) ; /* thank you and good night */
} ;
/*------------------------------------------------------------------------------
- * Write away configuration file stuff -- all or just the full lump(s).
+ * Dispose unwanted vty.
*
- * Returns: > 0 => blocked
- * 0 => all gone (up to last lump if !all)
- * < 0 => failed -- see vio->file_error
+ * Called from deathwatch -- must already be removed from deathwatch list.
*/
-static int
-uty_config_write(vty_io vio, bool all)
+static vty_io
+uty_dispose(vty_io vio)
{
- int ret ;
-
VTY_ASSERT_LOCKED() ;
- if (vio->file_error == 0)
- {
- ret = vio_fifo_write_nb(&vio->cmd_obuf, vio->file_fd, all) ;
-
- if (ret < 0)
- vio->file_error = errno ;
- }
- else
- ret = -1 ;
-
- return ret ;
-} ;
+ assert(vio->state == vc_closed) ;
-/*------------------------------------------------------------------------------
- * Write away any pending stuff, and return the VTY to normal.
- */
-extern int
-vty_close_config_write(struct vty* vty)
-{
- vty_io vio ;
- int err ;
+ /* Stop pointing at vout_base obuf */
+ vio->obuf = NULL ;
- VTY_LOCK() ;
+ /* Clear out vout and vin (may be the same) */
+ assert(vio->vin == vio->vin_base) ;
+ vio->vin_base = uty_vf_free(vio->vin_base) ;
- vio = vty->vio ;
+ assert(vio->vout == vio->vout_base) ;
+ if (vio->vout != vio->vin)
+ vio->vout_base = uty_vf_free(vio->vout_base) ;
- assert((vio->type == VTY_CONFIG_WRITE) && (vio->real_type != VTY_NONE)) ;
+ vio->vin = NULL ;
+ vio->vout = NULL ;
- uty_config_write(vio, true) ; /* write all that is left */
+ /* Remainder of contents of the vio */
+ vio->ebuf = vio_fifo_free(vio->ebuf) ;
- err = vio->file_error ;
+ /* Really cannot be a monitor any more ! */
+ assert(!vio->monitor) ;
+ vio->mbuf = vio_fifo_free(vio->mbuf) ;
- vio->type = vio->real_type ;
- vio->file_fd = -1 ;
- vio->file_error = 0 ;
+ XFREE(MTYPE_VTY, vio) ;
- VTY_UNLOCK() ;
-
- return err ;
+ return NULL ;
} ;
-/*==============================================================================
- * vio_sock level operations
- */
-
/*------------------------------------------------------------------------------
- * Initialise a new vio_sock structure.
+ * Close top of the vin stack and pop when done -- see uty_vf_read_close().
*
- * Requires that: the vio_sock structure is not currently in use.
+ * If succeeds in closing, unless is vin_base, pops the vin stack and if this
+ * is read-only will free the vio_vf and all its contents. (So if this is
+ * vin_base, it is left on the stack, but vf_closed/vf_closing.)
*
- * if fd >= 0 then: sock is open and ready read and write
- * otherwise: sock is not open
+ * If the given context is not NULL, having popped the vin stack, the context
+ * in the new top of stack is restored to the given context.
*
- * there are no errors, yet.
+ * On final close, will completely close the input, even if errors occur (and
+ * no errors are posted).
*
- * Sets timeout to no timeout at all -- timeout is optional.
+ * Returns: CMD_SUCCESS -- input completely closed
+ * CMD_WAITING -- waiting for input to close <=> not-blocking
+ * (not if final)
+ * CMD_IO_ERROR -- error or timeout
*
- * NB: MUST be in the CLI thread if the fd is >= 0 !
+ * NB: a uty_cmd_prepare() is required before command processing can continue.
*/
-static void
-uty_sock_init_new(vio_sock sock, int fd, void* info)
+extern cmd_return_code_t
+uty_vin_pop(vty_io vio, bool final, cmd_context context)
{
- VTY_ASSERT_LOCKED() ;
-
- if (fd >= 0)
- VTY_ASSERT_CLI_THREAD() ;
-
- memset(sock, 0, sizeof(struct vio_sock)) ;
+ cmd_return_code_t ret ;
- /* Zeroising the structure has set:
- *
- * action = all the actions set NULL
- *
- * error_seen = 0 -- no error, yet
- *
- * qf = NULL -- no qfile, yet
- * t_read = NULL ) no threads, yet
- * t_write = NULL )
- *
- * v_timeout = 0 -- no timeout set
- * timer_runing = 0 -- not running, yet
- * t_timer = NULL -- no timer thread, yet
- * qtr = NULL -- no qtimer, yet
- */
- sock->fd = fd ;
- sock->info = info ;
-
- sock->read_open = (fd >= 0) ;
- sock->write_open = (fd >= 0) ;
-
- if ((fd >= 0) && vty_cli_nexus)
- {
- sock->qf = qps_file_init_new(NULL, NULL);
- qps_add_file(vty_cli_nexus->selection, sock->qf, sock->fd, sock->info);
- } ;
-} ;
-
-/*------------------------------------------------------------------------------
- * Restart the timer.
- *
- * If a timeout time is set, then start or restart the timer with that value.
- *
- * If no timeout time is set, and the timer is running, unset it.
- */
-static void
-uty_sock_restart_timer(vio_sock sock)
-{
VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
- if (sock->v_timeout != 0)
+ ret = uty_vf_read_close(vio->vin, final) ;
+
+ if ((ret == CMD_SUCCESS) || final)
{
- assert(sock->action.timer.anon != NULL) ;
+ assert(vio->vin->vin_state == vf_closed) ;
- if (vty_cli_nexus)
+ if (vio->vin_depth > 1)
{
- if (sock->qtr == NULL) /* allocate qtr if required */
- sock->qtr = qtimer_init_new(NULL, vty_cli_nexus->pile,
- NULL, sock->info) ;
- qtimer_set(sock->qtr, qt_add_monotonic(QTIME(sock->v_timeout)),
- sock->action.timer.qnexus) ;
- }
- else
- {
- if (sock->t_timer != NULL)
- thread_cancel (sock->t_timer);
- sock->t_timer = thread_add_timer (vty_master,
- sock->action.timer.thread, sock->info, sock->v_timeout) ;
- } ;
+ vio_vf vf ;
- sock->timer_running = 1 ;
- }
- else if (sock->timer_running)
- {
- if (vty_cli_nexus)
- {
- if (sock->qtr != NULL)
- qtimer_unset(sock->qtr) ;
+ vf = ssl_pop(&vf, vio->vin, vin_next) ;
+ --vio->vin_depth ;
+
+ if (vf->vout_state == vf_closed)
+ uty_vf_free(vf) ;
}
else
{
- if (sock->t_timer != NULL)
- thread_cancel (sock->t_timer) ;
+ assert(vio->vin == vio->vin_base) ;
+ vio->vin_depth = 0 ; /* may already have been closed */
} ;
- sock->timer_running = 0 ;
- } ;
-} ;
-
-/*------------------------------------------------------------------------------
- * Set read on/off
- *
- * Returns: the on/off state set
- */
-static bool
-uty_sock_set_read(vio_sock sock, bool on)
-{
- VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
+ if (vio->real_depth > vio->vin_depth)
+ vio->real_depth = vio->vin_depth ;
- if (sock->fd < 0)
- return 0 ;
-
- if (on)
- {
- assert(sock->action.read.anon != NULL) ;
-
- if (vty_cli_nexus)
- qps_enable_mode(sock->qf, qps_read_mnum, sock->action.read.qnexus) ;
- else
- {
- if (sock->t_read != NULL)
- thread_cancel(sock->t_read) ;
-
- sock->t_read = thread_add_read(vty_master,
- sock->action.read.thread, sock->info, sock->fd) ;
- } ;
- }
- else
- {
- if (vty_cli_nexus)
- qps_disable_modes(sock->qf, qps_read_mbit) ;
- else
+ if (vio->vin->context != NULL)
{
- if (sock->t_read != NULL)
- thread_cancel (sock->t_read) ;
+ if (context != NULL)
+ vio->vin->context = cmd_context_restore(context, vio->vin->context);
+ else
+ vio->vin->context = cmd_context_free(vio->vin->context, false) ;
+ /* Not a copy */
} ;
} ;
- return on ;
+ return ret ;
} ;
/*------------------------------------------------------------------------------
- * Set write on/off
+ * Close top of the vout stack and pop when done -- see uty_vf_write_close().
+ *
+ * If is vout_base, does not completely close, unless is "final" -- if not
+ * final, CMD_SUCCESS means that the output buffers are empty and the
+ * vout_depth has been reduced, but the vio_vf is still writeable, held in
+ * vf_closing state.
+ *
+ * Unless is vout_base, pops the vout stack. If this is write-only will free
+ * the vio_vf and all its contents.
+ *
+ * If this is vout_base, does not actually close the vfd and does not close
+ * the vout side of the vf. This leaves an active vio->obuf (inter alia.)
+ *
+ * Before closing, discard anything after end_mark, then push any outstanding
+ * output.
*
- * Returns: the on/off state set
+ * Unless "final", the close is soft, that is, if there is any output still
+ * outstanding does not actually close the vout.
+ *
+ * If there is no outstanding output (or if final) will completely close the
+ * vf and free it (except for vout_base).
+ *
+ * Returns: CMD_SUCCESS -- input completely closed
+ * CMD_WAITING -- waiting for input to close <=> not-blocking
+ * CMD_IO_ERROR -- error or timeout
+ *
+ * NB: a uty_cmd_prepare() is required before command processing can continue.
+ *
+ * That is not done here because this may be called from outside the
+ * command loop -- in particular by uty_close().
+ *
+ * However, ensures that vio->obuf is up to date !
*/
-static bool
-uty_sock_set_write(vio_sock sock, bool on)
+extern cmd_return_code_t
+uty_vout_pop(vty_io vio, bool final)
{
+ cmd_return_code_t ret ;
+
VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
- if (sock->fd < 0)
- return 0 ;
+ ret = uty_vf_write_close(vio->vout, final) ;
- if (on)
+ if ((ret == CMD_SUCCESS) || final)
{
- assert(sock->action.write.anon != NULL) ;
-
- if (vty_cli_nexus)
- qps_enable_mode(sock->qf, qps_write_mnum, sock->action.write.qnexus) ;
- else
+ if (vio->vout_depth > 1)
{
- if (sock->t_write != NULL)
- thread_cancel(sock->t_write) ;
+ vio_vf vf ;
- sock->t_write = thread_add_write(vty_master,
- sock->action.write.thread, sock->info, sock->fd) ;
- } ;
- }
- else
- {
- if (vty_cli_nexus)
- qps_disable_modes(sock->qf, qps_write_mbit) ;
+ vf = ssl_pop(&vf, vio->vout, vout_next) ;
+ --vio->vout_depth ;
+
+ uty_vf_free(vf) ;
+ }
else
{
- if (sock->t_write != NULL)
- thread_cancel (sock->t_write) ;
- } ;
- } ;
+ assert(vio->vout == vio->vout_base) ;
+ if (final)
+ assert(vio->vout->vout_state == vf_closed) ;
- return on ;
-} ;
-
-/*------------------------------------------------------------------------------
- * Set read/write readiness -- for VTY_TERM
- *
- * Note that for VTY_TERM, set only one of read or write, and sets write for
- * preference.
- */
-extern void
-uty_sock_set_readiness(vio_sock sock, enum vty_readiness ready)
-{
- VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
+ vio->vout_depth = 0 ; /* may already have been closed */
- uty_sock_set_read(sock, (ready == read_ready)) ;
- uty_sock_set_write(sock, (ready >= write_ready)) ;
-} ;
+ assert(vio->vin_depth == 0) ;
+ vio->vout->depth_mark = 0 ; /* align with the end stop */
+ } ;
-/*------------------------------------------------------------------------------
- * Set a new timer value.
- */
-extern void
-uty_sock_set_timer(vio_sock sock, unsigned long timeout)
-{
- VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
+ vio->obuf = vio->vout->obuf ;
+ } ;
- sock->v_timeout = timeout ;
- if (sock->timer_running)
- uty_sock_restart_timer(sock) ;
+ return ret ;
} ;
/*------------------------------------------------------------------------------
- * Close given vty sock for reading.
+ * Try to output the close reason to the vout_base.
*
- * Sets timer to timeout for clearing any pending output.
- *
- * NB: if there is a socket, MUST be in the CLI thread
- */
-static void
-uty_sock_half_close(vio_sock sock)
-{
- VTY_ASSERT_LOCKED() ;
-
- sock->read_open = 0 ; /* make sure */
-
- if (sock->fd < 0)
- return ; /* nothing more if no socket */
-
- VTY_ASSERT_CLI_THREAD() ;
-
- shutdown(sock->fd, SHUT_RD) ; /* actual half close */
-
- uty_sock_set_read(sock, off) ;
- uty_sock_set_write(sock, on) ;
- sock->v_timeout = 30 ; /* for output to clear */
- uty_sock_restart_timer(sock) ;
-} ;
-
-/*------------------------------------------------------------------------------
- * Close given vio_sock, completely -- shut down any timer.
+ * This is the final act for the vout_base. Will attempt to output unless
+ * the thing is completely closed.
*
- * Structure is cleared of everything except the last error !
+ * Should by now not be any buffered output -- but if there is, that is
+ * discarded before the close reason is output.
*
- * NB: if there is a socket, MUST be in the CLI thread
+ * Any actual output may be done when the vout_base is finally closed.
*/
static void
-uty_sock_close(vio_sock sock)
+uty_vout_close_reason(vio_vf vf, const char* reason)
{
- VTY_ASSERT_LOCKED() ;
-
- sock->read_open = 0 ; /* make sure */
- sock->write_open = 0 ;
-
- if (sock->fd < 0)
- {
- assert( (sock->qf == NULL)
- && (sock->qtr == NULL)
- && (sock->t_read == NULL)
- && (sock->t_write == NULL)
- && (sock->t_timer == NULL) ) ;
- return ; /* no more to be done here */
- } ;
-
- VTY_ASSERT_CLI_THREAD() ;
- close(sock->fd) ;
-
- if (vty_cli_nexus)
- {
- assert((sock->qf != NULL) && (sock->fd == qps_file_fd(sock->qf))) ;
- qps_remove_file(sock->qf) ;
- qps_file_free(sock->qf) ;
- sock->qf = NULL ;
- } ;
+ if ((vf->vout_state == vf_closed) || (reason == NULL) || (*reason == '\0'))
+ return ;
- sock->fd = -1 ;
+ vio_fifo_clear(vf->obuf, true) ; /* clear any markers, too */
- if (sock->t_read != NULL)
- thread_cancel(sock->t_write) ;
- if (sock->t_write != NULL)
- thread_cancel(sock->t_write) ;
+ if (vf->vout_type < VOUT_SPECIALS)
+ assert(vf->vfd != NULL) ; /* make sure */
- sock->t_read = NULL ;
- sock->t_write = NULL ;
+ switch(vf->vout_type)
+ {
+ case VOUT_NONE:
+ zabort("invalid VOUT_NONE") ;
+ break ;
- sock->info = NULL ;
- sock->action.read.anon = NULL ;
- sock->action.write.anon = NULL ;
- sock->action.timer.anon = NULL ;
+ case VOUT_TERM:
+ uty_term_close_reason(vf, reason) ;
+ break ;
- if (sock->qtr != NULL)
- qtimer_free(sock->qtr) ;
- if (sock->t_timer != NULL)
- thread_cancel(sock->t_timer) ;
+ case VOUT_VTYSH:
+ break ;
- sock->v_timeout = 0 ;
- sock->qtr = NULL ;
- sock->t_timer = NULL ;
-} ;
+ case VOUT_FILE:
+ case VOUT_PIPE:
+ case VOUT_SHELL_ONLY:
+ break ;
-/*------------------------------------------------------------------------------
- * Dealing with an I/O error on VTY socket
- *
- * If this is the first error for this VTY, produce suitable log message.
- *
- * If is a "monitor", turn that off, *before* issuing log message.
- */
-static int
-uty_sock_error(vty_io vio, const char* what)
-{
- VTY_ASSERT_LOCKED() ;
- VTY_ASSERT_CLI_THREAD() ;
+ case VOUT_CONFIG:
+ break ;
- /* can no longer be a monitor ! *before* any logging ! */
- uty_set_monitor(vio, 0) ;
+ case VOUT_DEV_NULL:
+ break ;
- /* if this is the first error, log it */
- if (vio->sock.error_seen == 0)
- {
- const char* type ;
- switch (vio->type)
- {
- case VTY_TERM:
- type = "VTY Terminal" ;
- break ;
- case VTY_SHELL_SERV:
- type = "VTY Shell Server" ;
- break ;
- default:
- zabort("unknown VTY type for uty_sock_error()") ;
- } ;
+ case VOUT_STDOUT:
+ fprintf(stdout, "%% %s\n", reason) ;
+ break ;
- vio->sock.error_seen = errno ;
- uzlog(NULL, LOG_WARNING, "%s: %s failed on fd %d: %s",
- type, what, vio->sock.fd, errtoa(vio->sock.error_seen, 0).str) ;
- } ;
+ case VOUT_STDERR:
+ fprintf(stderr, "%% %s\n", reason) ;
+ break ;
- return -1 ;
+ default:
+ zabort("unknown VOUT type") ;
+ } ;
} ;
/*==============================================================================
- * Readiness and the VTY_TERM type VTY.
- *
- * For VTY_TERM the driving force is write ready. This is used to prompt the
- * VTY_TERM when there is outstanding output (obviously), but also if there
- * is buffered input in the keystroke stream.
- *
- * The VTY_TERM uses read ready only when it doesn't set write ready. Does
- * not set both at once.
- *
- * So there is only one, common, uty_ready function, which:
- *
- * 1. attempts to clear any output it can.
- *
- * The state of the output affects the CLI, so must always do this before
- * before invoking the CLI.
+ * vio_vf level operations
+ */
+
+/*------------------------------------------------------------------------------
+ * Create and initialise a new vio_vf structure.
*
- * If this write enters the "--more--" state, then will have tried to
- * write away the prompt.
+ * There are no errors, yet.
*
- * 2. invokes the CLI
+ * This leaves most things unset/NULL/false. Caller will need to:
*
- * Which will do either the standard CLI stuff or the special "--more--"
- * stuff.
+ * - uty_vin_push() and/or uty_vout_push()
*
- * 3. attempts to write any output there now is.
+ * - once those are done, the following optional items remain to be set
+ * if they are required:
*
- * If the CLI generated new output, as much as possible is written away
- * now.
+ * read_timeout -- default = 0 => no timeout
+ * write_timeout -- default = 0 => no timeout
*
- * If this write enters the "--more--" state, then it returns now_ready,
- * if the prompt was written away, which loops back to the CLI.
+ * - for pipes the child state needs to be set, and for out pipes the return
+ * state needs to be set in this vio_vf (the master) and in the next
+ * vout (the slave).
*
- * Note that this is arranging:
+ * NB: if there is no fd for this vio_vf, it should be set to -1, and the
+ * type (recommend vfd_none) and io_type are ignored.
*
- * a. to write away the "--more--" prompt as soon as the tranche of output to
- * which it refers, completes
+ * A VTY_STDOUT or a VTY_STDERR (output only, to the standard I/O) can
+ * be set up without an fd.
*
- * b. to enter the cli_more_wait CLI for the first time immediately after the
- * "--more--" prompt is written away.
+ * A VTY_CONFIG_READ can be set up with the fd of the input file, but
+ * MUST be type = vfd_file and io_type = vfd_io_read -- because the fd
+ * is for input only. The required VOUT_STDERR will be set by
+ * uty_vout_push().
*
- * The loop limits itself to one trache of command output each time.
+ * NB: if the parent vio is blocking, then the vf will be blocking, and so
+ * will any vfd. An individual vf may be set blocking by setting
+ * vfd_io_blocking in the io_type.
*
- * Resets the timer because something happened.
+ * The vty stuff opens all files, pipes etc. non-blocking. A non-blocking
+ * vf simulates blocking by local pselect. As far as the vfd level is
+ * concerned, once the file, pipe etc. is open, there is no difference
+ * between blocking and non-blocking except that non-blocking vfd are not
+ * allowed to set read/write ready.
*/
-static void
-uty_ready(vty_io vio)
+extern vio_vf
+uty_vf_new(vty_io vio, const char* name, int fd, vfd_type_t type,
+ vfd_io_type_t io_type)
{
- enum vty_readiness ready ;
+ vio_vf vf ;
VTY_ASSERT_LOCKED() ;
- vio->cmd_out_done = 0 ; /* not done any command output yet */
+ vf = XCALLOC (MTYPE_VTY, sizeof(struct vio_vf)) ;
- uty_write(vio) ; /* try to clear outstanding stuff */
- do
- {
- ready = uty_cli(vio) ; /* do any CLI work... */
- ready |= uty_write(vio) ; /* ...and any output that generates */
- } while (ready >= now_ready) ;
+ /* Zeroising the structure has set:
+ *
+ * vio = X -- set below
+ * name = X -- set below
+ *
+ * vin_type = VIN_NONE -- see uty_vin_push()
+ * vin_state = vf_closed -- see uty_vin_push()
+ * vin_next = NULL -- see uty_vin_push()
+ *
+ * context = NULL -- see uty_vin_new_context()
+ *
+ * cli = NULL -- no CLI, yet
+ *
+ * ibuf = NULL -- none, yet -- see uty_vin_push()
+ * cl = NULL -- none, yet -- see uty_vin_push()
+ * line_complete = false -- see uty_vin_push()
+ * line_number = 0 -- nothing yet
+ * line_step = 0 -- see uty_vin_push()
+ *
+ * vout_type = VOUT_NONE -- see uty_vout_push()
+ * vout_state = vf_closed -- see uty_vout_push()
+ * vout_next = NULL -- see uty_vout_push()
+ *
+ * pr_master = NULL -- none -- see uty_pipe_write_open()
+ *
+ * obuf = NULL -- none -- see uty_vout_push()
+ *
+ * depth_mark = 0 -- see uty_vout_push()
+ *
+ * vfd = NULL -- no vfd, yet
+ *
+ * blocking = X -- see below
+ * closing = false -- not on the closing list, yet.
+ *
+ * error_seen = 0 -- no error seen, yet
+ *
+ * read_timeout = 0 -- none
+ * write_timeout = 0 -- none
+ *
+ * child = 0 -- none )
+ * terminated = false -- not ) -- see uty_pipe_read/write_open()
+ * term_status = X -- none )
+ *
+ * pr_state = vf_closed -- no pipe return vfd
+ * -- see uty_pipe_read/write_open()
+ * pr_vfd = NULL -- no vfd -- see uty_pipe_read/write_open()
+ *
+ * pr_slave = NULL -- none -- see uty_pipe_read/write_open()
+ *
+ * pr_only = false -- see uty_pipe_read/write_open()
+ */
+ confirm((VIN_NONE == 0) && (VOUT_NONE == 0)) ;
+ confirm(vf_closed == 0) ;
+ confirm(QSTRING_INIT_ALL_ZEROS) ;
+
+ if (vio->blocking)
+ io_type |= vfd_io_blocking ; /* inherit blocking state */
+
+ vf->vio = vio ;
+ vf->blocking = (io_type & vfd_io_blocking) != 0 ;
- uty_sock_set_readiness(&vio->sock, ready) ;
- uty_sock_restart_timer(&vio->sock) ;
+ if (name != NULL)
+ vf->name = XSTRDUP(MTYPE_VTY_NAME, name) ;
+
+ if (fd >= 0)
+ vf->vfd = vio_vfd_new(fd, type, io_type, vf) ;
+
+ return vf ;
} ;
-/*==============================================================================
- * Reading from VTY_TERM.
+/*------------------------------------------------------------------------------
+ * Close the read side of the given vio_vf -- if can.
*
- * The select/pselect call-back ends up in uty_read_ready().
+ * Read closes the vio_vfd -- if the vio_vfd was read-only, this will fully
+ * close it, and the vio_vfd will have been freed.
*
- * Note that uty_write_ready() also calls uty_read_ready, in order to kick the
- * current CLI.
- */
-
-/*------------------------------------------------------------------------------
- * Callback -- qnexus: ready to read -> kicking CLI
+ * For not-final close, if there is any other related I/O, then waits until
+ * that has completed. For example, with a VIN_PIPE:
+ *
+ * * waits for any return input to complete, and pushes it to the relevant
+ * vout.
+ *
+ * * waits to collect the child, and its termination state.
+ *
+ * This means that a not-final close may return errors. For not-blocking vf
+ * may return waiting state. For blocking vf, may block and may later return
+ * timeout error.
+ *
+ * For a final close, if there is any related I/O then will attempt to complete
+ * it -- but will give up if would block. I/O errors on final close are
+ * ignored. A final close may be called in terminating state, so does not
+ * do any "vty_cmd_signal".
+ *
+ * Returns: CMD_SUCCESS -- is all closed
+ * CMD_WAITING -- cannot close at the moment <=> non-blocking
+ * (not if "final")
+ * CMD_IO_ERROR -- something went wrong
+ *
+ * NB: on "final" close returns CMD_SUCCESS no matter what happened, and all
+ * input will have been closed down, and the vf closed.
*/
-static void
-vty_read_qnexus(qps_file qf, void* file_info)
+static cmd_return_code_t
+uty_vf_read_close(vio_vf vf, bool final)
{
- vty_io vio = file_info;
+ cmd_return_code_t ret ;
- VTY_LOCK() ;
+ VTY_ASSERT_CAN_CLOSE_VF(vf) ;
- assert((vio->sock.fd == qf->fd) && (vio == vio->sock.info)) ;
+ ret = CMD_SUCCESS ;
- uty_ready(vio) ;
+ if (vf->vin_state == vf_closed)
+ return ret ; /* quit if already closed */
- VTY_UNLOCK() ;
-}
+ vf->vin_state = vf_closing ; /* TODO wipes out error etc ? */
-/*------------------------------------------------------------------------------
- * Callback -- threads: ready to read -> kicking CLI
- */
-static int
-vty_read_thread(struct thread *thread)
-{
- vty_io vio = THREAD_ARG (thread);
+ /* Do the vfd level read close and mark the vf no longer read_open */
+ if (vf->vin_type < VIN_SPECIALS)
+ vf->vfd = vio_vfd_read_close(vf->vfd) ;
- VTY_LOCK() ;
+ /* Now the vin_type specific clean up. */
+ switch(vf->vin_type)
+ {
+ case VIN_NONE:
+ zabort("invalid VIN_NONE") ;
+ break ;
- assert(vio->sock.fd == THREAD_FD (thread) && (vio == vio->sock.info)) ;
+ case VIN_TERM:
+ ret = uty_term_read_close(vf, final) ;
+ break ;
- vio->sock.t_read = NULL ; /* implicitly */
- uty_ready(vio);
+ case VIN_VTYSH:
+ zabort("tba VIN_VTYSH") ;
+ break ;
- VTY_UNLOCK() ;
- return 0 ;
-}
+ case VIN_CONFIG:
+ ret = uty_config_read_close(vf, final) ;
+ break ;
-/*------------------------------------------------------------------------------
- * Read a lump of bytes and shovel into the keystroke stream
- *
- * Steal keystroke if required -- see keystroke_input()
- *
- * Returns: 0 => nothing available
- * > 0 => read at least one byte
- * -1 => EOF (or not open, or failed)
- */
-extern int
-uty_read (vty_io vio, keystroke steal)
-{
- unsigned char buf[500] ;
- int get ;
+ case VIN_FILE:
+ ret = uty_file_read_close(vf, final) ;
+ break ;
- if (!vio->sock.read_open)
- return -1 ; /* at EOF if not open */
+ case VIN_PIPE:
+ ret = uty_pipe_read_close(vf, final) ;
+ break ;
- get = read_nb(vio->sock.fd, buf, sizeof(buf)) ;
- if (get >= 0)
- keystroke_input(vio->key_stream, buf, get, steal) ;
- else if (get < 0)
- {
- if (get == -1)
- uty_sock_error(vio, "read") ;
+ case VIN_DEV_NULL:
+ ret = CMD_SUCCESS ;
+ break ;
- vio->sock.read_open = 0 ;
- keystroke_input(vio->key_stream, NULL, 0, steal) ;
+ default:
+ zabort("unknown VIN type") ;
+ } ;
- get = -1 ;
+ if ((ret == CMD_SUCCESS) || final)
+ {
+ vf->vin_state = vf_closed ;
+ assert(vf->pr_state == vf_closed) ;
} ;
- return get ;
+ return ret ;
} ;
-/*==============================================================================
- * The write sock action for VTY_TERM type VTY
+/*------------------------------------------------------------------------------
+ * Close the write side of the given vio_vf, if can.
*
- * There are two sets of buffering:
+ * Discards anything beyond the current end_mark, and clears the end_mark.
*
- * cli -- command line -- which reflects the status of the command line
+ * Pushes any outstanding output, and if is pipe will attempt to collect
+ * the child. On not-final close, if cannot complete everything, will return
+ * CMD_WAITING for non-blocking, or block (and may time out). On final close,
+ * will do as much as possible without blocking, and will then close even if
+ * there is outstanding output or child has not been collected.
*
- * cmd -- command output -- which is written to the file only while
- * cmd_out_enabled.
+ * For not-final close, for example, with a VOUT_PIPE:
*
- * The cli output takes precedence.
+ * * waits for any return input to complete, and pushes it to the relevant
+ * vout.
*
- * Output of command stuff is subject to line_control, and may go through the
- * "--more--" mechanism.
- */
-
-static int uty_write_lc(vty_io vio, vio_fifo vf, vio_line_control lc) ;
-static int uty_write_fifo_lc(vty_io vio, vio_fifo vf, vio_line_control lc) ;
-
-/*------------------------------------------------------------------------------
- * Callback -- qnexus: ready to write -> try to empty buffers
- */
-static void
-vty_write_qnexus(qps_file qf, void* file_info)
-{
- vty_io vio = file_info ;
-
- VTY_LOCK() ;
-
- assert((vio->sock.fd == qf->fd) && (vio == vio->sock.info)) ;
-
- uty_ready(vio) ;
-
- VTY_UNLOCK() ;
-}
-
-/*------------------------------------------------------------------------------
- * Callback -- thread: ready to write -> try to empty buffers
- */
-static int
-vty_write_thread(struct thread *thread)
-{
- vty_io vio = THREAD_ARG (thread);
-
- VTY_LOCK() ;
-
- assert(vio->sock.fd == THREAD_FD (thread) && (vio == vio->sock.info)) ;
-
- vio->sock.t_write = NULL; /* implicitly */
- uty_ready(vio) ;
-
- VTY_UNLOCK() ;
- return 0 ;
-}
-
-/*------------------------------------------------------------------------------
- * Write as much as possible of what there is.
+ * * waits to collect the child, and its termination state.
*
- * If not cmd_in_progress, clears cli_blocked if both FIFOs are, or become,
- * empty.
+ * This means that a not-final close may return errors.
*
- * Note that if !write_open, or becomes !write_open, then the FIFOs are empty
- * and all output instantly successful.
+ * For a final close, if there is any related I/O then will attempt to complete
+ * it -- but will give up if would block. I/O errors on final close are
+ * ignored. A final close may be called in terminating state, so does not
+ * do any "vty_cmd_signal".
*
- * Sets write on if prevented from writing everything available for output
- * by write() threatening to block.
+ * If this is the vout_base, unless "final", does NOT actually close the vf or
+ * the vfd -- so the vout_base will still work -- but is marked vf_closing !
+ * The effect is, essentially, to try to empty out any buffers, but not to
+ * do anything that would prevent further output. This is used so that a
+ * command loop can close the vout_base in the usual way, waiting until all
+ * output is flushed, but when uty_close() is finally called, it can output
+ * any close reason there is to hand.
*
- * Returns: write_ready if should now set write on
- * now_ready if should loop back and try again
- * not_ready otherwise
+ * Returns: CMD_SUCCESS -- is all closed
+ * CMD_WAITING -- cannot close at the moment <=> non-blocking
+ * (not if "final")
+ * CMD_IO_ERROR -- something went wrong
+ *
+ * NB: on "final" all output will have been closed down, and the vfd closed,
+ * no matter what the return value says.
+ *
+ * NB: must not have open vins at this or a higher level in the stack.
+ *
+ * NB: does not at this stage discard the obuf.
*/
-static enum vty_readiness
-uty_write(vty_io vio)
+static cmd_return_code_t
+uty_vf_write_close(vio_vf vf, bool final)
{
- int ret ;
+ cmd_return_code_t ret ;
+ bool base ;
- VTY_ASSERT_LOCKED() ;
+ VTY_ASSERT_CAN_CLOSE_VF(vf) ;
- ret = -1 ;
- while (vio->sock.write_open)
- {
- /* Any outstanding line control output takes precedence */
- if (vio->cmd_lc != NULL)
- {
- ret = uty_write_lc(vio, &vio->cmd_obuf, vio->cmd_lc) ;
- if (ret != 0)
- break ;
- }
+ ret = CMD_SUCCESS ;
- /* Next: empty out the cli output */
- ret = vio_fifo_write_nb(&vio->cli_obuf, vio->sock.fd, true) ;
- if (ret != 0)
- break ;
+ if (vf->vout_state == vf_closed)
+ return ret ; /* quit if already closed */
- /* Finished now if not allowed to progress the command stuff */
- if (!vio->cmd_out_enabled)
- return not_ready ; /* done all can do */
+ vf->vout_state = vf_closing ; /* TODO wipes out error etc ? */
- /* Last: if there is something in the command buffer, do that */
- if (!vio_fifo_empty(&vio->cmd_obuf))
- {
- if (vio->cmd_out_done)
- break ; /* ...but not if done once */
-
- vio->cmd_out_done = 1 ; /* done this once */
+ base = (vf == vf->vio->vout_base) ;
- assert(!vio->cli_more_wait) ;
+ /* Must close vin before closing vout at the same level.
+ *
+ * Note that cannot currently be a pipe return slave, because if was
+ * slave to a VOUT_PIPE/VOUT_SHELL_ONLY that vout must have been closed
+ * already, and if was slave to a VIN_PIPE, then that too will have been
+ * closed already (because of the above).
+ */
+ assert( (vf->vio->vin_depth < vf->vio->vout->depth_mark)
+ || (vf->vio->vout_depth ==0) ) ;
+ assert(vf->pr_master == NULL) ;
- if (vio->cmd_lc != NULL)
- ret = uty_write_fifo_lc(vio, &vio->cmd_obuf, vio->cmd_lc) ;
- else
- ret = vio_fifo_write_nb(&vio->cmd_obuf, vio->sock.fd, true) ;
+ /* If there is anything in the obuf beyond the end_mark, then it is
+ * assumed to be surplus to requirements, and we clear the end_mark.
+ */
+ vio_fifo_back_to_end_mark(vf->obuf, false) ;
- /* If moved into "--more--" state@
- *
- * * the "--more--" prompt is ready to be written, so do that now
- *
- * * if that completes, then want to run the CLI *now* to perform the
- * first stage of the "--more--" process.
- */
- if (vio->cli_more_wait)
- {
- ret = vio_fifo_write_nb(&vio->cli_obuf, vio->sock.fd, true) ;
- if (ret == 0)
- return now_ready ;
- } ;
+ /* The vout_type specific close functions will attempt to write
+ * everything away.
+ *
+ * If "final", will only keep going until blocks -- at which point will
+ * bring everything to a shuddering halt.
+ */
+ switch(vf->vout_type)
+ {
+ case VOUT_NONE:
+ zabort("invalid VOUT_NONE") ;
+ break ;
- if (ret != 0)
- break ;
- }
+ case VOUT_TERM:
+ ret = uty_term_write_close(vf, final, base) ;
+ break ;
- /* Exciting stuff: there is nothing left to output...
- *
- * ... watch out for half closed state.
- */
- if (vio->half_closed)
- {
- if (vio->close_reason != NULL)
- {
- vio->cmd_in_progress = 1 ; /* TODO: not use vty_out ? */
+ case VOUT_VTYSH:
+ break ;
- struct vty* vty = vio->vty ;
- if (vio->cli_drawn || vio->cli_dirty)
- vty_out(vty, VTY_NEWLINE) ;
- vty_out(vty, "%% %s%s", vio->close_reason, VTY_NEWLINE) ;
+ case VOUT_FILE:
+ ret = uty_file_write_close(vf, final, base) ;
+ break ;
- vio->cmd_in_progress = 0 ;
+ case VOUT_PIPE:
+ case VOUT_SHELL_ONLY:
+ ret = uty_pipe_write_close(vf, final, base,
+ vf->vout_type == VOUT_SHELL_ONLY) ;
+ break ;
- vio->close_reason = NULL ; /* MUST discard now... */
- continue ; /* ... and write away */
- } ;
+ case VOUT_CONFIG:
+ ret = uty_file_write_close(vf, final, base) ; /* treat as file */
+ break ;
- if (!vio->closed) /* avoid recursion */
- uty_close(vio) ;
+ case VOUT_DEV_NULL:
+ case VOUT_STDOUT:
+ case VOUT_STDERR:
+ ret = CMD_SUCCESS ;
+ break ;
- return not_ready ; /* it's all over */
- } ;
+ default:
+ zabort("unknown VOUT type") ;
+ } ;
- /* For VTY_TERM: if the command line is not drawn, now is a good
- * time to do that.
- */
- if (vio->type == VTY_TERM)
- if (uty_cli_draw_if_required(vio))
- continue ; /* do that now. */
+ assert(vf->vin_state == vf_closed) ;
- /* There really is nothing left to output */
- return not_ready ;
- } ;
+ if (((ret == CMD_SUCCESS) && !base) || final)
+ {
- /* Arrives here if there is more to do, or failed (or was !write_open) */
+ assert(vf->vio->obuf == vf->obuf) ;
+ assert(vio_fifo_empty(vf->obuf)) ;
- if (ret >= 0)
- return write_ready ;
- /* If is write_open, then report the error
- *
- * If still read_open, let the reader pick up and report the error, when it
- * has finished anything it has buffered.
- */
- if (vio->sock.write_open)
- {
- if (!vio->sock.read_open)
- uty_sock_error(vio, "write") ;
+ if (vf->vout_type < VOUT_SPECIALS)
+ vf->vfd = vio_vfd_close(vf->vfd) ;
+ else
+ assert(vf->vfd == NULL) ;
- vio->sock.write_open = 0 ; /* crash close write */
+ vf->vout_state = vf_closed ;
} ;
- /* For whatever reason, is no longer write_open -- clear all buffers.
- */
- vio_fifo_clear(&vio->cli_obuf) ; /* throw away cli stuff */
- uty_out_clear(vio) ; /* throw away cmd stuff */
-
- vio->close_reason = NULL ; /* too late for this */
-
- return not_ready ; /* NB: NOT blocked by I/O */
+ return ret ;
} ;
/*------------------------------------------------------------------------------
- * Write as much as possible -- for "monitor" output.
- *
- * Outputs only:
+ * Free the given vio_vf structure and all its contents.
*
- * a. outstanding line control stuff.
+ * Expects the vfd to already have been closed, but will close it if not.
*
- * b. contents of CLI buffer
+ * Expects any cli to be closed, but will close it if not.
*
- * And:
- *
- * a. does not report any errors.
- *
- * b. does not change anything except the state of the buffers.
- *
- * In particular, for the qpthreaded world, does not attempt to change
- * the state of the qfile or any other "thread private" structures.
- *
- * Returns: > 0 => blocked
- * 0 => all gone
- * < 0 => failed (or !write_open)
+ * Assumes has been removed from any and all lists !
*/
-static int
-uty_write_monitor(vty_io vio)
+static vio_vf
+uty_vf_free(vio_vf vf)
{
- VTY_ASSERT_LOCKED() ;
+ assert((vf->vin_state == vf_closed) && (vf->vout_state == vf_closed)
+ && (vf->pr_state == vf_closed)) ;
- if (!vio->sock.write_open)
- return -1 ;
+ XFREE(MTYPE_VTY_NAME, vf->name) ;
- if (vio->cmd_lc != NULL)
- {
- int ret ;
- ret = uty_write_lc(vio, &vio->cmd_obuf, vio->cmd_lc) ;
+ assert(vf->cli == NULL) ;
- if (ret != 0)
- return ret ;
- } ;
+ vf->ibuf = vio_fifo_free(vf->ibuf) ;
+ vf->cl = qs_reset(vf->cl, free_it) ;
+ vf->obuf = vio_fifo_free(vf->obuf) ;
- return vio_fifo_write_nb(&vio->cli_obuf, vio->sock.fd, true) ;
+ vf->context = cmd_context_free(vf->context, false) ; /* not a copy */
+
+ vf->vfd = vio_vfd_close(vf->vfd) ; /* for completeness */
+ vf->pr_vfd = vio_vfd_close(vf->pr_vfd) ; /* for completeness */
+
+ XFREE(MTYPE_VTY, vf) ;
+
+ return NULL ;
} ;
+
+
+
/*------------------------------------------------------------------------------
- * Write the given FIFO to output -- subject to possible line control.
- *
- * Note that even if no "--more--" is set, will have set some height, so
- * that does not attempt to empty the FIFO completely all in one go.
- *
- * If the line control becomes "paused", it is time to enter "--more--" state
- * -- unless the FIFO is empty (or "--more--" is not enabled).
+ * Dealing with an I/O error on VTY socket
*
- * NB: expects that the sock is write_open
+ * If this is the first error for this VTY, produce suitable log message.
*
- * Returns: > 0 => blocked or completed one tranche
- * 0 => all gone
- * < 0 => failed
+ * If is a "monitor", turn that off, *before* issuing log message.
*/
-static int
-uty_write_fifo_lc(vty_io vio, vio_fifo vf, vio_line_control lc)
+extern int
+uty_vf_error(vio_vf vf, const char* what, int err)
{
- int ret ;
- char* src ;
- size_t have ;
+ vty_io vio = vf->vio ;
- /* Collect another line_control height's worth of output.
- *
- * Expect the line control to be empty at this point, but it does not have
- * to be.
- */
- vio_lc_set_pause(lc) ; /* clears lc->paused */
+ VTY_ASSERT_LOCKED() ;
+ VTY_ASSERT_CLI_THREAD() ;
- src = vio_fifo_get_rdr(vf, &have) ;
+ /* can no longer be a monitor ! *before* any logging ! */
+ uty_set_monitor(vio, off) ;
- while ((src != NULL) && (!lc->paused))
+ /* if this is the first error, log it */
+ if (vf->error_seen == 0)
{
- size_t take ;
- take = vio_lc_append(lc, src, have) ;
- src = vio_fifo_step_rdr(vf, &have, take) ;
- } ;
+ const char* type = "?" ; /* TODO */
- vio->cli_dirty = (lc->col != 0) ;
- /* Write the contents of the line control */
- ret = uty_write_lc(vio, vf, lc) ;
+ vf->error_seen = err ;
+ zlog(NULL, LOG_WARNING, "%s: %s failed on fd %d: %s",
+ type, what, vio_vfd_fd(vf->vfd), errtoa(err, 0).str) ;
+ } ;
- if (ret < 0)
- return ret ; /* give up now if failed. */
+ return -1 ;
+} ;
- if ((ret == 0) && vio_fifo_empty(vf))
- return 0 ; /* FIFO and line control empty */
- /* If should now do "--more--", now is the time to prepare for that.
- *
- * Entering more state issues a new prompt in the CLI buffer, which can
- * be written once line control write completes.
- *
- * The "--more--" cli will not do anything until the CLI buffer has
- * cleared.
- */
- if (lc->paused && vio->cli_more_enabled)
- uty_cli_enter_more_wait(vio) ;
- return 1 ; /* FIFO or line control, not empty */
-} ;
/*------------------------------------------------------------------------------
- * Write contents of line control (if any).
+ * Set required read ready state. Applies the current read timeout.
*
- * NB: expects that the sock is write_open
+ * Forces off if: vf->vin_state != vf_open
*
- * NB: does nothing other than write() and buffer management.
+ * Does nothing if: vf->vin_state == vf_closed
+ * or: vf->vfd == NULL
*
- * Returns: > 0 => blocked
- * 0 => all gone
- * < 0 => failed
+ * NB: must NOT be a "blocking" vf
*/
-static int
-uty_write_lc(vty_io vio, vio_fifo vf, vio_line_control lc)
+extern void
+uty_vf_set_read(vio_vf vf, on_off_b how)
{
- int ret ;
-
- ret = vio_lc_write_nb(vio->sock.fd, lc) ;
-
- if (ret <= 0)
- vio_fifo_sync_rdr(vf) ; /* finished with FIFO contents */
+ if (vf->vin_state != vf_open)
+ how = off ;
- return ret ;
+ if (vf->vin_state != vf_closed)
+ vio_vfd_set_read(vf->vfd, how, vf->read_timeout) ;
} ;
/*------------------------------------------------------------------------------
- * Start command output -- clears down the line control.
+ * Set required read ready timeout -- if already read on, restart it.
*
- * Requires that that current line is empty -- restarts the line control
- * on the basis that is at column 0.
+ * If this is a blocking vf, will set the timeout value, but since can never
+ * be "read on", will never attempt to restart any timer !
*/
extern void
-uty_cmd_output_start(vty_io vio)
+uty_vf_set_read_timeout(vio_vf vf, vty_timer_time read_timeout)
{
- if (vio->cmd_lc != NULL)
- vio_lc_clear(vio->cmd_lc) ;
+ vf->read_timeout = read_timeout ;
+
+ if (!vf->blocking)
+ uty_vf_set_read(vf, on) ;
} ;
/*------------------------------------------------------------------------------
- * Set the effective height for line control (if any)
+ * Set required write ready state. Applies the current write timeout.
*
- * If using line_control, may enable the "--more--" output handling.
+ * Forces off if: vf->vout_state != vf_open
*
- * If not, want some limit on the amount of stuff output at a time.
+ * Does nothing if: vf->vout_state == vf_closed
+ * or: vf->vfd == NULL
*
- * Sets the line control window width and height.
- * Sets cli_more_enabled if "--more--" is enabled.
+ * NB: must NOT be a "blocking" vf
*/
extern void
-uty_set_height(vty_io vio)
+uty_vf_set_write(vio_vf vf, on_off_b how)
{
- bool on ;
-
- on = 0 ; /* default state */
-
- if ((vio->cmd_lc != NULL) && !vio->half_closed)
- {
- int height ;
-
- height = 0 ; /* default state */
-
- if ((vio->width) != 0)
- {
- /* If window size is known, use lines or given height */
- if (vio->lines >= 0)
- height = vio->lines ;
- else
- {
- /* Window height, leaving one line from previous "page"
- * and one line for the "--more--" -- if at all possible
- */
- height = vio->height - 2 ;
- if (height < 1)
- height = 1 ;
- } ;
- }
- else
- {
- /* If window size not known, use lines if that has been set
- * explicitly for this terminal.
- */
- if (vio->lines_set)
- height = vio->lines ;
- } ;
-
- if (height > 0)
- on = 1 ; /* have a defined height */
- else
- height = 200 ; /* but no "--more--" */
+ if ((vf->vout_state != vf_open) && (vf->vout_state != vf_closing))
+ how = off ;
- vio_lc_set_window(vio->cmd_lc, vio->width, height) ;
- } ;
-
- vio->cli_more_enabled = on ;
+ if (vf->vout_state != vf_closed)
+ vio_vfd_set_write(vf->vfd, how, vf->write_timeout) ;
} ;
-/*==============================================================================
- * Timer for VTY_TERM (and VTY_SHELL_SERV).
- */
-
/*------------------------------------------------------------------------------
- * Timer has expired.
- *
- * If half_closed, then this is curtains -- have waited long enough !
+ * Set required write ready timeout -- if already write on, restart it.
*
- * Otherwise, half close the VTY and leave it to the death-watch to sweep up.
- */
-static void
-uty_timer_expired (vty_io vio)
-{
- VTY_ASSERT_LOCKED() ;
-
- if (vio->half_closed)
- return uty_close(vio) ; /* curtains */
-
- uty_half_close(vio, "Timed out") ; /* bring input side to a halt */
- } ;
-
-/*------------------------------------------------------------------------------
- * Callback -- qnexus: deal with timer timeout.
+ * If this is a blocking vf, will set the timeout value, but since can never
+ * be "write on", will never attempt to restart any timer !
*/
-static void
-vty_timer_qnexus (qtimer qtr, void* timer_info, qtime_t when)
-{
- vty_io vio = timer_info ;
-
- VTY_LOCK() ;
-
- uty_timer_expired(vio);
-
- VTY_UNLOCK() ;
-}
-
-/*------------------------------------------------------------------------------
- * Callback -- thread: deal with timer timeout.
- */
-static int
-vty_timer_thread (struct thread *thread)
+extern void
+uty_vf_set_write_timeout(vio_vf vf, vty_timer_time write_timeout)
{
- vty_io vio = THREAD_ARG (thread);
+ vf->write_timeout = write_timeout ;
- VTY_LOCK() ;
-
- vio->sock.t_timer = NULL ; /* implicitly */
-
- uty_timer_expired(vio) ;
-
- VTY_UNLOCK() ;
- return 0;
-}
+ if (!vf->blocking)
+ uty_vf_set_write(vf, on) ;
+} ;
/*==============================================================================
- * VTY Listener(s)
+ * Child care.
*
- * Have listeners for VTY_TERM and VTY_SHELL_SERV types of VTY.
- */
-
-typedef struct vty_listener* vty_listener ;
-
-struct vty_listener
-{
- vty_listener next ; /* ssl type list */
-
- enum vty_type type ;
-
- struct vio_sock sock ;
-};
-
-/* List of listeners so can tidy up. */
-static vty_listener vty_listeners_list = NULL ;
-
-/* Prototypes for listener stuff */
-static int uty_serv_sock_addrinfo (const char *hostname, unsigned short port) ;
-static int uty_serv_sock(const char* addr, unsigned short port) ;
-static int uty_serv_sock_open(sa_family_t family, int type, int protocol,
- struct sockaddr* sa, unsigned short port) ;
-static int uty_serv_vtysh(const char *path) ;
-static int vty_accept_thread(struct thread *thread) ;
-static void vty_accept_qnexus(qps_file qf, void* listener) ;
-static int uty_accept(vty_listener listener, int listen_sock) ;
-static int uty_accept_term(vty_listener listener) ;
-static int uty_accept_shell_serv (vty_listener listener) ;
-
-static void uty_serv_start_listener(int fd, enum vty_type type) ;
-
-/*------------------------------------------------------------------------------
- * If possible, will use getaddrinfo() to find all the things to listen on.
+ * Management of vio_child objects and the vio_childer_list.
+ *
+ * When child is registered it is placed on the vio_childer_list. It remains
+ * there until it is "collected", that is until a waitpid() has returned a
+ * "report" for the child.
+ *
+ * When a parent waits for a child to be collected, it may be in one of three
+ * states:
+ *
+ * TODO
+ *
+ * When a child is collected, or becomes overdue, the parent is signalled (if
+ * required) and the child->awaited state is cleared.
+ *
+ * When a parent "dismisses" a child, if it has not yet been collected it is
+ * "smacked" -- kill(SIGTERM) -- but kept on the register until it is
+ * collected. When a child which has been collected is dismissed it is freed.
+ *
+ * At "curtains" the register may contain children which have been dismissed,
+ * but not yet collected. Should NOT contain any children with living parents.
+ * All children remaining on the register are smacked, and all with no living
+ * parents are freed. (This could leave children on the register, but avoids
+ * the possibility of a dangling reference from a parent.)
*/
-
-#if defined(HAVE_IPV6) && !defined(NRL)
-# define VTY_USE_ADDRINFO 1
-#else
-# define VTY_USE_ADDRINFO 0
-#endif
+static void uty_child_collected(vio_child child, int report) ;
+static vty_timer_time vty_child_overdue(vio_timer timer, void* action_info) ;
+static void uty_child_signal_parent(vio_child child) ;
+static vio_child uty_child_free(vio_child child) ;
/*------------------------------------------------------------------------------
- * Open VTY listener(s)
+ * Set vty_child_signal_nexus() -- if required.
*
- * addr -- address ) to listen for VTY_TERM connections
- * port -- port )
- * path -- path for VTYSH connections -- if VTYSH_ENABLED
+ * Note that this is set/cleared under VTY_LOCK() and its own mutex. This
+ * allows it to be read under its own mutex and/or VTY_LOCK().
*/
extern void
-uty_open_listeners(const char *addr, unsigned short port, const char *path)
+uty_child_signal_nexus_set(vty_io vio)
{
VTY_ASSERT_LOCKED() ;
- /* If port is set to 0, do not listen on TCP/IP at all! */
- if (port)
- {
- int n ;
+ assert(vio->blocking) ;
- if (VTY_USE_ADDRINFO)
- n = uty_serv_sock_addrinfo(addr, port);
- else
- n = uty_serv_sock(addr, port);
+ if (!vty_is_cli_thread())
+ {
+ qpt_mutex_lock(vty_child_signal_mutex) ;
- if (n == 0)
- uzlog(NULL, LOG_ERR, "could not open any VTY listeners") ;
- }
+ vty_child_signal_nexus = qpn_find_self() ;
- /* If want to listen for vtysh, set up listener now */
- if (VTYSH_ENABLED && (path != NULL))
- uty_serv_vtysh(path) ;
+ qpt_mutex_unlock(vty_child_signal_mutex) ;
+ } ;
} ;
/*------------------------------------------------------------------------------
- * Close VTY listener
- *
- * addr -- address ) to listen for VTY_TERM connections
- * port -- port )
- * path -- path for VTYSH connections -- if VTYSH_ENABLED
+ * If there is a nexus to signal, clear the indicator and signal the
+ * associated thread.
*/
extern void
-uty_close_listeners(void)
+vty_child_signal_nexus_signal(void)
{
- vty_listener listener ;
+ qpt_mutex_lock(vty_child_signal_mutex) ;
- VTY_ASSERT_LOCKED() ;
+ if (vty_child_signal_nexus != NULL)
+ qpt_thread_signal(vty_child_signal_nexus->thread_id,
+ vty_child_signal_nexus->pselect_signal) ;
- while ((listener = ssl_pop(&listener, vty_listeners_list, next)) != NULL)
- {
- uty_sock_close(&listener->sock) ; /* no ceremony, no flowers */
- XFREE(MTYPE_VTY, listener) ;
- } ;
-} ;
+ qpt_mutex_unlock(vty_child_signal_mutex) ;
+}
/*------------------------------------------------------------------------------
- * Open listener(s) for VTY_TERM -- using getaddrinfo().
+ * Set vty_child_signal_nexus() -- if required.
*
- * Returns: number of listeners successfully opened.
+ * Note that this is set/cleared under VTY_LOCK() and its own mutex. This
+ * allows it to be read under its own mutex and/or VTY_LOCK().
*/
-static int
-uty_serv_sock_addrinfo (const char *hostname, unsigned short port)
+extern void
+uty_child_signal_nexus_clear(vty_io vio)
{
-#if VTY_USE_ADDRINFO
-
-# ifndef HAVE_IPV6
-# error Using getaddrinfo() but HAVE_IPV6 is not defined ??
-# endif
-
- int ret;
- int n ;
- struct addrinfo req;
- struct addrinfo *ainfo;
- struct addrinfo *ainfo_save;
- char port_str[16];
-
VTY_ASSERT_LOCKED() ;
- /* Want to listen, TCP-wise, on all available address families, on the
- * given port.
- */
- memset (&req, 0, sizeof (struct addrinfo));
- req.ai_flags = AI_PASSIVE;
- req.ai_family = AF_UNSPEC;
- req.ai_socktype = SOCK_STREAM;
- snprintf(port_str, sizeof(port_str), "%d", port);
-
- ret = getaddrinfo (hostname, port_str, &req, &ainfo);
+ assert(vio->blocking) ;
- if (ret != 0)
+ if (!vty_is_cli_thread())
{
- fprintf (stderr, "getaddrinfo failed: %s\n", eaitoa(ret, errno, 0).str);
- exit (1);
- }
-
- /* Open up sockets on all AF_INET and AF_INET6 addresses */
- ainfo_save = ainfo;
+ qpt_mutex_lock(vty_child_signal_mutex) ;
- n = 0 ;
- do
- {
- if ((ainfo->ai_family != AF_INET) && (ainfo->ai_family != AF_INET6))
- continue;
+ vty_child_signal_nexus = NULL ;
- assert(ainfo->ai_family == ainfo->ai_addr->sa_family) ;
-
- ret = uty_serv_sock_open(ainfo->ai_family, ainfo->ai_socktype,
- ainfo->ai_protocol, ainfo->ai_addr, port) ;
- if (ret >= 0)
- ++n ;
- }
- while ((ainfo = ainfo->ai_next) != NULL);
-
- freeaddrinfo (ainfo_save);
-
- return n ;
-
-#else
- zabort("uty_serv_sock_addrinfo not implemented") ;
-#endif /* VTY_USE_ADDRINFO */
-}
+ qpt_mutex_unlock(vty_child_signal_mutex) ;
+ } ;
+} ;
/*------------------------------------------------------------------------------
- * Open listener(s) for VTY_TERM -- not using getaddrinfo() !
+ * New child.
*
- * Returns: number of listeners successfully opened.
+ * NB: must register child promptly (under VTY_LOCK, at same time as fork)
+ * in order to avoid missing the SIG_CHLD !
*/
-static int
-uty_serv_sock(const char* addr, unsigned short port)
+extern vio_child
+uty_child_register(pid_t pid, vio_vf parent)
{
- int ret;
- int n ;
- union sockunion su_addr ;
- struct sockaddr* sa ;
+ vio_child child ;
VTY_ASSERT_LOCKED() ;
- n = 0 ; /* nothing opened yet */
+ child = XCALLOC(MTYPE_VTY, sizeof(struct vio_child)) ;
- /* If have an address, see what kind and whether valid */
- sa = NULL ;
-
- if (addr != NULL)
- {
- ret = str2sockunion (addr, &su_addr) ;
- if (ret == 0)
- sa = &su_addr.sa ;
- else
- uzlog(NULL, LOG_ERR, "bad address %s, cannot listen for VTY", addr);
- } ;
+ /* Zeroising has set:
+ *
+ * list -- NULLs -- added to vio_childer_list, below
+ *
+ * parent -- NULL -- parent vf set, below
+ *
+ * pid -- X -- child pid set below
+ * collected -- false -- not yet collected
+ * report -- X -- not relevant until collected
+ *
+ * awaited -- false -- not waiting for child
+ * overdue -- false -- child not overdue
+ * timer -- NULL -- no waiting timer set
+ */
- /* Try for AF_INET */
- ret = uty_serv_sock_open(AF_INET, SOCK_STREAM, 0, sa, port) ;
- if (ret >= 0)
- ++n ; /* opened socket */
- if (ret == 1)
- sa = NULL ; /* used the address */
-
-#if HAVE_IPV6
- /* Try for AF_INET6 */
- ret = uty_serv_sock_open(AF_INET6, SOCK_STREAM, 0, sa, port) ;
- if (ret >= 0)
- ++n ; /* opened socket */
- if (ret == 1)
- sa = NULL ; /* used the address */
-#endif
+ child->parent = parent ;
+ child->pid = pid ;
- /* If not used the address... something wrong */
- if (sa != NULL)
- uzlog(NULL, LOG_ERR, "could not use address %s, to listen for VTY", addr);
+ sdl_push(vio_childer_list, child, list) ;
- /* Done */
- return n ;
-}
+ return child ;
+} ;
/*------------------------------------------------------------------------------
- * Open a VTY_TERM listener socket.
+ * Set waiting for child to be collected.
*
- * The sockaddr 'sa' may be NULL or of a different address family, in which
- * case "any" address is used.
- *
- * If the sockaddr 'sa' is used, only the address portion is used.
- *
- * Returns: < 0 => failed
- * == 0 => OK -- did not use the sockaddr 'sa'.
- * > 1 => OK -- and did use the sockaddr 'sa'
+ * This is for !vf->blocking: set timer and leave, waiting for SIGCHLD event.
*/
-static int
-uty_serv_sock_open(sa_family_t family, int type, int protocol,
- struct sockaddr* sa, unsigned short port)
+extern void
+uty_child_awaited(vio_child child, vty_timer_time timeout)
{
- union sockunion su ;
- int sock ;
- int ret ;
+ VTY_ASSERT_CLI_THREAD_LOCKED() ;
- VTY_ASSERT_LOCKED() ;
-
- /* Is there an address and is it for this family ? */
- if ((sa != NULL) || (sa->sa_family == family))
- /* Set up sockunion containing required family and address */
- sockunion_new_sockaddr(&su, sa) ;
- else
- {
- /* no address or wrong family -- set up empty sockunion of
- * required family */
- sockunion_init_new(&su, family) ;
- sa = NULL ;
- } ;
-
- /* Open the socket and set its properties */
- sock = sockunion_socket(family, type, protocol) ;
- if (sock < 0)
- return -1 ;
-
- ret = sockopt_reuseaddr (sock);
-
- if (ret >= 0)
- ret = sockopt_reuseport (sock);
-
- if (ret >= 0)
- ret = set_nonblocking(sock);
-
-#if defined(HAVE_IPV6) && defined(IPV6_V6ONLY)
- /* Want only IPV6 on ipv6 socket (not mapped addresses)
- *
- * This distinguishes 0.0.0.0 from :: -- without this, bind() will reject the
- * attempt to bind to :: after binding to 0.0.0.0.
- */
- if ((ret >= 0) && (sa->sa_family == AF_INET6))
- {
- int on = 1;
- ret = setsockopt (sock, IPPROTO_IPV6, IPV6_V6ONLY, (void *)&on, sizeof(on));
- }
-#endif
-
- if (ret >= 0)
- ret = sockunion_bind (sock, &su, port, sa) ;
-
- if (ret >= 0)
- ret = sockunion_listen (sock, 3);
+ assert(child->parent != NULL) ;
+ assert(!child->parent->blocking) ;
- if (ret < 0)
- {
- close (sock);
- return -1 ;
- }
+ child->awaited = true ;
- /* Socket is open -- set VTY Term listener going */
- uty_serv_start_listener(sock, VTY_TERM) ;
+ if (child->timer == NULL)
+ child->timer = vio_timer_init_new(NULL, vty_child_overdue, child) ;
- /* Return OK and signal whether used address or not */
- return (sa != NULL) ? 1 : 0 ;
+ vio_timer_set(child->timer, timeout) ;
} ;
/*------------------------------------------------------------------------------
- * Open a VTY_SHEL_SERV listener socket (UNIX domain).
+ * See if parent can collect child -- directly.
+ *
+ * This is for vf->blocking,
+ *
+ * If can, will collect now -- marking collected and taking off the
+ * vio_childer_list.
+ *
+ * If cannot immediately collect, if final mark overdue and return.
*
- * Returns: < 0 => failed
- * >= 0 => OK
+ * Otherwise wait for up to timeout seconds for a suitable SIGCHLD or related
+ * wake-up signal.
+ *
+ * NB: this blocks with the VTY_LOCK() in its hands !! But this is only
+ * required for configuration file reading... and timeout is limited.
+ *
+ * Returns: true <=> collected
+ * false => timed out or final
*/
-static int
-uty_serv_vtysh(const char *path)
+extern bool
+uty_child_collect(vio_child child, vty_timer_time timeout, bool final)
{
- int ret;
- int sock, sa_len, path_len ;
- struct sockaddr_un sa_un ;
- mode_t old_mask;
- struct zprivs_ids_t ids;
+ bool first ;
VTY_ASSERT_LOCKED() ;
- /* worry about the path length */
- path_len = strlen(path) + 1 ;
- if (path_len >= (int)sizeof(sa_un.sun_path))
- {
- uzlog(NULL, LOG_ERR, "path too long for unix stream socket: '%s'", path);
- return -1 ;
- } ;
+ assert(child->parent != NULL) ;
+ assert(child->parent->blocking) ;
+ assert(child->timer == NULL) ;
- /* First of all, unlink existing socket */
- unlink (path);
+ assert(child->pid > 0) ;
- /* Make UNIX domain socket. */
- sock = socket (AF_UNIX, SOCK_STREAM, 0);
- if (sock < 0)
+ first = true ;
+ while (1)
{
- uzlog(NULL, LOG_ERR, "Cannot create unix stream socket: %s",
- errtoa(errno, 0).str) ;
- return -1 ;
- }
-
- /* Bind to the required path */
- memset (&sa_un, 0, sizeof(sa_un));
- sa_un.sun_family = AF_UNIX;
- strncpy (sa_un.sun_path, path, sizeof(sa_un.sun_path) - 1);
+ pid_t pid ;
+ int report ;
- sa_len = SUN_LEN(&sa_un) ;
-
-#ifdef HAVE_STRUCT_SOCKADDR_UN_SUN_LEN
- sa_un.sun_len = sa_len ;
-#endif
+ qps_mini_t qm ;
+ sigset_t* sig_mask = NULL ;
- old_mask = umask (0007);
+ if (child->collected)
+ return true ; /* have collected */
- ret = bind (sock, (struct sockaddr *) &sa_un, sa_len) ;
- if (ret < 0)
- uzlog(NULL, LOG_ERR, "Cannot bind path %s: %s", path, errtoa(errno, 0).str);
+ pid = waitpid(child->pid, &report, WNOHANG) ;
- if (ret >= 0)
- ret = set_nonblocking(sock);
-
- if (ret >= 0)
- {
- ret = listen (sock, 5);
- if (ret < 0)
- uzlog(NULL, LOG_ERR, "listen(fd %d) failed: %s", sock,
- errtoa(errno, 0).str) ;
- } ;
+ if (pid == child->pid)
+ {
+ /* Collected the child */
+ uty_child_collected(child, report) ;
- zprivs_get_ids(&ids);
+ return true ; /* have collected */
+ } ;
- if (ids.gid_vty > 0)
- {
- /* set group of socket */
- if ( chown (path, -1, ids.gid_vty) )
- uzlog (NULL, LOG_ERR, "uty_serv_vtysh: could chown socket, %s",
- errtoa(errno, 0).str) ;
- }
+ if (pid != 0)
+ {
+ int err = 0 ;
- umask (old_mask);
+ /* With the exception of EINTR, all other returns are, essentially,
+ * impossible...
+ *
+ * (1) ECHLD means that the given pid is not a child...
+ * ...which is impossible if not collected -- but treat as
+ * "overdue".
+ *
+ * (2) only other known error is EINVAL -- invalid options...
+ * absolutely impossible.
+ *
+ * (3) some pid other than the given child is an invalid response !
+ */
+ if (pid < 0)
+ {
+ if (errno == EINTR)
+ continue ;
- /* Give up now if failed along the way */
- if (ret < 0)
- {
- close (sock) ;
- return -1 ;
- } ;
+ err = errno ;
- /* Socket is open -- set VTY Term listener going */
- uty_serv_start_listener(sock, VTY_SHELL_SERV) ;
+ zlog_err("waitpid(%d) returned %s", child->pid,
+ errtoa(err, 0).str) ;
+ }
+ else
+ zlog_err("waitpid(%d) returned pid=%d", child->pid, pid) ;
- return 0 ;
-} ;
+ if (err != ECHILD)
+ zabort("impossible return from waitpid()") ;
-/*------------------------------------------------------------------------------
- * Socket is open -- set a VTY listener going
- *
- * Note that the vyt_listener structure is passed to the accept action function.
- */
-static void
-uty_serv_start_listener(int fd, enum vty_type type)
-{
- vty_listener listener ;
+ final = true ; /* treat ECHLD as last straw ! */
+ } ;
- listener = XCALLOC(MTYPE_VTY, sizeof (struct vty_listener));
+ /* Waiting for child. */
+ if (final)
+ {
+ child->overdue = true ;
+ return false ; /* overdue */
+ } ;
- ssl_push(vty_listeners_list, listener, next) ;
- uty_sock_init_new(&listener->sock, fd, listener) ;
+ /* Need to wait -- if this is the first time through, prepare for
+ * that.
+ */
+ if (first)
+ {
+ qps_mini_set(qm, -1, 0, 6) ;
- listener->type = type ;
+ if (vty_is_cli_thread())
+ sig_mask = NULL ;
+ else
+ sig_mask = vty_child_signal_nexus->pselect_mask ;
- if (vty_cli_nexus)
- listener->sock.action.read.qnexus = vty_accept_qnexus ;
- else
- listener->sock.action.read.thread = vty_accept_thread ;
+ first = false ;
+ } ;
- uty_sock_set_read(&listener->sock, on) ;
+ /* Wait on pselect. */
+ if (qps_mini_wait(qm, sig_mask, true) == 0)
+ final = true ; /* timed out => now final */
+ } ;
} ;
/*------------------------------------------------------------------------------
- * Accept action for the thread world -- create and dispatch VTY
+ * Dismiss child -- if not collected, smack but leave to be collected in
+ * due course (or swept up at "curtains").
*/
-static int
-vty_accept_thread(struct thread *thread)
+extern vio_child
+uty_child_dismiss(vio_child child, bool final)
{
- vty_listener listener = THREAD_ARG(thread) ;
- int result ;
+ VTY_ASSERT_LOCKED() ;
- VTY_LOCK() ;
+ if (child != NULL)
+ {
+ if (!child->collected)
+ {
+ assert(child->pid > 0) ;
- result = uty_accept(listener, THREAD_FD(thread));
+ kill(child->pid, SIGKILL) ; /* hasten the end */
- uty_sock_set_read(&listener->sock, on) ;
+ if (final)
+ {
+ assert(child->parent == NULL) ;
+ sdl_del(vio_childer_list, child, list) ;
+ child->collected = true ; /* forceably */
+ } ;
- VTY_UNLOCK() ;
- return result ;
-} ;
+ child->overdue = true ; /* too late for parent */
+ } ;
-/*------------------------------------------------------------------------------
- * Accept action for the qnexus world -- create and dispatch VTY
- */
-static void
-vty_accept_qnexus(qps_file qf, void* listener)
-{
- VTY_LOCK() ;
+ child->parent = NULL ; /* orphan from now on */
+ child->awaited = false ; /* nobody waiting */
- uty_accept(listener, qf->fd);
+ if (child->collected)
+ uty_child_free(child) ;
+ } ;
- VTY_UNLOCK() ;
-}
+ return NULL ;
+} ;
/*------------------------------------------------------------------------------
- * Accept action -- create and dispatch VTY_TERM or VTY_SHELL_SERV
+ * At "curtains" -- empty out anything left in the child register.
+ *
+ * The only children that can be left are dismissed children that have yet to
+ * be collected.
*/
-static int
-uty_accept(vty_listener listener, int listen_sock)
+extern void
+vty_child_close_register(void)
{
- VTY_ASSERT_LOCKED() ;
-
- assert(listener->sock.fd == listen_sock) ;
-
- switch (listener->type)
- {
- case VTY_TERM:
- return uty_accept_term(listener) ;
-
- case VTY_SHELL_SERV:
- return uty_accept_shell_serv(listener) ;
-
- default:
- zabort("unknown vty type") ;
- } ;
+ while (vio_childer_list != NULL)
+ uty_child_dismiss(vio_childer_list, true) ; /* final */
} ;
/*------------------------------------------------------------------------------
- * Accept action -- create and dispatch VTY_TERM
+ * See whether any children are ready for collection, and check each one
+ * against the register.
+ *
+ * Perform waitpid( , , WNOHANG) until no child is returned, and process
+ * each one against the register.
+ *
+ * The is done when a SIGCHLD is route through the event mechanism.
+ *
+ * If another SIGCHLD occurs while this is being done, that will later cause
+ * another call of this function -- at worst it may find that the child in
+ * question has already been collected.
+ *
+ * This is also done when about to block waiting for a child.
+ *
+ * Set any children that can be collected, collected and signal to any parents
+ * that their children are now ready.
*/
-static int
-uty_accept_term(vty_listener listener)
+extern void
+uty_sigchld(void)
{
- int sock_fd;
- union sockunion su;
- int ret;
- unsigned int on;
- struct prefix *p ;
-
- VTY_ASSERT_LOCKED() ;
-
- /* We can handle IPv4 or IPv6 socket. */
- sockunion_init_new(&su, AF_UNSPEC) ;
+ VTY_ASSERT_CLI_THREAD_LOCKED() ;
- sock_fd = sockunion_accept (listener->sock.fd, &su);
-
- if (sock_fd < 0)
- {
- if (sock_fd == -1)
- uzlog (NULL, LOG_WARNING, "can't accept vty socket : %s",
- errtoa(errno, 0).str) ;
- return -1;
- }
-
- /* Really MUST have non-blocking */
- ret = set_nonblocking(sock_fd) ; /* issues WARNING if fails */
- if (ret < 0)
- {
- close(sock_fd) ;
- return -1 ;
- } ;
-
- /* New socket is open... worry about access lists */
- p = sockunion2hostprefix (&su);
- ret = 0 ; /* so far, so good */
-
- if ((p->family == AF_INET) && vty_accesslist_name)
- {
- /* VTY's accesslist apply. */
- struct access_list* acl ;
-
- if ((acl = access_list_lookup (AFI_IP, vty_accesslist_name)) &&
- (access_list_apply (acl, p) == FILTER_DENY))
- ret = -1 ;
- }
-
-#ifdef HAVE_IPV6
- if ((p->family == AF_INET6) && vty_ipv6_accesslist_name)
+ while (1)
{
- /* VTY's ipv6 accesslist apply. */
- struct access_list* acl ;
-
- if ((acl = access_list_lookup (AFI_IP6, vty_ipv6_accesslist_name)) &&
- (access_list_apply (acl, p) == FILTER_DENY))
- ret = -1 ;
- }
-#endif /* HAVE_IPV6 */
+ vio_child child ;
+ pid_t pid ;
+ int report ;
- prefix_free (p);
+ pid = waitpid(-1, &report, WNOHANG) ;
- if (ret != 0)
- {
- uzlog (NULL, LOG_INFO, "Vty connection refused from %s", sutoa(&su).str) ;
- close (sock_fd);
- return 0;
- } ;
-
- /* Final options (optional) */
- on = 1 ;
- ret = setsockopt (sock_fd, IPPROTO_TCP, TCP_NODELAY,
- (void*)&on, sizeof (on));
- if (ret < 0)
- uzlog (NULL, LOG_INFO, "can't set sockopt to socket %d: %s",
- sock_fd, errtoa(errno, 0).str) ;
+ if (pid == 0)
+ break ;
- /* All set -- create the VTY_TERM */
- uty_new_term(sock_fd, &su);
+ if (pid < 0)
+ {
+ if (errno == EINTR)
+ continue ; /* loop on "Interrupted" */
- /* Log new VTY */
- uzlog (NULL, LOG_INFO, "Vty connection from %s (fd %d)", sutoa(&su).str,
- sock_fd) ;
+ if (errno != ECHILD) /* returns ECHLD if no children */
+ zlog_err("waitpid(-1) returned %s", errtoa(errno, 0).str) ;
- return 0;
-}
+ break ;
+ } ;
-/*------------------------------------------------------------------------------
- * Accept action -- create and dispatch VTY_SHELL_SERV
- */
-static int
-uty_accept_shell_serv (vty_listener listener)
-{
- int sock_fd ;
- int ret ;
- int client_len ;
- struct sockaddr_un client ;
+ child = vio_childer_list ;
+ while (1)
+ {
+ if (child == NULL)
+ {
+ zlog_err("waitpid(-1) returned pid %d, which is not registered",
+ pid) ;
+ break ;
+ } ;
- VTY_ASSERT_LOCKED() ;
+ if (child->pid == pid)
+ {
+ /* Have collected child.
+ *
+ * Remove from the vio_childer_list, set collected flag.
+ *
+ * We can leave any timer object -- if it goes off it will be
+ * ignored, because the child is no longer awaited. Timer will
+ * be discarded when the child is dismissed/freed.
+ *
+ * If no parent, can free child object now.
+ */
+ uty_child_collected(child, report) ;
- client_len = sizeof(client);
- memset (&client, 0, client_len);
+ if (child->parent == NULL)
+ uty_child_free(child) ;
+ else if (child->awaited)
+ uty_child_signal_parent(child) ;
- sock_fd = accept(listener->sock.fd, (struct sockaddr *) &client,
- (socklen_t *) &client_len) ;
+ break ;
+ } ;
- if (sock_fd < 0)
- {
- uzlog (NULL, LOG_WARNING, "can't accept vty shell socket : %s",
- errtoa(errno, 0).str) ;
- return -1;
- }
-
- /* Really MUST have non-blocking */
- ret = set_nonblocking(sock_fd) ; /* issues WARNING if fails */
- if (ret < 0)
- {
- close(sock_fd) ;
- return -1 ;
+ child = sdl_next(child, list) ;
+ } ;
} ;
-
- /* All set -- create the VTY_SHELL_SERV */
- if (VTYSH_DEBUG)
- printf ("VTY shell accept\n");
-
- uty_new_shell_serv(sock_fd) ;
-
- /* Log new VTY */
- uzlog (NULL, LOG_INFO, "Vty shell connection (fd %d)", sock_fd);
- return 0;
-}
-
-/*==============================================================================
- * Reading from the VTY_SHELL_SERV type sock.
- *
- * The select/pselect call-back ends up in utysh_read_ready().
- */
+} ;
/*------------------------------------------------------------------------------
- * Ready to read -> kicking the "SHELL_SERV CLI"
+ * Set the child collected and set the report.
*
- * End up here when there is something ready to be read.
- *
- * Will also end up here if an error has occurred, the other end has closed,
- * this end has half closed, etc. This fact is used to kick the CLI even when
- * there is no data to be read.
- *
- * Note that nothing is actually read here -- reading is done in the CLI itself,
- * if required.
- *
- * The CLI decides whether to re-enable read, or enable write, or both.
+ * Remove from the vio_childer_list -- is now either back in the hands of the
+ * parent, or ready to be freed.
*/
static void
-utysh_read_ready(vty_io vio)
+uty_child_collected(vio_child child, int report)
{
- uty_sock_set_read(&vio->sock, off) ;
+ assert(!child->collected) ; /* can only collect once */
- /* TODO: need minimal "CLI" for VTY_SHELL_SERV
- * NB: when output from command is flushed out, must append the
- * following four bytes: '\0' '\0' '\0' <ret>
- * Where <ret> is the command return code.
- */
+ sdl_del(vio_childer_list, child, list) ;
+
+ child->collected = true ;
+ child->report = report ;
} ;
/*------------------------------------------------------------------------------
- * Callback -- qnexus: ready to read -> kicking the "SHELL_SERV CLI"
+ * Set child as overdue -- vio_timer action routine.
+ *
+ * NB: the timer may go off after the child has been collected, but before the
+ * parent has got round to stopping the timer.
*/
-static void
-vtysh_read_qnexus(qps_file qf, void* file_info)
+static vty_timer_time
+vty_child_overdue(vio_timer timer, void* action_info)
{
- vty_io vio = file_info;
+ vio_child child ;
VTY_LOCK() ;
- assert((vio->sock.fd == qf->fd) && (vio == vio->sock.info)) ;
+ child = action_info ;
+ assert(timer == child->timer) ;
- utysh_read_ready(vio) ;
+ if (child->awaited)
+ {
+ child->overdue = true ;
+ uty_child_signal_parent(child) ;
+ } ;
VTY_UNLOCK() ;
-}
+
+ return 0 ; /* stop timer */
+} ;
/*------------------------------------------------------------------------------
- * Callback -- threads: ready to read -> kicking the "SHELL_SERV CLI"
+ * Signal that child is ready -- collected or overdue.
+ *
+ * Must be "awaited" -- so not "blocking"
*/
-static int
-vtysh_read_thread(struct thread *thread)
+static void
+uty_child_signal_parent(vio_child child)
{
- vty_io vio = THREAD_ARG (thread);
-
- VTY_LOCK() ;
-
- assert(vio->sock.fd == THREAD_FD (thread) && (vio == vio->sock.info)) ;
+ assert(child->awaited && (child->parent != NULL)) ;
- vio->sock.t_read = NULL ; /* implicitly */
- utysh_read_ready(vio);
+ assert(!child->parent->vio->blocking) ;
- VTY_UNLOCK() ;
- return 0 ;
-}
+ child->awaited = false ;
+ uty_cmd_signal(child->parent->vio, CMD_SUCCESS) ;
+} ;
/*------------------------------------------------------------------------------
- * Read a lump of bytes and shovel into the command line buffer
- *
- * Lines coming in are terminated by '\0'.
- *
- * Assumes that the incoming command line is empty or otherwise incomplete.
- *
- * Moves stuff from the "buf" qstring and appends to "cl" qstring, stopping
- * when get '\0' or empties the "buf".
- *
- * When empties "buf", reads a lump from the sock.
- *
- * Returns: 0 => command line is incomplete
- * 1 => have a complete command line
- * -1 => EOF (or not open, or failed)
+ * Free the child -- caller must ensure that any parent has disowned the child,
+ * and that it is collected (so not on the vio_childer_list).
*/
-extern int
-utysh_read (vty_io vio, qstring cl, qstring buf)
+static vio_child
+uty_child_free(vio_child child)
{
- int get ;
- char* cp ;
- char* ep ;
- size_t have ;
-
- while (1)
+ if (child != NULL)
{
- /* process what there is in the buffer */
- if (buf->len > buf->cp)
- {
- cp = qs_cp_char(buf) ;
- ep = qs_ep_char(buf) ;
- have = ep - cp ;
-
- ep = memchr(cp, '\0', have) ;
- if (ep != NULL)
- have = ep - cp ; /* have upto, but excluding '\0' */
+ assert(child->collected && (child->parent == NULL)) ;
- if (have > 0) /* take what have */
- {
- qs_insert(cl, cp, have) ;
- cl->cp += have ;
- buf->cp += have ;
- } ;
-
- if (ep != NULL) /* if found '\0' */
- {
- qs_term(cl) ; /* '\0' terminate */
- ++buf->cp ; /* step past it */
- return 1 ; /* have a complete line <<<<<<<<<<<<< */
- }
- } ;
-
- /* buffer is empty -- try and get some more stuff */
- assert(buf->len == buf->cp) ;
-
- if (!vio->sock.read_open)
- return -1 ; /* at EOF if not open <<<<<<<<<<<<< */
-
- qs_need(buf, 500) ; /* need a reasonable lump */
- qs_clear(buf) ; /* set cp = len = 0 */
-
- get = read_nb(vio->sock.fd, buf->body, buf->size) ;
- if (get > 0)
- buf->len = get ;
- else if (get == 0)
- return 0 ; /* have an incomplete line <<<<<<<<<<<< */
- else
- {
- if (get == -1)
- uty_sock_error(vio, "read") ;
-
- vio->sock.read_open = 0 ;
-
- return -1 ; /* at EOF or failed <<<<<<<<<<<<< */
- } ;
+ child->timer = vio_timer_reset(child->timer, free_it) ;
+ XFREE(MTYPE_VTY, child) ; /* sets child = NULL */
} ;
+
+ return child ;
} ;
/*==============================================================================
- * Output to vty which are set to "monitor".
- *
- * This is VERY TRICKY.
- *
- * If not running qpthreaded, then the objective is to get the message away
- * immediately -- do not wish it to be delayed in any way by the thread
- * system.
- *
- * So proceed as follows:
- *
- * a. wipe command line -- which adds output to the CLI buffer
- *
- * b. write the CLI buffer to the sock and any outstanding line control.
- *
- * c. write the monitor output.
- *
- * If that does not complete, put the tail end to the CLI buffer.
- *
- * d. restore any command line -- which adds output to the CLI buffer
- *
- * e. write the CLI buffer to the sock
- *
- * If that all succeeds, nothing has changed as far as the VTY stuff is
- * concerned -- except that possibly some CLI output was sent before it got
- * round to it.
- *
- * Note that step (b) will deal with any output hanging around from an
- * earlier step (e). If cannot complete that, then does not add fuel to the
- * fire -- but the message will be discarded.
- *
- * If that fails, or does not complete, then can set write on, to signal that
- * there is some output in the CLI buffer that needs to be sent, or some
- * error to be dealt with.
- *
- * The output should be tidy.
- *
- * To cut down the clutter, step (d) is performed only if the command line
- * is not empty (or if in cli_more_wait). Once a the user has started to enter
- * a command, the prompt and the command will remain visible.
- *
- * When logging an I/O error for a vty that happens to be a monitor, the
- * monitor-ness has already been turned off. The monitor output code does not
- * attempt to log any errors, sets write on so that the error will be picked
- * up that way.
- *
- * However, in the event of an assertion failure, it is possible that an
- * assertion will fail inside the monitor output. The monitor_busy flag
- * prevents disaster. It is also left set if I/O fails in monitor output, so
- * will not try to use the monitor again.
- *
- * Note that an assertion which is false for all vty monitors will recurse
- * through all the monitors, setting each one busy, in turn !
- *
-
-
- * TODO: sort out write on in the qpthreads world ??
+ * VTY Listener(s)
*
- * The problem is that the qpselect structure is designed to be accessed ONLY
- * within the thread to which it belongs. This makes it impossible for the
- * monitor output to set/clear read/write on the vty sock... so some way
- * around this is required.
+ * Have listeners for VTY_TERMINAL and VTY_SHELL_SERVER types of VTY.
*/
+/* List of listeners so can tidy up. */
+static vio_listener vty_listeners_list = NULL ;
+
/*------------------------------------------------------------------------------
- * Output logging information to all vty which are set to "monitor".
+ * Open VTY listener(s) for VTY_TERMINAL and VTY_SHELL_SERVER.
+ *
+ * addr -- address ) to listen for VTY_TERMINAL connections
+ * port -- port )
+ * path -- path for VTY_SHELL_SERVER connections -- if VTYSH_ENABLED
*/
extern void
-uty_log(struct logline* ll, struct zlog *zl, int priority,
- const char *format, va_list va)
+uty_open_listeners(const char *addr, unsigned short port, const char *path)
{
- vty_io vio ;
-
VTY_ASSERT_LOCKED() ;
- vio = sdl_head(vio_monitors_base) ;
-
- if (vio == NULL)
- return ; /* go no further if no "monitor" vtys */
-
- /* Prepare line for output. */
- uvzlog_line(ll, zl, priority, format, va, llt_crlf) ; /* with crlf */
-
- /* write to all known "monitor" vty
- *
- */
- while (vio != NULL)
- {
- if (!vio->monitor_busy)
- {
- int ret ;
-
- vio->monitor_busy = 1 ; /* close the door */
+ /* If port is set to 0, do not listen for VTY_TERMINAL at all! */
+ if (port)
+ uty_term_open_listeners(addr, port) ;
- uty_cli_pre_monitor(vio, ll->len - 2) ; /* claim the console */
+#if 0
+ /* If want to listen for vtysh, set up listener now */
+ if (VTYSH_ENABLED && (path != NULL))
+ uty_serv_vtysh(path) ;
+#endif
+} ;
- ret = uty_write_monitor(vio) ;
- if (ret == 0)
- {
- ret = write_nb(vio->sock.fd, ll->line, ll->len) ;
-
- if (ret >= 0)
- {
- ret = uty_cli_post_monitor(vio, ll->line + ret,
- ll->len - ret) ;
- if (ret > 0)
- ret = uty_write_monitor(vio) ;
- } ;
- } ;
+/*------------------------------------------------------------------------------
+ * Create listener and set it ready to accept.
+ *
+ * Adds to list of listeners for close down.
+ */
+extern void
+uty_add_listener(int fd, vio_vfd_accept* accept_action)
+{
+ vio_listener vl ;
- if (ret != 0)
- /* need to prod */ ;
+ VTY_ASSERT_LOCKED() ;
- if (ret >= 0)
- vio->monitor_busy = 0 ;
- } ;
+ vl = vio_listener_new(fd, accept_action) ;
- vio = sdl_next(vio, mon_list) ;
- } ;
+ ssl_push(vty_listeners_list, vl, next) ;
} ;
/*------------------------------------------------------------------------------
- * Async-signal-safe version of vty_log for fixed strings.
- *
- * This is last gasp operation.
+ * Close all VTY listeners
*/
-void
-vty_log_fixed (const char *buf, size_t len)
+extern void
+uty_close_listeners(void)
{
- vty_io vio ;
+ vio_listener listener ;
- /* Write to all known "monitor" vty
- *
- * Forget all the niceties -- about to die in any case.
- */
- vio = sdl_head(vio_monitors_base) ;
- while (vio != NULL)
- {
- write(vio->sock.fd, buf, len) ;
- write(vio->sock.fd, "\r\n", 2) ;
+ VTY_ASSERT_LOCKED() ;
- vio = sdl_next(vio, mon_list) ;
+ while ((listener = ssl_pop(&listener, vty_listeners_list, next)) != NULL)
+ {
+ vio_listener_close(listener) ; /* no ceremony, no flowers */
} ;
} ;