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
|
|
|
|
2016-11-08 16:46:29 +00:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2011-05-10 00:42:17 +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>
|
2011-05-10 00:42:17 +00:00
|
|
|
#include <pwd.h>
|
|
|
|
#include <grp.h>
|
2011-08-16 21:05:35 +00:00
|
|
|
#include <sys/stat.h>
|
2011-09-11 19:21:47 +00:00
|
|
|
#include <libgen.h>
|
1998-05-15 07:56:13 +00:00
|
|
|
|
|
|
|
#include "nest/bird.h"
|
2017-07-18 21:01:06 +00:00
|
|
|
#include "lib/coroutine.h"
|
1998-05-15 07:56:13 +00:00
|
|
|
#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");
|
|
|
|
}
|
|
|
|
|
2011-05-10 00:42:17 +00:00
|
|
|
/*
|
|
|
|
* Dropping privileges
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifdef CONFIG_RESTRICTED_PRIVILEGES
|
2016-04-12 09:14:54 +00:00
|
|
|
#include CONFIG_INCLUDE_SYSPRIV_H
|
2011-05-10 00:42:17 +00:00
|
|
|
#else
|
|
|
|
|
|
|
|
static inline void
|
2016-10-14 13:37:04 +00:00
|
|
|
drop_uid(uid_t uid UNUSED)
|
2011-05-10 00:42:17 +00:00
|
|
|
{
|
|
|
|
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 14:50:18 +00:00
|
|
|
/*
|
1998-11-27 19:37:07 +00:00
|
|
|
* Reading the Configuration
|
1998-05-24 14:50:18 +00:00
|
|
|
*/
|
|
|
|
|
2011-04-27 22:31:37 +00:00
|
|
|
#ifdef PATH_IPROUTE_DIR
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
add_num_const(char *name, int val)
|
|
|
|
{
|
2015-11-08 23:42:02 +00:00
|
|
|
struct symbol *s = cf_get_symbol(name);
|
2013-07-25 20:33:57 +00:00
|
|
|
s->class = SYM_CONSTANT | T_INT;
|
|
|
|
s->def = cfg_allocz(sizeof(struct f_val));
|
|
|
|
SYM_TYPE(s) = T_INT;
|
|
|
|
SYM_VAL(s).i = val;
|
2011-04-27 22:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* 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 10:25:15 +00:00
|
|
|
static char *config_name = PATH_CONFIG_FILE;
|
1998-11-27 19:37:07 +00:00
|
|
|
|
|
|
|
static int
|
2015-05-19 06:53:34 +00:00
|
|
|
cf_read(byte *dest, uint len, int fd)
|
1998-11-27 19:37:07 +00:00
|
|
|
{
|
2011-09-11 19:21:47 +00:00
|
|
|
int l = read(fd, dest, len);
|
1998-11-27 19:37:07 +00:00
|
|
|
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);
|
2011-04-27 22:31:37 +00:00
|
|
|
|
2015-03-02 08:41:14 +00:00
|
|
|
c->latency_limit = UNIX_DEFAULT_LATENCY_LIMIT;
|
|
|
|
c->watchdog_warning = UNIX_DEFAULT_WATCHDOG_WARNING;
|
|
|
|
|
2011-04-27 22:31:37 +00:00
|
|
|
#ifdef PATH_IPROUTE_DIR
|
2012-03-22 23:26:26 +00:00
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_protos", "ipp_", 256);
|
2011-04-27 22:31:37 +00: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 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
int
|
2004-06-05 09:11:07 +00:00
|
|
|
sysdep_commit(struct config *new, struct config *old UNUSED)
|
1999-12-06 13:45:56 +00:00
|
|
|
{
|
2010-04-07 09:00:36 +00:00
|
|
|
log_switch(debug_flag, &new->logfiles, new->syslog_name);
|
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);
|
2008-08-24 23:24:14 +00:00
|
|
|
int ret;
|
1999-02-05 21:37:34 +00:00
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
*cp = conf;
|
2011-09-11 19:21:47 +00:00
|
|
|
conf->file_fd = open(name, O_RDONLY);
|
|
|
|
if (conf->file_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;
|
2008-08-24 23:24:14 +00:00
|
|
|
ret = config_parse(conf);
|
2011-09-11 19:21:47 +00:00
|
|
|
close(conf->file_fd);
|
2008-08-24 23:24:14 +00:00
|
|
|
return ret;
|
2000-01-16 16:44:50 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
static struct config *
|
2000-01-16 16:44:50 +00:00
|
|
|
read_config(void)
|
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2011-09-11 19:21:47 +00:00
|
|
|
die("%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
|
2000-01-16 16:44:50 +00:00
|
|
|
else
|
|
|
|
die("Unable to open configuration file %s: %m", config_name);
|
|
|
|
}
|
2013-10-05 18:12:28 +00:00
|
|
|
|
|
|
|
return 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)
|
2011-09-11 19:21:47 +00:00
|
|
|
log(L_ERR "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
|
2000-01-16 16:44:50 +00:00
|
|
|
else
|
|
|
|
log(L_ERR "Unable to open configuration file %s: %m", config_name);
|
|
|
|
config_free(conf);
|
|
|
|
}
|
|
|
|
else
|
2012-12-26 11:40:48 +00:00
|
|
|
config_commit(conf, RECONFIG_HARD, 0);
|
2000-01-16 16:44:50 +00:00
|
|
|
}
|
|
|
|
|
2012-12-26 11:40:48 +00:00
|
|
|
static struct config *
|
|
|
|
cmd_read_config(char *name)
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
name = config_name;
|
2012-12-26 11:40:48 +00:00
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
cli_msg(-2, "Reading configuration from %s", name);
|
|
|
|
if (!unix_read_config(&conf, name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2012-04-09 12:19:28 +00:00
|
|
|
cli_msg(8002, "%s, line %d: %s", conf->err_file_name, conf->err_lino, conf->err_msg);
|
2000-01-16 16:44:50 +00:00
|
|
|
else
|
|
|
|
cli_msg(8002, "%s: %m", name);
|
|
|
|
config_free(conf);
|
2012-12-26 11:40:48 +00:00
|
|
|
conf = NULL;
|
2000-01-16 16:44:50 +00:00
|
|
|
}
|
2012-12-26 11:40:48 +00:00
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_check_config(char *name)
|
|
|
|
{
|
|
|
|
struct config *conf = cmd_read_config(name);
|
|
|
|
if (!conf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
cli_msg(20, "Configuration OK");
|
|
|
|
config_free(conf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cmd_reconfig_msg(int r)
|
|
|
|
{
|
|
|
|
switch (r)
|
2000-01-16 16:44:50 +00:00
|
|
|
{
|
2012-12-26 11:40:48 +00:00
|
|
|
case CONF_DONE: cli_msg( 3, "Reconfigured"); break;
|
|
|
|
case CONF_PROGRESS: cli_msg( 4, "Reconfiguration in progress"); break;
|
|
|
|
case CONF_QUEUED: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
|
|
|
|
case CONF_UNQUEUED: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
|
|
|
|
case CONF_CONFIRM: cli_msg(18, "Reconfiguration confirmed"); break;
|
|
|
|
case CONF_SHUTDOWN: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
|
|
|
|
case CONF_NOTHING: cli_msg(19, "Nothing to do"); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Hack for scheduled undo notification */
|
|
|
|
cli *cmd_reconfig_stored_cli;
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_reconfig_undo_notify(void)
|
|
|
|
{
|
|
|
|
if (cmd_reconfig_stored_cli)
|
|
|
|
{
|
|
|
|
cli *c = cmd_reconfig_stored_cli;
|
|
|
|
cli_printf(c, CLI_ASYNC_CODE, "Config timeout expired, starting undo");
|
|
|
|
cli_write_trigger(c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_reconfig(char *name, int type, int timeout)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
struct config *conf = cmd_read_config(name);
|
|
|
|
if (!conf)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int r = config_commit(conf, type, timeout);
|
|
|
|
|
|
|
|
if ((r >= 0) && (timeout > 0))
|
|
|
|
{
|
|
|
|
cmd_reconfig_stored_cli = this_cli;
|
|
|
|
cli_msg(-22, "Undo scheduled in %d s", timeout);
|
2000-01-16 16:44:50 +00:00
|
|
|
}
|
2012-12-26 11:40:48 +00:00
|
|
|
|
|
|
|
cmd_reconfig_msg(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_reconfig_confirm(void)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
int r = config_confirm();
|
|
|
|
cmd_reconfig_msg(r);
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
cmd_reconfig_undo(void)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cli_msg(-21, "Undo requested");
|
|
|
|
|
|
|
|
int r = config_undo();
|
|
|
|
cmd_reconfig_msg(r);
|
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
|
|
|
|
|
|
|
static int
|
2016-10-14 13:37:04 +00:00
|
|
|
cli_connect(sock *s, uint size UNUSED)
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
|
|
|
cli *c;
|
|
|
|
|
2000-05-29 22:10:18 +00:00
|
|
|
if (config->cli_debug)
|
|
|
|
log(L_INFO "CLI connect");
|
2017-07-18 21:51:09 +00:00
|
|
|
c = cli_new(s);
|
2016-04-06 09:49:34 +00:00
|
|
|
s->fast_rx = 1;
|
2017-07-18 21:51:09 +00:00
|
|
|
cli_run(c);
|
1999-10-29 12:10:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2011-05-15 14:29:44 +00:00
|
|
|
cli_init_unix(uid_t use_uid, gid_t use_gid)
|
1999-10-29 12:10:10 +00: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 13:26:11 +00:00
|
|
|
s->rbsize = 1024;
|
2016-04-06 09:49:34 +00:00
|
|
|
s->fast_rx = 1;
|
2014-05-18 09:42:26 +00:00
|
|
|
|
|
|
|
/* Return value intentionally ignored */
|
|
|
|
unlink(path_control_socket);
|
|
|
|
|
|
|
|
if (sk_open_unix(s, path_control_socket) < 0)
|
|
|
|
die("Cannot create control socket %s: %m", path_control_socket);
|
2011-05-15 14:29:44 +00: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 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
2013-10-05 17:30:12 +00:00
|
|
|
/*
|
|
|
|
* PID file
|
|
|
|
*/
|
|
|
|
|
|
|
|
static char *pid_file;
|
|
|
|
static int pid_fd;
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
open_pid_file(void)
|
|
|
|
{
|
|
|
|
if (!pid_file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
pid_fd = open(pid_file, O_WRONLY|O_CREAT, 0664);
|
|
|
|
if (pid_fd < 0)
|
|
|
|
die("Cannot create PID file %s: %m", pid_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
write_pid_file(void)
|
|
|
|
{
|
|
|
|
int pl, rv;
|
|
|
|
char ps[24];
|
|
|
|
|
|
|
|
if (!pid_file)
|
|
|
|
return;
|
|
|
|
|
|
|
|
/* We don't use PID file for uniqueness, so no need for locking */
|
|
|
|
|
|
|
|
pl = bsnprintf(ps, sizeof(ps), "%ld\n", (long) getpid());
|
|
|
|
if (pl < 0)
|
|
|
|
bug("PID buffer too small");
|
|
|
|
|
|
|
|
rv = ftruncate(pid_fd, 0);
|
|
|
|
if (rv < 0)
|
|
|
|
die("fruncate: %m");
|
|
|
|
|
|
|
|
rv = write(pid_fd, ps, pl);
|
|
|
|
if(rv < 0)
|
|
|
|
die("write: %m");
|
|
|
|
|
|
|
|
close(pid_fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
unlink_pid_file(void)
|
|
|
|
{
|
|
|
|
if (pid_file)
|
|
|
|
unlink(pid_file);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-13 20:15:36 +00:00
|
|
|
/*
|
|
|
|
* Shutdown
|
|
|
|
*/
|
|
|
|
|
2010-02-21 08:57:26 +00:00
|
|
|
void
|
|
|
|
cmd_shutdown(void)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cli_msg(7, "Shutdown requested");
|
|
|
|
order_shutdown();
|
|
|
|
}
|
|
|
|
|
1999-02-13 20:15:36 +00:00
|
|
|
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
|
|
|
{
|
2013-10-05 17:30:12 +00:00
|
|
|
unlink_pid_file();
|
2009-11-13 13:43:29 +00:00
|
|
|
unlink(path_control_socket);
|
2010-02-06 21:57:51 +00:00
|
|
|
log_msg(L_FATAL "Shutdown completed");
|
2009-11-13 13:54:43 +00:00
|
|
|
exit(0);
|
1999-02-13 20:15:36 +00:00
|
|
|
}
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
/*
|
|
|
|
* Signals
|
|
|
|
*/
|
|
|
|
|
2016-04-12 09:14:54 +00:00
|
|
|
volatile int async_config_flag;
|
|
|
|
volatile int async_dump_flag;
|
|
|
|
volatile int async_shutdown_flag;
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
static void
|
2004-06-05 09:11:07 +00:00
|
|
|
handle_sighup(int sig UNUSED)
|
1999-02-13 19:43:21 +00:00
|
|
|
{
|
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
|
2004-06-05 09:11:07 +00:00
|
|
|
handle_sigusr(int sig UNUSED)
|
1999-02-13 19:43:21 +00:00
|
|
|
{
|
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
|
2004-06-05 09:11:07 +00:00
|
|
|
handle_sigterm(int sig UNUSED)
|
1999-02-13 20:15:36 +00:00
|
|
|
{
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Caught SIGTERM...\n");
|
1999-02-13 20:15:36 +00:00
|
|
|
async_shutdown_flag = 1;
|
|
|
|
}
|
|
|
|
|
2015-03-02 08:41:14 +00:00
|
|
|
void watchdog_sigalrm(int sig UNUSED);
|
|
|
|
|
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);
|
2015-03-02 08:41:14 +00:00
|
|
|
sa.sa_handler = watchdog_sigalrm;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
1999-02-13 19:43:21 +00:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parsing of command-line arguments
|
|
|
|
*/
|
|
|
|
|
2016-09-08 14:27:40 +00:00
|
|
|
static char *opt_list = "c:dD:ps:P:u:g:flRh";
|
2010-04-07 09:00:36 +00:00
|
|
|
static int parse_and_exit;
|
|
|
|
char *bird_name;
|
2011-05-10 00:42:17 +00:00
|
|
|
static char *use_user;
|
|
|
|
static char *use_group;
|
2013-10-05 20:45:08 +00:00
|
|
|
static int run_in_foreground = 0;
|
2000-05-08 19:10:36 +00:00
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
static void
|
2016-09-08 14:27:40 +00:00
|
|
|
display_usage(void)
|
1999-02-13 19:43:21 +00:00
|
|
|
{
|
2016-09-08 14:27:40 +00:00
|
|
|
fprintf(stderr, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display_help(void)
|
|
|
|
{
|
|
|
|
display_usage();
|
|
|
|
|
|
|
|
fprintf(stderr,
|
|
|
|
"\n"
|
|
|
|
"Options: \n"
|
|
|
|
" -c <config-file> Use given configuration file instead\n"
|
|
|
|
" of prefix/etc/bird.conf\n"
|
|
|
|
" -d Enable debug messages and run bird in foreground\n"
|
|
|
|
" -D <debug-file> Log debug messages to given file instead of stderr\n"
|
|
|
|
" -f Run bird in foreground\n"
|
|
|
|
" -g <group> Use given group ID\n"
|
|
|
|
" -h, --help Display this information\n"
|
|
|
|
" -l Look for a configuration file and a communication socket\n"
|
|
|
|
" file in the current working directory\n"
|
|
|
|
" -p Test configuration file and exit without start\n"
|
|
|
|
" -P <pid-file> Create a PID file with given filename\n"
|
|
|
|
" -R Apply graceful restart recovery after start\n"
|
|
|
|
" -s <control-socket> Use given filename for a control socket\n"
|
|
|
|
" -u <user> Drop privileges and use given user ID\n"
|
|
|
|
" --version Display version of BIRD\n");
|
|
|
|
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
display_version(void)
|
|
|
|
{
|
|
|
|
fprintf(stderr, "BIRD version " BIRD_VERSION "\n");
|
|
|
|
exit(0);
|
1999-02-13 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
2010-04-07 09:00:36 +00: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 10:44:17 +00:00
|
|
|
|
2011-05-10 00:42:17 +00:00
|
|
|
static inline uid_t
|
|
|
|
get_uid(const char *s)
|
|
|
|
{
|
|
|
|
struct passwd *pw;
|
|
|
|
char *endptr;
|
2011-05-15 14:29:44 +00:00
|
|
|
long int rv;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
|
|
|
|
2011-05-10 00:42:17 +00:00
|
|
|
errno = 0;
|
2011-05-15 14:29:44 +00:00
|
|
|
rv = strtol(s, &endptr, 10);
|
2011-05-10 00:42:17 +00: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 14:29:44 +00:00
|
|
|
long int rv;
|
|
|
|
|
|
|
|
if (!s)
|
|
|
|
return 0;
|
2016-04-07 10:20:45 +00:00
|
|
|
|
2011-05-10 00:42:17 +00:00
|
|
|
errno = 0;
|
2011-05-15 14:29:44 +00:00
|
|
|
rv = strtol(s, &endptr, 10);
|
2011-05-10 00:42:17 +00:00
|
|
|
|
|
|
|
if (!errno && !*endptr)
|
|
|
|
return rv;
|
|
|
|
|
|
|
|
gr = getgrnam(s);
|
|
|
|
if (!gr)
|
|
|
|
die("Cannot find group '%s'", s);
|
|
|
|
|
|
|
|
return gr->gr_gid;
|
|
|
|
}
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
static void
|
|
|
|
parse_args(int argc, char **argv)
|
|
|
|
{
|
2016-04-07 10:20:45 +00:00
|
|
|
int config_changed = 0;
|
|
|
|
int socket_changed = 0;
|
1999-02-13 19:43:21 +00:00
|
|
|
int c;
|
|
|
|
|
2010-04-07 09:00:36 +00:00
|
|
|
bird_name = get_bird_name(argv[0], "bird");
|
2000-05-29 21:58:35 +00:00
|
|
|
if (argc == 2)
|
|
|
|
{
|
|
|
|
if (!strcmp(argv[1], "--version"))
|
2016-09-08 14:27:40 +00:00
|
|
|
display_version();
|
2000-05-29 21:58:35 +00:00
|
|
|
if (!strcmp(argv[1], "--help"))
|
2016-09-08 14:27:40 +00:00
|
|
|
display_help();
|
2000-05-29 21:58:35 +00:00
|
|
|
}
|
1999-02-13 19:43:21 +00:00
|
|
|
while ((c = getopt(argc, argv, opt_list)) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
config_name = optarg;
|
2016-04-07 10:20:45 +00:00
|
|
|
config_changed = 1;
|
1999-02-13 19:43:21 +00:00
|
|
|
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;
|
2009-11-19 10:44:17 +00:00
|
|
|
case 'p':
|
|
|
|
parse_and_exit = 1;
|
|
|
|
break;
|
1999-12-08 13:20:19 +00:00
|
|
|
case 's':
|
|
|
|
path_control_socket = optarg;
|
2016-04-07 10:20:45 +00:00
|
|
|
socket_changed = 1;
|
1999-12-08 13:20:19 +00:00
|
|
|
break;
|
2013-10-05 17:30:12 +00:00
|
|
|
case 'P':
|
|
|
|
pid_file = optarg;
|
|
|
|
break;
|
2011-05-10 00:42:17 +00:00
|
|
|
case 'u':
|
|
|
|
use_user = optarg;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
use_group = optarg;
|
|
|
|
break;
|
2013-10-05 20:45:08 +00:00
|
|
|
case 'f':
|
|
|
|
run_in_foreground = 1;
|
|
|
|
break;
|
2016-04-07 10:20:45 +00:00
|
|
|
case 'l':
|
|
|
|
if (!config_changed)
|
|
|
|
config_name = xbasename(config_name);
|
|
|
|
if (!socket_changed)
|
|
|
|
path_control_socket = xbasename(path_control_socket);
|
|
|
|
break;
|
2014-03-20 13:07:12 +00:00
|
|
|
case 'R':
|
|
|
|
graceful_restart_recovery();
|
|
|
|
break;
|
2016-09-08 14:27:40 +00:00
|
|
|
case 'h':
|
|
|
|
display_help();
|
|
|
|
break;
|
1999-02-13 19:43:21 +00:00
|
|
|
default:
|
2016-09-08 14:27:40 +00:00
|
|
|
fputc('\n', stderr);
|
|
|
|
display_usage();
|
|
|
|
exit(1);
|
1999-02-13 19:43:21 +00:00
|
|
|
}
|
|
|
|
if (optind < argc)
|
2016-09-08 14:27:40 +00:00
|
|
|
{
|
|
|
|
display_usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-02-13 19:43:21 +00:00
|
|
|
}
|
|
|
|
|
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("");
|
2010-04-07 09:00:36 +00:00
|
|
|
log_switch(debug_flag, NULL, NULL);
|
1999-02-13 19:43:21 +00:00
|
|
|
|
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();
|
2015-12-16 14:30:44 +00:00
|
|
|
// roa_init();
|
2012-12-26 11:40:48 +00:00
|
|
|
config_init();
|
1998-11-27 21:09:57 +00:00
|
|
|
|
2011-05-15 14:29:44 +00:00
|
|
|
uid_t use_uid = get_uid(use_user);
|
|
|
|
gid_t use_gid = get_gid(use_group);
|
|
|
|
|
2010-03-17 11:19:22 +00:00
|
|
|
if (!parse_and_exit)
|
2011-05-15 14:29:44 +00: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 11:19:22 +00:00
|
|
|
|
2013-10-05 17:30:12 +00:00
|
|
|
if (!parse_and_exit)
|
|
|
|
open_pid_file();
|
|
|
|
|
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
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
struct config *conf = read_config();
|
1998-05-26 21:42:05 +00:00
|
|
|
|
2009-11-19 10:44:17 +00:00
|
|
|
if (parse_and_exit)
|
|
|
|
exit(0);
|
|
|
|
|
2013-10-05 20:45:08 +00:00
|
|
|
if (!(debug_flag||run_in_foreground))
|
2000-05-08 19:10:36 +00:00
|
|
|
{
|
|
|
|
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();
|
2007-06-20 07:33:26 +00:00
|
|
|
close(0);
|
|
|
|
if (open("/dev/null", O_RDWR) < 0)
|
|
|
|
die("Cannot open /dev/null: %m");
|
|
|
|
dup2(0, 1);
|
|
|
|
dup2(0, 2);
|
2000-05-08 19:10:36 +00:00
|
|
|
}
|
|
|
|
|
2014-02-07 12:09:55 +00:00
|
|
|
main_thread_init();
|
|
|
|
|
2013-10-05 17:30:12 +00:00
|
|
|
write_pid_file();
|
|
|
|
|
1998-05-24 14:50:18 +00:00
|
|
|
signal_init();
|
|
|
|
|
2013-10-05 18:12:28 +00:00
|
|
|
config_commit(conf, RECONFIG_HARD, 0);
|
|
|
|
|
2014-03-20 13:07:12 +00:00
|
|
|
graceful_restart_init();
|
|
|
|
|
2000-05-04 20:52:28 +00:00
|
|
|
#ifdef LOCAL_DEBUG
|
|
|
|
async_dump_flag = 1;
|
|
|
|
#endif
|
1998-05-26 21:42:05 +00:00
|
|
|
|
2010-02-06 21:57:51 +00:00
|
|
|
log(L_INFO "Started");
|
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
|
|
|
}
|