cli.c 10.1 KB
Newer Older
1 2 3
/*
 *	BIRD Internet Routing Daemon -- Command-Line Interface
 *
Martin Mareš's avatar
Martin Mareš committed
4
 *	(c) 1999--2000 Martin Mares <mj@ucw.cz>
5 6 7 8
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

Martin Mareš's avatar
Martin Mareš committed
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
/**
 * DOC: Command line interface
 *
 * This module takes care of the BIRD's command-line interface (CLI).
 * The CLI exists to provide a way to control BIRD remotely and to inspect
 * its status. It uses a very simple textual protocol over a stream
 * connection provided by the platform dependent code (on UNIX systems,
 * it's a UNIX domain socket).
 *
 * Each session of the CLI consists of a sequence of request and replies,
 * slightly resembling the FTP and SMTP protocols.
 * Requests are commands encoded as a single line of text, replies are
 * sequences of lines starting with a four-digit code followed by either
 * a space (if it's the last line of the reply) or a minus sign (when the
 * reply is going to continue with the next line), the rest of the line
 * contains a textual message semantics of which depends on the numeric
 * code. If a reply line has the same code as the previous one and it's
 * a continuation line, the whole prefix can be replaced by a single
 * white space character.
 *
Martin Mareš's avatar
Martin Mareš committed
29
 * Reply codes starting with 0 stand for `action successfully completed' messages,
Martin Mareš's avatar
Martin Mareš committed
30 31 32 33
 * 1 means `table entry', 8 `runtime error' and 9 `syntax error'.
 *
 * Each CLI session is internally represented by a &cli structure and a
 * resource pool containing all resources associated with the connection,
Martin Mareš's avatar
Martin Mareš committed
34
 * so that it can be easily freed whenever the connection gets closed, not depending
Martin Mareš's avatar
Martin Mareš committed
35 36 37
 * on the current state of command processing.
 *
 * The CLI commands are declared as a part of the configuration grammar
38
 * by using the |CF_CLI| macro. When a command is received, it is processed
39
 * by the same lexical analyzer and parser as used for the configuration, but
Martin Mareš's avatar
Martin Mareš committed
40 41 42
 * it's switched to a special mode by prepending a fake token to the text,
 * so that it uses only the CLI command rules. Then the parser invokes
 * an execution routine corresponding to the command, which either constructs
43
 * the whole reply and returns it back or (in case it expects the reply will be long)
Martin Mareš's avatar
Martin Mareš committed
44
 * it prints a partial reply and asks the CLI module (using the @cont hook)
Martin Mareš's avatar
Martin Mareš committed
45
 * to call it again when the output is transferred to the user.
Martin Mareš's avatar
Martin Mareš committed
46 47 48 49
 *
 * The @this_cli variable points to a &cli structure of the session being
 * currently parsed, but it's of course available only in command handlers
 * not entered using the @cont hook.
50 51 52 53 54 55 56 57 58 59 60 61 62 63
 *
 * TX buffer management works as follows: At cli.tx_buf there is a
 * list of TX buffers (struct cli_out), cli.tx_write is the buffer
 * currently used by the producer (cli_printf(), cli_alloc_out()) and
 * cli.tx_pos is the buffer currently used by the consumer
 * (cli_write(), in system dependent code). The producer uses
 * cli_out.wpos ptr as the current write position and the consumer
 * uses cli_out.outpos ptr as the current read position. When the
 * producer produces something, it calls cli_write_trigger(). If there
 * is not enough space in the current buffer, the producer allocates
 * the new one. When the consumer processes everything in the buffer
 * queue, it calls cli_written(), tha frees all buffers (except the
 * first one) and schedules cli.event .
 * 
Martin Mareš's avatar
Martin Mareš committed
64 65
 */

66 67
#include "nest/bird.h"
#include "nest/cli.h"
68 69
#include "conf/conf.h"
#include "lib/string.h"
70 71 72

pool *cli_pool;

73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
static byte *
cli_alloc_out(cli *c, int size)
{
  struct cli_out *o;

  if (!(o = c->tx_write) || o->wpos + size > o->end)
    {
      if (!o && c->tx_buf)
	o = c->tx_buf;
      else
	{
	  o = mb_alloc(c->pool, sizeof(struct cli_out) + CLI_TX_BUF_SIZE);
	  if (c->tx_write)
	    c->tx_write->next = o;
	  else
	    c->tx_buf = o;
	  o->wpos = o->outpos = o->buf;
	  o->end = o->buf + CLI_TX_BUF_SIZE;
	}
      c->tx_write = o;
      if (!c->tx_pos)
	c->tx_pos = o;
95
      o->next = NULL;
96 97 98 99 100
    }
  o->wpos += size;
  return o->wpos - size;
}

Martin Mareš's avatar
Martin Mareš committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118
/**
 * cli_printf - send reply to a CLI connection
 * @c: CLI connection
 * @code: numeric code of the reply, negative for continuation lines
 * @msg: a printf()-like formatting string.
 *
 * This function send a single line of reply to a given CLI connection.
 * In works in all aspects like bsprintf() except that it automatically
 * prepends the reply line prefix.
 *
 * Please note that if the connection can be already busy sending some
 * data in which case cli_printf() stores the output to a temporary buffer,
 * so please avoid sending a large batch of replies without waiting
 * for the buffers to be flushed.
 *
 * If you want to write to the current CLI output, you can use the cli_msg()
 * macro instead.
 */
119 120 121 122
void
cli_printf(cli *c, int code, char *msg, ...)
{
  va_list args;
123
  byte buf[CLI_LINE_SIZE];
124
  int cd = code;
125
  int errcode;
126
  int size, cnt;
127

128 129 130 131 132 133 134
  if (cd < 0)
    {
      cd = -cd;
      if (cd == c->last_reply)
	size = bsprintf(buf, " ");
      else
	size = bsprintf(buf, "%04d-", cd);
135 136 137 138 139 140
      errcode = -8000;
    }
  else if (cd == CLI_ASYNC_CODE)
    {
      size = 1; buf[0] = '+'; 
      errcode = cd;
141
    }
142
  else
143 144 145 146 147
    {
      size = bsprintf(buf, "%04d ", cd);
      errcode = 8000;
    }

148
  c->last_reply = cd;
149
  va_start(args, msg);
150
  cnt = bvsnprintf(buf+size, sizeof(buf)-size-1, msg, args);
151
  va_end(args);
152 153
  if (cnt < 0)
    {
154
      cli_printf(c, errcode, "<line overflow>");
155 156 157
      return;
    }
  size += cnt;
158
  buf[size++] = '\n';
159 160 161 162 163 164 165
  memcpy(cli_alloc_out(c, size), buf, size);
}

static void
cli_copy_message(cli *c)
{
  byte *p, *q;
Pavel Tvrdík's avatar
Pavel Tvrdík committed
166
  uint cnt = 2;
167 168

  if (c->ring_overflow)
169
    {
170 171 172 173
      byte buf[64];
      int n = bsprintf(buf, "<%d messages lost>\n", c->ring_overflow);
      c->ring_overflow = 0;
      memcpy(cli_alloc_out(c, n), buf, n);
174
    }
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
  p = c->ring_read;
  while (*p)
    {
      cnt++;
      p++;
      if (p == c->ring_end)
	p = c->ring_buf;
      ASSERT(p != c->ring_write);
    }
  c->async_msg_size += cnt;
  q = cli_alloc_out(c, cnt);
  *q++ = '+';
  p = c->ring_read;
  do
    {
      *q = *p++;
      if (p == c->ring_end)
	p = c->ring_buf;
    }
  while (*q++);
  c->ring_read = p;
  q[-1] = '\n';
197 198
}

199 200 201 202 203 204 205
static void
cli_hello(cli *c)
{
  cli_printf(c, 1, "BIRD " BIRD_VERSION " ready.");
  c->cont = NULL;
}

206 207 208 209 210 211 212 213 214 215 216 217 218 219
static void
cli_free_out(cli *c)
{
  struct cli_out *o, *p;

  if (o = c->tx_buf)
    {
      o->wpos = o->outpos = o->buf;
      while (p = o->next)
	{
	  o->next = p->next;
	  mb_free(p);
	}
    }
220
  c->tx_write = c->tx_pos = NULL;
221
  c->async_msg_size = 0;
222 223
}

224 225 226 227 228 229 230 231
void
cli_written(cli *c)
{
  cli_free_out(c);
  ev_schedule(c->event);
}


232
static byte *cli_rh_pos;
Pavel Tvrdík's avatar
Pavel Tvrdík committed
233
static uint cli_rh_len;
234 235 236 237
static int cli_rh_trick_flag;
struct cli *this_cli;

static int
Pavel Tvrdík's avatar
Pavel Tvrdík committed
238
cli_cmd_read_hook(byte *buf, uint max, UNUSED int fd)
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
{
  if (!cli_rh_trick_flag)
    {
      cli_rh_trick_flag = 1;
      buf[0] = '!';
      return 1;
    }
  if (max > cli_rh_len)
    max = cli_rh_len;
  memcpy(buf, cli_rh_pos, max);
  cli_rh_pos += max;
  cli_rh_len -= max;
  return max;
}

static void
cli_command(struct cli *c)
{
  struct config f;
  int res;

260 261
  if (config->cli_debug > 1)
    log(L_TRACE "CLI: %s", c->rx_buf);
262
  bzero(&f, sizeof(f));
263 264 265 266 267 268 269
  f.mem = c->parser_pool;
  cf_read_hook = cli_cmd_read_hook;
  cli_rh_pos = c->rx_buf;
  cli_rh_len = strlen(c->rx_buf);
  cli_rh_trick_flag = 0;
  this_cli = c;
  lp_flush(c->parser_pool);
270
  res = cli_parse(&f);
271 272 273 274
  if (!res)
    cli_printf(c, 9001, f.err_msg);
}

275
static void
276 277 278 279 280
cli_event(void *data)
{
  cli *c = data;
  int err;

281 282 283 284
  while (c->ring_read != c->ring_write &&
      c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
    cli_copy_message(c);

285 286 287 288 289
  if (c->tx_pos)
    ;
  else if (c->cont)
    c->cont(c);
  else
290
    {
291 292
      err = cli_get_command(c);
      if (!err)
293
	return;
294 295 296
      if (err < 0)
	cli_printf(c, 9000, "Command too long");
      else
297
	cli_command(c);
298
    }
299 300

  cli_write_trigger(c);
301 302 303 304 305 306 307 308
}

cli *
cli_new(void *priv)
{
  pool *p = rp_new(cli_pool, "CLI");
  cli *c = mb_alloc(p, sizeof(cli));

309
  bzero(c, sizeof(cli));
310 311 312 313 314
  c->pool = p;
  c->priv = priv;
  c->event = ev_new(p);
  c->event->hook = cli_event;
  c->event->data = c;
315
  c->cont = cli_hello;
316
  c->parser_pool = lp_new(c->pool, 4096);
317
  c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE);
318
  ev_schedule(c->event);
319 320 321 322 323 324
  return c;
}

void
cli_kick(cli *c)
{
325 326
  if (!c->cont && !c->tx_pos)
    ev_schedule(c->event);
327 328
}

329 330 331 332
static list cli_log_hooks;
static int cli_log_inited;

void
Pavel Tvrdík's avatar
Pavel Tvrdík committed
333
cli_set_log_echo(cli *c, uint mask, uint size)
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353
{
  if (c->ring_buf)
    {
      mb_free(c->ring_buf);
      c->ring_buf = c->ring_end = c->ring_read = c->ring_write = NULL;
      rem_node(&c->n);
    }
  c->log_mask = mask;
  if (mask && size)
    {
      c->ring_buf = mb_alloc(c->pool, size);
      c->ring_end = c->ring_buf + size;
      c->ring_read = c->ring_write = c->ring_buf;
      add_tail(&cli_log_hooks, &c->n);
      c->log_threshold = size / 8;
    }
  c->ring_overflow = 0;
}

void
Pavel Tvrdík's avatar
Pavel Tvrdík committed
354
cli_echo(uint class, byte *msg)
355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
{
  unsigned len, free, i, l;
  cli *c;
  byte *m;

  if (!cli_log_inited || EMPTY_LIST(cli_log_hooks))
    return;
  len = strlen(msg) + 1;
  WALK_LIST(c, cli_log_hooks)
    {
      if (!(c->log_mask & (1 << class)))
	continue;
      if (c->ring_read <= c->ring_write)
	free = (c->ring_end - c->ring_buf) - (c->ring_write - c->ring_read + 1);
      else
	free = c->ring_read - c->ring_write - 1;
371 372
      if ((len > free) ||
	  (free < c->log_threshold && class < (unsigned) L_INFO[0]))
373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398
	{
	  c->ring_overflow++;
	  continue;
	}
      if (c->ring_read == c->ring_write)
	ev_schedule(c->event);
      m = msg;
      l = len;
      while (l)
	{
	  if (c->ring_read <= c->ring_write)
	    i = c->ring_end - c->ring_write;
	  else
	    i = c->ring_read - c->ring_write;
	  if (i > l)
	    i = l;
	  memcpy(c->ring_write, m, i);
	  m += i;
	  l -= i;
	  c->ring_write += i;
	  if (c->ring_write == c->ring_end)
	    c->ring_write = c->ring_buf;
	}
    }
}

399 400 401
/* Hack for scheduled undo notification */
extern cli *cmd_reconfig_stored_cli;

402 403 404
void
cli_free(cli *c)
{
405
  cli_set_log_echo(c, 0, 0);
406 407
  if (c->cleanup)
    c->cleanup(c);
408 409
  if (c == cmd_reconfig_stored_cli)
    cmd_reconfig_stored_cli = NULL;
410 411 412
  rfree(c->pool);
}

Martin Mareš's avatar
Martin Mareš committed
413 414 415 416 417 418
/**
 * cli_init - initialize the CLI module
 *
 * This function is called during BIRD startup to initialize
 * the internal data structures of the CLI module.
 */
419 420 421 422
void
cli_init(void)
{
  cli_pool = rp_new(&root_pool, "CLI");
423 424
  init_list(&cli_log_hooks);
  cli_log_inited = 1;
425
}