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" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 28 | #include <lib/version.h> |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 29 | #include "memory.h" |
| 30 | #include "prefix.h" |
| 31 | #include "log.h" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 32 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 33 | #include "sigevent.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 34 | |
| 35 | #include "bgpd/bgpd.h" |
| 36 | #include "bgpd/bgp_attr.h" |
| 37 | #include "bgpd/bgp_mplsvpn.h" |
| 38 | |
| 39 | /* bgpd options, we use GNU getopt library. */ |
| 40 | struct option longopts[] = |
| 41 | { |
| 42 | { "daemon", no_argument, NULL, 'd'}, |
| 43 | { "config_file", required_argument, NULL, 'f'}, |
| 44 | { "pid_file", required_argument, NULL, 'i'}, |
| 45 | { "bgp_port", required_argument, NULL, 'p'}, |
| 46 | { "vty_addr", required_argument, NULL, 'A'}, |
| 47 | { "vty_port", required_argument, NULL, 'P'}, |
| 48 | { "retain", no_argument, NULL, 'r'}, |
| 49 | { "no_kernel", no_argument, NULL, 'n'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 50 | { "user", required_argument, NULL, 'u'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 51 | { "version", no_argument, NULL, 'v'}, |
| 52 | { "help", no_argument, NULL, 'h'}, |
| 53 | { 0 } |
| 54 | }; |
| 55 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 56 | /* signal definitions */ |
| 57 | void sighup (void); |
| 58 | void sigint (void); |
| 59 | void sigusr1 (void); |
| 60 | |
| 61 | struct quagga_signal_t bgp_signals[] = |
| 62 | { |
| 63 | { |
| 64 | .signal = SIGHUP, |
| 65 | .handler = &sighup, |
| 66 | }, |
| 67 | { |
| 68 | .signal = SIGUSR1, |
| 69 | .handler = &sigusr1, |
| 70 | }, |
| 71 | { |
| 72 | .signal = SIGINT, |
| 73 | .handler = &sigint, |
| 74 | }, |
hasso | f571dab | 2004-03-22 08:55:25 +0000 | [diff] [blame] | 75 | { |
| 76 | .signal = SIGTERM, |
| 77 | .handler = &sigint, |
| 78 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 79 | }; |
| 80 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 81 | /* Configuration file and directory. */ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 82 | char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; |
| 83 | |
| 84 | /* Route retain mode flag. */ |
| 85 | int retain_mode = 0; |
| 86 | |
| 87 | /* Master of threads. */ |
| 88 | struct thread_master *master; |
| 89 | |
| 90 | /* Manually specified configuration file name. */ |
| 91 | char *config_file = NULL; |
| 92 | |
| 93 | /* Process ID saved for use by init system */ |
| 94 | char *pid_file = PATH_BGPD_PID; |
| 95 | |
| 96 | /* VTY port number and address. */ |
| 97 | int vty_port = BGP_VTY_PORT; |
| 98 | char *vty_addr = NULL; |
| 99 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 100 | /* privileges */ |
| 101 | zebra_capabilities_t _caps_p [] = |
| 102 | { |
| 103 | ZCAP_BIND, |
| 104 | }; |
| 105 | |
| 106 | struct zebra_privs_t bgpd_privs = |
| 107 | { |
paul | d81fadf | 2003-08-14 05:32:12 +0000 | [diff] [blame] | 108 | #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP) |
| 109 | .user = QUAGGA_USER, |
| 110 | .group = QUAGGA_GROUP, |
| 111 | #endif |
| 112 | #ifdef VTY_GROUP |
| 113 | .vty_group = VTY_GROUP, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 114 | #endif |
| 115 | .caps_p = _caps_p, |
| 116 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 117 | .cap_num_i = 0, |
| 118 | }; |
| 119 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 120 | /* Help information display. */ |
| 121 | static void |
| 122 | usage (char *progname, int status) |
| 123 | { |
| 124 | if (status != 0) |
| 125 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 126 | else |
| 127 | { |
| 128 | printf ("Usage : %s [OPTION...]\n\n\ |
| 129 | Daemon which manages kernel routing table management and \ |
| 130 | redistribution between different routing protocols.\n\n\ |
| 131 | -d, --daemon Runs in daemon mode\n\ |
| 132 | -f, --config_file Set configuration file name\n\ |
| 133 | -i, --pid_file Set process identifier file name\n\ |
| 134 | -p, --bgp_port Set bgp protocol's port number\n\ |
| 135 | -A, --vty_addr Set vty's bind address\n\ |
| 136 | -P, --vty_port Set vty's port number\n\ |
| 137 | -r, --retain When program terminates, retain added route by bgpd.\n\ |
| 138 | -n, --no_kernel Do not install route to kernel.\n\ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 139 | -u, --user User and group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 140 | -v, --version Print program version\n\ |
| 141 | -h, --help Display this help and exit\n\ |
| 142 | \n\ |
| 143 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 144 | } |
| 145 | |
| 146 | exit (status); |
| 147 | } |
| 148 | |
| 149 | /* SIGHUP handler. */ |
| 150 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 151 | sighup (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 152 | { |
| 153 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 154 | |
| 155 | /* Terminate all thread. */ |
| 156 | bgp_terminate (); |
| 157 | bgp_reset (); |
| 158 | zlog_info ("bgpd restarting!"); |
| 159 | |
| 160 | /* Reload config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 161 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 162 | |
| 163 | /* Create VTY's socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 164 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 165 | |
| 166 | /* Try to return to normal operation. */ |
| 167 | } |
| 168 | |
| 169 | /* SIGINT handler. */ |
| 170 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 171 | sigint (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 172 | { |
| 173 | zlog (NULL, LOG_INFO, "Terminating on signal"); |
| 174 | |
| 175 | if (! retain_mode) |
| 176 | bgp_terminate (); |
| 177 | |
| 178 | exit (0); |
| 179 | } |
| 180 | |
| 181 | /* SIGUSR1 handler. */ |
| 182 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 183 | sigusr1 (void) |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 184 | { |
| 185 | zlog_rotate (NULL); |
| 186 | } |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 187 | |
| 188 | /* Main routine of bgpd. Treatment of argument and start bgp finite |
| 189 | state machine is handled at here. */ |
| 190 | int |
| 191 | main (int argc, char **argv) |
| 192 | { |
| 193 | char *p; |
| 194 | int opt; |
| 195 | int daemon_mode = 0; |
| 196 | char *progname; |
| 197 | struct thread thread; |
| 198 | |
| 199 | /* Set umask before anything for security */ |
| 200 | umask (0027); |
| 201 | |
| 202 | /* Preserve name of myself. */ |
| 203 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 204 | |
| 205 | zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP, |
| 206 | LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON); |
| 207 | |
| 208 | /* BGP master init. */ |
| 209 | bgp_master_init (); |
| 210 | |
| 211 | /* Command line argument treatment. */ |
| 212 | while (1) |
| 213 | { |
paul | 96735ee | 2003-08-10 02:51:22 +0000 | [diff] [blame] | 214 | opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:v", longopts, 0); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 215 | |
| 216 | if (opt == EOF) |
| 217 | break; |
| 218 | |
| 219 | switch (opt) |
| 220 | { |
| 221 | case 0: |
| 222 | break; |
| 223 | case 'd': |
| 224 | daemon_mode = 1; |
| 225 | break; |
| 226 | case 'f': |
| 227 | config_file = optarg; |
| 228 | break; |
| 229 | case 'i': |
| 230 | pid_file = optarg; |
| 231 | break; |
| 232 | case 'p': |
| 233 | bm->port = atoi (optarg); |
| 234 | break; |
| 235 | case 'A': |
| 236 | vty_addr = optarg; |
| 237 | break; |
| 238 | case 'P': |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 239 | /* Deal with atoi() returning 0 on failure, and bgpd not |
| 240 | listening on bgp port... */ |
| 241 | if (strcmp(optarg, "0") == 0) |
| 242 | { |
| 243 | vty_port = 0; |
| 244 | break; |
| 245 | } |
| 246 | vty_port = atoi (optarg); |
| 247 | vty_port = (vty_port ? vty_port : BGP_VTY_PORT); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 248 | break; |
| 249 | case 'r': |
| 250 | retain_mode = 1; |
| 251 | break; |
| 252 | case 'n': |
| 253 | bgp_option_set (BGP_OPT_NO_FIB); |
| 254 | break; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 255 | case 'u': |
| 256 | bgpd_privs.user = bgpd_privs.group = optarg; |
| 257 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 258 | case 'v': |
| 259 | print_version (progname); |
| 260 | exit (0); |
| 261 | break; |
| 262 | case 'h': |
| 263 | usage (progname, 0); |
| 264 | break; |
| 265 | default: |
| 266 | usage (progname, 1); |
| 267 | break; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* Make thread master. */ |
| 272 | master = bm->master; |
| 273 | |
| 274 | /* Initializations. */ |
| 275 | srand (time (NULL)); |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 276 | signal_init (master, Q_SIGC(bgp_signals), bgp_signals); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame] | 277 | zprivs_init (&bgpd_privs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 278 | cmd_init (1); |
paul | b21b19c | 2003-06-15 01:28:29 +0000 | [diff] [blame] | 279 | vty_init (master); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 280 | memory_init (); |
| 281 | |
| 282 | /* BGP related initialization. */ |
| 283 | bgp_init (); |
| 284 | |
| 285 | /* Sort CLI commands. */ |
| 286 | sort_node (); |
| 287 | |
| 288 | /* Parse config file. */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 289 | vty_read_config (config_file, config_default); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 290 | |
| 291 | /* Turn into daemon if daemon_mode is set. */ |
| 292 | if (daemon_mode) |
| 293 | daemon (0, 0); |
| 294 | |
| 295 | /* Process ID file creation. */ |
| 296 | pid_output (pid_file); |
| 297 | |
| 298 | /* Make bgp vty socket. */ |
| 299 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
| 300 | |
| 301 | /* Print banner. */ |
paul | e8f2984 | 2003-08-12 13:08:31 +0000 | [diff] [blame] | 302 | zlog_info ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 303 | vty_port, bm->port); |
| 304 | |
| 305 | /* Start finite state machine, here we go! */ |
| 306 | while (thread_fetch (master, &thread)) |
| 307 | thread_call (&thread); |
| 308 | |
| 309 | /* Not reached. */ |
| 310 | exit (0); |
| 311 | } |