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"
|
|
|
|
#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"
|
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
|
2018-08-13 09:41:27 +00:00
|
|
|
int debug_flag = 1;
|
2000-06-19 23:50:02 +00:00
|
|
|
#else
|
2018-08-13 09:41:27 +00:00
|
|
|
int debug_flag = 0;
|
2000-06-19 23:50:02 +00:00
|
|
|
#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();
|
2017-06-01 10:33:20 +00:00
|
|
|
// XXXX 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");
|
|
|
|
}
|
|
|
|
|
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
|
|
|
|
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;
|
|
|
|
byte *t = c->rx_aux ? : s->rbuf;
|
|
|
|
byte *tend = s->rpos;
|
|
|
|
byte *d = c->rx_pos;
|
|
|
|
byte *dend = c->rx_buf + CLI_RX_BUF_SIZE - 2;
|
|
|
|
|
|
|
|
while (t < tend)
|
|
|
|
{
|
|
|
|
if (*t == '\r')
|
|
|
|
t++;
|
|
|
|
else if (*t == '\n')
|
|
|
|
{
|
|
|
|
t++;
|
|
|
|
c->rx_pos = c->rx_buf;
|
|
|
|
c->rx_aux = t;
|
|
|
|
*d = 0;
|
|
|
|
return (d < dend) ? 1 : -1;
|
|
|
|
}
|
|
|
|
else if (d < dend)
|
|
|
|
*d++ = *t++;
|
|
|
|
}
|
|
|
|
c->rx_aux = s->rpos = s->rbuf;
|
|
|
|
c->rx_pos = d;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
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);
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
s->data = c = cli_new(s);
|
1999-12-08 13:20:19 +00:00
|
|
|
s->pool = c->pool; /* We need to have all the socket buffers allocated in the cli pool */
|
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;
|
|
|
|
c->rx_aux = NULL;
|
2004-05-31 22:00:18 +00:00
|
|
|
rmove(s, c->pool);
|
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");
|
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");
|
|
|
|
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
|
|
|
|
2017-12-10 12:18:36 +00:00
|
|
|
net_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();
|
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
|
|
|
}
|