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"
50 debug("INTERNAL STATE DUMP\n\n");
54 // XXXX tm_dump_all();
68 #ifdef CONFIG_RESTRICTED_PRIVILEGES
69 #include CONFIG_INCLUDE_SYSPRIV_H
73 drop_uid(uid_t uid UNUSED
)
75 die("Cannot change user on this platform");
88 * Reading the Configuration
91 #ifdef PATH_IPROUTE_DIR
94 add_num_const(char *name
, int val
)
96 struct symbol
*s
= cf_get_symbol(name
);
97 s
->class = SYM_CONSTANT
| T_INT
;
98 s
->def
= cfg_allocz(sizeof(struct f_val
));
103 /* the code of read_iproute_table() is based on
104 rtnl_tab_initialize() from iproute2 package */
106 read_iproute_table(char *file
, char *prefix
, int max
)
108 char buf
[512], namebuf
[512];
113 strcpy(namebuf
, prefix
);
114 name
= namebuf
+ strlen(prefix
);
116 fp
= fopen(file
, "r");
120 while (fgets(buf
, sizeof(buf
), fp
))
124 while (*p
== ' ' || *p
== '\t')
127 if (*p
== '#' || *p
== '\n' || *p
== 0)
130 if (sscanf(p
, "0x%x %s\n", &val
, name
) != 2 &&
131 sscanf(p
, "0x%x %s #", &val
, name
) != 2 &&
132 sscanf(p
, "%d %s\n", &val
, name
) != 2 &&
133 sscanf(p
, "%d %s #", &val
, name
) != 2)
136 if (val
< 0 || val
> max
)
139 for(p
= name
; *p
; p
++)
140 if ((*p
< 'a' || *p
> 'z') && (*p
< '0' || *p
> '9') && (*p
!= '_'))
143 add_num_const(namebuf
, val
);
149 #endif // PATH_IPROUTE_DIR
152 static char *config_name
= PATH_CONFIG_FILE
;
155 cf_read(byte
*dest
, uint len
, int fd
)
157 int l
= read(fd
, dest
, len
);
159 cf_error("Read error");
164 sysdep_preconfig(struct config
*c
)
166 init_list(&c
->logfiles
);
168 c
->latency_limit
= UNIX_DEFAULT_LATENCY_LIMIT
;
169 c
->watchdog_warning
= UNIX_DEFAULT_WATCHDOG_WARNING
;
171 #ifdef PATH_IPROUTE_DIR
172 read_iproute_table(PATH_IPROUTE_DIR
"/rt_protos", "ipp_", 256);
173 read_iproute_table(PATH_IPROUTE_DIR
"/rt_realms", "ipr_", 256);
174 read_iproute_table(PATH_IPROUTE_DIR
"/rt_scopes", "ips_", 256);
175 read_iproute_table(PATH_IPROUTE_DIR
"/rt_tables", "ipt_", 256);
180 sysdep_commit(struct config
*new, struct config
*old UNUSED
)
182 log_switch(0, &new->logfiles
, new->syslog_name
);
187 unix_read_config(struct config
**cp
, char *name
)
189 struct config
*conf
= config_alloc(name
);
193 conf
->file_fd
= open(name
, O_RDONLY
);
194 if (conf
->file_fd
< 0)
196 cf_read_hook
= cf_read
;
197 ret
= config_parse(conf
);
198 close(conf
->file_fd
);
202 static struct config
*
207 if (!unix_read_config(&conf
, config_name
))
210 die("%s:%d:%d %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_chno
, conf
->err_msg
);
212 die("Unable to open configuration file %s: %m", config_name
);
223 log(L_INFO
"Reconfiguration requested by SIGHUP");
224 if (!unix_read_config(&conf
, config_name
))
227 log(L_ERR
"%s:%d:%d %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_chno
, conf
->err_msg
);
229 log(L_ERR
"Unable to open configuration file %s: %m", config_name
);
233 config_commit(conf
, RECONFIG_HARD
, 0);
236 static struct config
*
237 cmd_read_config(char *name
)
244 cli_msg(-2, "Reading configuration from %s", name
);
245 if (!unix_read_config(&conf
, name
))
248 cli_msg(8002, "%s:%d:%d %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_chno
, conf
->err_msg
);
250 cli_msg(8002, "%s: %m", name
);
259 cmd_check_config(char *name
)
261 struct config
*conf
= cmd_read_config(name
);
265 cli_msg(20, "Configuration OK");
270 cmd_reconfig_msg(int r
)
274 case CONF_DONE
: cli_msg( 3, "Reconfigured"); break;
275 case CONF_PROGRESS
: cli_msg( 4, "Reconfiguration in progress"); break;
276 case CONF_QUEUED
: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
277 case CONF_UNQUEUED
: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
278 case CONF_CONFIRM
: cli_msg(18, "Reconfiguration confirmed"); break;
279 case CONF_SHUTDOWN
: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
280 case CONF_NOTHING
: cli_msg(19, "Nothing to do"); break;
285 /* Hack for scheduled undo notification */
286 cli
*cmd_reconfig_stored_cli
;
289 cmd_reconfig_undo_notify(void)
291 if (cmd_reconfig_stored_cli
)
293 cli
*c
= cmd_reconfig_stored_cli
;
294 cli_printf(c
, CLI_ASYNC_CODE
, "Config timeout expired, starting undo");
295 cli_write_trigger(c
);
300 cmd_reconfig(char *name
, int type
, uint timeout
)
302 if (cli_access_restricted())
305 struct config
*conf
= cmd_read_config(name
);
309 int r
= config_commit(conf
, type
, timeout
);
311 if ((r
>= 0) && (timeout
> 0))
313 cmd_reconfig_stored_cli
= this_cli
;
314 cli_msg(-22, "Undo scheduled in %d s", timeout
);
321 cmd_reconfig_confirm(void)
323 if (cli_access_restricted())
326 int r
= config_confirm();
331 cmd_reconfig_undo(void)
333 if (cli_access_restricted())
336 cli_msg(-21, "Undo requested");
338 int r
= config_undo();
343 * Command-Line Interface
347 static char *path_control_socket
= PATH_CONTROL_SOCKET
;
357 struct cli_out
*o
= c
->tx_pos
;
359 int len
= o
->wpos
- o
->outpos
;
363 if (sk_send(s
, len
) <= 0)
369 /* Everything is written */
375 cli_write_trigger(cli
*c
)
390 cli_get_command(cli
*c
)
393 byte
*t
= c
->rx_aux
? : s
->rbuf
;
394 byte
*tend
= s
->rpos
;
396 byte
*dend
= c
->rx_buf
+ CLI_RX_BUF_SIZE
- 2;
405 c
->rx_pos
= c
->rx_buf
;
408 return (d
< dend
) ? 1 : -1;
413 c
->rx_aux
= s
->rpos
= s
->rbuf
;
419 cli_rx(sock
*s
, uint size UNUSED
)
426 cli_err(sock
*s
, int err
)
428 if (config
->cli_debug
)
431 log(L_INFO
"CLI connection dropped: %s", strerror(err
));
433 log(L_INFO
"CLI connection closed");
439 cli_connect(sock
*s
, uint size UNUSED
)
443 if (config
->cli_debug
)
444 log(L_INFO
"CLI connect");
447 s
->err_hook
= cli_err
;
448 s
->data
= c
= cli_new(s
);
449 s
->pool
= c
->pool
; /* We need to have all the socket buffers allocated in the cli pool */
451 c
->rx_pos
= c
->rx_buf
;
458 cli_init_unix(uid_t use_uid
, gid_t use_gid
)
463 s
= cli_sk
= sk_new(cli_pool
);
464 s
->type
= SK_UNIX_PASSIVE
;
465 s
->rx_hook
= cli_connect
;
469 /* Return value intentionally ignored */
470 unlink(path_control_socket
);
472 if (sk_open_unix(s
, path_control_socket
) < 0)
473 die("Cannot create control socket %s: %m", path_control_socket
);
475 if (use_uid
|| use_gid
)
476 if (chown(path_control_socket
, use_uid
, use_gid
) < 0)
479 if (chmod(path_control_socket
, 0660) < 0)
487 static char *pid_file
;
496 pid_fd
= open(pid_file
, O_WRONLY
|O_CREAT
, 0664);
498 die("Cannot create PID file %s: %m", pid_file
);
510 /* We don't use PID file for uniqueness, so no need for locking */
512 pl
= bsnprintf(ps
, sizeof(ps
), "%ld\n", (long) getpid());
514 bug("PID buffer too small");
516 rv
= ftruncate(pid_fd
, 0);
520 rv
= write(pid_fd
, ps
, pl
);
528 unlink_pid_file(void)
542 if (cli_access_restricted())
545 cli_msg(7, "Shutdown requested");
552 DBG("Shutting down...\n");
557 sysdep_shutdown_done(void)
560 unlink(path_control_socket
);
561 log_msg(L_FATAL
"Shutdown completed");
569 volatile int async_config_flag
;
570 volatile int async_dump_flag
;
571 volatile int async_shutdown_flag
;
574 handle_sighup(int sig UNUSED
)
576 DBG("Caught SIGHUP...\n");
577 async_config_flag
= 1;
581 handle_sigusr(int sig UNUSED
)
583 DBG("Caught SIGUSR...\n");
588 handle_sigterm(int sig UNUSED
)
590 DBG("Caught SIGTERM...\n");
591 async_shutdown_flag
= 1;
594 void watchdog_sigalrm(int sig UNUSED
);
601 bzero(&sa
, sizeof(sa
));
602 sa
.sa_handler
= handle_sigusr
;
603 sa
.sa_flags
= SA_RESTART
;
604 sigaction(SIGUSR1
, &sa
, NULL
);
605 sa
.sa_handler
= handle_sighup
;
606 sa
.sa_flags
= SA_RESTART
;
607 sigaction(SIGHUP
, &sa
, NULL
);
608 sa
.sa_handler
= handle_sigterm
;
609 sa
.sa_flags
= SA_RESTART
;
610 sigaction(SIGTERM
, &sa
, NULL
);
611 sa
.sa_handler
= watchdog_sigalrm
;
613 sigaction(SIGALRM
, &sa
, NULL
);
614 signal(SIGPIPE
, SIG_IGN
);
618 * Parsing of command-line arguments
621 static char *opt_list
= "bc:dD:ps:P:u:g:flRh";
622 static int parse_and_exit
;
624 static char *use_user
;
625 static char *use_group
;
626 static int run_in_foreground
= 0;
631 fprintf(stderr
, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name
);
642 " -c <config-file> Use given configuration file instead of\n"
643 " " PATH_CONFIG_FILE
"\n"
644 " -d Enable debug messages and run bird in foreground\n"
645 " -D <debug-file> Log debug messages to given file instead of stderr\n"
646 " -f Run bird in foreground\n"
647 " -g <group> Use given group ID\n"
648 " -h, --help Display this information\n"
649 " -l Look for a configuration file and a control socket\n"
650 " in the current working directory\n"
651 " -p Test configuration file and exit without start\n"
652 " -P <pid-file> Create a PID file with given filename\n"
653 " -R Apply graceful restart recovery after start\n"
654 " -s <control-socket> Use given filename for a control socket\n"
655 " -u <user> Drop privileges and use given user ID\n"
656 " --version Display version of BIRD\n");
662 display_version(void)
664 fprintf(stderr
, "BIRD version " BIRD_VERSION
"\n");
669 get_bird_name(char *s
, char *def
)
683 get_uid(const char *s
)
693 rv
= strtol(s
, &endptr
, 10);
695 if (!errno
&& !*endptr
)
700 die("Cannot find user '%s'", s
);
706 get_gid(const char *s
)
716 rv
= strtol(s
, &endptr
, 10);
718 if (!errno
&& !*endptr
)
723 die("Cannot find group '%s'", s
);
729 parse_args(int argc
, char **argv
)
731 int config_changed
= 0;
732 int socket_changed
= 0;
735 bird_name
= get_bird_name(argv
[0], "bird");
738 if (!strcmp(argv
[1], "--version"))
740 if (!strcmp(argv
[1], "--help"))
743 while ((c
= getopt(argc
, argv
, opt_list
)) >= 0)
747 config_name
= optarg
;
752 run_in_foreground
= 1;
755 log_init_debug(optarg
);
761 path_control_socket
= optarg
;
774 run_in_foreground
= 1;
778 config_name
= xbasename(config_name
);
780 path_control_socket
= xbasename(path_control_socket
);
783 graceful_restart_recovery();
805 main(int argc
, char **argv
)
807 #ifdef HAVE_LIBDMALLOC
808 if (!getenv("DMALLOC_OPTIONS"))
809 dmalloc_debug(0x2f03d00);
812 parse_args(argc
, argv
);
813 log_switch(1, NULL
, NULL
);
825 uid_t use_uid
= get_uid(use_user
);
826 gid_t use_gid
= get_gid(use_group
);
830 test_old_bird(path_control_socket
);
831 cli_init_unix(use_uid
, use_gid
);
844 proto_build(&proto_unix_kernel
);
845 proto_build(&proto_unix_iface
);
847 struct config
*conf
= read_config();
852 if (!run_in_foreground
)
861 if (open("/dev/null", O_RDWR
) < 0)
862 die("Cannot open /dev/null: %m");
873 config_commit(conf
, RECONFIG_HARD
, 0);
875 graceful_restart_init();
881 log(L_INFO
"Started");
882 DBG("Entering I/O loop.\n");
885 bug("I/O loop died");