2 * BIRD Internet Routing Daemon -- Unix Entry Point
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
6 * Can be freely distributed and used under the terms of the GNU GPL.
25 #include "nest/bird.h"
26 #include "lib/lists.h"
27 #include "lib/resource.h"
28 #include "lib/socket.h"
29 #include "lib/event.h"
30 #include "lib/timer.h"
31 #include "lib/string.h"
32 #include "nest/route.h"
33 #include "nest/protocol.h"
34 #include "nest/iface.h"
36 #include "nest/locks.h"
37 #include "conf/conf.h"
38 #include "filter/filter.h"
39 #include "filter/data.h"
51 debug("INTERNAL STATE DUMP\n\n");
55 // XXXX tm_dump_all();
69 #ifdef CONFIG_RESTRICTED_PRIVILEGES
70 #include CONFIG_INCLUDE_SYSPRIV_H
74 drop_uid(uid_t uid UNUSED
)
76 die("Cannot change user on this platform");
89 * Reading the Configuration
92 #ifdef PATH_IPROUTE_DIR
95 add_num_const(char *name
, int val
)
97 struct f_val
*v
= cfg_alloc(sizeof(struct f_val
));
98 *v
= (struct f_val
) { .type
= T_INT
, .val
.i
= val
};
99 cf_define_symbol(cf_get_symbol(name
), SYM_CONSTANT
| T_INT
, val
, v
);
102 /* the code of read_iproute_table() is based on
103 rtnl_tab_initialize() from iproute2 package */
105 read_iproute_table(char *file
, char *prefix
, int max
)
107 char buf
[512], namebuf
[512];
112 strcpy(namebuf
, prefix
);
113 name
= namebuf
+ strlen(prefix
);
115 fp
= fopen(file
, "r");
119 while (fgets(buf
, sizeof(buf
), fp
))
123 while (*p
== ' ' || *p
== '\t')
126 if (*p
== '#' || *p
== '\n' || *p
== 0)
129 if (sscanf(p
, "0x%x %s\n", &val
, name
) != 2 &&
130 sscanf(p
, "0x%x %s #", &val
, name
) != 2 &&
131 sscanf(p
, "%d %s\n", &val
, name
) != 2 &&
132 sscanf(p
, "%d %s #", &val
, name
) != 2)
135 if (val
< 0 || val
> max
)
138 for(p
= name
; *p
; p
++)
139 if ((*p
< 'a' || *p
> 'z') && (*p
< '0' || *p
> '9') && (*p
!= '_'))
142 add_num_const(namebuf
, val
);
148 #endif // PATH_IPROUTE_DIR
151 static char *config_name
= PATH_CONFIG_FILE
;
154 cf_read(byte
*dest
, uint len
, int fd
)
156 int l
= read(fd
, dest
, len
);
158 cf_error("Read error");
163 sysdep_preconfig(struct config
*c
)
165 init_list(&c
->logfiles
);
167 c
->latency_limit
= UNIX_DEFAULT_LATENCY_LIMIT
;
168 c
->watchdog_warning
= UNIX_DEFAULT_WATCHDOG_WARNING
;
170 #ifdef PATH_IPROUTE_DIR
171 read_iproute_table(PATH_IPROUTE_DIR
"/rt_protos", "ipp_", 256);
172 read_iproute_table(PATH_IPROUTE_DIR
"/rt_realms", "ipr_", 256);
173 read_iproute_table(PATH_IPROUTE_DIR
"/rt_scopes", "ips_", 256);
174 read_iproute_table(PATH_IPROUTE_DIR
"/rt_tables", "ipt_", 256);
179 sysdep_commit(struct config
*new, struct config
*old UNUSED
)
181 log_switch(0, &new->logfiles
, new->syslog_name
);
186 unix_read_config(struct config
**cp
, char *name
)
188 struct config
*conf
= config_alloc(name
);
192 conf
->file_fd
= open(name
, O_RDONLY
);
193 if (conf
->file_fd
< 0)
195 cf_read_hook
= cf_read
;
196 ret
= config_parse(conf
);
197 close(conf
->file_fd
);
201 static struct config
*
206 if (!unix_read_config(&conf
, config_name
))
209 die("%s:%d:%d %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_chno
, conf
->err_msg
);
211 die("Unable to open configuration file %s: %m", config_name
);
222 log(L_INFO
"Reconfiguration requested by SIGHUP");
223 if (!unix_read_config(&conf
, config_name
))
226 log(L_ERR
"%s:%d:%d %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_chno
, conf
->err_msg
);
228 log(L_ERR
"Unable to open configuration file %s: %m", config_name
);
232 config_commit(conf
, RECONFIG_HARD
, 0);
235 static struct config
*
236 cmd_read_config(char *name
)
243 cli_msg(-2, "Reading configuration from %s", name
);
244 if (!unix_read_config(&conf
, name
))
247 cli_msg(8002, "%s:%d:%d %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_chno
, conf
->err_msg
);
249 cli_msg(8002, "%s: %m", name
);
258 cmd_check_config(char *name
)
260 struct config
*conf
= cmd_read_config(name
);
264 cli_msg(20, "Configuration OK");
269 cmd_reconfig_msg(int r
)
273 case CONF_DONE
: cli_msg( 3, "Reconfigured"); break;
274 case CONF_PROGRESS
: cli_msg( 4, "Reconfiguration in progress"); break;
275 case CONF_QUEUED
: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
276 case CONF_UNQUEUED
: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
277 case CONF_CONFIRM
: cli_msg(18, "Reconfiguration confirmed"); break;
278 case CONF_SHUTDOWN
: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
279 case CONF_NOTHING
: cli_msg(19, "Nothing to do"); break;
284 /* Hack for scheduled undo notification */
285 cli
*cmd_reconfig_stored_cli
;
288 cmd_reconfig_undo_notify(void)
290 if (cmd_reconfig_stored_cli
)
292 cli
*c
= cmd_reconfig_stored_cli
;
293 cli_printf(c
, CLI_ASYNC_CODE
, "Config timeout expired, starting undo");
294 cli_write_trigger(c
);
299 cmd_reconfig(char *name
, int type
, uint timeout
)
301 if (cli_access_restricted())
304 struct config
*conf
= cmd_read_config(name
);
308 int r
= config_commit(conf
, type
, timeout
);
310 if ((r
>= 0) && (timeout
> 0))
312 cmd_reconfig_stored_cli
= this_cli
;
313 cli_msg(-22, "Undo scheduled in %d s", timeout
);
320 cmd_reconfig_confirm(void)
322 if (cli_access_restricted())
325 int r
= config_confirm();
330 cmd_reconfig_undo(void)
332 if (cli_access_restricted())
335 cli_msg(-21, "Undo requested");
337 int r
= config_undo();
342 cmd_reconfig_status(void)
344 int s
= config_status();
345 btime t
= config_timer_status();
349 case CONF_DONE
: cli_msg(-3, "Daemon is up and running"); break;
350 case CONF_PROGRESS
: cli_msg(-4, "Reconfiguration in progress"); break;
351 case CONF_QUEUED
: cli_msg(-5, "Reconfiguration in progress, next one enqueued"); break;
352 case CONF_SHUTDOWN
: cli_msg(-6, "Shutdown in progress"); break;
357 cli_msg(-22, "Configuration unconfirmed, undo in %t s", t
);
364 * Command-Line Interface
368 static char *path_control_socket
= PATH_CONTROL_SOCKET
;
378 struct cli_out
*o
= c
->tx_pos
;
380 int len
= o
->wpos
- o
->outpos
;
384 if (sk_send(s
, len
) <= 0)
390 /* Everything is written */
396 cli_write_trigger(cli
*c
)
411 cli_get_command(cli
*c
)
414 byte
*t
= c
->rx_aux
? : s
->rbuf
;
415 byte
*tend
= s
->rpos
;
417 byte
*dend
= c
->rx_buf
+ CLI_RX_BUF_SIZE
- 2;
426 c
->rx_pos
= c
->rx_buf
;
429 return (d
< dend
) ? 1 : -1;
434 c
->rx_aux
= s
->rpos
= s
->rbuf
;
440 cli_rx(sock
*s
, uint size UNUSED
)
447 cli_err(sock
*s
, int err
)
449 if (config
->cli_debug
)
452 log(L_INFO
"CLI connection dropped: %s", strerror(err
));
454 log(L_INFO
"CLI connection closed");
460 cli_connect(sock
*s
, uint size UNUSED
)
464 if (config
->cli_debug
)
465 log(L_INFO
"CLI connect");
468 s
->err_hook
= cli_err
;
469 s
->data
= c
= cli_new(s
);
470 s
->pool
= c
->pool
; /* We need to have all the socket buffers allocated in the cli pool */
472 c
->rx_pos
= c
->rx_buf
;
479 cli_init_unix(uid_t use_uid
, gid_t use_gid
)
484 s
= cli_sk
= sk_new(cli_pool
);
485 s
->type
= SK_UNIX_PASSIVE
;
486 s
->rx_hook
= cli_connect
;
490 /* Return value intentionally ignored */
491 unlink(path_control_socket
);
493 if (sk_open_unix(s
, path_control_socket
) < 0)
494 die("Cannot create control socket %s: %m", path_control_socket
);
496 if (use_uid
|| use_gid
)
497 if (chown(path_control_socket
, use_uid
, use_gid
) < 0)
500 if (chmod(path_control_socket
, 0660) < 0)
508 static char *pid_file
;
517 pid_fd
= open(pid_file
, O_WRONLY
|O_CREAT
, 0664);
519 die("Cannot create PID file %s: %m", pid_file
);
531 /* We don't use PID file for uniqueness, so no need for locking */
533 pl
= bsnprintf(ps
, sizeof(ps
), "%ld\n", (long) getpid());
535 bug("PID buffer too small");
537 rv
= ftruncate(pid_fd
, 0);
541 rv
= write(pid_fd
, ps
, pl
);
549 unlink_pid_file(void)
563 if (cli_access_restricted())
566 cli_msg(7, "Shutdown requested");
573 DBG("Shutting down...\n");
578 sysdep_shutdown_done(void)
581 unlink(path_control_socket
);
582 log_msg(L_FATAL
"Shutdown completed");
587 cmd_graceful_restart(void)
589 if (cli_access_restricted())
592 cli_msg(25, "Graceful restart requested");
601 volatile int async_config_flag
;
602 volatile int async_dump_flag
;
603 volatile int async_shutdown_flag
;
606 handle_sighup(int sig UNUSED
)
608 DBG("Caught SIGHUP...\n");
609 async_config_flag
= 1;
613 handle_sigusr(int sig UNUSED
)
615 DBG("Caught SIGUSR...\n");
620 handle_sigterm(int sig UNUSED
)
622 DBG("Caught SIGTERM...\n");
623 async_shutdown_flag
= 1;
626 void watchdog_sigalrm(int sig UNUSED
);
633 bzero(&sa
, sizeof(sa
));
634 sa
.sa_handler
= handle_sigusr
;
635 sa
.sa_flags
= SA_RESTART
;
636 sigaction(SIGUSR1
, &sa
, NULL
);
637 sa
.sa_handler
= handle_sighup
;
638 sa
.sa_flags
= SA_RESTART
;
639 sigaction(SIGHUP
, &sa
, NULL
);
640 sa
.sa_handler
= handle_sigterm
;
641 sa
.sa_flags
= SA_RESTART
;
642 sigaction(SIGTERM
, &sa
, NULL
);
643 sa
.sa_handler
= watchdog_sigalrm
;
645 sigaction(SIGALRM
, &sa
, NULL
);
646 signal(SIGPIPE
, SIG_IGN
);
650 * Parsing of command-line arguments
653 static char *opt_list
= "bc:dD:ps:P:u:g:flRh";
654 static int parse_and_exit
;
656 static char *use_user
;
657 static char *use_group
;
658 static int run_in_foreground
= 0;
663 fprintf(stderr
, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name
);
674 " -c <config-file> Use given configuration file instead of\n"
675 " " PATH_CONFIG_FILE
"\n"
676 " -d Enable debug messages and run bird in foreground\n"
677 " -D <debug-file> Log debug messages to given file instead of stderr\n"
678 " -f Run bird in foreground\n"
679 " -g <group> Use given group ID\n"
680 " -h, --help Display this information\n"
681 " -l Look for a configuration file and a control socket\n"
682 " in the current working directory\n"
683 " -p Test configuration file and exit without start\n"
684 " -P <pid-file> Create a PID file with given filename\n"
685 " -R Apply graceful restart recovery after start\n"
686 " -s <control-socket> Use given filename for a control socket\n"
687 " -u <user> Drop privileges and use given user ID\n"
688 " --version Display version of BIRD\n");
694 display_version(void)
696 fprintf(stderr
, "BIRD version " BIRD_VERSION
"\n");
701 get_bird_name(char *s
, char *def
)
715 get_uid(const char *s
)
725 rv
= strtol(s
, &endptr
, 10);
727 if (!errno
&& !*endptr
)
732 die("Cannot find user '%s'", s
);
738 get_gid(const char *s
)
748 rv
= strtol(s
, &endptr
, 10);
750 if (!errno
&& !*endptr
)
755 die("Cannot find group '%s'", s
);
761 parse_args(int argc
, char **argv
)
763 int config_changed
= 0;
764 int socket_changed
= 0;
767 bird_name
= get_bird_name(argv
[0], "bird");
770 if (!strcmp(argv
[1], "--version"))
772 if (!strcmp(argv
[1], "--help"))
775 while ((c
= getopt(argc
, argv
, opt_list
)) >= 0)
779 config_name
= optarg
;
784 run_in_foreground
= 1;
787 log_init_debug(optarg
);
793 path_control_socket
= optarg
;
806 run_in_foreground
= 1;
810 config_name
= xbasename(config_name
);
812 path_control_socket
= xbasename(path_control_socket
);
815 graceful_restart_recovery();
837 main(int argc
, char **argv
)
839 #ifdef HAVE_LIBDMALLOC
840 if (!getenv("DMALLOC_OPTIONS"))
841 dmalloc_debug(0x2f03d00);
844 parse_args(argc
, argv
);
845 log_switch(1, NULL
, NULL
);
857 uid_t use_uid
= get_uid(use_user
);
858 gid_t use_gid
= get_gid(use_group
);
862 test_old_bird(path_control_socket
);
863 cli_init_unix(use_uid
, use_gid
);
876 proto_build(&proto_unix_kernel
);
877 proto_build(&proto_unix_iface
);
879 struct config
*conf
= read_config();
884 if (!run_in_foreground
)
893 if (open("/dev/null", O_RDWR
) < 0)
894 die("Cannot open /dev/null: %m");
905 config_commit(conf
, RECONFIG_HARD
, 0);
907 graceful_restart_init();
913 log(L_INFO
"Started");
914 DBG("Entering I/O loop.\n");
917 bug("I/O loop died");