Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (7.32 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)
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

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

    
103
static void
104
read_config(void)
105
{
106
  struct config *conf;
107

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

    
118
void
119
async_config(void)
120
{
121
  struct config *conf;
122

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

    
136
void
137
cmd_reconfig(char *name)
138
{
139
  struct config *conf;
140

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

    
171
/*
172
 *        Command-Line Interface
173
 */
174

    
175
static sock *cli_sk;
176
static char *path_control_socket = PATH_CONTROL_SOCKET;
177

    
178
int
179
cli_write(cli *c)
180
{
181
  sock *s = c->priv;
182

    
183
  if (c->tx_pos)
184
    {
185
      struct cli_out *o = c->tx_pos;
186
      c->tx_pos = o->next;
187
      s->tbuf = o->outpos;
188
      if (sk_send(s, o->wpos - o->outpos) > 0)
189
        ev_schedule(c->event);
190
    }
191
  return !c->tx_pos;
192
}
193

    
194
int
195
cli_get_command(cli *c)
196
{
197
  sock *s = c->priv;
198
  byte *t = c->rx_aux ? : s->rbuf;
199
  byte *tend = s->rpos;
200
  byte *d = c->rx_pos;
201
  byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;
202

    
203
  while (t < tend)
204
    {
205
      if (*t == '\r')
206
        t++;
207
      else if (*t == '\n')
208
        {
209
          t++;
210
          c->rx_pos = c->rx_buf;
211
          c->rx_aux = t;
212
          *d = 0;
213
          return (d < dend) ? 1 : -1;
214
        }
215
      else if (d < dend)
216
        *d++ = *t++;
217
    }
218
  c->rx_aux = s->rpos = s->rbuf;
219
  c->rx_pos = d;
220
  return 0;
221
}
222

    
223
static int
224
cli_rx(sock *s, int size)
225
{
226
  cli_kick(s->data);
227
  return 0;
228
}
229

    
230
static void
231
cli_tx(sock *s)
232
{
233
  cli *c = s->data;
234

    
235
  if (cli_write(c))
236
    cli_written(c);
237
}
238

    
239
static void
240
cli_err(sock *s, int err)
241
{
242
  if (config->cli_debug)
243
    {
244
      if (err)
245
        log(L_INFO "CLI connection dropped: %s", strerror(err));
246
      else
247
        log(L_INFO "CLI connection closed");
248
    }
249
  cli_free(s->data);
250
  sk_close(s);
251
}
252

    
253
static int
254
cli_connect(sock *s, int size)
255
{
256
  cli *c;
257

    
258
  if (config->cli_debug)
259
    log(L_INFO "CLI connect");
260
  s->rx_hook = cli_rx;
261
  s->tx_hook = cli_tx;
262
  s->err_hook = cli_err;
263
  s->data = c = cli_new(s);
264
  s->pool = c->pool;                /* We need to have all the socket buffers allocated in the cli pool */
265
  c->rx_pos = c->rx_buf;
266
  c->rx_aux = NULL;
267
  return 1;
268
}
269

    
270
static void
271
cli_init_unix(void)
272
{
273
  sock *s;
274

    
275
  cli_init();
276
  s = cli_sk = sk_new(cli_pool);
277
  s->type = SK_UNIX_PASSIVE;
278
  s->rx_hook = cli_connect;
279
  s->rbsize = 1024;
280
  if (sk_open_unix(s, path_control_socket) < 0)
281
    die("Unable to create control socket %s", path_control_socket);
282
}
283

    
284
/*
285
 *        Shutdown
286
 */
287

    
288
void
289
async_shutdown(void)
290
{
291
  DBG("Shutting down...\n");
292
  order_shutdown();
293
}
294

    
295
void
296
sysdep_shutdown_done(void)
297
{
298
  unlink(PATH_CONTROL_SOCKET);
299
  die("System shutdown completed");
300
}
301

    
302
/*
303
 *        Signals
304
 */
305

    
306
static void
307
handle_sighup(int sig)
308
{
309
  DBG("Caught SIGHUP...\n");
310
  async_config_flag = 1;
311
}
312

    
313
static void
314
handle_sigusr(int sig)
315
{
316
  DBG("Caught SIGUSR...\n");
317
  async_dump_flag = 1;
318
}
319

    
320
static void
321
handle_sigterm(int sig)
322
{
323
  DBG("Caught SIGTERM...\n");
324
  async_shutdown_flag = 1;
325
}
326

    
327
static void
328
signal_init(void)
329
{
330
  struct sigaction sa;
331

    
332
  bzero(&sa, sizeof(sa));
333
  sa.sa_handler = handle_sigusr;
334
  sa.sa_flags = SA_RESTART;
335
  sigaction(SIGUSR1, &sa, NULL);
336
  sa.sa_handler = handle_sighup;
337
  sa.sa_flags = SA_RESTART;
338
  sigaction(SIGHUP, &sa, NULL);
339
  sa.sa_handler = handle_sigterm;
340
  sa.sa_flags = SA_RESTART;
341
  sigaction(SIGTERM, &sa, NULL);
342
  signal(SIGPIPE, SIG_IGN);
343
}
344

    
345
/*
346
 *        Parsing of command-line arguments
347
 */
348

    
349
static char *opt_list = "c:dD:s:";
350

    
351
static void
352
usage(void)
353
{
354
  fprintf(stderr, "Usage: bird [-c <config-file>] [-d] [-D <debug-file>] [-s <control-socket>]\n");
355
  exit(1);
356
}
357

    
358
static void
359
parse_args(int argc, char **argv)
360
{
361
  int c;
362

    
363
  if (argc == 2)
364
    {
365
      if (!strcmp(argv[1], "--version"))
366
        {
367
          fprintf(stderr, "BIRD version " BIRD_VERSION "\n");
368
          exit(0);
369
        }
370
      if (!strcmp(argv[1], "--help"))
371
        usage();
372
    }
373
  while ((c = getopt(argc, argv, opt_list)) >= 0)
374
    switch (c)
375
      {
376
      case 'c':
377
        config_name = optarg;
378
        break;
379
      case 'd':
380
        debug_flag |= 1;
381
        break;
382
      case 'D':
383
        log_init_debug(optarg);
384
        debug_flag |= 2;
385
        break;
386
      case 's':
387
        path_control_socket = optarg;
388
        break;
389
      default:
390
        usage();
391
      }
392
  if (optind < argc)
393
    usage();
394
}
395

    
396
/*
397
 *        Hic Est main()
398
 */
399

    
400
int
401
main(int argc, char **argv)
402
{
403
#ifdef HAVE_LIBDMALLOC
404
  if (!getenv("DMALLOC_OPTIONS"))
405
    dmalloc_debug(0x2f03d00);
406
#endif
407

    
408
  parse_args(argc, argv);
409
  if (debug_flag == 1)
410
    log_init_debug("");
411
  log_init(debug_flag, 1);
412

    
413
  DBG("Initializing.\n");
414
  resource_init();
415
  olock_init();
416
  io_init();
417
  rt_init();
418
  if_init();
419

    
420
  protos_build();
421
  proto_build(&proto_unix_kernel);
422
  proto_build(&proto_unix_iface);
423

    
424
  read_config();
425

    
426
  if (!debug_flag)
427
    {
428
      pid_t pid = fork();
429
      if (pid < 0)
430
        die("fork: %m");
431
      if (pid)
432
        return 0;
433
      setsid();
434
    }
435

    
436
  signal_init();
437

    
438
  cli_init_unix();
439

    
440
#ifdef LOCAL_DEBUG
441
  async_dump_flag = 1;
442
#endif
443

    
444
  DBG("Entering I/O loop.\n");
445

    
446
  io_loop();
447
  bug("I/O loop died");
448
}