Statistics
| Branch: | Revision:

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

History | View | Annotate | Download (5.23 KB)

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

    
9
#include <stdio.h>
10
#include <string.h>
11
#include <stdlib.h>
12
#include <fcntl.h>
13
#include <unistd.h>
14
#include <sys/signal.h>
15

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

    
28
#include "unix.h"
29
#include "krt.h"
30

    
31
int shutting_down;
32

    
33
/*
34
 *        Debugging
35
 */
36

    
37
void
38
async_dump(void)
39
{
40
  debug("INTERNAL STATE DUMP\n\n");
41

    
42
  rdump(&root_pool);
43
  sk_dump_all();
44
  tm_dump_all();
45
  if_dump_all();
46
  neigh_dump_all();
47
  rta_dump_all();
48
  rt_dump_all();
49
  protos_dump_all();
50

    
51
  debug("\n");
52
}
53

    
54
/*
55
 *        Reading the Configuration
56
 */
57

    
58
static int conf_fd;
59
static char *config_name = PATH_CONFIG;
60

    
61
static int
62
cf_read(byte *dest, unsigned int len)
63
{
64
  int l = read(conf_fd, dest, len);
65
  if (l < 0)
66
    cf_error("Read error");
67
  return l;
68
}
69

    
70
static void
71
read_config(void)
72
{
73
  struct config *conf = config_alloc(config_name);
74

    
75
  conf_fd = open(config_name, O_RDONLY);
76
  if (conf_fd < 0)
77
    die("Unable to open configuration file %s: %m", config_name);
78
  cf_read_hook = cf_read;
79
  if (!config_parse(conf))
80
    die("%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
81
  config_commit(conf);
82
}
83

    
84
void
85
async_config(void)
86
{
87
  debug("Asynchronous reconfigurations are not supported in demo version\n");
88
}
89

    
90
/*
91
 *        Command-Line Interface
92
 */
93

    
94
static sock *cli_sk;
95

    
96
int
97
cli_write(cli *c)
98
{
99
  sock *s = c->priv;
100

    
101
  if (c->tx_pos)
102
    {
103
      struct cli_out *o = c->tx_pos;
104
      c->tx_pos = o->next;
105
      s->tbuf = o->outpos;
106
      return sk_send(s, o->wpos - o->outpos);
107
    }
108
  return 1;
109
}
110

    
111
int
112
cli_get_command(cli *c)
113
{
114
  sock *s = c->priv;
115
  byte *t = c->rx_aux ? : s->rbuf;
116
  byte *tend = s->rpos;
117
  byte *d = c->rx_pos;
118
  byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;
119

    
120
  while (t < tend)
121
    {
122
      if (*t == '\r')
123
        t++;
124
      else if (*t == '\n')
125
        {
126
          t++;
127
          c->rx_pos = c->rx_buf;
128
          c->rx_aux = t;
129
          *d = 0;
130
          return (d < dend) ? 1 : -1;
131
        }
132
      else if (d < dend)
133
        *d++ = *t++;
134
    }
135
  c->rx_aux = s->rpos = s->rbuf;
136
  c->rx_pos = d;
137
  return 0;
138
}
139

    
140
static int
141
cli_rx(sock *s, int size)
142
{
143
  cli_kick(s->data);
144
  return 0;
145
}
146

    
147
static void
148
cli_tx(sock *s)
149
{
150
  cli *c = s->data;
151

    
152
  if (cli_write(c))
153
    cli_written(c);
154
}
155

    
156
static void
157
cli_err(sock *s, int err)
158
{
159
  if (err)
160
    log(L_INFO "CLI connection dropped: %s", strerror(err));
161
  else
162
    log(L_INFO "CLI connection closed");
163
  s->type = SK_DELETED;
164
  cli_free(s->data);
165
}
166

    
167
static int
168
cli_connect(sock *s, int size)
169
{
170
  cli *c;
171

    
172
  log(L_INFO "CLI connect");
173
  s->rx_hook = cli_rx;
174
  s->tx_hook = cli_tx;
175
  s->err_hook = cli_err;
176
  s->rbsize = 1024;
177
  s->data = c = cli_new(s);
178
  c->rx_pos = c->rx_buf;
179
  c->rx_aux = NULL;
180
  return 1;
181
}
182

    
183
static void
184
cli_init_unix(void)
185
{
186
  sock *s;
187

    
188
  cli_init();
189
  s = cli_sk = sk_new(cli_pool);
190
  s->type = SK_UNIX_PASSIVE;
191
  s->rx_hook = cli_connect;
192
  sk_open_unix(s, PATH_CONTROL_SOCKET);
193
}
194

    
195
/*
196
 *        Shutdown
197
 */
198

    
199
void
200
async_shutdown(void)
201
{
202
  debug("Shutting down...\n");
203
  shutting_down = 1;
204
  protos_shutdown();
205
}
206

    
207
void
208
protos_shutdown_notify(void)
209
{
210
  unlink(PATH_CONTROL_SOCKET);
211
  die("System shutdown completed");
212
}
213

    
214
/*
215
 *        Signals
216
 */
217

    
218
static void
219
handle_sighup(int sig)
220
{
221
  debug("Caught SIGHUP...\n");
222
  async_config_flag = 1;
223
}
224

    
225
static void
226
handle_sigusr(int sig)
227
{
228
  debug("Caught SIGUSR...\n");
229
  async_dump_flag = 1;
230
}
231

    
232
static void
233
handle_sigterm(int sig)
234
{
235
  debug("Caught SIGTERM...\n");
236
  async_shutdown_flag = 1;
237
}
238

    
239
static void
240
signal_init(void)
241
{
242
  struct sigaction sa;
243

    
244
  bzero(&sa, sizeof(sa));
245
  sa.sa_handler = handle_sigusr;
246
  sa.sa_flags = SA_RESTART;
247
  sigaction(SIGUSR1, &sa, NULL);
248
  sa.sa_handler = handle_sighup;
249
  sa.sa_flags = SA_RESTART;
250
  sigaction(SIGHUP, &sa, NULL);
251
  sa.sa_handler = handle_sigterm;
252
  sa.sa_flags = SA_RESTART;
253
  sigaction(SIGTERM, &sa, NULL);
254
  signal(SIGPIPE, SIG_IGN);
255
}
256

    
257
/*
258
 *        Parsing of command-line arguments
259
 */
260

    
261
static char *opt_list = "c:d:";
262

    
263
static void
264
usage(void)
265
{
266
  fprintf(stderr, "Usage: bird [-c <config-file>] [-d <debug-file>]\n");
267
  exit(1);
268
}
269

    
270
static void
271
parse_args(int argc, char **argv)
272
{
273
  int c;
274

    
275
  while ((c = getopt(argc, argv, opt_list)) >= 0)
276
    switch (c)
277
      {
278
      case 'c':
279
        config_name = optarg;
280
        break;
281
      case 'd':
282
        log_init_debug(optarg);
283
        break;
284
      default:
285
        usage();
286
      }
287
  if (optind < argc)
288
    usage();
289
}
290

    
291
/*
292
 *        Hic Est main()
293
 */
294

    
295
int
296
main(int argc, char **argv)
297
{
298
#ifdef HAVE_LIBDMALLOC
299
  if (!getenv("DMALLOC_OPTIONS"))
300
    dmalloc_debug(0x2f03d00);
301
#endif
302

    
303
  log_init_debug(NULL);
304
  setvbuf(stdout, NULL, _IONBF, 0);        /* FIXME: Kill some day. */
305
  setvbuf(stderr, NULL, _IONBF, 0);
306
  parse_args(argc, argv);
307

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

    
310
  debug("Initializing.\n");
311
  resource_init();
312
  io_init();
313
  rt_init();
314
  if_init();
315

    
316
  protos_build();
317
  add_tail(&protocol_list, &proto_unix_kernel.n);
318
  add_tail(&protocol_list, &proto_unix_iface.n);
319

    
320
  read_config();
321

    
322
  signal_init();
323

    
324
  cli_init_unix();
325

    
326
  protos_start();
327

    
328
  ev_run_list(&global_event_list);
329
  async_dump();
330

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

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