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" |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 32 | #include "privs.h" |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 33 | |
| 34 | #include "bgpd/bgpd.h" |
| 35 | #include "bgpd/bgp_attr.h" |
| 36 | #include "bgpd/bgp_mplsvpn.h" |
| 37 | |
| 38 | /* bgpd options, we use GNU getopt library. */ |
| 39 | struct option longopts[] = |
| 40 | { |
| 41 | { "daemon", no_argument, NULL, 'd'}, |
| 42 | { "config_file", required_argument, NULL, 'f'}, |
| 43 | { "pid_file", required_argument, NULL, 'i'}, |
| 44 | { "bgp_port", required_argument, NULL, 'p'}, |
| 45 | { "vty_addr", required_argument, NULL, 'A'}, |
| 46 | { "vty_port", required_argument, NULL, 'P'}, |
| 47 | { "retain", no_argument, NULL, 'r'}, |
| 48 | { "no_kernel", no_argument, NULL, 'n'}, |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 49 | { "user", required_argument, NULL, 'u'}, |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 50 | { "version", no_argument, NULL, 'v'}, |
| 51 | { "help", no_argument, NULL, 'h'}, |
| 52 | { 0 } |
| 53 | }; |
| 54 | |
| 55 | /* Configuration file and directory. */ |
| 56 | char config_current[] = BGP_DEFAULT_CONFIG; |
| 57 | char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; |
| 58 | |
| 59 | /* Route retain mode flag. */ |
| 60 | int retain_mode = 0; |
| 61 | |
| 62 | /* Master of threads. */ |
| 63 | struct thread_master *master; |
| 64 | |
| 65 | /* Manually specified configuration file name. */ |
| 66 | char *config_file = NULL; |
| 67 | |
| 68 | /* Process ID saved for use by init system */ |
| 69 | char *pid_file = PATH_BGPD_PID; |
| 70 | |
| 71 | /* VTY port number and address. */ |
| 72 | int vty_port = BGP_VTY_PORT; |
| 73 | char *vty_addr = NULL; |
| 74 | |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 75 | /* privileges */ |
| 76 | zebra_capabilities_t _caps_p [] = |
| 77 | { |
| 78 | ZCAP_BIND, |
| 79 | }; |
| 80 | |
| 81 | struct zebra_privs_t bgpd_privs = |
| 82 | { |
| 83 | #if defined(ZEBRA_USER) && defined(ZEBRA_GROUP) |
| 84 | .user = ZEBRA_USER, |
| 85 | .group = ZEBRA_GROUP, |
| 86 | #endif |
| 87 | .caps_p = _caps_p, |
| 88 | .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]), |
| 89 | .cap_num_i = 0, |
| 90 | }; |
| 91 | |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 92 | /* Help information display. */ |
| 93 | static void |
| 94 | usage (char *progname, int status) |
| 95 | { |
| 96 | if (status != 0) |
| 97 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 98 | else |
| 99 | { |
| 100 | printf ("Usage : %s [OPTION...]\n\n\ |
| 101 | Daemon which manages kernel routing table management and \ |
| 102 | redistribution between different routing protocols.\n\n\ |
| 103 | -d, --daemon Runs in daemon mode\n\ |
| 104 | -f, --config_file Set configuration file name\n\ |
| 105 | -i, --pid_file Set process identifier file name\n\ |
| 106 | -p, --bgp_port Set bgp protocol's port number\n\ |
| 107 | -A, --vty_addr Set vty's bind address\n\ |
| 108 | -P, --vty_port Set vty's port number\n\ |
| 109 | -r, --retain When program terminates, retain added route by bgpd.\n\ |
| 110 | -n, --no_kernel Do not install route to kernel.\n\ |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 111 | -u, --user User and group to run as\n\ |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 112 | -v, --version Print program version\n\ |
| 113 | -h, --help Display this help and exit\n\ |
| 114 | \n\ |
| 115 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); |
| 116 | } |
| 117 | |
| 118 | exit (status); |
| 119 | } |
| 120 | |
| 121 | /* SIGHUP handler. */ |
| 122 | void |
| 123 | sighup (int sig) |
| 124 | { |
| 125 | zlog (NULL, LOG_INFO, "SIGHUP received"); |
| 126 | |
| 127 | /* Terminate all thread. */ |
| 128 | bgp_terminate (); |
| 129 | bgp_reset (); |
| 130 | zlog_info ("bgpd restarting!"); |
| 131 | |
| 132 | /* Reload config file. */ |
| 133 | vty_read_config (config_file, config_current, config_default); |
| 134 | |
| 135 | /* Create VTY's socket */ |
paul | 4fc4e7a | 2003-01-22 19:47:09 +0000 | [diff] [blame] | 136 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 137 | |
| 138 | /* Try to return to normal operation. */ |
| 139 | } |
| 140 | |
| 141 | /* SIGINT handler. */ |
| 142 | void |
| 143 | sigint (int sig) |
| 144 | { |
| 145 | zlog (NULL, LOG_INFO, "Terminating on signal"); |
| 146 | |
| 147 | if (! retain_mode) |
| 148 | bgp_terminate (); |
| 149 | |
| 150 | exit (0); |
| 151 | } |
| 152 | |
| 153 | /* SIGUSR1 handler. */ |
| 154 | void |
| 155 | sigusr1 (int sig) |
| 156 | { |
| 157 | zlog_rotate (NULL); |
| 158 | } |
| 159 | |
| 160 | /* Signale wrapper. */ |
| 161 | RETSIGTYPE * |
| 162 | signal_set (int signo, void (*func)(int)) |
| 163 | { |
| 164 | int ret; |
| 165 | struct sigaction sig; |
| 166 | struct sigaction osig; |
| 167 | |
| 168 | sig.sa_handler = func; |
| 169 | sigemptyset (&sig.sa_mask); |
| 170 | sig.sa_flags = 0; |
| 171 | #ifdef SA_RESTART |
| 172 | sig.sa_flags |= SA_RESTART; |
| 173 | #endif /* SA_RESTART */ |
| 174 | |
| 175 | ret = sigaction (signo, &sig, &osig); |
| 176 | |
| 177 | if (ret < 0) |
| 178 | return (SIG_ERR); |
| 179 | else |
| 180 | return (osig.sa_handler); |
| 181 | } |
| 182 | |
| 183 | /* Initialization of signal handles. */ |
| 184 | void |
| 185 | signal_init () |
| 186 | { |
| 187 | signal_set (SIGHUP, sighup); |
| 188 | signal_set (SIGINT, sigint); |
| 189 | signal_set (SIGTERM, sigint); |
| 190 | signal_set (SIGPIPE, SIG_IGN); |
| 191 | signal_set (SIGUSR1, sigusr1); |
| 192 | } |
| 193 | |
| 194 | /* Main routine of bgpd. Treatment of argument and start bgp finite |
| 195 | state machine is handled at here. */ |
| 196 | int |
| 197 | main (int argc, char **argv) |
| 198 | { |
| 199 | char *p; |
| 200 | int opt; |
| 201 | int daemon_mode = 0; |
| 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 | |
| 211 | zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP, |
| 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 | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 220 | opt = getopt_long (argc, argv, "df:hp:A:P:rnu:v", 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; |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 261 | case 'u': |
| 262 | bgpd_privs.user = bgpd_privs.group = optarg; |
| 263 | break; |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 264 | case 'v': |
| 265 | print_version (progname); |
| 266 | exit (0); |
| 267 | break; |
| 268 | case 'h': |
| 269 | usage (progname, 0); |
| 270 | break; |
| 271 | default: |
| 272 | usage (progname, 1); |
| 273 | break; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* Make thread master. */ |
| 278 | master = bm->master; |
| 279 | |
| 280 | /* Initializations. */ |
| 281 | srand (time (NULL)); |
| 282 | signal_init (); |
paul | edd7c24 | 2003-06-04 13:59:38 +0000 | [diff] [blame^] | 283 | zprivs_init (&bgpd_privs); |
paul | 718e374 | 2002-12-13 20:15:29 +0000 | [diff] [blame] | 284 | cmd_init (1); |
| 285 | vty_init (); |
| 286 | memory_init (); |
| 287 | |
| 288 | /* BGP related initialization. */ |
| 289 | bgp_init (); |
| 290 | |
| 291 | /* Sort CLI commands. */ |
| 292 | sort_node (); |
| 293 | |
| 294 | /* Parse config file. */ |
| 295 | vty_read_config (config_file, config_current, config_default); |
| 296 | |
| 297 | /* Turn into daemon if daemon_mode is set. */ |
| 298 | if (daemon_mode) |
| 299 | daemon (0, 0); |
| 300 | |
| 301 | /* Process ID file creation. */ |
| 302 | pid_output (pid_file); |
| 303 | |
| 304 | /* Make bgp vty socket. */ |
| 305 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); |
| 306 | |
| 307 | /* Print banner. */ |
| 308 | zlog_info ("BGPd %s starting: vty@%d, bgp@%d", ZEBRA_VERSION, |
| 309 | vty_port, bm->port); |
| 310 | |
| 311 | /* Start finite state machine, here we go! */ |
| 312 | while (thread_fetch (master, &thread)) |
| 313 | thread_call (&thread); |
| 314 | |
| 315 | /* Not reached. */ |
| 316 | exit (0); |
| 317 | } |