summaryrefslogtreecommitdiffstats
path: root/sysdep/unix/main.c
blob: 4aeb9c1976bae9dcf02aa0075a73fc10aeb38928 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/*
 *	BIRD Internet Routing Daemon -- Unix Entry Point
 *
 *	(c) 1998 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/signal.h>

#include "nest/bird.h"
#include "lib/lists.h"
#include "lib/resource.h"
#include "lib/socket.h"
#include "nest/route.h"
#include "nest/protocol.h"
#include "nest/iface.h"
#include "conf/conf.h"

#include "unix.h"
#include "krt.h"

/*
 *	Debugging
 */

static void
handle_sigusr(int sig)
{
  debug("SIGUSR1: Debugging dump...\n\n");

  sk_dump_all();
  tm_dump_all();
  if_dump_all();
  neigh_dump_all();
  rta_dump_all();
  rt_dump_all();
  protos_dump_all();

  debug("\n");
}

static void
signal_init(void)
{
  static struct sigaction sa;

  sa.sa_handler = handle_sigusr;
  sa.sa_flags = SA_RESTART;
  if (sigaction(SIGUSR1, &sa, NULL) < 0)
    die("sigaction: %m");
  signal(SIGPIPE, SIG_IGN);
}

/*
 *	Reading the Configuration
 */

static int conf_fd;

static int
cf_read(byte *dest, unsigned int len)
{
  int l = read(conf_fd, dest, len);
  if (l < 0)
    cf_error("Read error");
  return l;
}

static void
read_config(void)
{
  cf_lex_init_tables();
  cf_allocate();
  conf_fd = open(PATH_CONFIG, O_RDONLY);
  if (conf_fd < 0)
    die("Unable to open configuration file " PATH_CONFIG ": %m");
  protos_preconfig();
  cf_read_hook = cf_read;
  cf_lex_init(1);
  cf_parse();
  add_tail(&protocol_list, &proto_unix_kernel.n); /* FIXME: Must be _always_ the last one */
  protos_postconfig();
}

/*
 *	Hic Est main()
 */

int
main(void)
{
  log(L_INFO "Launching BIRD -1.-1-pre-omega...");

  log_init_debug(NULL);

  debug("Initializing.\n");
  resource_init();
  io_init();
  rt_init();
  if_init();

  protos_build();
  protos_init();

  debug("Reading configuration file.\n");
  read_config();

  signal_init();

  scan_if_init();
  auto_router_id();

  protos_start();

  handle_sigusr(0);

  debug("Entering I/O loop.\n");

  io_loop();
  die("I/O loop died");
}