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"
48 static int debug_flag
= 1;
50 static int debug_flag
= 0;
56 debug("INTERNAL STATE DUMP\n\n");
60 // XXXX tm_dump_all();
74 #ifdef CONFIG_RESTRICTED_PRIVILEGES
75 #include CONFIG_INCLUDE_SYSPRIV_H
79 drop_uid(uid_t uid UNUSED
)
81 die("Cannot change user on this platform");
94 * Reading the Configuration
97 #ifdef PATH_IPROUTE_DIR
100 add_num_const(char *name
, int val
)
102 struct symbol
*s
= cf_get_symbol(name
);
103 s
->class = SYM_CONSTANT
| T_INT
;
104 s
->def
= cfg_allocz(sizeof(struct f_val
));
109 /* the code of read_iproute_table() is based on
110 rtnl_tab_initialize() from iproute2 package */
112 read_iproute_table(char *file
, char *prefix
, int max
)
114 char buf
[512], namebuf
[512];
119 strcpy(namebuf
, prefix
);
120 name
= namebuf
+ strlen(prefix
);
122 fp
= fopen(file
, "r");
126 while (fgets(buf
, sizeof(buf
), fp
))
130 while (*p
== ' ' || *p
== '\t')
133 if (*p
== '#' || *p
== '\n' || *p
== 0)
136 if (sscanf(p
, "0x%x %s\n", &val
, name
) != 2 &&
137 sscanf(p
, "0x%x %s #", &val
, name
) != 2 &&
138 sscanf(p
, "%d %s\n", &val
, name
) != 2 &&
139 sscanf(p
, "%d %s #", &val
, name
) != 2)
142 if (val
< 0 || val
> max
)
145 for(p
= name
; *p
; p
++)
146 if ((*p
< 'a' || *p
> 'z') && (*p
< '0' || *p
> '9') && (*p
!= '_'))
149 add_num_const(namebuf
, val
);
155 #endif // PATH_IPROUTE_DIR
158 static char *config_name
= PATH_CONFIG_FILE
;
161 cf_read(byte
*dest
, uint len
, int fd
)
163 int l
= read(fd
, dest
, len
);
165 cf_error("Read error");
170 sysdep_preconfig(struct config
*c
)
172 init_list(&c
->logfiles
);
174 c
->latency_limit
= UNIX_DEFAULT_LATENCY_LIMIT
;
175 c
->watchdog_warning
= UNIX_DEFAULT_WATCHDOG_WARNING
;
177 #ifdef PATH_IPROUTE_DIR
178 read_iproute_table(PATH_IPROUTE_DIR
"/rt_protos", "ipp_", 256);
179 read_iproute_table(PATH_IPROUTE_DIR
"/rt_realms", "ipr_", 256);
180 read_iproute_table(PATH_IPROUTE_DIR
"/rt_scopes", "ips_", 256);
181 read_iproute_table(PATH_IPROUTE_DIR
"/rt_tables", "ipt_", 256);
186 sysdep_commit(struct config
*new, struct config
*old UNUSED
)
188 log_switch(debug_flag
, &new->logfiles
, new->syslog_name
);
193 unix_read_config(struct config
**cp
, char *name
)
195 struct config
*conf
= config_alloc(name
);
199 conf
->file_fd
= open(name
, O_RDONLY
);
200 if (conf
->file_fd
< 0)
202 cf_read_hook
= cf_read
;
203 ret
= config_parse(conf
);
204 close(conf
->file_fd
);
208 static struct config
*
213 if (!unix_read_config(&conf
, config_name
))
216 die("%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
218 die("Unable to open configuration file %s: %m", config_name
);
229 log(L_INFO
"Reconfiguration requested by SIGHUP");
230 if (!unix_read_config(&conf
, config_name
))
233 log(L_ERR
"%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
235 log(L_ERR
"Unable to open configuration file %s: %m", config_name
);
239 config_commit(conf
, RECONFIG_HARD
, 0);
242 static struct config
*
243 cmd_read_config(char *name
)
250 cli_msg(-2, "Reading configuration from %s", name
);
251 if (!unix_read_config(&conf
, name
))
254 cli_msg(8002, "%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
256 cli_msg(8002, "%s: %m", name
);
265 cmd_check_config(char *name
)
267 struct config
*conf
= cmd_read_config(name
);
271 cli_msg(20, "Configuration OK");
276 cmd_reconfig_msg(int r
)
280 case CONF_DONE
: cli_msg( 3, "Reconfigured"); break;
281 case CONF_PROGRESS
: cli_msg( 4, "Reconfiguration in progress"); break;
282 case CONF_QUEUED
: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
283 case CONF_UNQUEUED
: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
284 case CONF_CONFIRM
: cli_msg(18, "Reconfiguration confirmed"); break;
285 case CONF_SHUTDOWN
: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
286 case CONF_NOTHING
: cli_msg(19, "Nothing to do"); break;
291 /* Hack for scheduled undo notification */
292 cli
*cmd_reconfig_stored_cli
;
295 cmd_reconfig_undo_notify(void)
297 if (cmd_reconfig_stored_cli
)
299 cli
*c
= cmd_reconfig_stored_cli
;
300 cli_printf(c
, CLI_ASYNC_CODE
, "Config timeout expired, starting undo");
301 cli_write_trigger(c
);
306 cmd_reconfig(char *name
, int type
, uint timeout
)
308 if (cli_access_restricted())
311 struct config
*conf
= cmd_read_config(name
);
315 int r
= config_commit(conf
, type
, timeout
);
317 if ((r
>= 0) && (timeout
> 0))
319 cmd_reconfig_stored_cli
= this_cli
;
320 cli_msg(-22, "Undo scheduled in %d s", timeout
);
327 cmd_reconfig_confirm(void)
329 if (cli_access_restricted())
332 int r
= config_confirm();
337 cmd_reconfig_undo(void)
339 if (cli_access_restricted())
342 cli_msg(-21, "Undo requested");
344 int r
= config_undo();
349 * Command-Line Interface
353 static char *path_control_socket
= PATH_CONTROL_SOCKET
;
363 struct cli_out
*o
= c
->tx_pos
;
365 int len
= o
->wpos
- o
->outpos
;
369 if (sk_send(s
, len
) <= 0)
375 /* Everything is written */
381 cli_write_trigger(cli
*c
)
396 cli_get_command(cli
*c
)
399 byte
*t
= c
->rx_aux
? : s
->rbuf
;
400 byte
*tend
= s
->rpos
;
402 byte
*dend
= c
->rx_buf
+ CLI_RX_BUF_SIZE
- 2;
411 c
->rx_pos
= c
->rx_buf
;
414 return (d
< dend
) ? 1 : -1;
419 c
->rx_aux
= s
->rpos
= s
->rbuf
;
425 cli_rx(sock
*s
, uint size UNUSED
)
432 cli_err(sock
*s
, int err
)
434 if (config
->cli_debug
)
437 log(L_INFO
"CLI connection dropped: %s", strerror(err
));
439 log(L_INFO
"CLI connection closed");
445 cli_connect(sock
*s
, uint size UNUSED
)
449 if (config
->cli_debug
)
450 log(L_INFO
"CLI connect");
453 s
->err_hook
= cli_err
;
454 s
->data
= c
= cli_new(s
);
455 s
->pool
= c
->pool
; /* We need to have all the socket buffers allocated in the cli pool */
457 c
->rx_pos
= c
->rx_buf
;
464 cli_init_unix(uid_t use_uid
, gid_t use_gid
)
469 s
= cli_sk
= sk_new(cli_pool
);
470 s
->type
= SK_UNIX_PASSIVE
;
471 s
->rx_hook
= cli_connect
;
475 /* Return value intentionally ignored */
476 unlink(path_control_socket
);
478 if (sk_open_unix(s
, path_control_socket
) < 0)
479 die("Cannot create control socket %s: %m", path_control_socket
);
481 if (use_uid
|| use_gid
)
482 if (chown(path_control_socket
, use_uid
, use_gid
) < 0)
485 if (chmod(path_control_socket
, 0660) < 0)
493 static char *pid_file
;
502 pid_fd
= open(pid_file
, O_WRONLY
|O_CREAT
, 0664);
504 die("Cannot create PID file %s: %m", pid_file
);
516 /* We don't use PID file for uniqueness, so no need for locking */
518 pl
= bsnprintf(ps
, sizeof(ps
), "%ld\n", (long) getpid());
520 bug("PID buffer too small");
522 rv
= ftruncate(pid_fd
, 0);
526 rv
= write(pid_fd
, ps
, pl
);
534 unlink_pid_file(void)
548 if (cli_access_restricted())
551 cli_msg(7, "Shutdown requested");
558 DBG("Shutting down...\n");
563 sysdep_shutdown_done(void)
566 unlink(path_control_socket
);
567 log_msg(L_FATAL
"Shutdown completed");
575 volatile int async_config_flag
;
576 volatile int async_dump_flag
;
577 volatile int async_shutdown_flag
;
580 handle_sighup(int sig UNUSED
)
582 DBG("Caught SIGHUP...\n");
583 async_config_flag
= 1;
587 handle_sigusr(int sig UNUSED
)
589 DBG("Caught SIGUSR...\n");
594 handle_sigterm(int sig UNUSED
)
596 DBG("Caught SIGTERM...\n");
597 async_shutdown_flag
= 1;
600 void watchdog_sigalrm(int sig UNUSED
);
607 bzero(&sa
, sizeof(sa
));
608 sa
.sa_handler
= handle_sigusr
;
609 sa
.sa_flags
= SA_RESTART
;
610 sigaction(SIGUSR1
, &sa
, NULL
);
611 sa
.sa_handler
= handle_sighup
;
612 sa
.sa_flags
= SA_RESTART
;
613 sigaction(SIGHUP
, &sa
, NULL
);
614 sa
.sa_handler
= handle_sigterm
;
615 sa
.sa_flags
= SA_RESTART
;
616 sigaction(SIGTERM
, &sa
, NULL
);
617 sa
.sa_handler
= watchdog_sigalrm
;
619 sigaction(SIGALRM
, &sa
, NULL
);
620 signal(SIGPIPE
, SIG_IGN
);
624 * Parsing of command-line arguments
627 static char *opt_list
= "c:dD:ps:P:u:g:flRh";
628 static int parse_and_exit
;
630 static char *use_user
;
631 static char *use_group
;
632 static int run_in_foreground
= 0;
637 fprintf(stderr
, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name
);
648 " -c <config-file> Use given configuration file instead\n"
649 " of prefix/etc/bird.conf\n"
650 " -d Enable debug messages and run bird in foreground\n"
651 " -D <debug-file> Log debug messages to given file instead of stderr\n"
652 " -f Run bird in foreground\n"
653 " -g <group> Use given group ID\n"
654 " -h, --help Display this information\n"
655 " -l Look for a configuration file and a communication socket\n"
656 " file in the current working directory\n"
657 " -p Test configuration file and exit without start\n"
658 " -P <pid-file> Create a PID file with given filename\n"
659 " -R Apply graceful restart recovery after start\n"
660 " -s <control-socket> Use given filename for a control socket\n"
661 " -u <user> Drop privileges and use given user ID\n"
662 " --version Display version of BIRD\n");
668 display_version(void)
670 fprintf(stderr
, "BIRD version " BIRD_VERSION
"\n");
675 get_bird_name(char *s
, char *def
)
689 get_uid(const char *s
)
699 rv
= strtol(s
, &endptr
, 10);
701 if (!errno
&& !*endptr
)
706 die("Cannot find user '%s'", s
);
712 get_gid(const char *s
)
722 rv
= strtol(s
, &endptr
, 10);
724 if (!errno
&& !*endptr
)
729 die("Cannot find group '%s'", s
);
735 parse_args(int argc
, char **argv
)
737 int config_changed
= 0;
738 int socket_changed
= 0;
741 bird_name
= get_bird_name(argv
[0], "bird");
744 if (!strcmp(argv
[1], "--version"))
746 if (!strcmp(argv
[1], "--help"))
749 while ((c
= getopt(argc
, argv
, opt_list
)) >= 0)
753 config_name
= optarg
;
760 log_init_debug(optarg
);
767 path_control_socket
= optarg
;
780 run_in_foreground
= 1;
784 config_name
= xbasename(config_name
);
786 path_control_socket
= xbasename(path_control_socket
);
789 graceful_restart_recovery();
811 main(int argc
, char **argv
)
813 #ifdef HAVE_LIBDMALLOC
814 if (!getenv("DMALLOC_OPTIONS"))
815 dmalloc_debug(0x2f03d00);
818 parse_args(argc
, argv
);
821 log_switch(debug_flag
, NULL
, NULL
);
833 uid_t use_uid
= get_uid(use_user
);
834 gid_t use_gid
= get_gid(use_group
);
838 test_old_bird(path_control_socket
);
839 cli_init_unix(use_uid
, use_gid
);
852 proto_build(&proto_unix_kernel
);
853 proto_build(&proto_unix_iface
);
855 struct config
*conf
= read_config();
860 if (!(debug_flag
||run_in_foreground
))
869 if (open("/dev/null", O_RDWR
) < 0)
870 die("Cannot open /dev/null: %m");
881 config_commit(conf
, RECONFIG_HARD
, 0);
883 graceful_restart_init();
889 log(L_INFO
"Started");
890 DBG("Entering I/O loop.\n");
893 bug("I/O loop died");