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
|
|
|
|
2016-11-09 00:46:29 +08:00
|
|
|
#ifndef _GNU_SOURCE
|
|
|
|
#define _GNU_SOURCE
|
|
|
|
#endif
|
2011-05-10 08:42:17 +08:00
|
|
|
|
1998-05-15 15:56:13 +08:00
|
|
|
#include <stdio.h>
|
1999-03-04 19:36:26 +08:00
|
|
|
#include <stdlib.h>
|
1998-11-28 03:37:07 +08:00
|
|
|
#include <fcntl.h>
|
|
|
|
#include <unistd.h>
|
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>
|
2021-02-10 23:53:57 +08:00
|
|
|
#include <sys/utsname.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"
|
2017-05-31 01:12:35 +08:00
|
|
|
#include "lib/timer.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"
|
2019-02-08 20:38:12 +08:00
|
|
|
#include "filter/data.h"
|
1998-05-24 22:50:18 +08:00
|
|
|
|
|
|
|
#include "unix.h"
|
1998-10-18 20:50:43 +08:00
|
|
|
#include "krt.h"
|
1998-05-24 22:50:18 +08:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Debugging
|
|
|
|
*/
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
void
|
|
|
|
async_dump(void)
|
1998-05-24 22:50:18 +08:00
|
|
|
{
|
1999-02-14 03:43:21 +08:00
|
|
|
debug("INTERNAL STATE DUMP\n\n");
|
1998-05-24 22:50:18 +08:00
|
|
|
|
1999-03-30 04:14:33 +08:00
|
|
|
rdump(&root_pool);
|
1998-05-24 22:50:18 +08:00
|
|
|
sk_dump_all();
|
2017-06-01 18:33:20 +08:00
|
|
|
// XXXX 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
|
2016-04-12 17:14:54 +08:00
|
|
|
#include CONFIG_INCLUDE_SYSPRIV_H
|
2011-05-10 08:42:17 +08:00
|
|
|
#else
|
|
|
|
|
|
|
|
static inline void
|
2016-10-14 21:37:04 +08:00
|
|
|
drop_uid(uid_t uid UNUSED)
|
2011-05-10 08:42:17 +08: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 23:30:00 +08:00
|
|
|
|
|
|
|
if (setgroups(0, NULL) < 0)
|
|
|
|
die("setgroups: %m");
|
2011-05-10 08:42:17 +08:00
|
|
|
}
|
|
|
|
|
2021-02-10 23:53:57 +08: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 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
|
2020-02-04 17:34:46 +08:00
|
|
|
add_num_const(char *name, int val, const char *file, const uint line)
|
2011-04-28 06:31:37 +08:00
|
|
|
{
|
2019-02-15 20:53:17 +08:00
|
|
|
struct f_val *v = cfg_alloc(sizeof(struct f_val));
|
|
|
|
*v = (struct f_val) { .type = T_INT, .val.i = val };
|
2020-02-04 17:34:46 +08:00
|
|
|
struct symbol *sym = cf_get_symbol(name);
|
2022-03-06 23:37:30 +08:00
|
|
|
if (sym->class && cf_symbol_is_local(sym))
|
2020-02-04 17:34:46 +08:00
|
|
|
cf_error("Error reading value for %s from %s:%d: already defined", name, file, line);
|
|
|
|
|
|
|
|
cf_define_symbol(sym, SYM_CONSTANT | T_INT, val, v);
|
2011-04-28 06:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* the code of read_iproute_table() is based on
|
|
|
|
rtnl_tab_initialize() from iproute2 package */
|
|
|
|
static void
|
2021-04-07 22:14:20 +08:00
|
|
|
read_iproute_table(char *file, char *prefix, uint max)
|
2011-04-28 06:31:37 +08:00
|
|
|
{
|
|
|
|
char buf[512], namebuf[512];
|
|
|
|
char *name;
|
2021-04-07 22:14:20 +08:00
|
|
|
uint val;
|
2011-04-28 06:31:37 +08:00
|
|
|
FILE *fp;
|
|
|
|
|
|
|
|
strcpy(namebuf, prefix);
|
|
|
|
name = namebuf + strlen(prefix);
|
|
|
|
|
|
|
|
fp = fopen(file, "r");
|
|
|
|
if (!fp)
|
|
|
|
return;
|
|
|
|
|
2020-02-04 17:34:46 +08:00
|
|
|
for (uint line = 1; fgets(buf, sizeof(buf), fp); line++)
|
2011-04-28 06:31:37 +08:00
|
|
|
{
|
|
|
|
char *p = buf;
|
|
|
|
|
|
|
|
while (*p == ' ' || *p == '\t')
|
|
|
|
p++;
|
|
|
|
|
|
|
|
if (*p == '#' || *p == '\n' || *p == 0)
|
|
|
|
continue;
|
2016-11-14 21:53:10 +08:00
|
|
|
|
2011-04-28 06:31:37 +08:00
|
|
|
if (sscanf(p, "0x%x %s\n", &val, name) != 2 &&
|
|
|
|
sscanf(p, "0x%x %s #", &val, name) != 2 &&
|
2021-04-07 22:14:20 +08:00
|
|
|
sscanf(p, "%u %s\n", &val, name) != 2 &&
|
|
|
|
sscanf(p, "%u %s #", &val, name) != 2)
|
2011-04-28 06:31:37 +08:00
|
|
|
continue;
|
|
|
|
|
2021-04-07 22:14:20 +08:00
|
|
|
if (val > max)
|
2011-04-28 06:31:37 +08:00
|
|
|
continue;
|
|
|
|
|
|
|
|
for(p = name; *p; p++)
|
2019-10-22 22:20:38 +08:00
|
|
|
if ((*p < 'a' || *p > 'z') && (*p < 'A' || *p > 'Z') && (*p < '0' || *p > '9') && (*p != '_'))
|
2011-04-28 06:31:37 +08:00
|
|
|
*p = '_';
|
|
|
|
|
2020-02-04 17:34:46 +08:00
|
|
|
add_num_const(namebuf, val, file, line);
|
2011-04-28 06:31:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2015-05-19 14:53:34 +08:00
|
|
|
cf_read(byte *dest, uint 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;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
2015-03-02 16:41:14 +08:00
|
|
|
c->latency_limit = UNIX_DEFAULT_LATENCY_LIMIT;
|
|
|
|
c->watchdog_warning = UNIX_DEFAULT_WATCHDOG_WARNING;
|
|
|
|
|
2011-04-28 06:31:37 +08:00
|
|
|
#ifdef PATH_IPROUTE_DIR
|
2021-04-07 22:14:20 +08:00
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_protos", "ipp_", 255);
|
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_realms", "ipr_", 0xffffffff);
|
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_scopes", "ips_", 255);
|
|
|
|
read_iproute_table(PATH_IPROUTE_DIR "/rt_tables", "ipt_", 0xffffffff);
|
2011-04-28 06:31:37 +08:00
|
|
|
#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
|
|
|
{
|
2018-12-04 23:55:25 +08:00
|
|
|
log_switch(0, &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
|
2020-04-09 04:25:15 +08:00
|
|
|
unix_read_config(struct config **cp, const 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;
|
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
|
|
|
}
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
static struct config *
|
2000-01-17 00:44:50 +08:00
|
|
|
read_config(void)
|
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2018-09-11 22:55:41 +08:00
|
|
|
die("%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg);
|
2000-01-17 00:44:50 +08:00
|
|
|
else
|
|
|
|
die("Unable to open configuration file %s: %m", config_name);
|
|
|
|
}
|
2013-10-06 02:12:28 +08:00
|
|
|
|
|
|
|
return conf;
|
1998-11-28 03:37:07 +08:00
|
|
|
}
|
1999-02-06 05:37:34 +08:00
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
void
|
|
|
|
async_config(void)
|
|
|
|
{
|
2000-01-17 00:44:50 +08:00
|
|
|
struct config *conf;
|
|
|
|
|
2022-11-10 04:09:16 +08:00
|
|
|
config_free_old();
|
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
log(L_INFO "Reconfiguration requested by SIGHUP");
|
|
|
|
if (!unix_read_config(&conf, config_name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2018-09-11 22:55:41 +08:00
|
|
|
log(L_ERR "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, 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
|
2012-12-26 19:40:48 +08:00
|
|
|
config_commit(conf, RECONFIG_HARD, 0);
|
2000-01-17 00:44:50 +08:00
|
|
|
}
|
|
|
|
|
2012-12-26 19:40:48 +08:00
|
|
|
static struct config *
|
2020-04-09 04:25:15 +08:00
|
|
|
cmd_read_config(const char *name)
|
2000-01-17 00:44:50 +08:00
|
|
|
{
|
|
|
|
struct config *conf;
|
|
|
|
|
|
|
|
if (!name)
|
|
|
|
name = config_name;
|
2012-12-26 19:40:48 +08:00
|
|
|
|
2000-01-17 00:44:50 +08:00
|
|
|
cli_msg(-2, "Reading configuration from %s", name);
|
|
|
|
if (!unix_read_config(&conf, name))
|
|
|
|
{
|
|
|
|
if (conf->err_msg)
|
2018-09-11 22:55:41 +08:00
|
|
|
cli_msg(8002, "%s:%d:%d %s", conf->err_file_name, conf->err_lino, conf->err_chno, conf->err_msg);
|
2000-01-17 00:44:50 +08:00
|
|
|
else
|
|
|
|
cli_msg(8002, "%s: %m", name);
|
|
|
|
config_free(conf);
|
2012-12-26 19:40:48 +08:00
|
|
|
conf = NULL;
|
2000-01-17 00:44:50 +08:00
|
|
|
}
|
2012-12-26 19:40:48 +08:00
|
|
|
|
|
|
|
return conf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2020-04-09 04:25:15 +08:00
|
|
|
cmd_check_config(const char *name)
|
2012-12-26 19:40:48 +08:00
|
|
|
{
|
2022-11-10 05:02:46 +08:00
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
2012-12-26 19:40:48 +08: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-17 00:44:50 +08:00
|
|
|
{
|
2012-12-26 19:40:48 +08: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-09 04:25:15 +08:00
|
|
|
cmd_reconfig(const char *name, int type, uint timeout)
|
2012-12-26 19:40:48 +08:00
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
2022-11-10 04:09:16 +08:00
|
|
|
config_free_old();
|
|
|
|
|
2012-12-26 19:40:48 +08: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-17 00:44:50 +08:00
|
|
|
}
|
2012-12-26 19:40:48 +08: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-14 03:43:21 +08:00
|
|
|
}
|
|
|
|
|
2019-06-12 22:13:21 +08: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 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;
|
2022-12-11 00:32:42 +08:00
|
|
|
byte *t = s->rbuf;
|
1999-10-29 20:10:10 +08: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-11 00:32:42 +08:00
|
|
|
*d = 0;
|
1999-10-29 20:10:10 +08:00
|
|
|
t++;
|
2022-12-11 00:32:42 +08: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 20:10:10 +08:00
|
|
|
c->rx_pos = c->rx_buf;
|
2022-12-11 00:32:42 +08:00
|
|
|
|
1999-10-29 20:10:10 +08:00
|
|
|
return (d < dend) ? 1 : -1;
|
|
|
|
}
|
|
|
|
else if (d < dend)
|
|
|
|
*d++ = *t++;
|
|
|
|
}
|
2022-12-11 00:32:42 +08:00
|
|
|
|
|
|
|
s->rpos = s->rbuf;
|
1999-10-29 20:10:10 +08:00
|
|
|
c->rx_pos = d;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2016-10-14 21:37:04 +08:00
|
|
|
cli_rx(sock *s, uint 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);
|
|
|
|
}
|
|
|
|
|
2021-09-08 00:22:32 +08: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 20:10:10 +08:00
|
|
|
static int
|
2016-10-14 21:37:04 +08:00
|
|
|
cli_connect(sock *s, uint 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 */
|
2016-04-06 17:49:34 +08:00
|
|
|
s->fast_rx = 1;
|
1999-10-29 20:10:10 +08:00
|
|
|
c->rx_pos = c->rx_buf;
|
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;
|
2021-09-08 00:22:32 +08:00
|
|
|
s->err_hook = cli_connect_err;
|
2000-04-26 21:26:11 +08:00
|
|
|
s->rbsize = 1024;
|
2016-04-06 17:49:34 +08:00
|
|
|
s->fast_rx = 1;
|
2014-05-18 17:42:26 +08: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 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
|
|
|
}
|
|
|
|
|
2013-10-06 01:30:12 +08: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 23:01:29 +08:00
|
|
|
pl = bsnprintf(ps, sizeof(ps), "%ld\n", (s64) getpid());
|
2013-10-06 01:30:12 +08:00
|
|
|
if (pl < 0)
|
|
|
|
bug("PID buffer too small");
|
|
|
|
|
|
|
|
rv = ftruncate(pid_fd, 0);
|
|
|
|
if (rv < 0)
|
|
|
|
die("fruncate: %m");
|
2016-11-14 21:53:10 +08:00
|
|
|
|
2013-10-06 01:30:12 +08: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-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");
|
2019-06-18 22:27:21 +08:00
|
|
|
order_shutdown(0);
|
2010-02-21 16:57:26 +08:00
|
|
|
}
|
|
|
|
|
1999-02-14 04:15:36 +08:00
|
|
|
void
|
|
|
|
async_shutdown(void)
|
|
|
|
{
|
2000-03-13 05:01:38 +08:00
|
|
|
DBG("Shutting down...\n");
|
2019-06-18 22:27:21 +08:00
|
|
|
order_shutdown(0);
|
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
|
|
|
{
|
2013-10-06 01:30:12 +08:00
|
|
|
unlink_pid_file();
|
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
|
|
|
}
|
|
|
|
|
2019-06-18 22:27:21 +08:00
|
|
|
void
|
|
|
|
cmd_graceful_restart(void)
|
|
|
|
{
|
|
|
|
if (cli_access_restricted())
|
|
|
|
return;
|
|
|
|
|
|
|
|
cli_msg(25, "Graceful restart requested");
|
|
|
|
order_shutdown(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
/*
|
|
|
|
* Signals
|
|
|
|
*/
|
|
|
|
|
2019-10-04 18:20:02 +08: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 17:14:54 +08:00
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2015-03-02 16:41:14 +08:00
|
|
|
void watchdog_sigalrm(int sig UNUSED);
|
|
|
|
|
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);
|
2015-03-02 16:41:14 +08:00
|
|
|
sa.sa_handler = watchdog_sigalrm;
|
|
|
|
sa.sa_flags = 0;
|
|
|
|
sigaction(SIGALRM, &sa, NULL);
|
1999-02-14 03:43:21 +08:00
|
|
|
signal(SIGPIPE, SIG_IGN);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Parsing of command-line arguments
|
|
|
|
*/
|
|
|
|
|
2022-03-09 16:13:20 +08:00
|
|
|
static char *opt_list = "bc:dD:ps:P:u:g:flRh";
|
2020-05-31 19:21:55 +08:00
|
|
|
int parse_and_exit;
|
2010-04-07 17:00:36 +08:00
|
|
|
char *bird_name;
|
2011-05-10 08:42:17 +08:00
|
|
|
static char *use_user;
|
|
|
|
static char *use_group;
|
2013-10-06 04:45:08 +08:00
|
|
|
static int run_in_foreground = 0;
|
2000-05-09 03:10:36 +08:00
|
|
|
|
1999-02-14 03:43:21 +08:00
|
|
|
static void
|
2016-09-08 22:27:40 +08:00
|
|
|
display_usage(void)
|
1999-02-14 03:43:21 +08:00
|
|
|
{
|
2016-09-08 22:27:40 +08: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-05 06:49:26 +08:00
|
|
|
" -c <config-file> Use given configuration file instead of\n"
|
|
|
|
" " PATH_CONFIG_FILE "\n"
|
2016-09-08 22:27:40 +08: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-05 06:49:26 +08:00
|
|
|
" -l Look for a configuration file and a control socket\n"
|
|
|
|
" in the current working directory\n"
|
2016-09-08 22:27:40 +08: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-14 03:43:21 +08:00
|
|
|
}
|
|
|
|
|
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;
|
2016-04-07 18:20:45 +08:00
|
|
|
|
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)
|
|
|
|
{
|
2016-04-07 18:20:45 +08:00
|
|
|
int config_changed = 0;
|
|
|
|
int socket_changed = 0;
|
1999-02-14 03:43:21 +08:00
|
|
|
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"))
|
2016-09-08 22:27:40 +08:00
|
|
|
display_version();
|
2000-05-30 05:58:35 +08:00
|
|
|
if (!strcmp(argv[1], "--help"))
|
2016-09-08 22:27:40 +08:00
|
|
|
display_help();
|
2000-05-30 05:58:35 +08:00
|
|
|
}
|
1999-02-14 03:43:21 +08:00
|
|
|
while ((c = getopt(argc, argv, opt_list)) >= 0)
|
|
|
|
switch (c)
|
|
|
|
{
|
|
|
|
case 'c':
|
|
|
|
config_name = optarg;
|
2016-04-07 18:20:45 +08:00
|
|
|
config_changed = 1;
|
1999-02-14 03:43:21 +08:00
|
|
|
break;
|
|
|
|
case 'd':
|
2018-12-04 23:55:25 +08:00
|
|
|
log_init_debug("");
|
2018-08-28 22:45:50 +08:00
|
|
|
run_in_foreground = 1;
|
1999-12-06 21:45:56 +08:00
|
|
|
break;
|
|
|
|
case 'D':
|
1999-02-14 03:43:21 +08:00
|
|
|
log_init_debug(optarg);
|
|
|
|
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;
|
2016-04-07 18:20:45 +08:00
|
|
|
socket_changed = 1;
|
1999-12-08 21:20:19 +08:00
|
|
|
break;
|
2013-10-06 01:30:12 +08:00
|
|
|
case 'P':
|
|
|
|
pid_file = optarg;
|
|
|
|
break;
|
2011-05-10 08:42:17 +08:00
|
|
|
case 'u':
|
|
|
|
use_user = optarg;
|
|
|
|
break;
|
|
|
|
case 'g':
|
|
|
|
use_group = optarg;
|
|
|
|
break;
|
2013-10-06 04:45:08 +08:00
|
|
|
case 'f':
|
|
|
|
run_in_foreground = 1;
|
|
|
|
break;
|
2016-04-07 18:20:45 +08: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 21:07:12 +08:00
|
|
|
case 'R':
|
|
|
|
graceful_restart_recovery();
|
|
|
|
break;
|
2016-09-08 22:27:40 +08:00
|
|
|
case 'h':
|
|
|
|
display_help();
|
|
|
|
break;
|
1999-02-14 03:43:21 +08:00
|
|
|
default:
|
2016-09-08 22:27:40 +08:00
|
|
|
fputc('\n', stderr);
|
|
|
|
display_usage();
|
|
|
|
exit(1);
|
1999-02-14 03:43:21 +08:00
|
|
|
}
|
|
|
|
if (optind < argc)
|
2016-09-08 22:27:40 +08:00
|
|
|
{
|
|
|
|
display_usage();
|
|
|
|
exit(1);
|
|
|
|
}
|
1999-02-14 03:43:21 +08:00
|
|
|
}
|
|
|
|
|
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);
|
2018-12-04 23:55:25 +08:00
|
|
|
log_switch(1, NULL, NULL);
|
1999-02-14 03:43:21 +08:00
|
|
|
|
2021-04-02 01:20:13 +08:00
|
|
|
random_init();
|
2017-12-10 20:18:36 +08:00
|
|
|
net_init();
|
1998-11-28 05:09:57 +08:00
|
|
|
resource_init();
|
2017-05-31 01:12:35 +08:00
|
|
|
timer_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();
|
2015-12-16 22:30:44 +08:00
|
|
|
// roa_init();
|
2012-12-26 19:40:48 +08:00
|
|
|
config_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
|
|
|
|
2013-10-06 01:30:12 +08:00
|
|
|
if (!parse_and_exit)
|
|
|
|
open_pid_file();
|
|
|
|
|
1998-10-18 19:53:21 +08:00
|
|
|
protos_build();
|
1998-11-28 05:09:57 +08:00
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
struct config *conf = read_config();
|
1998-05-27 05:42:05 +08:00
|
|
|
|
2009-11-19 18:44:17 +08:00
|
|
|
if (parse_and_exit)
|
|
|
|
exit(0);
|
|
|
|
|
2018-08-28 22:45:50 +08:00
|
|
|
if (!run_in_foreground)
|
2000-05-09 03:10:36 +08:00
|
|
|
{
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2014-02-07 20:09:55 +08:00
|
|
|
main_thread_init();
|
|
|
|
|
2013-10-06 01:30:12 +08:00
|
|
|
write_pid_file();
|
|
|
|
|
1998-05-24 22:50:18 +08:00
|
|
|
signal_init();
|
|
|
|
|
2013-10-06 02:12:28 +08:00
|
|
|
config_commit(conf, RECONFIG_HARD, 0);
|
|
|
|
|
2014-03-20 21:07:12 +08:00
|
|
|
graceful_restart_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
|
|
|
}
|