Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / main.c @ 6578a604

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

    
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
      s->tbuf = o->outpos;
187
      if (sk_send(s, o->wpos - o->outpos) > 0)
188
        {
189
          c->tx_pos = o->next;
190
          ev_schedule(c->event);
191
        }
192
    }
193
  return !c->tx_pos;
194
}
195

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

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

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

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

    
237
  if (cli_write(c))
238
    cli_written(c);
239
}
240

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

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

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

    
272
static void
273
cli_init_unix(void)
274
{
275
  sock *s;
276

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

    
286
/*
287
 *        Shutdown
288
 */
289

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

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

    
304
/*
305
 *        Signals
306
 */
307

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

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

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

    
329
static void
330
signal_init(void)
331
{
332
  struct sigaction sa;
333

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

    
347
/*
348
 *        Parsing of command-line arguments
349
 */
350

    
351
static char *opt_list = "c:dD:s:";
352

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

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

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

    
398
/*
399
 *        Hic Est main()
400
 */
401

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

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

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

    
422
  protos_build();
423
  proto_build(&proto_unix_kernel);
424
  proto_build(&proto_unix_iface);
425

    
426
  read_config();
427

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

    
438
  signal_init();
439

    
440
  cli_init_unix();
441

    
442
#ifdef LOCAL_DEBUG
443
  async_dump_flag = 1;
444
#endif
445

    
446
  DBG("Entering I/O loop.\n");
447

    
448
  io_loop();
449
  bug("I/O loop died");
450
}