1998-05-15 15:56:13 +08:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Unix Entry Point
|
|
|
|
*
|
2000-01-17 00:44:50 +08:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-15 15:56:13 +08:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
1999-02-14 03:43:21 +08:00
|
|
|
#include <string.h>
|
1999-03-04 19:36:26 +08:00
|
|
|
#include <stdlib.h>
|
1998-11-28 03:37:07 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
1998-05-24 22:50:18 +08:00
|
|
|
#include <sys/signal.h>
|
1998-05-15 15:56:13 +08:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/lists.h"
|
|
|
|
#include "lib/resource.h"
|
1998-05-24 22:50:18 +08:00
|
|
|
#include "lib/socket.h"
|
1999-02-12 06:51:15 +08:00
|
|
|
#include "lib/event.h"
|
1998-05-15 15:56:13 +08:00
|
|
|
#include "nest/route.h"
|
1998-05-24 22:50:18 +08:00
|
|
|
#include "nest/protocol.h"
|
1998-05-27 05:42:05 +08:00
|
|
|
#include "nest/iface.h"
|
1999-10-29 20:10:10 +08:00
|
|
|
#include "nest/cli.h"
|
1999-12-10 02:54:20 +08:00
|
|
|
#include "nest/locks.h"
|
1998-11-28 03:37:07 +08:00
|
|
|
#include "conf/conf.h"
|
1999-01-16 00:49:17 +08:00
|
|
|
#include "filter/filter.h"
|
1998-05-24 22:50:18 +08:00
|
|
|
|
|
|
|
#include "unix.h"
|
1998-10-18 20:50:43 +08:00
|
|
|
#include "krt.h"
|
1998-05-24 22:50:18 +08:00
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
int shutting_down;
|
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
/*
|
|
|
|
* Debugging
|
|
|
|
*/
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
void
|
|
|
|
async_dump(void)
|
1998-05-24 22:50:18 +08:00
|
|
|
{
|
1999-02-14 03:43:21 +08:00
|
|
|
debug("INTERNAL STATE DUMP\n\n");
|
1998-05-24 22:50:18 +08:00
|
|
|
|
1999-03-30 04:14:33 +08:00
|
|
|
rdump(&root_pool);
|
1998-05-24 22:50:18 +08:00
|
|
|
sk_dump_all();
|
|
|
|
tm_dump_all();
|
1998-05-27 05:42:05 +08:00
|
|
|
if_dump_all();
|
1998-06-02 05:41:32 +08:00
|
|
|
neigh_dump_all();
|
1998-05-24 22:50:18 +08:00
|
|
|
rta_dump_all();
|
|
|
|
rt_dump_all();
|
1998-07-10 03:37:39 +08:00
|
|
|
protos_dump_all();
|
1998-05-24 22:50:18 +08:00
|
|
|
|
|
|
|
debug("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-28 03:37:07 +08:00
|
|
|
* Reading the Configuration
|
1998-05-24 22:50:18 +08:00
|
|
|
*/
|
|
|
|
|
1998-11-28 03:37:07 +08:00
|
|
|
static int conf_fd;
|
1999-02-14 03:43:21 +08:00
|
|
|
static char *config_name = PATH_CONFIG;
|
1998-11-28 03:37:07 +08:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
1999-12-06 21:45:56 +08:00
|
|
|
void
|
|
|
|
sysdep_preconfig(struct config *c)
|
|
|
|
{
|
|
|
|
init_list(&c->logfiles);
|
|
|
|
}
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
int
|
|
|
|
sysdep_commit(struct config *new, struct config *old)
|
1999-12-06 21:45:56 +08:00
|
|
|
{
|
2000-01-17 00:44:50 +08:00
|
|
|
log_switch(&new->logfiles);
|
|
|
|
return 0;
|
1999-12-06 21:45:56 +08:00
|
|
|
}
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
static int
|
|
|
|
unix_read_config(struct config **cp, char *name)
|
1998-11-28 03:37:07 +08:00
|
|
|
{
|
2000-01-17 00:44:50 +08:00
|
|
|
struct config *conf = config_alloc(name);
|
1999-02-06 05:37:34 +08:00
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
*cp = conf;
|
|
|
|
conf_fd = open(name, O_RDONLY);
|
1998-11-28 03:37:07 +08:00
|
|
|
if (conf_fd < 0)
|
2000-01-17 00:44:50 +08:00
|
|
|
return 0;
|
1998-11-28 03:37:07 +08:00
|
|
|
cf_read_hook = cf_read;
|
2000-01-17 00:44:50 +08:00
|
|
|
return config_parse(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_config(void)
|
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
|
|
|
die("%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
|
|
|
|
else
|
|
|
|
die("Unable to open configuration file %s: %m", config_name);
|
|
|
|
}
|
1999-02-06 05:37:34 +08:00
|
|
|
config_commit(conf);
|
1998-11-28 03:37:07 +08:00
|
|
|
}
|
1999-02-06 05:37:34 +08:00
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
void
|
|
|
|
async_config(void)
|
|
|
|
{
|
2000-01-17 00:44:50 +08:00
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
log(L_INFO "Reconfiguration requested by SIGHUP");
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
|
|
|
log(L_ERR "%s, line %d: %s", config_name, conf->err_lino, conf->err_msg);
|
|
|
|
else
|
|
|
|
log(L_ERR "Unable to open configuration file %s: %m", config_name);
|
|
|
|
config_free(conf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
config_commit(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_reconfig(char *name)
|
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
name = config_name;
|
|
|
|
cli_msg(-2, "Reading configuration from %s", name);
|
|
|
|
if (!unix_read_config(&conf, name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
|
|
|
cli_msg(8002, "%s, line %d: %s", name, conf->err_lino, conf->err_msg);
|
|
|
|
else
|
|
|
|
cli_msg(8002, "%s: %m", name);
|
|
|
|
config_free(conf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
switch (config_commit(conf))
|
|
|
|
{
|
|
|
|
case CONF_DONE:
|
|
|
|
cli_msg(3, "Reconfigured.");
|
|
|
|
break;
|
|
|
|
case CONF_PROGRESS:
|
|
|
|
cli_msg(4, "Reconfiguration in progress.");
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
cli_msg(5, "Reconfiguration already in progress, queueing new config");
|
|
|
|
}
|
|
|
|
}
|
1999-02-14 03:43:21 +08:00
|
|
|
}
|
|
|
|
|
1999-10-29 20:10:10 +08:00
|
|
|
/*
|
|
|
|
* Command-Line Interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
static sock *cli_sk;
|
1999-12-08 21:20:19 +08:00
|
|
|
static char *path_control_socket = PATH_CONTROL_SOCKET;
|
1999-10-29 20:10:10 +08:00
|
|
|
|
|
|
|
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);
|
1999-12-08 21:20:19 +08:00
|
|
|
s->pool = c->pool; /* We need to have all the socket buffers allocated in the cli pool */
|
1999-10-29 20:10:10 +08:00
|
|
|
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;
|
1999-12-08 21:20:19 +08:00
|
|
|
if (sk_open_unix(s, path_control_socket) < 0)
|
|
|
|
die("Unable to create control socket %s", path_control_socket);
|
1999-10-29 20:10:10 +08:00
|
|
|
}
|
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
/*
|
|
|
|
* Shutdown
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
async_shutdown(void)
|
|
|
|
{
|
|
|
|
debug("Shutting down...\n");
|
|
|
|
shutting_down = 1;
|
|
|
|
protos_shutdown();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
protos_shutdown_notify(void)
|
|
|
|
{
|
1999-10-29 20:10:10 +08:00
|
|
|
unlink(PATH_CONTROL_SOCKET);
|
1999-02-14 04:15:36 +08:00
|
|
|
die("System shutdown completed");
|
|
|
|
}
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
/*
|
|
|
|
* 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;
|
|
|
|
}
|
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
static void
|
|
|
|
handle_sigterm(int sig)
|
|
|
|
{
|
|
|
|
debug("Caught SIGTERM...\n");
|
|
|
|
async_shutdown_flag = 1;
|
|
|
|
}
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
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);
|
1999-02-14 04:15:36 +08:00
|
|
|
sa.sa_handler = handle_sigterm;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
1999-02-14 03:43:21 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parsing of command-line arguments
|
|
|
|
*/
|
|
|
|
|
1999-12-08 21:20:19 +08:00
|
|
|
static char *opt_list = "c:dD:s:";
|
1999-12-06 21:45:56 +08:00
|
|
|
static int debug_flag = 1; /* FIXME: Turn off for production use */
|
1999-02-14 03:43:21 +08:00
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
1999-12-08 21:20:19 +08:00
|
|
|
fprintf(stderr, "Usage: bird [-c <config-file>] [-d] [-D <debug-file>] [-s <control-socket>]\n");
|
1999-02-14 03:43:21 +08:00
|
|
|
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':
|
1999-12-06 21:45:56 +08:00
|
|
|
debug_flag |= 1;
|
|
|
|
break;
|
|
|
|
case 'D':
|
1999-02-14 03:43:21 +08:00
|
|
|
log_init_debug(optarg);
|
1999-12-06 21:45:56 +08:00
|
|
|
debug_flag |= 2;
|
1999-02-14 03:43:21 +08:00
|
|
|
break;
|
1999-12-08 21:20:19 +08:00
|
|
|
case 's':
|
|
|
|
path_control_socket = optarg;
|
|
|
|
break;
|
1999-02-14 03:43:21 +08:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (optind < argc)
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1998-06-03 16:43:44 +08:00
|
|
|
/*
|
|
|
|
* Hic Est main()
|
|
|
|
*/
|
1998-05-15 15:56:13 +08:00
|
|
|
|
|
|
|
int
|
1999-02-14 03:43:21 +08:00
|
|
|
main(int argc, char **argv)
|
1998-05-15 15:56:13 +08:00
|
|
|
{
|
1999-03-04 19:36:26 +08:00
|
|
|
#ifdef HAVE_LIBDMALLOC
|
|
|
|
if (!getenv("DMALLOC_OPTIONS"))
|
|
|
|
dmalloc_debug(0x2f03d00);
|
|
|
|
#endif
|
|
|
|
|
1999-10-29 20:10:10 +08:00
|
|
|
setvbuf(stdout, NULL, _IONBF, 0); /* FIXME: Kill some day. */
|
1999-04-02 19:45:55 +08:00
|
|
|
setvbuf(stderr, NULL, _IONBF, 0);
|
1999-02-14 03:43:21 +08:00
|
|
|
parse_args(argc, argv);
|
1999-12-06 21:45:56 +08:00
|
|
|
if (debug_flag == 1)
|
|
|
|
log_init_debug("");
|
|
|
|
log_init(debug_flag);
|
1999-02-14 03:43:21 +08:00
|
|
|
|
1999-10-31 23:43:44 +08:00
|
|
|
log(L_INFO "Launching BIRD " BIRD_VERSION "...");
|
1998-11-28 03:37:07 +08:00
|
|
|
|
|
|
|
debug("Initializing.\n");
|
1998-11-28 05:09:57 +08:00
|
|
|
resource_init();
|
1999-12-10 02:54:20 +08:00
|
|
|
olock_init();
|
1998-05-24 22:50:18 +08:00
|
|
|
io_init();
|
1998-05-20 19:54:33 +08:00
|
|
|
rt_init();
|
1998-05-27 05:42:05 +08:00
|
|
|
if_init();
|
1998-11-28 05:09:57 +08:00
|
|
|
|
1998-10-18 19:53:21 +08:00
|
|
|
protos_build();
|
1998-12-07 01:40:42 +08:00
|
|
|
add_tail(&protocol_list, &proto_unix_kernel.n);
|
1999-03-27 05:44:38 +08:00
|
|
|
add_tail(&protocol_list, &proto_unix_iface.n);
|
1998-11-28 05:09:57 +08:00
|
|
|
|
|
|
|
read_config();
|
1998-05-27 05:42:05 +08:00
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
signal_init();
|
|
|
|
|
1999-10-29 20:10:10 +08:00
|
|
|
cli_init_unix();
|
|
|
|
|
1999-02-12 06:51:15 +08:00
|
|
|
ev_run_list(&global_event_list);
|
1999-02-14 03:43:21 +08:00
|
|
|
async_dump();
|
1998-05-27 05:42:05 +08:00
|
|
|
|
|
|
|
debug("Entering I/O loop.\n");
|
1998-05-15 15:56:13 +08:00
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
io_loop();
|
1998-12-20 22:27:37 +08:00
|
|
|
bug("I/O loop died");
|
1998-05-15 15:56:13 +08:00
|
|
|
}
|