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