1998-05-15 07:56:13 +00:00
|
|
|
/*
|
|
|
|
* BIRD Internet Routing Daemon -- Unix Entry Point
|
|
|
|
*
|
2000-01-16 16:44:50 +00:00
|
|
|
* (c) 1998--2000 Martin Mares <mj@ucw.cz>
|
1998-05-15 07:56:13 +00:00
|
|
|
*
|
|
|
|
* Can be freely distributed and used under the terms of the GNU GPL.
|
|
|
|
*/
|
|
|
|
|
2000-05-04 20:52:28 +00:00
|
|
|
#undef LOCAL_DEBUG
|
2000-03-12 21:01:38 +00:00
|
|
|
|
1998-05-15 07:56:13 +00:00
|
|
|
#include <stdio.h>
|
1999-03-04 11:36:26 +00:00
|
|
|
#include <stdlib.h>
|
1998-11-27 19:37:07 +00:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
2004-05-31 13:25:00 +00:00
|
|
|
#include <signal.h>
|
1998-05-15 07:56:13 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
|
|
|
#include "lib/lists.h"
|
|
|
|
#include "lib/resource.h"
|
1998-05-24 14:50:18 +00:00
|
|
|
#include "lib/socket.h"
|
1999-02-11 22:51:15 +00:00
|
|
|
#include "lib/event.h"
|
2000-03-31 23:30:21 +00:00
|
|
|
#include "lib/string.h"
|
1998-05-15 07:56:13 +00:00
|
|
|
#include "nest/route.h"
|
1998-05-24 14:50:18 +00:00
|
|
|
#include "nest/protocol.h"
|
1998-05-26 21:42:05 +00:00
|
|
|
#include "nest/iface.h"
|
1999-10-29 12:10:10 +00:00
|
|
|
#include "nest/cli.h"
|
1999-12-09 18:54:20 +00:00
|
|
|
#include "nest/locks.h"
|
1998-11-27 19:37:07 +00:00
|
|
|
#include "conf/conf.h"
|
1999-01-15 16:49:17 +00:00
|
|
|
#include "filter/filter.h"
|
1998-05-24 14:50:18 +00:00
|
|
|
|
|
|
|
#include "unix.h"
|
1998-10-18 12:50:43 +00:00
|
|
|
#include "krt.h"
|
1998-05-24 14:50:18 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Debugging
|
|
|
|
*/
|
|
|
|
|
2000-06-19 23:50:02 +00:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
static int debug_flag = 1;
|
|
|
|
#else
|
|
|
|
static int debug_flag = 0;
|
|
|
|
#endif
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
void
|
|
|
|
async_dump(void)
|
1998-05-24 14:50:18 +00:00
|
|
|
{
|
1999-02-13 19:43:21 +00:00
|
|
|
debug("INTERNAL STATE DUMP\n\n");
|
1998-05-24 14:50:18 +00:00
|
|
|
|
1999-03-29 20:14:33 +00:00
|
|
|
rdump(&root_pool);
|
1998-05-24 14:50:18 +00:00
|
|
|
sk_dump_all();
|
|
|
|
tm_dump_all();
|
1998-05-26 21:42:05 +00:00
|
|
|
if_dump_all();
|
1998-06-01 21:41:32 +00:00
|
|
|
neigh_dump_all();
|
1998-05-24 14:50:18 +00:00
|
|
|
rta_dump_all();
|
|
|
|
rt_dump_all();
|
1998-07-09 19:37:39 +00:00
|
|
|
protos_dump_all();
|
1998-05-24 14:50:18 +00:00
|
|
|
|
|
|
|
debug("\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
1998-11-27 19:37:07 +00:00
|
|
|
* Reading the Configuration
|
1998-05-24 14:50:18 +00:00
|
|
|
*/
|
|
|
|
|
1998-11-27 19:37:07 +00:00
|
|
|
static int conf_fd;
|
1999-02-13 19:43:21 +00:00
|
|
|
static char *config_name = PATH_CONFIG;
|
1998-11-27 19:37:07 +00: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 13:45:56 +00:00
|
|
|
void
|
|
|
|
sysdep_preconfig(struct config *c)
|
|
|
|
{
|
|
|
|
init_list(&c->logfiles);
|
|
|
|
}
|
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
int
|
|
|
|
sysdep_commit(struct config *new, struct config *old)
|
1999-12-06 13:45:56 +00:00
|
|
|
{
|
2000-06-19 23:50:02 +00:00
|
|
|
log_switch(debug_flag, &new->logfiles);
|
2000-01-16 16:44:50 +00:00
|
|
|
return 0;
|
1999-12-06 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
static int
|
|
|
|
unix_read_config(struct config **cp, char *name)
|
1998-11-27 19:37:07 +00:00
|
|
|
{
|
2000-01-16 16:44:50 +00:00
|
|
|
struct config *conf = config_alloc(name);
|
1999-02-05 21:37:34 +00:00
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
*cp = conf;
|
|
|
|
conf_fd = open(name, O_RDONLY);
|
1998-11-27 19:37:07 +00:00
|
|
|
if (conf_fd < 0)
|
2000-01-16 16:44:50 +00:00
|
|
|
return 0;
|
1998-11-27 19:37:07 +00:00
|
|
|
cf_read_hook = cf_read;
|
2000-01-16 16:44:50 +00: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-05 21:37:34 +00:00
|
|
|
config_commit(conf);
|
1998-11-27 19:37:07 +00:00
|
|
|
}
|
1999-02-05 21:37:34 +00:00
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
void
|
|
|
|
async_config(void)
|
|
|
|
{
|
2000-01-16 16:44:50 +00: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-16 17:40:26 +00:00
|
|
|
case CONF_SHUTDOWN:
|
|
|
|
cli_msg(6, "Reconfiguration ignored, shutting down.");
|
|
|
|
break;
|
2000-01-16 16:44:50 +00:00
|
|
|
default:
|
|
|
|
cli_msg(5, "Reconfiguration already in progress, queueing new config");
|
|
|
|
}
|
|
|
|
}
|
1999-02-13 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
/*
|
|
|
|
* Command-Line Interface
|
|
|
|
*/
|
|
|
|
|
|
|
|
static sock *cli_sk;
|
1999-12-08 13:20:19 +00:00
|
|
|
static char *path_control_socket = PATH_CONTROL_SOCKET;
|
1999-10-29 12:10:10 +00:00
|
|
|
|
|
|
|
int
|
|
|
|
cli_write(cli *c)
|
|
|
|
{
|
|
|
|
sock *s = c->priv;
|
|
|
|
|
|
|
|
if (c->tx_pos)
|
|
|
|
{
|
|
|
|
struct cli_out *o = c->tx_pos;
|
|
|
|
s->tbuf = o->outpos;
|
2000-04-27 22:35:08 +00:00
|
|
|
if (sk_send(s, o->wpos - o->outpos) > 0)
|
2004-05-31 17:55:30 +00:00
|
|
|
{
|
|
|
|
c->tx_pos = o->next;
|
|
|
|
ev_schedule(c->event);
|
|
|
|
}
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
2000-04-27 22:35:08 +00:00
|
|
|
return !c->tx_pos;
|
1999-10-29 12:10:10 +00: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-29 22:10:18 +00: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 12:10:10 +00:00
|
|
|
cli_free(s->data);
|
2000-03-30 10:43:37 +00:00
|
|
|
sk_close(s);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
|
|
|
cli_connect(sock *s, int size)
|
|
|
|
{
|
|
|
|
cli *c;
|
|
|
|
|
2000-05-29 22:10:18 +00:00
|
|
|
if (config->cli_debug)
|
|
|
|
log(L_INFO "CLI connect");
|
1999-10-29 12:10:10 +00: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 13:20:19 +00:00
|
|
|
s->pool = c->pool; /* We need to have all the socket buffers allocated in the cli pool */
|
1999-10-29 12:10:10 +00: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 13:26:11 +00:00
|
|
|
s->rbsize = 1024;
|
1999-12-08 13:20:19 +00:00
|
|
|
if (sk_open_unix(s, path_control_socket) < 0)
|
|
|
|
die("Unable to create control socket %s", path_control_socket);
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
1999-02-13 20:15:36 +00:00
|
|
|
/*
|
|
|
|
* Shutdown
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
async_shutdown(void)
|
|
|
|
{
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Shutting down...\n");
|
2000-01-16 17:40:26 +00:00
|
|
|
order_shutdown();
|
1999-02-13 20:15:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2000-01-16 17:40:26 +00:00
|
|
|
sysdep_shutdown_done(void)
|
1999-02-13 20:15:36 +00:00
|
|
|
{
|
1999-10-29 12:10:10 +00:00
|
|
|
unlink(PATH_CONTROL_SOCKET);
|
1999-02-13 20:15:36 +00:00
|
|
|
die("System shutdown completed");
|
|
|
|
}
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
/*
|
|
|
|
* Signals
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sighup(int sig)
|
|
|
|
{
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Caught SIGHUP...\n");
|
1999-02-13 19:43:21 +00:00
|
|
|
async_config_flag = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
handle_sigusr(int sig)
|
|
|
|
{
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Caught SIGUSR...\n");
|
1999-02-13 19:43:21 +00:00
|
|
|
async_dump_flag = 1;
|
|
|
|
}
|
|
|
|
|
1999-02-13 20:15:36 +00:00
|
|
|
static void
|
|
|
|
handle_sigterm(int sig)
|
|
|
|
{
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Caught SIGTERM...\n");
|
1999-02-13 20:15:36 +00:00
|
|
|
async_shutdown_flag = 1;
|
|
|
|
}
|
|
|
|
|
1999-02-13 19:43:21 +00: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-13 20:15:36 +00:00
|
|
|
sa.sa_handler = handle_sigterm;
|
|
|
|
sa.sa_flags = SA_RESTART;
|
|
|
|
sigaction(SIGTERM, &sa, NULL);
|
1999-02-13 19:43:21 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parsing of command-line arguments
|
|
|
|
*/
|
|
|
|
|
1999-12-08 13:20:19 +00:00
|
|
|
static char *opt_list = "c:dD:s:";
|
2000-05-08 19:10:36 +00:00
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
1999-12-08 13:20:19 +00:00
|
|
|
fprintf(stderr, "Usage: bird [-c <config-file>] [-d] [-D <debug-file>] [-s <control-socket>]\n");
|
1999-02-13 19:43:21 +00:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2000-05-29 21:58:35 +00: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-13 19:43:21 +00:00
|
|
|
while ((c = getopt(argc, argv, opt_list)) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
config_name = optarg;
|
|
|
|
break;
|
|
|
|
case 'd':
|
1999-12-06 13:45:56 +00:00
|
|
|
debug_flag |= 1;
|
|
|
|
break;
|
|
|
|
case 'D':
|
1999-02-13 19:43:21 +00:00
|
|
|
log_init_debug(optarg);
|
1999-12-06 13:45:56 +00:00
|
|
|
debug_flag |= 2;
|
1999-02-13 19:43:21 +00:00
|
|
|
break;
|
1999-12-08 13:20:19 +00:00
|
|
|
case 's':
|
|
|
|
path_control_socket = optarg;
|
|
|
|
break;
|
1999-02-13 19:43:21 +00:00
|
|
|
default:
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
if (optind < argc)
|
|
|
|
usage();
|
|
|
|
}
|
|
|
|
|
1998-06-03 08:43:44 +00:00
|
|
|
/*
|
|
|
|
* Hic Est main()
|
|
|
|
*/
|
1998-05-15 07:56:13 +00:00
|
|
|
|
|
|
|
int
|
1999-02-13 19:43:21 +00:00
|
|
|
main(int argc, char **argv)
|
1998-05-15 07:56:13 +00:00
|
|
|
{
|
1999-03-04 11:36:26 +00:00
|
|
|
#ifdef HAVE_LIBDMALLOC
|
|
|
|
if (!getenv("DMALLOC_OPTIONS"))
|
|
|
|
dmalloc_debug(0x2f03d00);
|
|
|
|
#endif
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
parse_args(argc, argv);
|
1999-12-06 13:45:56 +00:00
|
|
|
if (debug_flag == 1)
|
|
|
|
log_init_debug("");
|
2000-06-09 07:32:57 +00:00
|
|
|
log_init(debug_flag, 1);
|
1999-02-13 19:43:21 +00:00
|
|
|
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Initializing.\n");
|
1998-11-27 21:09:57 +00:00
|
|
|
resource_init();
|
1999-12-09 18:54:20 +00:00
|
|
|
olock_init();
|
1998-05-24 14:50:18 +00:00
|
|
|
io_init();
|
1998-05-20 11:54:33 +00:00
|
|
|
rt_init();
|
1998-05-26 21:42:05 +00:00
|
|
|
if_init();
|
1998-11-27 21:09:57 +00:00
|
|
|
|
1998-10-18 11:53:21 +00:00
|
|
|
protos_build();
|
2000-04-01 10:19:47 +00:00
|
|
|
proto_build(&proto_unix_kernel);
|
|
|
|
proto_build(&proto_unix_iface);
|
1998-11-27 21:09:57 +00:00
|
|
|
|
|
|
|
read_config();
|
1998-05-26 21:42:05 +00:00
|
|
|
|
2000-05-08 19:10:36 +00:00
|
|
|
if (!debug_flag)
|
|
|
|
{
|
|
|
|
pid_t pid = fork();
|
|
|
|
if (pid < 0)
|
|
|
|
die("fork: %m");
|
2000-05-08 22:31:34 +00:00
|
|
|
if (pid)
|
2000-05-08 19:10:36 +00:00
|
|
|
return 0;
|
|
|
|
setsid();
|
|
|
|
}
|
|
|
|
|
1998-05-24 14:50:18 +00:00
|
|
|
signal_init();
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
cli_init_unix();
|
|
|
|
|
2000-05-04 20:52:28 +00:00
|
|
|
#ifdef LOCAL_DEBUG
|
|
|
|
async_dump_flag = 1;
|
|
|
|
#endif
|
1998-05-26 21:42:05 +00:00
|
|
|
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Entering I/O loop.\n");
|
1998-05-15 07:56:13 +00:00
|
|
|
|
1998-05-24 14:50:18 +00:00
|
|
|
io_loop();
|
1998-12-20 14:27:37 +00:00
|
|
|
bug("I/O loop died");
|
1998-05-15 07:56:13 +00:00
|
|
|
}
|