summaryrefslogtreecommitdiff
path: root/sysdep/unix/main.c
blob: 8e2678a2ed8c337eaac4f13b72d1aa4229e4227e (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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
/*
 *	BIRD Internet Routing Daemon -- Unix Entry Point
 *
 *	(c) 1998--1999 Martin Mares <mj@ucw.cz>
 *
 *	Can be freely distributed and used under the terms of the GNU GPL.
 */

#include <stdio.h>
#include <string.h>
#include <stdlib.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 "lib/event.h"
#include "nest/route.h"
#include "nest/protocol.h"
#include "nest/iface.h"
#include "nest/cli.h"
#include "conf/conf.h"
#include "filter/filter.h"

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

int shutting_down;

/*
 *	Debugging
 */

void
async_dump(void)
{
  debug("INTERNAL STATE DUMP\n\n");

  rdump(&root_pool);
  sk_dump_all();
  tm_dump_all();
  if_dump_all();
  neigh_dump_all();
  rta_dump_all();
  rt_dump_all();
  protos_dump_all();

  debug("\n");
}

/*
 *	Reading the Configuration
 */

static int conf_fd;
static char *config_name = PATH_CONFIG;

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;
}

void
sysdep_preconfig(struct config *c)
{
  init_list(&c->logfiles);
}

void
sysdep_commit(struct config *c)
{
  log_switch(&c->logfiles);
}

static void
read_config(void)
{
  struct config *conf = config_alloc(config_name);

  conf_fd = open(config_name, O_RDONLY);
  if (conf_fd < 0)
    die("Unable to open configuration file %s: %m", config_name);
  cf_read_hook = cf_read;
  if (!config_parse(conf))
    die("%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
  config_commit(conf);
}

void
async_config(void)
{
  debug("Asynchronous reconfigurations are not supported in demo version\n");
}

/*
 *	Command-Line Interface
 */

static sock *cli_sk;

int
cli_write(cli *c)
{
  sock *s = c->priv;

  if (c->tx_pos)
    {
      struct cli_out *o = c->tx_pos;
      c->tx_pos = o->next;
      s->tbuf = o->outpos;
      return sk_send(s, o->wpos - o->outpos);
    }
  return 1;
}

int
cli_get_command(cli *c)
{
  sock *s = c->priv;
  byte *t = c->rx_aux ? : s->rbuf;
  byte *tend = s->rpos;
  byte *d = c->rx_pos;
  byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;

  while (t < tend)
    {
      if (*t == '\r')
	t++;
      else if (*t == '\n')
	{
	  t++;
	  c->rx_pos = c->rx_buf;
	  c->rx_aux = t;
	  *d = 0;
	  return (d < dend) ? 1 : -1;
	}
      else if (d < dend)
	*d++ = *t++;
    }
  c->rx_aux = s->rpos = s->rbuf;
  c->rx_pos = d;
  return 0;
}

static int
cli_rx(sock *s, int size)
{
  cli_kick(s->data);
  return 0;
}

static void
cli_tx(sock *s)
{
  cli *c = s->data;

  if (cli_write(c))
    cli_written(c);
}

static void
cli_err(sock *s, int err)
{
  if (err)
    log(L_INFO "CLI connection dropped: %s", strerror(err));
  else
    log(L_INFO "CLI connection closed");
  s->type = SK_DELETED;
  cli_free(s->data);
}

static int
cli_connect(sock *s, int size)
{
  cli *c;

  log(L_INFO "CLI connect");
  s->rx_hook = cli_rx;
  s->tx_hook = cli_tx;
  s->err_hook = cli_err;
  s->rbsize = 1024;
  s->data = c = cli_new(s);
  c->rx_pos = c->rx_buf;
  c->rx_aux = NULL;
  return 1;
}

static void
cli_init_unix(void)
{
  sock *s;

  cli_init();
  s = cli_sk = sk_new(cli_pool);
  s->type = SK_UNIX_PASSIVE;
  s->rx_hook = cli_connect;
  sk_open_unix(s, PATH_CONTROL_SOCKET);
}

/*
 *	Shutdown
 */

void
async_shutdown(void)
{
  debug("Shutting down...\n");
  shutting_down = 1;
  protos_shutdown();
}

void
protos_shutdown_notify(void)
{
  unlink(PATH_CONTROL_SOCKET);
  die("System shutdown completed");
}

/*
 *	Signals
 */

static void
handle_sighup(int sig)
{
  debug("Caught SIGHUP...\n");
  async_config_flag = 1;
}

static void
handle_sigusr(int sig)
{
  debug("Caught SIGUSR...\n");
  async_dump_flag = 1;
}

static void
handle_sigterm(int sig)
{
  debug("Caught SIGTERM...\n");
  async_shutdown_flag = 1;
}

static void
signal_init(void)
{
  struct sigaction sa;

  bzero(&sa, sizeof(sa));
  sa.sa_handler = handle_sigusr;
  sa.sa_flags = SA_RESTART;
  sigaction(SIGUSR1, &sa, NULL);
  sa.sa_handler = handle_sighup;
  sa.sa_flags = SA_RESTART;
  sigaction(SIGHUP, &sa, NULL);
  sa.sa_handler = handle_sigterm;
  sa.sa_flags = SA_RESTART;
  sigaction(SIGTERM, &sa, NULL);
  signal(SIGPIPE, SIG_IGN);
}

/*
 *	Parsing of command-line arguments
 */

static char *opt_list = "c:dD:";
static int debug_flag = 1;		/* FIXME: Turn off for production use */

static void
usage(void)
{
  fprintf(stderr, "Usage: bird [-c <config-file>] [-d] [-D <debug-file>]\n");
  exit(1);
}

static void
parse_args(int argc, char **argv)
{
  int c;

  while ((c = getopt(argc, argv, opt_list)) >= 0)
    switch (c)
      {
      case 'c':
	config_name = optarg;
	break;
      case 'd':
	debug_flag |= 1;
	break;
      case 'D':
	log_init_debug(optarg);
	debug_flag |= 2;
	break;
      default:
	usage();
      }
  if (optind < argc)
    usage();
}

/*
 *	Hic Est main()
 */

int
main(int argc, char **argv)
{
#ifdef HAVE_LIBDMALLOC
  if (!getenv("DMALLOC_OPTIONS"))
    dmalloc_debug(0x2f03d00);
#endif

  setvbuf(stdout, NULL, _IONBF, 0);	/* FIXME: Kill some day. */
  setvbuf(stderr, NULL, _IONBF, 0);
  parse_args(argc, argv);
  if (debug_flag == 1)
    log_init_debug("");
  log_init(debug_flag);

  log(L_INFO "Launching BIRD " BIRD_VERSION "...");

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

  protos_build();
  add_tail(&protocol_list, &proto_unix_kernel.n);
  add_tail(&protocol_list, &proto_unix_iface.n);

  read_config();

  signal_init();

  cli_init_unix();

  protos_start();

  ev_run_list(&global_event_list);
  async_dump();

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

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