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/string.h"
31 #include "nest/route.h"
32 #include "nest/protocol.h"
33 #include "nest/iface.h"
35 #include "nest/locks.h"
36 #include "conf/conf.h"
37 #include "filter/filter.h"
47 static int debug_flag
= 1;
49 static int debug_flag
= 0;
55 debug("INTERNAL STATE DUMP\n\n");
73 #ifdef CONFIG_RESTRICTED_PRIVILEGES
74 #include CONFIG_INCLUDE_SYSPRIV_H
78 drop_uid(uid_t uid UNUSED
)
80 die("Cannot change user on this platform");
93 * Reading the Configuration
96 #ifdef PATH_IPROUTE_DIR
99 add_num_const(char *name
, int val
)
101 struct symbol
*s
= cf_get_symbol(name
);
102 s
->class = SYM_CONSTANT
| T_INT
;
103 s
->def
= cfg_allocz(sizeof(struct f_val
));
108 /* the code of read_iproute_table() is based on
109 rtnl_tab_initialize() from iproute2 package */
111 read_iproute_table(char *file
, char *prefix
, int max
)
113 char buf
[512], namebuf
[512];
118 strcpy(namebuf
, prefix
);
119 name
= namebuf
+ strlen(prefix
);
121 fp
= fopen(file
, "r");
125 while (fgets(buf
, sizeof(buf
), fp
))
129 while (*p
== ' ' || *p
== '\t')
132 if (*p
== '#' || *p
== '\n' || *p
== 0)
135 if (sscanf(p
, "0x%x %s\n", &val
, name
) != 2 &&
136 sscanf(p
, "0x%x %s #", &val
, name
) != 2 &&
137 sscanf(p
, "%d %s\n", &val
, name
) != 2 &&
138 sscanf(p
, "%d %s #", &val
, name
) != 2)
141 if (val
< 0 || val
> max
)
144 for(p
= name
; *p
; p
++)
145 if ((*p
< 'a' || *p
> 'z') && (*p
< '0' || *p
> '9') && (*p
!= '_'))
148 add_num_const(namebuf
, val
);
154 #endif // PATH_IPROUTE_DIR
157 static char *config_name
= PATH_CONFIG_FILE
;
160 cf_read(byte
*dest
, uint len
, int fd
)
162 int l
= read(fd
, dest
, len
);
164 cf_error("Read error");
169 sysdep_preconfig(struct config
*c
)
171 init_list(&c
->logfiles
);
173 c
->latency_limit
= UNIX_DEFAULT_LATENCY_LIMIT
;
174 c
->watchdog_warning
= UNIX_DEFAULT_WATCHDOG_WARNING
;
176 #ifdef PATH_IPROUTE_DIR
177 read_iproute_table(PATH_IPROUTE_DIR
"/rt_protos", "ipp_", 256);
178 read_iproute_table(PATH_IPROUTE_DIR
"/rt_realms", "ipr_", 256);
179 read_iproute_table(PATH_IPROUTE_DIR
"/rt_scopes", "ips_", 256);
180 read_iproute_table(PATH_IPROUTE_DIR
"/rt_tables", "ipt_", 256);
185 sysdep_commit(struct config
*new, struct config
*old UNUSED
)
187 log_switch(debug_flag
, &new->logfiles
, new->syslog_name
);
192 unix_read_config(struct config
**cp
, char *name
)
194 struct config
*conf
= config_alloc(name
);
198 conf
->file_fd
= open(name
, O_RDONLY
);
199 if (conf
->file_fd
< 0)
201 cf_read_hook
= cf_read
;
202 ret
= config_parse(conf
);
203 close(conf
->file_fd
);
207 static struct config
*
212 if (!unix_read_config(&conf
, config_name
))
215 die("%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
217 die("Unable to open configuration file %s: %m", config_name
);
228 log(L_INFO
"Reconfiguration requested by SIGHUP");
229 if (!unix_read_config(&conf
, config_name
))
232 log(L_ERR
"%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
234 log(L_ERR
"Unable to open configuration file %s: %m", config_name
);
238 config_commit(conf
, RECONFIG_HARD
, 0);
241 static struct config
*
242 cmd_read_config(char *name
)
249 cli_msg(-2, "Reading configuration from %s", name
);
250 if (!unix_read_config(&conf
, name
))
253 cli_msg(8002, "%s, line %d: %s", conf
->err_file_name
, conf
->err_lino
, conf
->err_msg
);
255 cli_msg(8002, "%s: %m", name
);
264 cmd_check_config(char *name
)
266 struct config
*conf
= cmd_read_config(name
);
270 cli_msg(20, "Configuration OK");
275 cmd_reconfig_msg(int r
)
279 case CONF_DONE
: cli_msg( 3, "Reconfigured"); break;
280 case CONF_PROGRESS
: cli_msg( 4, "Reconfiguration in progress"); break;
281 case CONF_QUEUED
: cli_msg( 5, "Reconfiguration already in progress, queueing new config"); break;
282 case CONF_UNQUEUED
: cli_msg(17, "Reconfiguration already in progress, removing queued config"); break;
283 case CONF_CONFIRM
: cli_msg(18, "Reconfiguration confirmed"); break;
284 case CONF_SHUTDOWN
: cli_msg( 6, "Reconfiguration ignored, shutting down"); break;
285 case CONF_NOTHING
: cli_msg(19, "Nothing to do"); break;
290 /* Hack for scheduled undo notification */
291 cli
*cmd_reconfig_stored_cli
;
294 cmd_reconfig_undo_notify(void)
296 if (cmd_reconfig_stored_cli
)
298 cli
*c
= cmd_reconfig_stored_cli
;
299 cli_printf(c
, CLI_ASYNC_CODE
, "Config timeout expired, starting undo");
300 cli_write_trigger(c
);
305 cmd_reconfig(char *name
, int type
, int timeout
)
307 if (cli_access_restricted())
310 struct config
*conf
= cmd_read_config(name
);
314 int r
= config_commit(conf
, type
, timeout
);
316 if ((r
>= 0) && (timeout
> 0))
318 cmd_reconfig_stored_cli
= this_cli
;
319 cli_msg(-22, "Undo scheduled in %d s", timeout
);
326 cmd_reconfig_confirm(void)
328 if (cli_access_restricted())
331 int r
= config_confirm();
336 cmd_reconfig_undo(void)
338 if (cli_access_restricted())
341 cli_msg(-21, "Undo requested");
343 int r
= config_undo();
348 * Command-Line Interface
352 static char *path_control_socket
= PATH_CONTROL_SOCKET
;
362 struct cli_out
*o
= c
->tx_pos
;
364 int len
= o
->wpos
- o
->outpos
;
368 if (sk_send(s
, len
) <= 0)
374 /* Everything is written */
380 cli_write_trigger(cli
*c
)
395 cli_get_command(cli
*c
)
398 byte
*t
= c
->rx_aux
? : s
->rbuf
;
399 byte
*tend
= s
->rpos
;
401 byte
*dend
= c
->rx_buf
+ CLI_RX_BUF_SIZE
- 2;
410 c
->rx_pos
= c
->rx_buf
;
413 return (d
< dend
) ? 1 : -1;
418 c
->rx_aux
= s
->rpos
= s
->rbuf
;
424 cli_rx(sock
*s
, uint size UNUSED
)
431 cli_err(sock
*s
, int err
)
433 if (config
->cli_debug
)
436 log(L_INFO
"CLI connection dropped: %s", strerror(err
));
438 log(L_INFO
"CLI connection closed");
444 cli_connect(sock
*s
, uint size UNUSED
)
448 if (config
->cli_debug
)
449 log(L_INFO
"CLI connect");
452 s
->err_hook
= cli_err
;
453 s
->data
= c
= cli_new(s
);
454 s
->pool
= c
->pool
; /* We need to have all the socket buffers allocated in the cli pool */
456 c
->rx_pos
= c
->rx_buf
;
463 cli_init_unix(uid_t use_uid
, gid_t use_gid
)
468 s
= cli_sk
= sk_new(cli_pool
);
469 s
->type
= SK_UNIX_PASSIVE
;
470 s
->rx_hook
= cli_connect
;
474 /* Return value intentionally ignored */
475 unlink(path_control_socket
);
477 if (sk_open_unix(s
, path_control_socket
) < 0)
478 die("Cannot create control socket %s: %m", path_control_socket
);
480 if (use_uid
|| use_gid
)
481 if (chown(path_control_socket
, use_uid
, use_gid
) < 0)
484 if (chmod(path_control_socket
, 0660) < 0)
492 static char *pid_file
;
501 pid_fd
= open(pid_file
, O_WRONLY
|O_CREAT
, 0664);
503 die("Cannot create PID file %s: %m", pid_file
);
515 /* We don't use PID file for uniqueness, so no need for locking */
517 pl
= bsnprintf(ps
, sizeof(ps
), "%ld\n", (long) getpid());
519 bug("PID buffer too small");
521 rv
= ftruncate(pid_fd
, 0);
525 rv
= write(pid_fd
, ps
, pl
);
533 unlink_pid_file(void)
547 if (cli_access_restricted())
550 cli_msg(7, "Shutdown requested");
557 DBG("Shutting down...\n");
562 sysdep_shutdown_done(void)
565 unlink(path_control_socket
);
566 log_msg(L_FATAL
"Shutdown completed");
574 volatile int async_config_flag
;
575 volatile int async_dump_flag
;
576 volatile int async_shutdown_flag
;
579 handle_sighup(int sig UNUSED
)
581 DBG("Caught SIGHUP...\n");
582 async_config_flag
= 1;
586 handle_sigusr(int sig UNUSED
)
588 DBG("Caught SIGUSR...\n");
593 handle_sigterm(int sig UNUSED
)
595 DBG("Caught SIGTERM...\n");
596 async_shutdown_flag
= 1;
599 void watchdog_sigalrm(int sig UNUSED
);
606 bzero(&sa
, sizeof(sa
));
607 sa
.sa_handler
= handle_sigusr
;
608 sa
.sa_flags
= SA_RESTART
;
609 sigaction(SIGUSR1
, &sa
, NULL
);
610 sa
.sa_handler
= handle_sighup
;
611 sa
.sa_flags
= SA_RESTART
;
612 sigaction(SIGHUP
, &sa
, NULL
);
613 sa
.sa_handler
= handle_sigterm
;
614 sa
.sa_flags
= SA_RESTART
;
615 sigaction(SIGTERM
, &sa
, NULL
);
616 sa
.sa_handler
= watchdog_sigalrm
;
618 sigaction(SIGALRM
, &sa
, NULL
);
619 signal(SIGPIPE
, SIG_IGN
);
623 * Parsing of command-line arguments
626 static char *opt_list
= "c:dD:ps:P:u:g:flRh";
627 static int parse_and_exit
;
629 static char *use_user
;
630 static char *use_group
;
631 static int run_in_foreground
= 0;
636 fprintf(stderr
, "Usage: %s [--version] [--help] [-c <config-file>] [OPTIONS]\n", bird_name
);
647 " -c <config-file> Use given configuration file instead\n"
648 " of prefix/etc/bird.conf\n"
649 " -d Enable debug messages and run bird in foreground\n"
650 " -D <debug-file> Log debug messages to given file instead of stderr\n"
651 " -f Run bird in foreground\n"
652 " -g <group> Use given group ID\n"
653 " -h, --help Display this information\n"
654 " -l Look for a configuration file and a communication socket\n"
655 " file in the current working directory\n"
656 " -p Test configuration file and exit without start\n"
657 " -P <pid-file> Create a PID file with given filename\n"
658 " -R Apply graceful restart recovery after start\n"
659 " -s <control-socket> Use given filename for a control socket\n"
660 " -u <user> Drop privileges and use given user ID\n"
661 " --version Display version of BIRD\n");
667 display_version(void)
669 fprintf(stderr
, "BIRD version " BIRD_VERSION
"\n");
674 get_bird_name(char *s
, char *def
)
688 get_uid(const char *s
)
698 rv
= strtol(s
, &endptr
, 10);
700 if (!errno
&& !*endptr
)
705 die("Cannot find user '%s'", s
);
711 get_gid(const char *s
)
721 rv
= strtol(s
, &endptr
, 10);
723 if (!errno
&& !*endptr
)
728 die("Cannot find group '%s'", s
);
734 parse_args(int argc
, char **argv
)
736 int config_changed
= 0;
737 int socket_changed
= 0;
740 bird_name
= get_bird_name(argv
[0], "bird");
743 if (!strcmp(argv
[1], "--version"))
745 if (!strcmp(argv
[1], "--help"))
748 while ((c
= getopt(argc
, argv
, opt_list
)) >= 0)
752 config_name
= optarg
;
759 log_init_debug(optarg
);
766 path_control_socket
= optarg
;
779 run_in_foreground
= 1;
783 config_name
= xbasename(config_name
);
785 path_control_socket
= xbasename(path_control_socket
);
788 graceful_restart_recovery();
810 main(int argc
, char **argv
)
812 #ifdef HAVE_LIBDMALLOC
813 if (!getenv("DMALLOC_OPTIONS"))
814 dmalloc_debug(0x2f03d00);
817 parse_args(argc
, argv
);
820 log_switch(debug_flag
, NULL
, NULL
);
830 uid_t use_uid
= get_uid(use_user
);
831 gid_t use_gid
= get_gid(use_group
);
835 test_old_bird(path_control_socket
);
836 cli_init_unix(use_uid
, use_gid
);
849 proto_build(&proto_unix_kernel
);
850 proto_build(&proto_unix_iface
);
852 struct config
*conf
= read_config();
857 if (!(debug_flag
||run_in_foreground
))
866 if (open("/dev/null", O_RDWR
) < 0)
867 die("Cannot open /dev/null: %m");
878 config_commit(conf
, RECONFIG_HARD
, 0);
880 graceful_restart_init();
886 log(L_INFO
"Started");
887 DBG("Entering I/O loop.\n");
890 bug("I/O loop died");