jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * IS-IS Rout(e)ing protocol - isis_main.c |
| 3 | * |
| 4 | * Copyright (C) 2001,2002 Sampo Saaristo |
| 5 | * Tampere University of Technology |
| 6 | * Institute of Communications Engineering |
| 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify it |
| 9 | * under the terms of the GNU General Public Licenseas published by the Free |
| 10 | * Software Foundation; either version 2 of the License, or (at your option) |
| 11 | * any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful,but WITHOUT |
| 14 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 15 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for |
| 16 | * more details. |
| 17 | |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 21 | */ |
| 22 | |
| 23 | #include <stdio.h> |
| 24 | #include <zebra.h> |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 25 | |
| 26 | #include "getopt.h" |
| 27 | #include "thread.h" |
| 28 | #include "log.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 29 | #include <lib/version.h> |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 30 | #include "command.h" |
| 31 | #include "vty.h" |
| 32 | #include "memory.h" |
| 33 | #include "stream.h" |
| 34 | #include "if.h" |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 35 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 36 | #include "sigevent.h" |
hasso | c729c65 | 2004-10-13 08:36:47 +0000 | [diff] [blame] | 37 | #include "filter.h" |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 38 | |
| 39 | #include "isisd/dict.h" |
| 40 | #include "include-netbsd/iso.h" |
| 41 | #include "isisd/isis_constants.h" |
| 42 | #include "isisd/isis_common.h" |
| 43 | #include "isisd/isis_flags.h" |
| 44 | #include "isisd/isis_circuit.h" |
| 45 | #include "isisd/isisd.h" |
| 46 | #include "isisd/isis_dynhn.h" |
| 47 | |
| 48 | /* Default configuration file name */ |
| 49 | #define ISISD_DEFAULT_CONFIG "isisd.conf" |
| 50 | /* Default vty port */ |
jardin | fc58e87 | 2003-12-23 10:42:45 +0000 | [diff] [blame] | 51 | #define ISISD_VTY_PORT 2608 |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 52 | |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 53 | /* isisd privileges */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 54 | zebra_capabilities_t _caps_p[] = { |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 55 | ZCAP_RAW, |
| 56 | ZCAP_BIND |
| 57 | }; |
| 58 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 59 | struct zebra_privs_t isisd_privs = { |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 60 | #if defined(QUAGGA_USER) |
| 61 | .user = QUAGGA_USER, |
| 62 | #endif |
| 63 | #if defined QUAGGA_GROUP |
| 64 | .group = QUAGGA_GROUP, |
| 65 | #endif |
| 66 | #ifdef VTY_GROUP |
| 67 | .vty_group = VTY_GROUP, |
| 68 | #endif |
| 69 | .caps_p = _caps_p, |
| 70 | .cap_num_p = 2, |
| 71 | .cap_num_i = 0 |
| 72 | }; |
| 73 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 74 | /* isisd options */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 75 | struct option longopts[] = { |
| 76 | {"daemon", no_argument, NULL, 'd'}, |
| 77 | {"config_file", required_argument, NULL, 'f'}, |
| 78 | {"pid_file", required_argument, NULL, 'i'}, |
| 79 | {"vty_addr", required_argument, NULL, 'A'}, |
| 80 | {"vty_port", required_argument, NULL, 'P'}, |
| 81 | {"user", required_argument, NULL, 'u'}, |
| 82 | {"version", no_argument, NULL, 'v'}, |
| 83 | {"help", no_argument, NULL, 'h'}, |
| 84 | {0} |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | /* Configuration file and directory. */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 88 | char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG; |
| 89 | char *config_file = NULL; |
| 90 | |
| 91 | /* isisd program name. */ |
| 92 | char *progname; |
| 93 | |
| 94 | int daemon_mode = 0; |
| 95 | |
| 96 | /* Master of threads. */ |
| 97 | struct thread_master *master; |
| 98 | |
hasso | c3aac6f | 2004-02-20 18:44:21 +0000 | [diff] [blame] | 99 | /* Process ID saved for use by init system */ |
hasso | 1cd8084 | 2004-10-07 20:07:40 +0000 | [diff] [blame] | 100 | const char *pid_file = PATH_ISISD_PID; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 101 | |
| 102 | /* for reload */ |
hasso | 37da8c0 | 2004-05-19 11:38:40 +0000 | [diff] [blame] | 103 | char _cwd[MAXPATHLEN]; |
| 104 | char _progpath[MAXPATHLEN]; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 105 | int _argc; |
| 106 | char **_argv; |
| 107 | char **_envp; |
| 108 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 109 | /* Help information display. */ |
| 110 | static void |
| 111 | usage (int status) |
| 112 | { |
| 113 | if (status != 0) |
| 114 | fprintf (stderr, "Try `%s --help' for more information.\n", progname); |
| 115 | else |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 116 | { |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 117 | printf ("Usage : %s [OPTION...]\n\n\ |
| 118 | Daemon which manages IS-IS routing\n\n\ |
| 119 | -d, --daemon Runs in daemon mode\n\ |
| 120 | -f, --config_file Set configuration file name\n\ |
hasso | c3aac6f | 2004-02-20 18:44:21 +0000 | [diff] [blame] | 121 | -i, --pid_file Set process identifier file name\n\ |
| 122 | -A, --vty_addr Set vty's bind address\n\ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 123 | -P, --vty_port Set vty's port number\n\ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 124 | -u, --user User and group to run as\n\ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 125 | -v, --version Print program version\n\ |
| 126 | -h, --help Display this help and exit\n\ |
| 127 | \n\ |
hasso | aa0b9f9 | 2004-09-28 15:05:56 +0000 | [diff] [blame] | 128 | Report bugs to http://bugzilla.quagga.net\n", progname); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 129 | } |
| 130 | |
| 131 | exit (status); |
| 132 | } |
| 133 | |
| 134 | |
| 135 | void |
| 136 | reload () |
| 137 | { |
| 138 | zlog_info ("Reload"); |
| 139 | /* FIXME: Clean up func call here */ |
| 140 | vty_finish (); |
| 141 | execve (_progpath, _argv, _envp); |
| 142 | } |
| 143 | |
| 144 | void |
| 145 | terminate (int i) |
| 146 | { |
| 147 | exit (i); |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Signal handlers |
| 152 | */ |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 153 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 154 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 155 | sighup (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 156 | { |
| 157 | zlog_info ("SIGHUP received"); |
| 158 | reload (); |
| 159 | |
| 160 | return; |
| 161 | } |
| 162 | |
| 163 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 164 | sigint (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 165 | { |
| 166 | zlog_info ("SIGINT received"); |
| 167 | terminate (0); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 168 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 169 | return; |
| 170 | } |
| 171 | |
| 172 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 173 | sigterm (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 174 | { |
| 175 | zlog_info ("SIGTERM received"); |
| 176 | terminate (0); |
| 177 | } |
| 178 | |
| 179 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 180 | sigusr1 (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 181 | { |
| 182 | zlog_info ("SIGUSR1 received"); |
| 183 | zlog_rotate (NULL); |
| 184 | } |
| 185 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 186 | struct quagga_signal_t isisd_signals[] = |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 187 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 188 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 189 | .signal = SIGHUP, |
| 190 | .handler = &sighup, |
| 191 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 192 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 193 | .signal = SIGUSR1, |
| 194 | .handler = &sigusr1, |
| 195 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 196 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 197 | .signal = SIGINT, |
| 198 | .handler = &sigint, |
| 199 | }, |
| 200 | { |
| 201 | .signal = SIGTERM, |
| 202 | .handler = &sigterm, |
| 203 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 204 | }; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 205 | |
| 206 | /* |
| 207 | * Main routine of isisd. Parse arguments and handle IS-IS state machine. |
| 208 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 209 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 210 | main (int argc, char **argv, char **envp) |
| 211 | { |
| 212 | char *p; |
| 213 | int opt, vty_port = ISISD_VTY_PORT; |
| 214 | struct thread thread; |
| 215 | char *config_file = NULL; |
| 216 | char *vty_addr = NULL; |
| 217 | |
| 218 | /* Get the programname without the preceding path. */ |
| 219 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 220 | |
| 221 | zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_ISIS, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 222 | LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 223 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 224 | /* for reload */ |
| 225 | _argc = argc; |
| 226 | _argv = argv; |
| 227 | _envp = envp; |
| 228 | getcwd (_cwd, sizeof (_cwd)); |
| 229 | if (*argv[0] == '.') |
| 230 | snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]); |
| 231 | else |
| 232 | snprintf (_progpath, sizeof (_progpath), "%s", argv[0]); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 233 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 234 | /* Command line argument treatment. */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 235 | while (1) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 236 | { |
hasso | c3aac6f | 2004-02-20 18:44:21 +0000 | [diff] [blame] | 237 | opt = getopt_long (argc, argv, "df:i:hA:p:P:u:v", longopts, 0); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 238 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 239 | if (opt == EOF) |
| 240 | break; |
| 241 | |
| 242 | switch (opt) |
| 243 | { |
| 244 | case 0: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 245 | break; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 246 | case 'd': |
| 247 | daemon_mode = 1; |
| 248 | break; |
| 249 | case 'f': |
| 250 | config_file = optarg; |
| 251 | break; |
| 252 | case 'i': |
| 253 | pid_file = optarg; |
| 254 | break; |
| 255 | case 'A': |
| 256 | vty_addr = optarg; |
| 257 | break; |
| 258 | case 'P': |
| 259 | /* Deal with atoi() returning 0 on failure, and isisd not |
| 260 | listening on isisd port... */ |
| 261 | if (strcmp (optarg, "0") == 0) |
| 262 | { |
| 263 | vty_port = 0; |
| 264 | break; |
| 265 | } |
| 266 | vty_port = atoi (optarg); |
| 267 | vty_port = (vty_port ? vty_port : ISISD_VTY_PORT); |
| 268 | break; |
| 269 | case 'u': |
| 270 | isisd_privs.user = isisd_privs.group = optarg; |
| 271 | break; |
| 272 | break; |
| 273 | case 'v': |
| 274 | printf ("ISISd version %s\n", ISISD_VERSION); |
| 275 | printf ("Copyright (c) 2001-2002 Sampo Saaristo," |
| 276 | " Ofer Wald and Hannes Gredler\n"); |
| 277 | print_version ("Zebra"); |
| 278 | exit (0); |
| 279 | break; |
| 280 | case 'h': |
| 281 | usage (0); |
| 282 | break; |
| 283 | default: |
| 284 | usage (1); |
| 285 | break; |
| 286 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 287 | } |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 288 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 289 | /* thread master */ |
| 290 | master = thread_master_create (); |
| 291 | |
| 292 | /* random seed from time */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 293 | srand (time (NULL)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 294 | |
| 295 | /* |
| 296 | * initializations |
| 297 | */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 298 | zprivs_init (&isisd_privs); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 299 | signal_init (master, Q_SIGC (isisd_signals), isisd_signals); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 300 | cmd_init (1); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 301 | vty_init (master); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 302 | memory_init (); |
hasso | c729c65 | 2004-10-13 08:36:47 +0000 | [diff] [blame] | 303 | access_list_init(); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 304 | isis_init (); |
| 305 | dyn_cache_init (); |
| 306 | sort_node (); |
| 307 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 308 | /* parse config file */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 309 | /* this is needed three times! because we have interfaces before the areas */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 310 | vty_read_config (config_file, config_default); |
| 311 | vty_read_config (config_file, config_default); |
| 312 | vty_read_config (config_file, config_default); |
hasso | 00995cf | 2004-05-19 13:43:50 +0000 | [diff] [blame] | 313 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 314 | /* demonize */ |
| 315 | if (daemon_mode) |
| 316 | daemon (0, 0); |
| 317 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 318 | /* Process ID file creation. */ |
hasso | c3aac6f | 2004-02-20 18:44:21 +0000 | [diff] [blame] | 319 | pid_output (pid_file); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 320 | |
| 321 | /* Make isis vty socket. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 322 | vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 323 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 324 | /* Print banner. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 325 | #if defined(ZEBRA_VERSION) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 326 | zlog_info ("ISISd %s starting: vty@%d", ZEBRA_VERSION, vty_port); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 327 | #elif defined(QUAGGA_VERSION) |
| 328 | zlog_info ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
| 329 | #endif |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 330 | #ifdef HAVE_IPV6 |
| 331 | zlog_info ("IPv6 enabled"); |
| 332 | #endif |
| 333 | /* Start finite state machine. */ |
| 334 | while (thread_fetch (master, &thread)) |
| 335 | thread_call (&thread); |
| 336 | |
| 337 | /* Not reached. */ |
| 338 | exit (0); |
| 339 | } |