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.
23 #include "nest/bird.h"
24 #include "lib/lists.h"
25 #include "lib/resource.h"
26 #include "lib/socket.h"
27 #include "lib/event.h"
28 #include "lib/string.h"
29 #include "nest/route.h"
30 #include "nest/protocol.h"
31 #include "nest/iface.h"
33 #include "nest/locks.h"
34 #include "conf/conf.h"
35 #include "filter/filter.h"
45 static int debug_flag
= 1;
47 static int debug_flag
= 0;
53 debug("INTERNAL STATE DUMP\n\n");
71 #ifdef CONFIG_RESTRICTED_PRIVILEGES
72 #include "lib/syspriv.h"
78 die("Cannot change user on this platform");
91 * Reading the Configuration
94 #ifdef PATH_IPROUTE_DIR
97 add_num_const(char *name
, int val
)
99 struct symbol
*s
= cf_get_symbol(name
);
100 s
->class = SYM_CONSTANT
| T_INT
;
101 s
->def
= cfg_allocz(sizeof(struct f_val
));
106 /* the code of read_iproute_table() is based on
107 rtnl_tab_initialize() from iproute2 package */
109 read_iproute_table(char *file
, char *prefix
, int max
)
111 char buf
[512], namebuf
[512];
116 strcpy(namebuf
, prefix
);
117 name
= namebuf
+ strlen(prefix
);
119 fp
= fopen(file
, "r");
123 while (fgets(buf
, sizeof(buf
), fp
))
127 while (*p
== ' ' || *p
== '\t')
130 if (*p
== '#' || *p
== '\n' || *p
== 0)
133 if (sscanf(p
, "0x%x %s\n", &val
, name
) != 2 &&
134 sscanf(p
, "0x%x %s #", &val
, name
) != 2 &&
135 sscanf(p
, "%d %s\n", &val
, name
) != 2 &&
136 sscanf(p
, "%d %s #", &val
, name
) != 2)
139 if (val
< 0 || val
> max
)
142 for(p
= name
; *p
; p
++)
143 if ((*p
< 'a' || *p
> 'z') && (*p
< '0' || *p
> '9') && (*p
!= '_'))
146 add_num_const(namebuf
, val
);
152 #endif // PATH_IPROUTE_DIR
155 static char *config_name
= PATH_CONFIG_FILE
;
158 cf_read(byte
*dest
, uint len
, int fd
)
160 int l
= read(fd
, dest
, len
);
162 cf_error("Read error");
167 sysdep_preconfig(struct config
*c
)
169 init_list(&c
->logfiles
);
171 c
->latency_limit
= UNIX_DEFAULT_LATENCY_LIMIT
;
172 c
->watchdog_warning
= UNIX_DEFAULT_WATCHDOG_WARNING
;
174 #ifdef PATH_IPROUTE_DIR
175 read_iproute_table(PATH_IPROUTE_DIR
"/rt_protos", "ipp_", 256);
176 read_iproute_table(PATH_IPROUTE_DIR
"/rt_realms", "ipr_", 256);
177 read_iproute_table(PATH_IPROUTE_DIR
"/rt_scopes", "ips_", 256);
178 read_iproute_table(PATH_IPROUTE_DIR
"/rt_tables", "ipt_", 256);
183 sysdep_commit(struct config
*new, struct config
*old UNUSED
)
185 log_switch(debug_flag
, &new->logfiles
, new->syslog_name
);
190 unix_read_config(struct config
**cp
, char *name
)
192 struct config
*conf
= config_alloc(name
);
196 conf
->file_fd
= open(name
, O_RDONLY
);
197 if (conf
->file_fd
< 0)
199 cf_read_hook
= cf_read
;
200 ret
= config_parse(conf
);
201 close(conf
->file_fd
);
205 static struct config
*
210 if (!unix_read_config(&conf
, config_name
))
213 die("%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
215 die("Unable to open configuration file %s: %m", config_name
);
226 log(L_INFO
"Reconfiguration requested by SIGHUP");
227 if (!unix_read_config(&conf
, config_name
))
230 log(L_ERR
"%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
232 log(L_ERR
"Unable to open configuration file %s: %m", config_name
);
236 config_commit(conf
, RECONFIG_HARD
, 0);
239 static struct config
*
240 cmd_read_config(char *name
)
247 cli_msg(-2, "Reading configuration from %s", name
);
248 if (!unix_read_config(&conf
, name
))
251 cli_msg(8002, "%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
253 cli_msg(8002, "%s: %m", name
);
262 cmd_check_config(char *name
)
264 struct config
*conf
= cmd_read_config(name
);
268 cli_msg(20, "Configuration OK");
273 cmd_reconfig_msg(int r
)
277 case CONF_DONE
: cli_msg( 3, "Reconfigured"); break;
278 case CONF_PROGRESS
: cli_msg( 4, "Reconfiguration in progress"); break;
279 case CONF_QUEUED
: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
280 case CONF_UNQUEUED
: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
281 case CONF_CONFIRM
: cli_msg(18, "Reconfiguration confirmed"); break;
282 case CONF_SHUTDOWN
: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
283 case CONF_NOTHING
: cli_msg(19, "Nothing to do"); break;
288 /* Hack for scheduled undo notification */
289 cli
*cmd_reconfig_stored_cli
;
292 cmd_reconfig_undo_notify(void)
294 if (cmd_reconfig_stored_cli
)
296 cli
*c
= cmd_reconfig_stored_cli
;
297 cli_printf(c
, CLI_ASYNC_CODE
, "Config timeout expired, starting undo");
298 cli_write_trigger(c
);
303 cmd_reconfig(char *name
, int type
, int timeout
)
305 if (cli_access_restricted())
308 struct config
*conf
= cmd_read_config(name
);
312 int r
= config_commit(conf
, type
, timeout
);
314 if ((r
>= 0) && (timeout
> 0))
316 cmd_reconfig_stored_cli
= this_cli
;
317 cli_msg(-22, "Undo scheduled in %d s", timeout
);
324 cmd_reconfig_confirm(void)
326 if (cli_access_restricted())
329 int r
= config_confirm();
334 cmd_reconfig_undo(void)
336 if (cli_access_restricted())
339 cli_msg(-21, "Undo requested");
341 int r
= config_undo();
346 * Command-Line Interface
350 static char *path_control_socket
= PATH_CONTROL_SOCKET
;
360 struct cli_out
*o
= c
->tx_pos
;
362 int len
= o
->wpos
- o
->outpos
;
366 if (sk_send(s
, len
) <= 0)
372 /* Everything is written */
378 cli_write_trigger(cli
*c
)
393 cli_get_command(cli
*c
)
396 byte
*t
= c
->rx_aux
? : s
->rbuf
;
397 byte
*tend
= s
->rpos
;
399 byte
*dend
= c
->rx_buf
+ CLI_RX_BUF_SIZE
- 2;
408 c
->rx_pos
= c
->rx_buf
;
411 return (d
< dend
) ? 1 : -1;
416 c
->rx_aux
= s
->rpos
= s
->rbuf
;
422 cli_rx(sock
*s
, int size UNUSED
)
429 cli_err(sock
*s
, int err
)
431 if (config
->cli_debug
)
434 log(L_INFO
"CLI connection dropped: %s", strerror(err
));
436 log(L_INFO
"CLI connection closed");
442 cli_connect(sock
*s
, int size UNUSED
)
446 if (config
->cli_debug
)
447 log(L_INFO
"CLI connect");
450 s
->err_hook
= cli_err
;
451 s
->data
= c
= cli_new(s
);
452 s
->pool
= c
->pool
; /* We need to have all the socket buffers allocated in the cli pool */
454 c
->rx_pos
= c
->rx_buf
;
461 cli_init_unix(uid_t use_uid
, gid_t use_gid
)
466 s
= cli_sk
= sk_new(cli_pool
);
467 s
->type
= SK_UNIX_PASSIVE
;
468 s
->rx_hook
= cli_connect
;
472 /* Return value intentionally ignored */
473 unlink(path_control_socket
);
475 if (sk_open_unix(s
, path_control_socket
) < 0)
476 die("Cannot create control socket %s: %m", path_control_socket
);
478 if (use_uid
|| use_gid
)
479 if (chown(path_control_socket
, use_uid
, use_gid
) < 0)
482 if (chmod(path_control_socket
, 0660) < 0)
490 static char *pid_file
;
499 pid_fd
= open(pid_file
, O_WRONLY
|O_CREAT
, 0664);
501 die("Cannot create PID file %s: %m", pid_file
);
513 /* We don't use PID file for uniqueness, so no need for locking */
515 pl
= bsnprintf(ps
, sizeof(ps
), "%ld\n", (long) getpid());
517 bug("PID buffer too small");
519 rv
= ftruncate(pid_fd
, 0);
523 rv
= write(pid_fd
, ps
, pl
);
531 unlink_pid_file(void)
545 if (cli_access_restricted())
548 cli_msg(7, "Shutdown requested");
555 DBG("Shutting down...\n");
560 sysdep_shutdown_done(void)
563 unlink(path_control_socket
);
564 log_msg(L_FATAL
"Shutdown completed");
573 handle_sighup(int sig UNUSED
)
575 DBG("Caught SIGHUP...\n");
576 async_config_flag
= 1;
580 handle_sigusr(int sig UNUSED
)
582 DBG("Caught SIGUSR...\n");
587 handle_sigterm(int sig UNUSED
)
589 DBG("Caught SIGTERM...\n");
590 async_shutdown_flag
= 1;
593 void watchdog_sigalrm(int sig UNUSED
);
600 bzero(&sa
, sizeof(sa
));
601 sa
.sa_handler
= handle_sigusr
;
602 sa
.sa_flags
= SA_RESTART
;
603 sigaction(SIGUSR1
, &sa
, NULL
);
604 sa
.sa_handler
= handle_sighup
;
605 sa
.sa_flags
= SA_RESTART
;
606 sigaction(SIGHUP
, &sa
, NULL
);
607 sa
.sa_handler
= handle_sigterm
;
608 sa
.sa_flags
= SA_RESTART
;
609 sigaction(SIGTERM
, &sa
, NULL
);
610 sa
.sa_handler
= watchdog_sigalrm
;
612 sigaction(SIGALRM
, &sa
, NULL
);
613 signal(SIGPIPE
, SIG_IGN
);
617 * Parsing of command-line arguments
620 static char *opt_list
= "c:dD:ps:P:u:g:flRh";
621 static int parse_and_exit
;
623 static char *use_user
;
624 static char *use_group
;
625 static int run_in_foreground
= 0;
630 fprintf(stderr
, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name
);
641 " -c <config-file> Use given configuration file instead\n"
642 " of prefix/etc/bird.conf\n"
643 " -d Enable debug messages and run bird in foreground\n"
644 " -D <debug-file> Log debug messages to given file instead of stderr\n"
645 " -f Run bird in foreground\n"
646 " -g <group> Use given group ID\n"
647 " -h, --help Display this information\n"
648 " -l Look for a configuration file and a communication socket\n"
649 " file in the current working directory\n"
650 " -p Test configuration file and exit without start\n"
651 " -P <pid-file> Create a PID file with given filename\n"
652 " -R Apply graceful restart recovery after start\n"
653 " -s <control-socket> Use given filename for a control socket\n"
654 " -u <user> Drop privileges and use given user ID\n"
655 " --version Display version of BIRD\n");
661 display_version(void)
663 fprintf(stderr
, "BIRD version " BIRD_VERSION
"\n");
668 get_bird_name(char *s
, char *def
)
682 get_uid(const char *s
)
692 rv
= strtol(s
, &endptr
, 10);
694 if (!errno
&& !*endptr
)
699 die("Cannot find user '%s'", s
);
705 get_gid(const char *s
)
715 rv
= strtol(s
, &endptr
, 10);
717 if (!errno
&& !*endptr
)
722 die("Cannot find group '%s'", s
);
728 parse_args(int argc
, char **argv
)
730 int config_changed
= 0;
731 int socket_changed
= 0;
734 bird_name
= get_bird_name(argv
[0], "bird");
737 if (!strcmp(argv
[1], "--version"))
739 if (!strcmp(argv
[1], "--help"))
742 while ((c
= getopt(argc
, argv
, opt_list
)) >= 0)
746 config_name
= optarg
;
753 log_init_debug(optarg
);
760 path_control_socket
= optarg
;
773 run_in_foreground
= 1;
777 config_name
= xbasename(config_name
);
779 path_control_socket
= xbasename(path_control_socket
);
782 graceful_restart_recovery();
804 main(int argc
, char **argv
)
806 #ifdef HAVE_LIBDMALLOC
807 if (!getenv("DMALLOC_OPTIONS"))
808 dmalloc_debug(0x2f03d00);
811 parse_args(argc
, argv
);
814 log_switch(debug_flag
, NULL
, NULL
);
824 uid_t use_uid
= get_uid(use_user
);
825 gid_t use_gid
= get_gid(use_group
);
829 test_old_bird(path_control_socket
);
830 cli_init_unix(use_uid
, use_gid
);
843 proto_build(&proto_unix_kernel
);
844 proto_build(&proto_unix_iface
);
846 struct config
*conf
= read_config();
851 if (!(debug_flag
||run_in_foreground
))
860 if (open("/dev/null", O_RDWR
) < 0)
861 die("Cannot open /dev/null: %m");
872 config_commit(conf
, RECONFIG_HARD
, 0);
874 graceful_restart_init();
880 log(L_INFO
"Started");
881 DBG("Entering I/O loop.\n");
884 bug("I/O loop died");