]>
git.ipfire.org Git - thirdparty/bird.git/blob - sysdep/unix/main.c
2 * BIRD Internet Routing Daemon -- Unix Entry Point
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
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"
27 #include "nest/locks.h"
28 #include "conf/conf.h"
29 #include "filter/filter.h"
39 static int debug_flag
= 1;
41 static int debug_flag
= 0;
47 debug("INTERNAL STATE DUMP\n\n");
62 * Reading the Configuration
66 static char *config_name
= PATH_CONFIG
;
69 cf_read(byte
*dest
, unsigned int len
)
71 int l
= read(conf_fd
, dest
, len
);
73 cf_error("Read error");
78 sysdep_preconfig(struct config
*c
)
80 init_list(&c
->logfiles
);
84 sysdep_commit(struct config
*new, struct config
*old UNUSED
)
86 log_switch(debug_flag
, &new->logfiles
);
91 unix_read_config(struct config
**cp
, char *name
)
93 struct config
*conf
= config_alloc(name
);
97 conf_fd
= open(name
, O_RDONLY
);
100 cf_read_hook
= cf_read
;
101 ret
= config_parse(conf
);
111 if (!unix_read_config(&conf
, config_name
))
114 die("%s, line %d: %s", config_name
, conf
->err_lino
, conf
->err_msg
);
116 die("Unable to open configuration file %s: %m", config_name
);
118 config_commit(conf
, RECONFIG_HARD
);
126 log(L_INFO
"Reconfiguration requested by SIGHUP");
127 if (!unix_read_config(&conf
, config_name
))
130 log(L_ERR
"%s, line %d: %s", config_name
, conf
->err_lino
, conf
->err_msg
);
132 log(L_ERR
"Unable to open configuration file %s: %m", config_name
);
136 config_commit(conf
, RECONFIG_HARD
);
140 cmd_reconfig(char *name
, int type
)
146 cli_msg(-2, "Reading configuration from %s", name
);
147 if (!unix_read_config(&conf
, name
))
150 cli_msg(8002, "%s, line %d: %s", name
, conf
->err_lino
, conf
->err_msg
);
152 cli_msg(8002, "%s: %m", name
);
157 switch (config_commit(conf
, type
))
160 cli_msg(3, "Reconfigured.");
163 cli_msg(4, "Reconfiguration in progress.");
166 cli_msg(6, "Reconfiguration ignored, shutting down.");
169 cli_msg(5, "Reconfiguration already in progress, queueing new config");
175 * Command-Line Interface
179 static char *path_control_socket
= PATH_CONTROL_SOCKET
;
188 struct cli_out
*o
= c
->tx_pos
;
190 if (sk_send(s
, o
->wpos
- o
->outpos
) > 0)
193 ev_schedule(c
->event
);
200 cli_get_command(cli
*c
)
203 byte
*t
= c
->rx_aux
? : s
->rbuf
;
204 byte
*tend
= s
->rpos
;
206 byte
*dend
= c
->rx_buf
+ CLI_RX_BUF_SIZE
- 2;
215 c
->rx_pos
= c
->rx_buf
;
218 return (d
< dend
) ? 1 : -1;
223 c
->rx_aux
= s
->rpos
= s
->rbuf
;
229 cli_rx(sock
*s
, int size UNUSED
)
245 cli_err(sock
*s
, int err
)
247 if (config
->cli_debug
)
250 log(L_INFO
"CLI connection dropped: %s", strerror(err
));
252 log(L_INFO
"CLI connection closed");
258 cli_connect(sock
*s
, int size UNUSED
)
262 if (config
->cli_debug
)
263 log(L_INFO
"CLI connect");
266 s
->err_hook
= cli_err
;
267 s
->data
= c
= cli_new(s
);
268 s
->pool
= c
->pool
; /* We need to have all the socket buffers allocated in the cli pool */
269 c
->rx_pos
= c
->rx_buf
;
281 s
= cli_sk
= sk_new(cli_pool
);
282 s
->type
= SK_UNIX_PASSIVE
;
283 s
->rx_hook
= cli_connect
;
285 if (sk_open_unix(s
, path_control_socket
) < 0)
286 die("Unable to create control socket %s", path_control_socket
);
296 DBG("Shutting down...\n");
301 sysdep_shutdown_done(void)
303 unlink(PATH_CONTROL_SOCKET
);
304 die("System shutdown completed");
312 handle_sighup(int sig UNUSED
)
314 DBG("Caught SIGHUP...\n");
315 async_config_flag
= 1;
319 handle_sigusr(int sig UNUSED
)
321 DBG("Caught SIGUSR...\n");
326 handle_sigterm(int sig UNUSED
)
328 DBG("Caught SIGTERM...\n");
329 async_shutdown_flag
= 1;
337 bzero(&sa
, sizeof(sa
));
338 sa
.sa_handler
= handle_sigusr
;
339 sa
.sa_flags
= SA_RESTART
;
340 sigaction(SIGUSR1
, &sa
, NULL
);
341 sa
.sa_handler
= handle_sighup
;
342 sa
.sa_flags
= SA_RESTART
;
343 sigaction(SIGHUP
, &sa
, NULL
);
344 sa
.sa_handler
= handle_sigterm
;
345 sa
.sa_flags
= SA_RESTART
;
346 sigaction(SIGTERM
, &sa
, NULL
);
347 signal(SIGPIPE
, SIG_IGN
);
351 * Parsing of command-line arguments
354 static char *opt_list
= "c:dD:s:";
359 fprintf(stderr
, "Usage: bird [-c <config-file>] [-d] [-D <debug-file>] [-s <control-socket>]\n");
364 parse_args(int argc
, char **argv
)
370 if (!strcmp(argv
[1], "--version"))
372 fprintf(stderr
, "BIRD version " BIRD_VERSION
"\n");
375 if (!strcmp(argv
[1], "--help"))
378 while ((c
= getopt(argc
, argv
, opt_list
)) >= 0)
382 config_name
= optarg
;
388 log_init_debug(optarg
);
392 path_control_socket
= optarg
;
406 main(int argc
, char **argv
)
408 #ifdef HAVE_LIBDMALLOC
409 if (!getenv("DMALLOC_OPTIONS"))
410 dmalloc_debug(0x2f03d00);
413 parse_args(argc
, argv
);
416 log_init(debug_flag
, 1);
418 test_old_bird(path_control_socket
);
420 DBG("Initializing.\n");
428 proto_build(&proto_unix_kernel
);
429 proto_build(&proto_unix_iface
);
442 if (open("/dev/null", O_RDWR
) < 0)
443 die("Cannot open /dev/null: %m");
456 DBG("Entering I/O loop.\n");
459 bug("I/O loop died");