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