paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 1 | /* Main routine of bgpd. |
| 2 | Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro |
| 3 | |
| 4 | This file is part of GNU Zebra. |
| 5 | |
| 6 | GNU Zebra is free software; you can redistribute it and/or modify it |
| 7 | under the terms of the GNU General Public License as published by the |
| 8 | Free Software Foundation; either version 2, or (at your option) any |
| 9 | later version. |
| 10 | |
| 11 | GNU Zebra is distributed in the hope that it will be useful, but |
| 12 | WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 | General Public License for more details. |
| 15 | |
| 16 | You should have received a copy of the GNU General Public License |
| 17 | along with GNU Zebra; see the file COPYING. If not, write to the Free |
| 18 | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
| 19 | 02111-1307, USA. */ |
| 20 | |
| 21 | #include <zebra.h> |
| 22 | |
| 23 | #include "vector.h" |
| 24 | #include "vty.h" |
| 25 | #include "command.h" |
| 26 | #include "getopt.h" |
| 27 | #include "thread.h" |
| 28 | #include "version.h" |
| 29 | #include "memory.h" |
| 30 | #include "prefix.h" |
| 31 | #include "log.h" |
| 32 | |
| 33 | #include "bgpd/bgpd.h" |
| 34 | #include "bgpd/bgp_attr.h" |
| 35 | #include "bgpd/bgp_mplsvpn.h" |
| 36 | |
| 37 | /* bgpd options, we use GNU getopt library. */ |
| 38 | struct option longopts[] = |
| 39 | { |
| 40 | { "daemon", no_argument, NULL, 'd'}, |
| 41 | { "config_file", required_argument, NULL, 'f'}, |
| 42 | { "pid_file", required_argument, NULL, 'i'}, |
| 43 | { "bgp_port", required_argument, NULL, 'p'}, |
| 44 | { "vty_addr", required_argument, NULL, 'A'}, |
| 45 | { "vty_port", required_argument, NULL, 'P'}, |
| 46 | { "retain", no_argument, NULL, 'r'}, |
| 47 | { "no_kernel", no_argument, NULL, 'n'}, |
| 48 | { "version", no_argument, NULL, 'v'}, |
| 49 | { "help", no_argument, NULL, 'h'}, |
| 50 | { 0 } |
| 51 | }; |
| 52 | |
| 53 | /* Configuration file and directory. */ |
| 54 | char config_current[] = BGP_DEFAULT_CONFIG; |
| 55 | char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; |
| 56 | |
| 57 | /* Route retain mode flag. */ |
| 58 | int retain_mode = 0; |
| 59 | |
| 60 | /* Master of threads. */ |
| 61 | struct thread_master *master; |
| 62 | |
| 63 | /* Manually specified configuration file name. */ |
| 64 | char *config_file = NULL; |
| 65 | |
| 66 | /* Process ID saved for use by init system */ |
| 67 | char *pid_file = PATH_BGPD_PID; |
| 68 | |
| 69 | /* VTY port number and address. */ |
| 70 | int vty_port = BGP_VTY_PORT; |
| 71 | char *vty_addr = NULL; |
| 72 | |
| 73 | /* Help information display. */ |
| 74 | static void |
| 75 | usage (char *progname, int status) |
| 76 | { |
| 77 | if (status != 0) |
| 78 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 79 | else |
| 80 | { |
| 81 | printf ("Usage : %s [OPTION...]\n\n\ |
| 82 | Daemon which manages kernel routing table management and \ |
| 83 | redistribution between different routing protocols.\n\n\ |
| 84 | -d, --daemon Runs in daemon mode\n\ |
| 85 | -f, --config_file Set configuration file name\n\ |
| 86 | -i, --pid_file Set process identifier file name\n\ |
| 87 | -p, --bgp_port Set bgp protocol's port number\n\ |
| 88 | -A, --vty_addr Set vty's bind address\n\ |
| 89 | -P, --vty_port Set vty's port number\n\ |
| 90 | -r, --retain When program terminates, retain added route by bgpd.\n\ |
| 91 | -n, --no_kernel Do not install route to kernel.\n\ |
| 92 | -v, --version Print program version\n\ |
| 93 | -h, --help Display this help and exit\n\ |
| 94 | \n\ |
| 95 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 96 | } |
| 97 | |
| 98 | exit (status); |
| 99 | } |
| 100 | |
| 101 | /* SIGHUP handler. */ |
| 102 | void |
| 103 | sighup (int sig) |
| 104 | { |
| 105 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 106 | |
| 107 | /* Terminate all thread. */ |
| 108 | bgp_terminate (); |
| 109 | bgp_reset (); |
| 110 | zlog_info ("bgpd restarting!"); |
| 111 | |
| 112 | /* Reload config file. */ |
| 113 | vty_read_config (config_file, config_current, config_default); |
| 114 | |
| 115 | /* Create VTY's socket */ |
| 116 | vty_serv_sock (vty_addr, vty_port ? vty_port : BGP_VTY_PORT, BGP_VTYSH_PATH); |
| 117 | |
| 118 | /* Try to return to normal operation. */ |
| 119 | } |
| 120 | |
| 121 | /* SIGINT handler. */ |
| 122 | void |
| 123 | sigint (int sig) |
| 124 | { |
| 125 | zlog (NULL, LOG_INFO, "Terminating on signal"); |
| 126 | |
| 127 | if (! retain_mode) |
| 128 | bgp_terminate (); |
| 129 | |
| 130 | exit (0); |
| 131 | } |
| 132 | |
| 133 | /* SIGUSR1 handler. */ |
| 134 | void |
| 135 | sigusr1 (int sig) |
| 136 | { |
| 137 | zlog_rotate (NULL); |
| 138 | } |
| 139 | |
| 140 | /* Signale wrapper. */ |
| 141 | RETSIGTYPE * |
| 142 | signal_set (int signo, void (*func)(int)) |
| 143 | { |
| 144 | int ret; |
| 145 | struct sigaction sig; |
| 146 | struct sigaction osig; |
| 147 | |
| 148 | sig.sa_handler = func; |
| 149 | sigemptyset (&sig.sa_mask); |
| 150 | sig.sa_flags = 0; |
| 151 | #ifdef SA_RESTART |
| 152 | sig.sa_flags |= SA_RESTART; |
| 153 | #endif /* SA_RESTART */ |
| 154 | |
| 155 | ret = sigaction (signo, &sig, &osig); |
| 156 | |
| 157 | if (ret < 0) |
| 158 | return (SIG_ERR); |
| 159 | else |
| 160 | return (osig.sa_handler); |
| 161 | } |
| 162 | |
| 163 | /* Initialization of signal handles. */ |
| 164 | void |
| 165 | signal_init () |
| 166 | { |
| 167 | signal_set (SIGHUP, sighup); |
| 168 | signal_set (SIGINT, sigint); |
| 169 | signal_set (SIGTERM, sigint); |
| 170 | signal_set (SIGPIPE, SIG_IGN); |
| 171 | signal_set (SIGUSR1, sigusr1); |
| 172 | } |
| 173 | |
| 174 | /* Main routine of bgpd. Treatment of argument and start bgp finite |
| 175 | state machine is handled at here. */ |
| 176 | int |
| 177 | main (int argc, char **argv) |
| 178 | { |
| 179 | char *p; |
| 180 | int opt; |
| 181 | int daemon_mode = 0; |
| 182 | char *progname; |
| 183 | struct thread thread; |
| 184 | |
| 185 | /* Set umask before anything for security */ |
| 186 | umask (0027); |
| 187 | |
| 188 | /* Preserve name of myself. */ |
| 189 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 190 | |
| 191 | zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP, |
| 192 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 193 | |
| 194 | /* BGP master init. */ |
| 195 | bgp_master_init (); |
| 196 | |
| 197 | /* Command line argument treatment. */ |
| 198 | while (1) |
| 199 | { |
| 200 | opt = getopt_long (argc, argv, "df:hp:A:P:rnv", longopts, 0); |
| 201 | |
| 202 | if (opt == EOF) |
| 203 | break; |
| 204 | |
| 205 | switch (opt) |
| 206 | { |
| 207 | case 0: |
| 208 | break; |
| 209 | case 'd': |
| 210 | daemon_mode = 1; |
| 211 | break; |
| 212 | case 'f': |
| 213 | config_file = optarg; |
| 214 | break; |
| 215 | case 'i': |
| 216 | pid_file = optarg; |
| 217 | break; |
| 218 | case 'p': |
| 219 | bm->port = atoi (optarg); |
| 220 | break; |
| 221 | case 'A': |
| 222 | vty_addr = optarg; |
| 223 | break; |
| 224 | case 'P': |
| 225 | vty_port = atoi (optarg); |
| 226 | break; |
| 227 | case 'r': |
| 228 | retain_mode = 1; |
| 229 | break; |
| 230 | case 'n': |
| 231 | bgp_option_set (BGP_OPT_NO_FIB); |
| 232 | break; |
| 233 | case 'v': |
| 234 | print_version (progname); |
| 235 | exit (0); |
| 236 | break; |
| 237 | case 'h': |
| 238 | usage (progname, 0); |
| 239 | break; |
| 240 | default: |
| 241 | usage (progname, 1); |
| 242 | break; |
| 243 | } |
| 244 | } |
| 245 | |
| 246 | /* Make thread master. */ |
| 247 | master = bm->master; |
| 248 | |
| 249 | /* Initializations. */ |
| 250 | srand (time (NULL)); |
| 251 | signal_init (); |
| 252 | cmd_init (1); |
| 253 | vty_init (); |
| 254 | memory_init (); |
| 255 | |
| 256 | /* BGP related initialization. */ |
| 257 | bgp_init (); |
| 258 | |
| 259 | /* Sort CLI commands. */ |
| 260 | sort_node (); |
| 261 | |
| 262 | /* Parse config file. */ |
| 263 | vty_read_config (config_file, config_current, config_default); |
| 264 | |
| 265 | /* Turn into daemon if daemon_mode is set. */ |
| 266 | if (daemon_mode) |
| 267 | daemon (0, 0); |
| 268 | |
| 269 | /* Process ID file creation. */ |
| 270 | pid_output (pid_file); |
| 271 | |
| 272 | /* Make bgp vty socket. */ |
| 273 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
| 274 | |
| 275 | /* Print banner. */ |
| 276 | zlog_info ("BGPd %s starting: vty@%d, bgp@%d", ZEBRA_VERSION, |
| 277 | vty_port, bm->port); |
| 278 | |
| 279 | /* Start finite state machine, here we go! */ |
| 280 | while (thread_fetch (master, &thread)) |
| 281 | thread_call (&thread); |
| 282 | |
| 283 | /* Not reached. */ |
| 284 | exit (0); |
| 285 | } |