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>
|
2021-02-10 15:53:57 +00:00
|
|
|
#include <sys/utsname.h>
|
2011-09-11 19:21:47 +00:00
|
|
|
#include <libgen.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"
|
2017-05-30 17:12:35 +00:00
|
|
|
#include "lib/timer.h"
|
2024-06-24 09:10:07 +00:00
|
|
|
#include "lib/tlists.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"
|
2022-09-14 23:38:18 +00:00
|
|
|
#include "nest/mpls.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"
|
2019-02-08 12:38:12 +00:00
|
|
|
#include "filter/data.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
|
|
|
|
*/
|
|
|
|
|
2024-11-14 19:43:35 +00:00
|
|
|
static void
|
|
|
|
async_dump_report(struct dump_request *dr UNUSED, int state, const char *fmt, ...)
|
|
|
|
{
|
|
|
|
va_list args;
|
|
|
|
va_start(args, fmt);
|
|
|
|
vlog(((state > 1000) ? L_ERR : L_INFO)[0], fmt, args);
|
|
|
|
va_end(args);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
async_dump_run(struct dump_request *dreq)
|
1998-05-24 14:50:18 +00:00
|
|
|
{
|
2024-11-14 19:43:35 +00:00
|
|
|
RDUMP("ASYNC STATE DUMP\n");
|
1998-05-24 14:50:18 +00:00
|
|
|
|
2024-11-14 19:43:35 +00:00
|
|
|
rdump(dreq, &root_pool);
|
|
|
|
sk_dump_all(dreq);
|
2017-06-01 10:33:20 +00:00
|
|
|
// XXXX tm_dump_all();
|
2024-11-14 19:43:35 +00:00
|
|
|
if_dump_all(dreq);
|
|
|
|
neigh_dump_all(dreq);
|
|
|
|
rta_dump_all(dreq);
|
|
|
|
rt_dump_all(dreq);
|
|
|
|
protos_dump_all(dreq);
|
1998-05-24 14:50:18 +00:00
|
|
|
|
|
|
|
debug("\n");
|
|
|
|
}
|
|
|
|
|
2024-11-14 19:43:35 +00:00
|
|
|
void
|
|
|
|
async_dump(void)
|
|
|
|
{
|
|
|
|
struct dump_request *dr = dump_to_file_init(0);
|
|
|
|
dr->report = async_dump_report;
|
|
|
|
dump_to_file_run(dr, "bird.dump", "async dump", async_dump_run);
|
|
|
|
}
|
|
|
|
|
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");
|
2019-08-21 15:30:00 +00:00
|
|
|
|
|
|
|
if (setgroups(0, NULL) < 0)
|
|
|
|
die("setgroups: %m");
|
2011-05-10 00:42:17 +00:00
|
|
|
}
|
|
|
|
|
2021-02-10 15:53:57 +00:00
|
|
|
/*
|
|
|
|
* Hostname
|
|
|
|
*/
|
|
|
|
|
|
|
|
char *
|
|
|
|
get_hostname(linpool *lp)
|
|
|
|
{
|
|
|
|
struct utsname uts = {};
|
|
|
|
|
|
|
|
if (uname(&uts) < 0)
|
|
|
|
return NULL;
|
|
|
|
|
|
|
|
return lp_strdup(lp, uts.nodename);
|
|
|
|
}
|
|
|
|
|
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
|
2023-06-13 08:51:03 +00:00
|
|
|
add_num_const(struct config *conf, char *name, int val, const char *file, const uint line)
|
2011-04-27 22:31:37 +00:00
|
|
|
{
|
2019-02-15 12:53:17 +00:00
|
|
|
struct f_val *v = cfg_alloc(sizeof(struct f_val));
|
|
|
|
*v = (struct f_val) { .type = T_INT, .val.i = val };
|
2023-06-13 08:51:03 +00:00
|
|
|
struct symbol *sym = cf_get_symbol(conf, name);
|
|
|
|
if (sym->class && cf_symbol_is_local(conf, sym))
|
2020-02-04 09:34:46 +00:00
|
|
|
cf_error("Error reading value for %s from %s:%d: already defined", name, file, line);
|
|
|
|
|
2023-06-13 08:51:03 +00:00
|
|
|
cf_define_symbol(conf, sym, SYM_CONSTANT | T_INT, val, v);
|
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
|
2023-06-13 08:51:03 +00:00
|
|
|
read_iproute_table(struct config *conf, char *file, char *prefix, uint max)
|
2011-04-27 22:31:37 +00:00
|
|
|
{
|
|
|
|
char buf[512], namebuf[512];
|
|
|
|
char *name;
|
2021-04-07 14:14:20 +00:00
|
|
|
uint val;
|
2011-04-27 22:31:37 +00:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
strcpy(namebuf, prefix);
|
|
|
|
name = namebuf + strlen(prefix);
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
2020-02-04 09:34:46 +00:00
|
|
|
for (uint line = 1; fgets(buf, sizeof(buf), fp); line++)
|
2011-04-27 22:31:37 +00:00
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '#' || *p == '\n' || *p == 0)
|
|
|
|
continue;
|
2016-11-14 13:53:10 +00:00
|
|
|
|
2011-04-27 22:31:37 +00:00
|
|
|
if (sscanf(p, "0x%x %s\n", &val, name) != 2 &&
|
|
|
|
sscanf(p, "0x%x %s #", &val, name) != 2 &&
|
2021-04-07 14:14:20 +00:00
|
|
|
sscanf(p, "%u %s\n", &val, name) != 2 &&
|
|
|
|
sscanf(p, "%u %s #", &val, name) != 2)
|
2011-04-27 22:31:37 +00:00
|
|
|
continue;
|
|
|
|
|
2021-04-07 14:14:20 +00:00
|
|
|
if (val > max)
|
2011-04-27 22:31:37 +00:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for(p = name; *p; p++)
|
2019-10-22 14:20:38 +00:00
|
|
|
if ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && (*p != '_'))
|
2011-04-27 22:31:37 +00:00
|
|
|
*p = '_';
|
|
|
|
|
2023-06-13 08:51:03 +00:00
|
|
|
add_num_const(conf, namebuf, val, file, line);
|
2011-04-27 22:31:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
static void cli_preconfig(struct config *c);
|
|
|
|
|
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
|
2023-06-13 08:51:03 +00:00
|
|
|
read_iproute_table(c, PATH_IPROUTE_DIR "/rt_protos", "ipp_", 255);
|
|
|
|
read_iproute_table(c, PATH_IPROUTE_DIR "/rt_realms", "ipr_", 0xffffffff);
|
|
|
|
read_iproute_table(c, PATH_IPROUTE_DIR "/rt_scopes", "ips_", 255);
|
|
|
|
read_iproute_table(c, PATH_IPROUTE_DIR "/rt_tables", "ipt_", 0xffffffff);
|
2011-04-27 22:31:37 +00:00
|
|
|
#endif
|
2024-06-24 09:10:07 +00:00
|
|
|
|
|
|
|
cli_preconfig(c);
|
1999-12-06 13:45:56 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
static void cli_commit(struct config *new, struct config *old);
|
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
int
|
2024-06-24 09:10:07 +00:00
|
|
|
sysdep_commit(struct config *new, struct config *old)
|
1999-12-06 13:45:56 +00:00
|
|
|
{
|
2018-12-04 15:55:25 +00:00
|
|
|
log_switch(0, &new->logfiles, new->syslog_name);
|
2024-06-24 09:10:07 +00:00
|
|
|
cli_commit(new, old);
|
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
|
2020-04-08 20:25:15 +00:00
|
|
|
unix_read_config(struct config **cp, const 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)
|
2018-09-11 14:55:41 +00:00
|
|
|
die("%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, 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;
|
|
|
|
|
2022-11-09 20:09:16 +00:00
|
|
|
config_free_old();
|
|
|
|
|
2000-01-16 16:44:50 +00:00
|
|
|
log(L_INFO "Reconfiguration requested by SIGHUP");
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2018-09-11 14:55:41 +00:00
|
|
|
log(L_ERR "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, 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 *
|
2020-04-08 20:25:15 +00:00
|
|
|
cmd_read_config(const 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)
|
2018-09-11 14:55:41 +00:00
|
|
|
cli_msg(8002, "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, 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
|
2020-04-08 20:25:15 +00:00
|
|
|
cmd_check_config(const char *name)
|
2012-12-26 11:40:48 +00:00
|
|
|
{
|
2022-11-09 21:02:46 +00:00
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
2012-12-26 11:40:48 +00:00
|
|
|
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
|
2020-04-08 20:25:15 +00:00
|
|
|
cmd_reconfig(const char *name, int type, uint timeout)
|
2012-12-26 11:40:48 +00:00
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
2022-11-09 20:09:16 +00:00
|
|
|
config_free_old();
|
|
|
|
|
2012-12-26 11:40:48 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-06-12 14:13:21 +00:00
|
|
|
void
|
|
|
|
cmd_reconfig_status(void)
|
|
|
|
{
|
|
|
|
int s = config_status();
|
|
|
|
btime t = config_timer_status();
|
|
|
|
|
|
|
|
switch (s)
|
|
|
|
{
|
|
|
|
case CONF_DONE: cli_msg(-3, "Daemon is up and running"); break;
|
|
|
|
case CONF_PROGRESS: cli_msg(-4, "Reconfiguration in progress"); break;
|
|
|
|
case CONF_QUEUED: cli_msg(-5, "Reconfiguration in progress, next one enqueued"); break;
|
|
|
|
case CONF_SHUTDOWN: cli_msg(-6, "Shutdown in progress"); break;
|
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (t >= 0)
|
|
|
|
cli_msg(-22, "Configuration unconfirmed, undo in %t s", t);
|
|
|
|
|
|
|
|
cli_msg(0, "");
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
/*
|
|
|
|
* Command-Line Interface
|
|
|
|
*/
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
static struct cli_config initial_control_socket_config = {
|
|
|
|
.name = PATH_CONTROL_SOCKET,
|
|
|
|
.mode = 0660,
|
|
|
|
};
|
|
|
|
#define path_control_socket initial_control_socket_config.name
|
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
static struct cli_config *main_control_socket_config = NULL;
|
2024-06-24 09:10:07 +00:00
|
|
|
|
|
|
|
#define TLIST_PREFIX cli_listener
|
|
|
|
#define TLIST_TYPE struct cli_listener
|
|
|
|
#define TLIST_ITEM n
|
|
|
|
#define TLIST_WANT_ADD_TAIL
|
|
|
|
#define TLIST_WANT_WALK
|
2024-06-24 09:09:46 +00:00
|
|
|
static struct cli_listener {
|
2024-06-24 09:10:07 +00:00
|
|
|
TLIST_DEFAULT_NODE;
|
2024-06-24 09:09:46 +00:00
|
|
|
sock *s;
|
|
|
|
struct cli_config *config;
|
|
|
|
} *main_control_socket = NULL;
|
2009-07-14 12:18:54 +00:00
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
#include "lib/tlists.h"
|
|
|
|
|
|
|
|
static TLIST_LIST(cli_listener) cli_listeners;
|
|
|
|
|
2009-07-14 12:18:54 +00:00
|
|
|
static void
|
1999-10-29 12:10:10 +00:00
|
|
|
cli_write(cli *c)
|
|
|
|
{
|
|
|
|
sock *s = c->priv;
|
|
|
|
|
2009-07-14 12:18:54 +00:00
|
|
|
while (c->tx_pos)
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
|
|
|
struct cli_out *o = c->tx_pos;
|
2009-07-14 12:18:54 +00:00
|
|
|
|
|
|
|
int len = o->wpos - o->outpos;
|
1999-10-29 12:10:10 +00:00
|
|
|
s->tbuf = o->outpos;
|
2009-07-14 12:18:54 +00:00
|
|
|
o->outpos = o->wpos;
|
|
|
|
|
|
|
|
if (sk_send(s, len) <= 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
c->tx_pos = o->next;
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
2009-07-14 12:18:54 +00: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 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
cli_get_command(cli *c)
|
|
|
|
{
|
|
|
|
sock *s = c->priv;
|
2022-12-10 16:32:42 +00:00
|
|
|
byte *t = s->rbuf;
|
1999-10-29 12:10:10 +00:00
|
|
|
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')
|
|
|
|
{
|
2022-12-10 16:32:42 +00:00
|
|
|
*d = 0;
|
1999-10-29 12:10:10 +00:00
|
|
|
t++;
|
2022-12-10 16:32:42 +00:00
|
|
|
|
|
|
|
/* Move remaining data and reset pointers */
|
|
|
|
uint rest = (t < tend) ? (tend - t) : 0;
|
|
|
|
memmove(s->rbuf, t, rest);
|
|
|
|
s->rpos = s->rbuf + rest;
|
1999-10-29 12:10:10 +00:00
|
|
|
c->rx_pos = c->rx_buf;
|
2022-12-10 16:32:42 +00:00
|
|
|
|
1999-10-29 12:10:10 +00:00
|
|
|
return (d < dend) ? 1 : -1;
|
|
|
|
}
|
|
|
|
else if (d < dend)
|
|
|
|
*d++ = *t++;
|
|
|
|
}
|
2022-12-10 16:32:42 +00:00
|
|
|
|
|
|
|
s->rpos = s->rbuf;
|
1999-10-29 12:10:10 +00:00
|
|
|
c->rx_pos = d;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-10-14 13:37:04 +00:00
|
|
|
cli_rx(sock *s, uint size UNUSED)
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
|
|
|
cli_kick(s->data);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
|
2021-09-07 16:22:32 +00:00
|
|
|
static void
|
|
|
|
cli_connect_err(sock *s UNUSED, int err)
|
|
|
|
{
|
|
|
|
ASSERT_DIE(err);
|
|
|
|
if (config->cli_debug)
|
|
|
|
log(L_INFO "Failed to accept CLI connection: %s", strerror(err));
|
|
|
|
}
|
|
|
|
|
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");
|
1999-10-29 12:10:10 +00:00
|
|
|
s->rx_hook = cli_rx;
|
|
|
|
s->tx_hook = cli_tx;
|
|
|
|
s->err_hook = cli_err;
|
2024-06-24 11:46:12 +00:00
|
|
|
s->data = c = cli_new(s, ((struct cli_listener *) s->data)->config);
|
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 */
|
2016-04-06 09:49:34 +00:00
|
|
|
s->fast_rx = 1;
|
1999-10-29 12:10:10 +00:00
|
|
|
c->rx_pos = c->rx_buf;
|
2004-05-31 22:00:18 +00:00
|
|
|
rmove(s, c->pool);
|
1999-10-29 12:10:10 +00:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
static struct cli_listener *
|
2024-06-24 09:10:07 +00:00
|
|
|
cli_listen(struct cli_config *cf)
|
1999-10-29 12:10:10 +00:00
|
|
|
{
|
2024-06-24 09:09:46 +00:00
|
|
|
struct cli_listener *l = mb_allocz(cli_pool, sizeof *l);
|
|
|
|
l->config = cf;
|
|
|
|
sock *s = l->s = sk_new(cli_pool);
|
1999-10-29 12:10:10 +00:00
|
|
|
s->type = SK_UNIX_PASSIVE;
|
|
|
|
s->rx_hook = cli_connect;
|
2021-09-07 16:22:32 +00:00
|
|
|
s->err_hook = cli_connect_err;
|
2024-06-24 11:46:12 +00:00
|
|
|
s->data = l;
|
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 */
|
2024-06-24 09:09:46 +00:00
|
|
|
unlink(cf->name);
|
2014-05-18 09:42:26 +00:00
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
if (sk_open_unix(s, cf->name) < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "Cannot create control socket %s: %m", cf->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-15 14:29:44 +00:00
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
if (cf->uid || cf->gid)
|
|
|
|
if (chown(cf->name, cf->uid, cf->gid) < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "Cannot chown control socket %s: %m", cf->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
2011-05-15 14:29:44 +00:00
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
if (chmod(cf->name, cf->mode) < 0)
|
|
|
|
{
|
|
|
|
log(L_ERR "Cannot chmod control socket %s: %m", cf->name);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
cli_listener_add_tail(&cli_listeners, l);
|
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
return l;
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
static void
|
|
|
|
cli_deafen(struct cli_listener *l)
|
|
|
|
{
|
|
|
|
rfree(l->s);
|
|
|
|
unlink(l->config->name);
|
|
|
|
cli_listener_rem_node(&cli_listeners, l);
|
|
|
|
mb_free(l);
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:09:46 +00:00
|
|
|
static void
|
|
|
|
cli_init_unix(uid_t use_uid, gid_t use_gid)
|
|
|
|
{
|
|
|
|
ASSERT_DIE(main_control_socket_config == NULL);
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
main_control_socket_config = &initial_control_socket_config;
|
|
|
|
main_control_socket_config->uid = use_uid;
|
|
|
|
main_control_socket_config->gid = use_gid;
|
2024-06-24 09:09:46 +00:00
|
|
|
|
|
|
|
ASSERT_DIE(main_control_socket == NULL);
|
2024-06-24 09:10:07 +00:00
|
|
|
main_control_socket = cli_listen(main_control_socket_config);
|
2024-06-24 09:09:46 +00:00
|
|
|
if (!main_control_socket)
|
|
|
|
die("Won't run without control socket");
|
1999-10-29 12:10:10 +00:00
|
|
|
}
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
static void
|
|
|
|
cli_preconfig(struct config *c)
|
|
|
|
{
|
2024-08-29 12:37:19 +00:00
|
|
|
if (!main_control_socket_config)
|
|
|
|
return;
|
|
|
|
|
2024-06-24 09:10:07 +00:00
|
|
|
struct cli_config *ccf = mb_alloc(cli_pool, sizeof *ccf);
|
|
|
|
memcpy(ccf, main_control_socket_config, sizeof *ccf);
|
|
|
|
ccf->n = (struct cli_config_node) {};
|
|
|
|
ccf->config = c;
|
|
|
|
cli_config_add_tail(&c->cli, ccf);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
cli_commit(struct config *new, struct config *old)
|
|
|
|
{
|
|
|
|
if (new->shutdown)
|
|
|
|
{
|
|
|
|
/* Keep the main CLI throughout the shutdown */
|
|
|
|
initial_control_socket_config.config = new;
|
|
|
|
main_control_socket->config = &initial_control_socket_config;
|
|
|
|
}
|
|
|
|
|
|
|
|
WALK_TLIST(cli_config, c, &new->cli)
|
|
|
|
{
|
|
|
|
_Bool seen = 0;
|
|
|
|
WALK_TLIST(cli_listener, l, &cli_listeners)
|
|
|
|
if (l->config->config != new)
|
|
|
|
if (!strcmp(l->config->name, c->name))
|
|
|
|
{
|
|
|
|
ASSERT_DIE(l->config->config == old);
|
|
|
|
l->config = c;
|
|
|
|
seen = 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!seen)
|
|
|
|
cli_listen(c);
|
|
|
|
}
|
|
|
|
|
|
|
|
WALK_TLIST_DELSAFE(cli_listener, l, &cli_listeners)
|
|
|
|
if (l->config->config != new)
|
|
|
|
cli_deafen(l);
|
|
|
|
}
|
|
|
|
|
2024-06-24 09:09:46 +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 */
|
|
|
|
|
2019-10-01 15:01:29 +00:00
|
|
|
pl = bsnprintf(ps, sizeof(ps), "%ld\n", (s64) getpid());
|
2013-10-05 17:30:12 +00:00
|
|
|
if (pl < 0)
|
|
|
|
bug("PID buffer too small");
|
|
|
|
|
|
|
|
rv = ftruncate(pid_fd, 0);
|
|
|
|
if (rv < 0)
|
|
|
|
die("fruncate: %m");
|
2016-11-14 13:53:10 +00:00
|
|
|
|
2013-10-05 17:30:12 +00:00
|
|
|
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");
|
2019-06-18 14:27:21 +00:00
|
|
|
order_shutdown(0);
|
2010-02-21 08:57:26 +00:00
|
|
|
}
|
|
|
|
|
1999-02-13 20:15:36 +00:00
|
|
|
void
|
|
|
|
async_shutdown(void)
|
|
|
|
{
|
2000-03-12 21:01:38 +00:00
|
|
|
DBG("Shutting down...\n");
|
2019-06-18 14:27:21 +00:00
|
|
|
order_shutdown(0);
|
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();
|
2024-06-24 09:10:07 +00:00
|
|
|
cli_deafen(main_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
|
|
|
}
|
|
|
|
|
2019-06-18 14:27:21 +00:00
|
|
|
void
|
|
|
|
cmd_graceful_restart(void)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cli_msg(25, "Graceful restart requested");
|
|
|
|
order_shutdown(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-13 19:43:21 +00:00
|
|
|
/*
|
|
|
|
* Signals
|
|
|
|
*/
|
|
|
|
|
2019-10-04 10:20:02 +00:00
|
|
|
volatile sig_atomic_t async_config_flag;
|
|
|
|
volatile sig_atomic_t async_dump_flag;
|
|
|
|
volatile sig_atomic_t async_shutdown_flag;
|
2016-04-12 09:14:54 +00:00
|
|
|
|
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
|
|
|
|
*/
|
|
|
|
|
2022-03-09 08:13:20 +00:00
|
|
|
static char *opt_list = "bc:dD:ps:P:u:g:flRh";
|
2020-05-31 11:21:55 +00:00
|
|
|
int parse_and_exit;
|
2010-04-07 09:00:36 +00:00
|
|
|
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"
|
2019-01-04 22:49:26 +00:00
|
|
|
" -c <config-file> Use given configuration file instead of\n"
|
|
|
|
" " PATH_CONFIG_FILE "\n"
|
2016-09-08 14:27:40 +00:00
|
|
|
" -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"
|
2019-01-04 22:49:26 +00:00
|
|
|
" -l Look for a configuration file and a control socket\n"
|
|
|
|
" in the current working directory\n"
|
2016-09-08 14:27:40 +00:00
|
|
|
" -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':
|
2018-12-04 15:55:25 +00:00
|
|
|
log_init_debug("");
|
2018-08-28 14:45:50 +00:00
|
|
|
run_in_foreground = 1;
|
1999-12-06 13:45:56 +00:00
|
|
|
break;
|
|
|
|
case 'D':
|
1999-02-13 19:43:21 +00:00
|
|
|
log_init_debug(optarg);
|
|
|
|
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);
|
2018-12-04 15:55:25 +00:00
|
|
|
log_switch(1, NULL, NULL);
|
1999-02-13 19:43:21 +00:00
|
|
|
|
2021-04-01 17:20:13 +00:00
|
|
|
random_init();
|
1998-11-27 21:09:57 +00:00
|
|
|
resource_init();
|
2017-05-30 17:12:35 +00:00
|
|
|
timer_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();
|
2022-09-14 23:38:18 +00:00
|
|
|
mpls_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);
|
|
|
|
|
2024-08-29 12:37:19 +00:00
|
|
|
cli_init();
|
|
|
|
|
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();
|
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);
|
|
|
|
|
2018-08-28 14:45:50 +00:00
|
|
|
if (!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
|
|
|
}
|