Statistics
| Branch: | Revision:

iof-bird-daemon / sysdep / unix / main.c @ 7a2105be

History | View | Annotate | Download (3.43 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 "conf/conf.h"
25
#include "filter/filter.h"
26

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

    
30
int shutting_down;
31

    
32
/*
33
 *        Debugging
34
 */
35

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

    
41
  sk_dump_all();
42
  tm_dump_all();
43
  if_dump_all();
44
  neigh_dump_all();
45
  rta_dump_all();
46
  rt_dump_all();
47
  protos_dump_all();
48

    
49
  debug("\n");
50
}
51

    
52
/*
53
 *        Reading the Configuration
54
 */
55

    
56
static int conf_fd;
57
static char *config_name = PATH_CONFIG;
58

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

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

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

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

    
88
/*
89
 *        Shutdown
90
 */
91

    
92
void
93
async_shutdown(void)
94
{
95
  debug("Shutting down...\n");
96
  shutting_down = 1;
97
  protos_shutdown();
98
}
99

    
100
void
101
protos_shutdown_notify(void)
102
{
103
  die("System shutdown completed");
104
}
105

    
106
/*
107
 *        Signals
108
 */
109

    
110
static void
111
handle_sighup(int sig)
112
{
113
  debug("Caught SIGHUP...\n");
114
  async_config_flag = 1;
115
}
116

    
117
static void
118
handle_sigusr(int sig)
119
{
120
  debug("Caught SIGUSR...\n");
121
  async_dump_flag = 1;
122
}
123

    
124
static void
125
handle_sigterm(int sig)
126
{
127
  debug("Caught SIGTERM...\n");
128
  async_shutdown_flag = 1;
129
}
130

    
131
static void
132
signal_init(void)
133
{
134
  struct sigaction sa;
135

    
136
  bzero(&sa, sizeof(sa));
137
  sa.sa_handler = handle_sigusr;
138
  sa.sa_flags = SA_RESTART;
139
  sigaction(SIGUSR1, &sa, NULL);
140
  sa.sa_handler = handle_sighup;
141
  sa.sa_flags = SA_RESTART;
142
  sigaction(SIGHUP, &sa, NULL);
143
  sa.sa_handler = handle_sigterm;
144
  sa.sa_flags = SA_RESTART;
145
  sigaction(SIGTERM, &sa, NULL);
146
  signal(SIGPIPE, SIG_IGN);
147
}
148

    
149
/*
150
 *        Parsing of command-line arguments
151
 */
152

    
153
static char *opt_list = "c:d:";
154

    
155
static void
156
usage(void)
157
{
158
  fprintf(stderr, "Usage: bird [-c <config-file>] [-d <debug-file>]\n");
159
  exit(1);
160
}
161

    
162
static void
163
parse_args(int argc, char **argv)
164
{
165
  int c;
166

    
167
  while ((c = getopt(argc, argv, opt_list)) >= 0)
168
    switch (c)
169
      {
170
      case 'c':
171
        config_name = optarg;
172
        break;
173
      case 'd':
174
        log_init_debug(optarg);
175
        break;
176
      default:
177
        usage();
178
      }
179
  if (optind < argc)
180
    usage();
181
}
182

    
183
/*
184
 *        Hic Est main()
185
 */
186

    
187
int
188
main(int argc, char **argv)
189
{
190
#ifdef HAVE_LIBDMALLOC
191
  if (!getenv("DMALLOC_OPTIONS"))
192
    dmalloc_debug(0x2f03d00);
193
#endif
194

    
195
  log_init_debug(NULL);
196
  parse_args(argc, argv);
197

    
198
  log(L_INFO "Launching BIRD 0.0.0...");
199

    
200
  debug("Initializing.\n");
201
  resource_init();
202
  io_init();
203
  rt_init();
204
  if_init();
205

    
206
  protos_build();
207
  add_tail(&protocol_list, &proto_unix_kernel.n);
208

    
209
  read_config();
210

    
211
  signal_init();
212

    
213
  protos_start();
214

    
215
  ev_run_list(&global_event_list);
216
  async_dump();
217

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

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