Statistics
| Branch: | Revision:

iof-bird-daemon / nest / cli.c @ e81b440f

History | View | Annotate | Download (9.84 KB)

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

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

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

    
71
pool *cli_pool;
72

    
73
static byte *
74
cli_alloc_out(cli *c, int size)
75
{
76
  struct cli_out *o;
77

    
78
  if (!(o = c->tx_write) || o->wpos + size > o->end)
79
    {
80
      if (!o && c->tx_buf)
81
        o = c->tx_buf;
82
      else
83
        {
84
          o = mb_alloc(c->pool, sizeof(struct cli_out) + CLI_TX_BUF_SIZE);
85
          if (c->tx_write)
86
            c->tx_write->next = o;
87
          else
88
            c->tx_buf = o;
89
          o->wpos = o->outpos = o->buf;
90
          o->end = o->buf + CLI_TX_BUF_SIZE;
91
        }
92
      c->tx_write = o;
93
      if (!c->tx_pos)
94
        c->tx_pos = o;
95
      o->next = NULL;
96
    }
97
  o->wpos += size;
98
  return o->wpos - size;
99
}
100

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

    
127
  if (cd < 0)
128
    {
129
      cd = -cd;
130
      if (cd == c->last_reply)
131
        size = bsprintf(buf, " ");
132
      else
133
        size = bsprintf(buf, "%04d-", cd);
134
    }
135
  else
136
    size = bsprintf(buf, "%04d ", cd);
137
  c->last_reply = cd;
138
  va_start(args, msg);
139
  cnt = bvsnprintf(buf+size, sizeof(buf)-size-1, msg, args);
140
  va_end(args);
141
  if (cnt < 0)
142
    {
143
      cli_printf(c, code < 0 ? -8000 : 8000, "<line overflow>");
144
      return;
145
    }
146
  size += cnt;
147
  buf[size++] = '\n';
148
  memcpy(cli_alloc_out(c, size), buf, size);
149
}
150

    
151
static void
152
cli_copy_message(cli *c)
153
{
154
  byte *p, *q;
155
  unsigned int cnt = 2;
156

    
157
  if (c->ring_overflow)
158
    {
159
      byte buf[64];
160
      int n = bsprintf(buf, "<%d messages lost>\n", c->ring_overflow);
161
      c->ring_overflow = 0;
162
      memcpy(cli_alloc_out(c, n), buf, n);
163
    }
164
  p = c->ring_read;
165
  while (*p)
166
    {
167
      cnt++;
168
      p++;
169
      if (p == c->ring_end)
170
        p = c->ring_buf;
171
      ASSERT(p != c->ring_write);
172
    }
173
  c->async_msg_size += cnt;
174
  q = cli_alloc_out(c, cnt);
175
  *q++ = '+';
176
  p = c->ring_read;
177
  do
178
    {
179
      *q = *p++;
180
      if (p == c->ring_end)
181
        p = c->ring_buf;
182
    }
183
  while (*q++);
184
  c->ring_read = p;
185
  q[-1] = '\n';
186
}
187

    
188
static void
189
cli_hello(cli *c)
190
{
191
  cli_printf(c, 1, "BIRD " BIRD_VERSION " ready.");
192
  c->cont = NULL;
193
}
194

    
195
static void
196
cli_free_out(cli *c)
197
{
198
  struct cli_out *o, *p;
199

    
200
  if (o = c->tx_buf)
201
    {
202
      o->wpos = o->outpos = o->buf;
203
      while (p = o->next)
204
        {
205
          o->next = p->next;
206
          mb_free(p);
207
        }
208
    }
209
  c->tx_write = c->tx_pos = NULL;
210
  c->async_msg_size = 0;
211
}
212

    
213
void
214
cli_written(cli *c)
215
{
216
  cli_free_out(c);
217
  ev_schedule(c->event);
218
}
219

    
220

    
221
static byte *cli_rh_pos;
222
static unsigned int cli_rh_len;
223
static int cli_rh_trick_flag;
224
struct cli *this_cli;
225

    
226
static int
227
cli_cmd_read_hook(byte *buf, unsigned int max)
228
{
229
  if (!cli_rh_trick_flag)
230
    {
231
      cli_rh_trick_flag = 1;
232
      buf[0] = '!';
233
      return 1;
234
    }
235
  if (max > cli_rh_len)
236
    max = cli_rh_len;
237
  memcpy(buf, cli_rh_pos, max);
238
  cli_rh_pos += max;
239
  cli_rh_len -= max;
240
  return max;
241
}
242

    
243
static void
244
cli_command(struct cli *c)
245
{
246
  struct config f;
247
  int res;
248

    
249
  if (config->cli_debug > 1)
250
    log(L_TRACE "CLI: %s", c->rx_buf);
251
  bzero(&f, sizeof(f));
252
  f.mem = c->parser_pool;
253
  cf_read_hook = cli_cmd_read_hook;
254
  cli_rh_pos = c->rx_buf;
255
  cli_rh_len = strlen(c->rx_buf);
256
  cli_rh_trick_flag = 0;
257
  this_cli = c;
258
  lp_flush(c->parser_pool);
259
  res = cli_parse(&f);
260
  if (!res)
261
    cli_printf(c, 9001, f.err_msg);
262
}
263

    
264
static void
265
cli_event(void *data)
266
{
267
  cli *c = data;
268
  int err;
269

    
270
  while (c->ring_read != c->ring_write &&
271
      c->async_msg_size < CLI_MAX_ASYNC_QUEUE)
272
    cli_copy_message(c);
273

    
274
  if (c->tx_pos)
275
    ;
276
  else if (c->cont)
277
    c->cont(c);
278
  else
279
    {
280
      err = cli_get_command(c);
281
      if (!err)
282
        return;
283
      if (err < 0)
284
        cli_printf(c, 9000, "Command too long");
285
      else
286
        cli_command(c);
287
    }
288

    
289
  cli_write_trigger(c);
290
}
291

    
292
cli *
293
cli_new(void *priv)
294
{
295
  pool *p = rp_new(cli_pool, "CLI");
296
  cli *c = mb_alloc(p, sizeof(cli));
297

    
298
  bzero(c, sizeof(cli));
299
  c->pool = p;
300
  c->priv = priv;
301
  c->event = ev_new(p);
302
  c->event->hook = cli_event;
303
  c->event->data = c;
304
  c->cont = cli_hello;
305
  c->parser_pool = lp_new(c->pool, 4096);
306
  c->rx_buf = mb_alloc(c->pool, CLI_RX_BUF_SIZE);
307
  ev_schedule(c->event);
308
  return c;
309
}
310

    
311
void
312
cli_kick(cli *c)
313
{
314
  if (!c->cont && !c->tx_pos)
315
    ev_schedule(c->event);
316
}
317

    
318
static list cli_log_hooks;
319
static int cli_log_inited;
320

    
321
void
322
cli_set_log_echo(cli *c, unsigned int mask, unsigned int size)
323
{
324
  if (c->ring_buf)
325
    {
326
      mb_free(c->ring_buf);
327
      c->ring_buf = c->ring_end = c->ring_read = c->ring_write = NULL;
328
      rem_node(&c->n);
329
    }
330
  c->log_mask = mask;
331
  if (mask && size)
332
    {
333
      c->ring_buf = mb_alloc(c->pool, size);
334
      c->ring_end = c->ring_buf + size;
335
      c->ring_read = c->ring_write = c->ring_buf;
336
      add_tail(&cli_log_hooks, &c->n);
337
      c->log_threshold = size / 8;
338
    }
339
  c->ring_overflow = 0;
340
}
341

    
342
void
343
cli_echo(unsigned int class, byte *msg)
344
{
345
  unsigned len, free, i, l;
346
  cli *c;
347
  byte *m;
348

    
349
  if (!cli_log_inited || EMPTY_LIST(cli_log_hooks))
350
    return;
351
  len = strlen(msg) + 1;
352
  WALK_LIST(c, cli_log_hooks)
353
    {
354
      if (!(c->log_mask & (1 << class)))
355
        continue;
356
      if (c->ring_read <= c->ring_write)
357
        free = (c->ring_end - c->ring_buf) - (c->ring_write - c->ring_read + 1);
358
      else
359
        free = c->ring_read - c->ring_write - 1;
360
      if ((len > free) ||
361
          (free < c->log_threshold && class < (unsigned) L_INFO[0]))
362
        {
363
          c->ring_overflow++;
364
          continue;
365
        }
366
      if (c->ring_read == c->ring_write)
367
        ev_schedule(c->event);
368
      m = msg;
369
      l = len;
370
      while (l)
371
        {
372
          if (c->ring_read <= c->ring_write)
373
            i = c->ring_end - c->ring_write;
374
          else
375
            i = c->ring_read - c->ring_write;
376
          if (i > l)
377
            i = l;
378
          memcpy(c->ring_write, m, i);
379
          m += i;
380
          l -= i;
381
          c->ring_write += i;
382
          if (c->ring_write == c->ring_end)
383
            c->ring_write = c->ring_buf;
384
        }
385
    }
386
}
387

    
388
void
389
cli_free(cli *c)
390
{
391
  cli_set_log_echo(c, 0, 0);
392
  if (c->cleanup)
393
    c->cleanup(c);
394
  rfree(c->pool);
395
}
396

    
397
/**
398
 * cli_init - initialize the CLI module
399
 *
400
 * This function is called during BIRD startup to initialize
401
 * the internal data structures of the CLI module.
402
 */
403
void
404
cli_init(void)
405
{
406
  cli_pool = rp_new(&root_pool, "CLI");
407
  init_list(&cli_log_hooks);
408
  cli_log_inited = 1;
409
}