Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / main.c @ e0a45fb4

History | View | Annotate | Download (8.1 KB)

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

    
9
#undef LOCAL_DEBUG
10

    
11
#include <stdio.h>
12
#include <stdlib.h>
13
#include <fcntl.h>
14
#include <unistd.h>
15
#include <signal.h>
16

    
17
#include "nest/bird.h"
18
#include "lib/lists.h"
19
#include "lib/resource.h"
20
#include "lib/socket.h"
21
#include "lib/event.h"
22
#include "lib/string.h"
23
#include "nest/route.h"
24
#include "nest/protocol.h"
25
#include "nest/iface.h"
26
#include "nest/cli.h"
27
#include "nest/locks.h"
28
#include "conf/conf.h"
29
#include "filter/filter.h"
30

    
31
#include "unix.h"
32
#include "krt.h"
33

    
34
/*
35
 *        Debugging
36
 */
37

    
38
#ifdef DEBUGGING
39
static int debug_flag = 1;
40
#else
41
static int debug_flag = 0;
42
#endif
43

    
44
void
45
async_dump(void)
46
{
47
  debug("INTERNAL STATE DUMP\n\n");
48

    
49
  rdump(&root_pool);
50
  sk_dump_all();
51
  tm_dump_all();
52
  if_dump_all();
53
  neigh_dump_all();
54
  rta_dump_all();
55
  rt_dump_all();
56
  protos_dump_all();
57

    
58
  debug("\n");
59
}
60

    
61
/*
62
 *        Reading the Configuration
63
 */
64

    
65
static int conf_fd;
66
static char *config_name = PATH_CONFIG;
67

    
68
static int
69
cf_read(byte *dest, unsigned int len)
70
{
71
  int l = read(conf_fd, dest, len);
72
  if (l < 0)
73
    cf_error("Read error");
74
  return l;
75
}
76

    
77
void
78
sysdep_preconfig(struct config *c)
79
{
80
  init_list(&c->logfiles);
81
}
82

    
83
int
84
sysdep_commit(struct config *new, struct config *old UNUSED)
85
{
86
  log_switch(debug_flag, &new->logfiles);
87
  return 0;
88
}
89

    
90
static int
91
unix_read_config(struct config **cp, char *name)
92
{
93
  struct config *conf = config_alloc(name);
94
  int ret;
95

    
96
  *cp = conf;
97
  conf_fd = open(name, O_RDONLY);
98
  if (conf_fd < 0)
99
    return 0;
100
  cf_read_hook = cf_read;
101
  ret = config_parse(conf);
102
  close(conf_fd);
103
  return ret;
104
}
105

    
106
static void
107
read_config(void)
108
{
109
  struct config *conf;
110

    
111
  if (!unix_read_config(&conf, config_name))
112
    {
113
      if (conf->err_msg)
114
        die("%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
115
      else
116
        die("Unable to open configuration file %s: %m", config_name);
117
    }
118
  config_commit(conf, RECONFIG_HARD);
119
}
120

    
121
void
122
async_config(void)
123
{
124
  struct config *conf;
125

    
126
  log(L_INFO "Reconfiguration requested by SIGHUP");
127
  if (!unix_read_config(&conf, config_name))
128
    {
129
      if (conf->err_msg)
130
        log(L_ERR "%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
131
      else
132
        log(L_ERR "Unable to open configuration file %s: %m", config_name);
133
      config_free(conf);
134
    }
135
  else
136
    config_commit(conf, RECONFIG_HARD);
137
}
138

    
139
void
140
cmd_reconfig(char *name, int type)
141
{
142
  struct config *conf;
143

    
144
  if (cli_access_restricted())
145
    return;
146

    
147
  if (!name)
148
    name = config_name;
149
  cli_msg(-2, "Reading configuration from %s", name);
150
  if (!unix_read_config(&conf, name))
151
    {
152
      if (conf->err_msg)
153
        cli_msg(8002, "%s, line %d: %s", name, conf->err_lino, conf->err_msg);
154
      else
155
        cli_msg(8002, "%s: %m", name);
156
      config_free(conf);
157
    }
158
  else
159
    {
160
      switch (config_commit(conf, type))
161
        {
162
        case CONF_DONE:
163
          cli_msg(3, "Reconfigured.");
164
          break;
165
        case CONF_PROGRESS:
166
          cli_msg(4, "Reconfiguration in progress.");
167
          break;
168
        case CONF_SHUTDOWN:
169
          cli_msg(6, "Reconfiguration ignored, shutting down.");
170
          break;
171
        default:
172
          cli_msg(5, "Reconfiguration already in progress, queueing new config");
173
        }
174
    }
175
}
176

    
177
/*
178
 *        Command-Line Interface
179
 */
180

    
181
static sock *cli_sk;
182
static char *path_control_socket = PATH_CONTROL_SOCKET;
183

    
184

    
185
static void
186
cli_write(cli *c)
187
{
188
  sock *s = c->priv;
189

    
190
  while (c->tx_pos)
191
    {
192
      struct cli_out *o = c->tx_pos;
193

    
194
      int len = o->wpos - o->outpos;
195
      s->tbuf = o->outpos;
196
      o->outpos = o->wpos;
197

    
198
      if (sk_send(s, len) <= 0)
199
        return;
200

    
201
      c->tx_pos = o->next;
202
    }
203

    
204
  /* Everything is written */
205
  s->tbuf = NULL;
206
  cli_written(c);
207
}
208

    
209
void
210
cli_write_trigger(cli *c)
211
{
212
  sock *s = c->priv;
213

    
214
  if (s->tbuf == NULL)
215
    cli_write(c);
216
}
217

    
218
static void
219
cli_tx(sock *s)
220
{
221
  cli_write(s->data);
222
}
223

    
224
int
225
cli_get_command(cli *c)
226
{
227
  sock *s = c->priv;
228
  byte *t = c->rx_aux ? : s->rbuf;
229
  byte *tend = s->rpos;
230
  byte *d = c->rx_pos;
231
  byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;
232

    
233
  while (t < tend)
234
    {
235
      if (*t == '\r')
236
        t++;
237
      else if (*t == '\n')
238
        {
239
          t++;
240
          c->rx_pos = c->rx_buf;
241
          c->rx_aux = t;
242
          *d = 0;
243
          return (d < dend) ? 1 : -1;
244
        }
245
      else if (d < dend)
246
        *d++ = *t++;
247
    }
248
  c->rx_aux = s->rpos = s->rbuf;
249
  c->rx_pos = d;
250
  return 0;
251
}
252

    
253
static int
254
cli_rx(sock *s, int size UNUSED)
255
{
256
  cli_kick(s->data);
257
  return 0;
258
}
259

    
260
static void
261
cli_err(sock *s, int err)
262
{
263
  if (config->cli_debug)
264
    {
265
      if (err)
266
        log(L_INFO "CLI connection dropped: %s", strerror(err));
267
      else
268
        log(L_INFO "CLI connection closed");
269
    }
270
  cli_free(s->data);
271
}
272

    
273
static int
274
cli_connect(sock *s, int size UNUSED)
275
{
276
  cli *c;
277

    
278
  if (config->cli_debug)
279
    log(L_INFO "CLI connect");
280
  s->rx_hook = cli_rx;
281
  s->tx_hook = cli_tx;
282
  s->err_hook = cli_err;
283
  s->data = c = cli_new(s);
284
  s->pool = c->pool;                /* We need to have all the socket buffers allocated in the cli pool */
285
  c->rx_pos = c->rx_buf;
286
  c->rx_aux = NULL;
287
  rmove(s, c->pool);
288
  return 1;
289
}
290

    
291
static void
292
cli_init_unix(void)
293
{
294
  sock *s;
295

    
296
  cli_init();
297
  s = cli_sk = sk_new(cli_pool);
298
  s->type = SK_UNIX_PASSIVE;
299
  s->rx_hook = cli_connect;
300
  s->rbsize = 1024;
301
  if (sk_open_unix(s, path_control_socket) < 0)
302
    die("Unable to create control socket %s", path_control_socket);
303
}
304

    
305
/*
306
 *        Shutdown
307
 */
308

    
309
void
310
cmd_shutdown(void)
311
{
312
  if (cli_access_restricted())
313
    return;
314

    
315
  cli_msg(7, "Shutdown requested");
316
  order_shutdown();
317
}
318

    
319
void
320
async_shutdown(void)
321
{
322
  DBG("Shutting down...\n");
323
  order_shutdown();
324
}
325

    
326
void
327
sysdep_shutdown_done(void)
328
{
329
  unlink(path_control_socket);
330
  log_msg(L_FATAL "Shutdown completed");
331
  exit(0);
332
}
333

    
334
/*
335
 *        Signals
336
 */
337

    
338
static void
339
handle_sighup(int sig UNUSED)
340
{
341
  DBG("Caught SIGHUP...\n");
342
  async_config_flag = 1;
343
}
344

    
345
static void
346
handle_sigusr(int sig UNUSED)
347
{
348
  DBG("Caught SIGUSR...\n");
349
  async_dump_flag = 1;
350
}
351

    
352
static void
353
handle_sigterm(int sig UNUSED)
354
{
355
  DBG("Caught SIGTERM...\n");
356
  async_shutdown_flag = 1;
357
}
358

    
359
static void
360
signal_init(void)
361
{
362
  struct sigaction sa;
363

    
364
  bzero(&sa, sizeof(sa));
365
  sa.sa_handler = handle_sigusr;
366
  sa.sa_flags = SA_RESTART;
367
  sigaction(SIGUSR1, &sa, NULL);
368
  sa.sa_handler = handle_sighup;
369
  sa.sa_flags = SA_RESTART;
370
  sigaction(SIGHUP, &sa, NULL);
371
  sa.sa_handler = handle_sigterm;
372
  sa.sa_flags = SA_RESTART;
373
  sigaction(SIGTERM, &sa, NULL);
374
  signal(SIGPIPE, SIG_IGN);
375
}
376

    
377
/*
378
 *        Parsing of command-line arguments
379
 */
380

    
381
static char *opt_list = "c:dD:ps:";
382

    
383
static void
384
usage(void)
385
{
386
  fprintf(stderr, "Usage: bird [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>]\n");
387
  exit(1);
388
}
389

    
390
int parse_and_exit;
391

    
392
static void
393
parse_args(int argc, char **argv)
394
{
395
  int c;
396

    
397
  if (argc == 2)
398
    {
399
      if (!strcmp(argv[1], "--version"))
400
        {
401
          fprintf(stderr, "BIRD version " BIRD_VERSION "\n");
402
          exit(0);
403
        }
404
      if (!strcmp(argv[1], "--help"))
405
        usage();
406
    }
407
  while ((c = getopt(argc, argv, opt_list)) >= 0)
408
    switch (c)
409
      {
410
      case 'c':
411
        config_name = optarg;
412
        break;
413
      case 'd':
414
        debug_flag |= 1;
415
        break;
416
      case 'D':
417
        log_init_debug(optarg);
418
        debug_flag |= 2;
419
        break;
420
      case 'p':
421
        parse_and_exit = 1;
422
        break;
423
      case 's':
424
        path_control_socket = optarg;
425
        break;
426
      default:
427
        usage();
428
      }
429
  if (optind < argc)
430
    usage();
431
}
432

    
433
/*
434
 *        Hic Est main()
435
 */
436

    
437
int
438
main(int argc, char **argv)
439
{
440
#ifdef HAVE_LIBDMALLOC
441
  if (!getenv("DMALLOC_OPTIONS"))
442
    dmalloc_debug(0x2f03d00);
443
#endif
444

    
445
  parse_args(argc, argv);
446
  if (debug_flag == 1)
447
    log_init_debug("");
448
  log_init(debug_flag, 1);
449

    
450
  if (!parse_and_exit)
451
    test_old_bird(path_control_socket);
452

    
453
  DBG("Initializing.\n");
454
  resource_init();
455
  olock_init();
456
  io_init();
457
  rt_init();
458
  if_init();
459

    
460
  protos_build();
461
  proto_build(&proto_unix_kernel);
462
  proto_build(&proto_unix_iface);
463

    
464
  read_config();
465

    
466
  if (parse_and_exit)
467
    exit(0);
468

    
469
  if (!debug_flag)
470
    {
471
      pid_t pid = fork();
472
      if (pid < 0)
473
        die("fork: %m");
474
      if (pid)
475
        return 0;
476
      setsid();
477
      close(0);
478
      if (open("/dev/null", O_RDWR) < 0)
479
        die("Cannot open /dev/null: %m");
480
      dup2(0, 1);
481
      dup2(0, 2);
482
    }
483

    
484
  signal_init();
485

    
486
  cli_init_unix();
487

    
488
#ifdef LOCAL_DEBUG
489
  async_dump_flag = 1;
490
#endif
491

    
492
  log(L_INFO "Started");
493
  DBG("Entering I/O loop.\n");
494

    
495
  io_loop();
496
  bug("I/O loop died");
497
}