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.
|
|
|
|
*/
|
|
|
|
|
2000-05-05 04:52:28 +08:00
|
|
|
#undef LOCAL_DEBUG
|
2000-03-13 05:01:38 +08:00
|
|
|
|
1998-05-15 15:56:13 +08:00
|
|
|
#include <stdio.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"
|
2000-04-01 07:30:21 +08:00
|
|
|
#include "lib/string.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
|
|
|
|
|
|
|
/*
|
|
|
|
* 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;
|
2000-01-17 01:40:26 +08:00
|
|
|
case CONF_SHUTDOWN:
|
|
|
|
cli_msg(6, "Reconfiguration ignored, shutting down.");
|
|
|
|
break;
|
2000-01-17 00:44:50 +08:00
|
|
|
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;
|
2000-04-28 06:35:08 +08:00
|
|
|
if (sk_send(s, o->wpos - o->outpos) > 0)
|
|
|
|
ev_schedule(c->event);
|
1999-10-29 20:10:10 +08:00
|
|
|
}
|
2000-04-28 06:35:08 +08:00
|
|
|
return !c->tx_pos;
|
1999-10-29 20:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
|
|
|
{
|
2000-05-30 06:10:18 +08:00
|
|
|
if (config->cli_debug)
|
|
|
|
{
|
|
|
|
if (err)
|
|
|
|
log(L_INFO "CLI connection dropped: %s", strerror(err));
|
|
|
|
else
|
|
|
|
log(L_INFO "CLI connection closed");
|
|
|
|
}
|
1999-10-29 20:10:10 +08:00
|
|
|
cli_free(s->data);
|
2000-03-30 18:43:37 +08:00
|
|
|
sk_close(s);
|
1999-10-29 20:10:10 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cli_connect(sock *s, int size)
|
|
|
|
{
|
|
|
|
cli *c;
|
|
|
|
|
2000-05-30 06:10:18 +08:00
|
|
|
if (config->cli_debug)
|
|
|
|
log(L_INFO "CLI connect");
|
1999-10-29 20:10:10 +08:00
|
|
|
s->rx_hook = cli_rx;
|
|
|
|
s->tx_hook = cli_tx;
|
|
|
|
s->err_hook = cli_err;
|
|
|
|
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;
|
2000-04-26 21:26:11 +08:00
|
|
|
s->rbsize = 1024;
|
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)
|
|
|
|
{
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("Shutting down...\n");
|
2000-01-17 01:40:26 +08:00
|
|
|
order_shutdown();
|
1999-02-14 04:15:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-01-17 01:40:26 +08:00
|
|
|
sysdep_shutdown_done(void)
|
1999-02-14 04:15:36 +08:00
|
|
|
{
|
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)
|
|
|
|
{
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("Caught SIGHUP...\n");
|
1999-02-14 03:43:21 +08:00
|
|
|
async_config_flag = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sigusr(int sig)
|
|
|
|
{
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("Caught SIGUSR...\n");
|
1999-02-14 03:43:21 +08:00
|
|
|
async_dump_flag = 1;
|
|
|
|
}
|
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
static void
|
|
|
|
handle_sigterm(int sig)
|
|
|
|
{
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("Caught SIGTERM...\n");
|
1999-02-14 04:15:36 +08:00
|
|
|
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:";
|
2000-05-09 03:10:36 +08:00
|
|
|
|
|
|
|
#ifdef DEBUGGING
|
|
|
|
static int debug_flag = 1;
|
|
|
|
#else
|
|
|
|
static int debug_flag = 0;
|
|
|
|
#endif
|
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;
|
|
|
|
|
2000-05-30 05:58:35 +08:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[1], "--version"))
|
|
|
|
{
|
|
|
|
fprintf(stderr, "BIRD version " BIRD_VERSION "\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
if (!strcmp(argv[1], "--help"))
|
|
|
|
usage();
|
|
|
|
}
|
1999-02-14 03:43:21 +08:00
|
|
|
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-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("");
|
2000-06-09 15:32:57 +08:00
|
|
|
log_init(debug_flag, 1);
|
1999-02-14 03:43:21 +08:00
|
|
|
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("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();
|
2000-04-01 18:19:47 +08:00
|
|
|
proto_build(&proto_unix_kernel);
|
|
|
|
proto_build(&proto_unix_iface);
|
1998-11-28 05:09:57 +08:00
|
|
|
|
|
|
|
read_config();
|
1998-05-27 05:42:05 +08:00
|
|
|
|
2000-05-09 03:10:36 +08:00
|
|
|
if (!debug_flag)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
die("fork: %m");
|
2000-05-09 06:31:34 +08:00
|
|
|
if (pid)
|
2000-05-09 03:10:36 +08:00
|
|
|
return 0;
|
|
|
|
setsid();
|
|
|
|
}
|
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
signal_init();
|
|
|
|
|
1999-10-29 20:10:10 +08:00
|
|
|
cli_init_unix();
|
|
|
|
|
2000-05-05 04:52:28 +08:00
|
|
|
#ifdef LOCAL_DEBUG
|
|
|
|
async_dump_flag = 1;
|
|
|
|
#endif
|
1998-05-27 05:42:05 +08:00
|
|
|
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("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
|
|
|
}
|