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
|
|
|
|
2011-05-10 08:42:17 +08:00
|
|
|
#define _GNU_SOURCE 1
|
|
|
|
|
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>
|
2004-05-31 21:25:00 +08:00
|
|
|
#include <signal.h>
|
2011-05-10 08:42:17 +08:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2011-08-17 05:05:35 +08:00
|
|
|
#include <sys/stat.h>
|
2011-09-12 03:21:47 +08:00
|
|
|
#include <libgen.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
|
|
|
|
*/
|
|
|
|
|
2000-06-20 07:50:02 +08:00
|
|
|
#ifdef DEBUGGING
|
|
|
|
static int debug_flag = 1;
|
|
|
|
#else
|
|
|
|
static int debug_flag = 0;
|
|
|
|
#endif
|
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2011-05-10 08:42:17 +08:00
|
|
|
/*
|
|
|
|
* Dropping privileges
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RESTRICTED_PRIVILEGES
|
|
|
|
#include "lib/syspriv.h"
|
|
|
|
#else
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
drop_uid(uid_t uid)
|
|
|
|
{
|
|
|
|
die("Cannot change user on this platform");
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
drop_gid(gid_t gid)
|
|
|
|
{
|
|
|
|
if (setgid(gid) < 0)
|
|
|
|
die("setgid: %m");
|
|
|
|
}
|
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
/*
|
1998-11-28 03:37:07 +08:00
|
|
|
* Reading the Configuration
|
1998-05-24 22:50:18 +08:00
|
|
|
*/
|
|
|
|
|
2011-04-28 06:31:37 +08:00
|
|
|
#ifdef PATH_IPROUTE_DIR
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
add_num_const(char *name, int val)
|
|
|
|
{
|
|
|
|
struct symbol *s = cf_find_symbol(name);
|
|
|
|
s->class = SYM_NUMBER;
|
|
|
|
s->def = NULL;
|
|
|
|
s->aux = val;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* the code of read_iproute_table() is based on
|
|
|
|
rtnl_tab_initialize() from iproute2 package */
|
|
|
|
static void
|
|
|
|
read_iproute_table(char *file, char *prefix, int max)
|
|
|
|
{
|
|
|
|
char buf[512], namebuf[512];
|
|
|
|
char *name;
|
|
|
|
int val;
|
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
strcpy(namebuf, prefix);
|
|
|
|
name = namebuf + strlen(prefix);
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
|
|
|
while (fgets(buf, sizeof(buf), fp))
|
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '#' || *p == '\n' || *p == 0)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (sscanf(p, "0x%x %s\n", &val, name) != 2 &&
|
|
|
|
sscanf(p, "0x%x %s #", &val, name) != 2 &&
|
|
|
|
sscanf(p, "%d %s\n", &val, name) != 2 &&
|
|
|
|
sscanf(p, "%d %s #", &val, name) != 2)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
if (val < 0 || val > max)
|
|
|
|
continue;
|
|
|
|
|
|
|
|
for(p = name; *p; p++)
|
|
|
|
if ((*p < 'a' || *p > 'z') && (*p < '0' || *p > '9') && (*p != '_'))
|
|
|
|
*p = '_';
|
|
|
|
|
|
|
|
add_num_const(namebuf, val);
|
|
|
|
}
|
|
|
|
|
|
|
|
fclose(fp);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif // PATH_IPROUTE_DIR
|
|
|
|
|
|
|
|
|
2012-05-03 18:25:15 +08:00
|
|
|
static char *config_name = PATH_CONFIG_FILE;
|
1998-11-28 03:37:07 +08:00
|
|
|
|
|
|
|
static int
|
2011-09-12 03:21:47 +08:00
|
|
|
cf_read(byte *dest, unsigned int len, int fd)
|
1998-11-28 03:37:07 +08:00
|
|
|
{
|
2011-09-12 03:21:47 +08:00
|
|
|
int l = read(fd, dest, len);
|
1998-11-28 03:37:07 +08:00
|
|
|
if (l < 0)
|
|
|
|
cf_error("Read error");
|
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2011-09-12 03:21:47 +08:00
|
|
|
static int
|
|
|
|
cf_open(char *filename)
|
|
|
|
{
|
|
|
|
char full_name[BIRD_FNAME_MAX];
|
|
|
|
char *cur = filename;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (*filename != '/') {
|
2012-03-19 20:00:00 +08:00
|
|
|
char dir[BIRD_FNAME_MAX];
|
|
|
|
strncpy(dir, config_name, sizeof(dir));
|
|
|
|
dir[sizeof(dir)-1] = 0;
|
|
|
|
snprintf(full_name, sizeof(full_name), "%s/%s", dirname(dir), filename);
|
2011-10-10 07:01:58 +08:00
|
|
|
full_name[sizeof(full_name)-1] = 0;
|
2011-09-12 03:21:47 +08:00
|
|
|
cur = full_name;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((ret = open(cur, O_RDONLY)) == -1)
|
|
|
|
cf_error("Unable to open included configuration file: %s", cur);
|
2011-10-10 07:01:58 +08:00
|
|
|
|
|
|
|
return ret;
|
2011-09-12 03:21:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-12-06 21:45:56 +08:00
|
|
|
void
|
|
|
|
sysdep_preconfig(struct config *c)
|
|
|
|
{
|
|
|
|
init_list(&c->logfiles);
|
2011-04-28 06:31:37 +08:00
|
|
|
|
|
|
|
#ifdef PATH_IPROUTE_DIR
|
2012-03-23 07:26:26 +08:00
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_protos", "ipp_", 256);
|
2011-04-28 06:31:37 +08:00
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_realms", "ipr_", 256);
|
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_scopes", "ips_", 256);
|
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_tables", "ipt_", 256);
|
|
|
|
#endif
|
1999-12-06 21:45:56 +08:00
|
|
|
}
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
int
|
2004-06-05 17:11:07 +08:00
|
|
|
sysdep_commit(struct config *new, struct config *old UNUSED)
|
1999-12-06 21:45:56 +08:00
|
|
|
{
|
2010-04-07 17:00:36 +08:00
|
|
|
log_switch(debug_flag, &new->logfiles, new->syslog_name);
|
2000-01-17 00:44:50 +08:00
|
|
|
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);
|
2008-08-25 07:24:14 +08:00
|
|
|
int ret;
|
1999-02-06 05:37:34 +08:00
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
*cp = conf;
|
2011-09-12 03:21:47 +08:00
|
|
|
conf->file_fd = open(name, O_RDONLY);
|
|
|
|
if (conf->file_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;
|
2011-09-12 03:21:47 +08:00
|
|
|
cf_open_hook = cf_open;
|
2008-08-25 07:24:14 +08:00
|
|
|
ret = config_parse(conf);
|
2011-09-12 03:21:47 +08:00
|
|
|
close(conf->file_fd);
|
2008-08-25 07:24:14 +08:00
|
|
|
return ret;
|
2000-01-17 00:44:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
read_config(void)
|
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2011-09-12 03:21:47 +08:00
|
|
|
die("%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
|
2000-01-17 00:44:50 +08:00
|
|
|
else
|
|
|
|
die("Unable to open configuration file %s: %m", config_name);
|
|
|
|
}
|
2009-06-20 05:49:34 +08:00
|
|
|
config_commit(conf, RECONFIG_HARD);
|
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)
|
2011-09-12 03:21:47 +08:00
|
|
|
log(L_ERR "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
|
2000-01-17 00:44:50 +08:00
|
|
|
else
|
|
|
|
log(L_ERR "Unable to open configuration file %s: %m", config_name);
|
|
|
|
config_free(conf);
|
|
|
|
}
|
|
|
|
else
|
2009-06-20 05:49:34 +08:00
|
|
|
config_commit(conf, RECONFIG_HARD);
|
2000-01-17 00:44:50 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2009-06-20 05:49:34 +08:00
|
|
|
cmd_reconfig(char *name, int type)
|
2000-01-17 00:44:50 +08:00
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
2010-02-21 16:57:26 +08:00
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
if (!name)
|
|
|
|
name = config_name;
|
|
|
|
cli_msg(-2, "Reading configuration from %s", name);
|
|
|
|
if (!unix_read_config(&conf, name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2012-04-09 20:19:28 +08:00
|
|
|
cli_msg(8002, "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
|
2000-01-17 00:44:50 +08:00
|
|
|
else
|
|
|
|
cli_msg(8002, "%s: %m", name);
|
|
|
|
config_free(conf);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-06-20 05:49:34 +08:00
|
|
|
switch (config_commit(conf, type))
|
2000-01-17 00:44:50 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
|
2009-07-14 20:18:54 +08:00
|
|
|
|
|
|
|
static void
|
1999-10-29 20:10:10 +08:00
|
|
|
cli_write(cli *c)
|
|
|
|
{
|
|
|
|
sock *s = c->priv;
|
|
|
|
|
2009-07-14 20:18:54 +08:00
|
|
|
while (c->tx_pos)
|
1999-10-29 20:10:10 +08:00
|
|
|
{
|
|
|
|
struct cli_out *o = c->tx_pos;
|
2009-07-14 20:18:54 +08:00
|
|
|
|
|
|
|
int len = o->wpos - o->outpos;
|
1999-10-29 20:10:10 +08:00
|
|
|
s->tbuf = o->outpos;
|
2009-07-14 20:18:54 +08:00
|
|
|
o->outpos = o->wpos;
|
|
|
|
|
|
|
|
if (sk_send(s, len) <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
c->tx_pos = o->next;
|
1999-10-29 20:10:10 +08:00
|
|
|
}
|
2009-07-14 20:18:54 +08:00
|
|
|
|
|
|
|
/* Everything is written */
|
|
|
|
s->tbuf = NULL;
|
|
|
|
cli_written(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cli_write_trigger(cli *c)
|
|
|
|
{
|
|
|
|
sock *s = c->priv;
|
|
|
|
|
|
|
|
if (s->tbuf == NULL)
|
|
|
|
cli_write(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cli_tx(sock *s)
|
|
|
|
{
|
|
|
|
cli_write(s->data);
|
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
|
2004-06-05 17:11:07 +08:00
|
|
|
cli_rx(sock *s, int size UNUSED)
|
1999-10-29 20:10:10 +08:00
|
|
|
{
|
|
|
|
cli_kick(s->data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2004-06-05 17:11:07 +08:00
|
|
|
cli_connect(sock *s, int size UNUSED)
|
1999-10-29 20:10:10 +08:00
|
|
|
{
|
|
|
|
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;
|
2004-06-01 06:00:18 +08:00
|
|
|
rmove(s, c->pool);
|
1999-10-29 20:10:10 +08:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-15 22:29:44 +08:00
|
|
|
cli_init_unix(uid_t use_uid, gid_t use_gid)
|
1999-10-29 20:10:10 +08:00
|
|
|
{
|
|
|
|
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;
|
2010-03-17 19:19:22 +08:00
|
|
|
sk_open_unix(s, path_control_socket);
|
2011-05-15 22:29:44 +08:00
|
|
|
|
|
|
|
if (use_uid || use_gid)
|
|
|
|
if (chown(path_control_socket, use_uid, use_gid) < 0)
|
|
|
|
die("chown: %m");
|
|
|
|
|
|
|
|
if (chmod(path_control_socket, 0660) < 0)
|
|
|
|
die("chmod: %m");
|
1999-10-29 20:10:10 +08:00
|
|
|
}
|
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
/*
|
|
|
|
* Shutdown
|
|
|
|
*/
|
|
|
|
|
2010-02-21 16:57:26 +08:00
|
|
|
void
|
|
|
|
cmd_shutdown(void)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cli_msg(7, "Shutdown requested");
|
|
|
|
order_shutdown();
|
|
|
|
}
|
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
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
|
|
|
{
|
2009-11-13 21:43:29 +08:00
|
|
|
unlink(path_control_socket);
|
2010-02-07 05:57:51 +08:00
|
|
|
log_msg(L_FATAL "Shutdown completed");
|
2009-11-13 21:54:43 +08:00
|
|
|
exit(0);
|
1999-02-14 04:15:36 +08:00
|
|
|
}
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
/*
|
|
|
|
* Signals
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2004-06-05 17:11:07 +08:00
|
|
|
handle_sighup(int sig UNUSED)
|
1999-02-14 03:43:21 +08:00
|
|
|
{
|
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
|
2004-06-05 17:11:07 +08:00
|
|
|
handle_sigusr(int sig UNUSED)
|
1999-02-14 03:43:21 +08:00
|
|
|
{
|
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
|
2004-06-05 17:11:07 +08:00
|
|
|
handle_sigterm(int sig UNUSED)
|
1999-02-14 04:15:36 +08:00
|
|
|
{
|
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
|
|
|
|
*/
|
|
|
|
|
2011-05-10 08:42:17 +08:00
|
|
|
static char *opt_list = "c:dD:ps:u:g:";
|
2010-04-07 17:00:36 +08:00
|
|
|
static int parse_and_exit;
|
|
|
|
char *bird_name;
|
2011-05-10 08:42:17 +08:00
|
|
|
static char *use_user;
|
|
|
|
static char *use_group;
|
2000-05-09 03:10:36 +08:00
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
2011-05-10 08:42:17 +08:00
|
|
|
fprintf(stderr, "Usage: %s [-c <config-file>] [-d] [-D <debug-file>] [-p] [-s <control-socket>] [-u <user>] [-g <group>]\n", bird_name);
|
1999-02-14 03:43:21 +08:00
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2010-04-07 17:00:36 +08:00
|
|
|
static inline char *
|
|
|
|
get_bird_name(char *s, char *def)
|
|
|
|
{
|
|
|
|
char *t;
|
|
|
|
if (!s)
|
|
|
|
return def;
|
|
|
|
t = strrchr(s, '/');
|
|
|
|
if (!t)
|
|
|
|
return s;
|
|
|
|
if (!t[1])
|
|
|
|
return def;
|
|
|
|
return t+1;
|
|
|
|
}
|
2009-11-19 18:44:17 +08:00
|
|
|
|
2011-05-10 08:42:17 +08:00
|
|
|
static inline uid_t
|
|
|
|
get_uid(const char *s)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
char *endptr;
|
2011-05-15 22:29:44 +08:00
|
|
|
long int rv;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
|
2011-05-10 08:42:17 +08:00
|
|
|
errno = 0;
|
2011-05-15 22:29:44 +08:00
|
|
|
rv = strtol(s, &endptr, 10);
|
2011-05-10 08:42:17 +08:00
|
|
|
|
|
|
|
if (!errno && !*endptr)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
pw = getpwnam(s);
|
|
|
|
if (!pw)
|
|
|
|
die("Cannot find user '%s'", s);
|
|
|
|
|
|
|
|
return pw->pw_uid;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline gid_t
|
|
|
|
get_gid(const char *s)
|
|
|
|
{
|
|
|
|
struct group *gr;
|
|
|
|
char *endptr;
|
2011-05-15 22:29:44 +08:00
|
|
|
long int rv;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
2011-05-10 08:42:17 +08:00
|
|
|
|
|
|
|
errno = 0;
|
2011-05-15 22:29:44 +08:00
|
|
|
rv = strtol(s, &endptr, 10);
|
2011-05-10 08:42:17 +08:00
|
|
|
|
|
|
|
if (!errno && !*endptr)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
gr = getgrnam(s);
|
|
|
|
if (!gr)
|
|
|
|
die("Cannot find group '%s'", s);
|
|
|
|
|
|
|
|
return gr->gr_gid;
|
|
|
|
}
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
static void
|
|
|
|
parse_args(int argc, char **argv)
|
|
|
|
{
|
|
|
|
int c;
|
|
|
|
|
2010-04-07 17:00:36 +08:00
|
|
|
bird_name = get_bird_name(argv[0], "bird");
|
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;
|
2009-11-19 18:44:17 +08:00
|
|
|
case 'p':
|
|
|
|
parse_and_exit = 1;
|
|
|
|
break;
|
1999-12-08 21:20:19 +08:00
|
|
|
case 's':
|
|
|
|
path_control_socket = optarg;
|
|
|
|
break;
|
2011-05-10 08:42:17 +08:00
|
|
|
case 'u':
|
|
|
|
use_user = optarg;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
use_group = 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("");
|
2010-04-07 17:00:36 +08:00
|
|
|
log_switch(debug_flag, NULL, NULL);
|
1999-02-14 03:43:21 +08:00
|
|
|
|
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();
|
2012-03-19 00:32:30 +08:00
|
|
|
roa_init();
|
1998-11-28 05:09:57 +08:00
|
|
|
|
2011-05-15 22:29:44 +08:00
|
|
|
uid_t use_uid = get_uid(use_user);
|
|
|
|
gid_t use_gid = get_gid(use_group);
|
|
|
|
|
2010-03-17 19:19:22 +08:00
|
|
|
if (!parse_and_exit)
|
2011-05-15 22:29:44 +08:00
|
|
|
{
|
|
|
|
test_old_bird(path_control_socket);
|
|
|
|
cli_init_unix(use_uid, use_gid);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (use_gid)
|
|
|
|
drop_gid(use_gid);
|
|
|
|
|
|
|
|
if (use_uid)
|
|
|
|
drop_uid(use_uid);
|
2010-03-17 19:19:22 +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
|
|
|
|
2009-11-19 18:44:17 +08:00
|
|
|
if (parse_and_exit)
|
|
|
|
exit(0);
|
|
|
|
|
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();
|
2007-06-20 15:33:26 +08:00
|
|
|
close(0);
|
|
|
|
if (open("/dev/null", O_RDWR) < 0)
|
|
|
|
die("Cannot open /dev/null: %m");
|
|
|
|
dup2(0, 1);
|
|
|
|
dup2(0, 2);
|
2000-05-09 03:10:36 +08:00
|
|
|
}
|
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
signal_init();
|
|
|
|
|
2000-05-05 04:52:28 +08:00
|
|
|
#ifdef LOCAL_DEBUG
|
|
|
|
async_dump_flag = 1;
|
|
|
|
#endif
|
1998-05-27 05:42:05 +08:00
|
|
|
|
2010-02-07 05:57:51 +08:00
|
|
|
log(L_INFO "Started");
|
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
|
|
|
}
|