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 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 23 | #include <zebra.h> |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 24 | |
| 25 | #include "getopt.h" |
| 26 | #include "thread.h" |
| 27 | #include "log.h" |
gdt | 5e4fa16 | 2004-03-16 14:38:36 +0000 | [diff] [blame] | 28 | #include <lib/version.h> |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 29 | #include "command.h" |
| 30 | #include "vty.h" |
| 31 | #include "memory.h" |
| 32 | #include "stream.h" |
| 33 | #include "if.h" |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 34 | #include "privs.h" |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 35 | #include "sigevent.h" |
hasso | c729c65 | 2004-10-13 08:36:47 +0000 | [diff] [blame] | 36 | #include "filter.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 */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 53 | zebra_capabilities_t _caps_p[] = { |
paul | ceacedb | 2005-09-29 14:39:32 +0000 | [diff] [blame] | 54 | ZCAP_NET_RAW, |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 55 | ZCAP_BIND |
| 56 | }; |
| 57 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 58 | struct zebra_privs_t isisd_privs = { |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 59 | #if defined(QUAGGA_USER) |
| 60 | .user = QUAGGA_USER, |
| 61 | #endif |
| 62 | #if defined QUAGGA_GROUP |
| 63 | .group = QUAGGA_GROUP, |
| 64 | #endif |
| 65 | #ifdef VTY_GROUP |
| 66 | .vty_group = VTY_GROUP, |
| 67 | #endif |
| 68 | .caps_p = _caps_p, |
| 69 | .cap_num_p = 2, |
| 70 | .cap_num_i = 0 |
| 71 | }; |
| 72 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 73 | /* isisd options */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 74 | struct option longopts[] = { |
| 75 | {"daemon", no_argument, NULL, 'd'}, |
| 76 | {"config_file", required_argument, NULL, 'f'}, |
| 77 | {"pid_file", required_argument, NULL, 'i'}, |
| 78 | {"vty_addr", required_argument, NULL, 'A'}, |
| 79 | {"vty_port", required_argument, NULL, 'P'}, |
| 80 | {"user", required_argument, NULL, 'u'}, |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 81 | {"group", required_argument, NULL, 'g'}, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 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\ |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 124 | -u, --user User to run as\n\ |
| 125 | -g, --group Group to run as\n\ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 126 | -v, --version Print program version\n\ |
| 127 | -h, --help Display this help and exit\n\ |
| 128 | \n\ |
hasso | aa0b9f9 | 2004-09-28 15:05:56 +0000 | [diff] [blame] | 129 | Report bugs to http://bugzilla.quagga.net\n", progname); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | exit (status); |
| 133 | } |
| 134 | |
| 135 | |
| 136 | void |
| 137 | reload () |
| 138 | { |
hasso | 529d65b | 2004-12-24 00:14:50 +0000 | [diff] [blame] | 139 | zlog_debug ("Reload"); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 140 | /* FIXME: Clean up func call here */ |
ajs | cdb6ee9 | 2005-02-23 15:48:32 +0000 | [diff] [blame] | 141 | vty_reset (); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 142 | execve (_progpath, _argv, _envp); |
| 143 | } |
| 144 | |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 145 | static void |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 146 | terminate (int i) |
| 147 | { |
| 148 | exit (i); |
| 149 | } |
| 150 | |
| 151 | /* |
| 152 | * Signal handlers |
| 153 | */ |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 154 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 155 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 156 | sighup (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 157 | { |
hasso | 529d65b | 2004-12-24 00:14:50 +0000 | [diff] [blame] | 158 | zlog_debug ("SIGHUP received"); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 159 | reload (); |
| 160 | |
| 161 | return; |
| 162 | } |
| 163 | |
| 164 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 165 | sigint (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 166 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 167 | zlog_notice ("Terminating on signal SIGINT"); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 168 | terminate (0); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 172 | sigterm (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 173 | { |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 174 | zlog_notice ("Terminating on signal SIGTERM"); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 175 | terminate (0); |
| 176 | } |
| 177 | |
| 178 | void |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 179 | sigusr1 (void) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 180 | { |
hasso | 529d65b | 2004-12-24 00:14:50 +0000 | [diff] [blame] | 181 | zlog_debug ("SIGUSR1 received"); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 182 | zlog_rotate (NULL); |
| 183 | } |
| 184 | |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 185 | struct quagga_signal_t isisd_signals[] = |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 186 | { |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 187 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 188 | .signal = SIGHUP, |
| 189 | .handler = &sighup, |
| 190 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 191 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 192 | .signal = SIGUSR1, |
| 193 | .handler = &sigusr1, |
| 194 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 195 | { |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 196 | .signal = SIGINT, |
| 197 | .handler = &sigint, |
| 198 | }, |
| 199 | { |
| 200 | .signal = SIGTERM, |
| 201 | .handler = &sigterm, |
| 202 | }, |
paul | 2d75d05 | 2004-01-19 21:31:15 +0000 | [diff] [blame] | 203 | }; |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 204 | |
| 205 | /* |
| 206 | * Main routine of isisd. Parse arguments and handle IS-IS state machine. |
| 207 | */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 208 | int |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 209 | main (int argc, char **argv, char **envp) |
| 210 | { |
| 211 | char *p; |
| 212 | int opt, vty_port = ISISD_VTY_PORT; |
| 213 | struct thread thread; |
| 214 | char *config_file = NULL; |
| 215 | char *vty_addr = NULL; |
| 216 | |
| 217 | /* Get the programname without the preceding path. */ |
| 218 | progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]); |
| 219 | |
ajs | 274a4a4 | 2004-12-07 15:39:31 +0000 | [diff] [blame] | 220 | zlog_default = openzlog (progname, ZLOG_ISIS, |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 221 | LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 222 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 223 | /* for reload */ |
| 224 | _argc = argc; |
| 225 | _argv = argv; |
| 226 | _envp = envp; |
| 227 | getcwd (_cwd, sizeof (_cwd)); |
| 228 | if (*argv[0] == '.') |
| 229 | snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]); |
| 230 | else |
| 231 | snprintf (_progpath, sizeof (_progpath), "%s", argv[0]); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 232 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 233 | /* Command line argument treatment. */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 234 | while (1) |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 235 | { |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 236 | opt = getopt_long (argc, argv, "df:i:hA:p:P:u:g:v", longopts, 0); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 237 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 238 | if (opt == EOF) |
| 239 | break; |
| 240 | |
| 241 | switch (opt) |
| 242 | { |
| 243 | case 0: |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 244 | break; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 245 | case 'd': |
| 246 | daemon_mode = 1; |
| 247 | break; |
| 248 | case 'f': |
| 249 | config_file = optarg; |
| 250 | break; |
| 251 | case 'i': |
| 252 | pid_file = optarg; |
| 253 | break; |
| 254 | case 'A': |
| 255 | vty_addr = optarg; |
| 256 | break; |
| 257 | case 'P': |
| 258 | /* Deal with atoi() returning 0 on failure, and isisd not |
| 259 | listening on isisd port... */ |
| 260 | if (strcmp (optarg, "0") == 0) |
| 261 | { |
| 262 | vty_port = 0; |
| 263 | break; |
| 264 | } |
| 265 | vty_port = atoi (optarg); |
| 266 | vty_port = (vty_port ? vty_port : ISISD_VTY_PORT); |
| 267 | break; |
| 268 | case 'u': |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 269 | isisd_privs.user = optarg; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 270 | break; |
hasso | c065230 | 2004-11-25 19:33:48 +0000 | [diff] [blame] | 271 | case 'g': |
| 272 | isisd_privs.group = optarg; |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 273 | break; |
| 274 | case 'v': |
| 275 | printf ("ISISd version %s\n", ISISD_VERSION); |
| 276 | printf ("Copyright (c) 2001-2002 Sampo Saaristo," |
| 277 | " Ofer Wald and Hannes Gredler\n"); |
| 278 | print_version ("Zebra"); |
| 279 | exit (0); |
| 280 | break; |
| 281 | case 'h': |
| 282 | usage (0); |
| 283 | break; |
| 284 | default: |
| 285 | usage (1); |
| 286 | break; |
| 287 | } |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 288 | } |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 289 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 290 | /* thread master */ |
| 291 | master = thread_master_create (); |
| 292 | |
| 293 | /* random seed from time */ |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 294 | srand (time (NULL)); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 295 | |
| 296 | /* |
| 297 | * initializations |
| 298 | */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 299 | zprivs_init (&isisd_privs); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 300 | signal_init (master, Q_SIGC (isisd_signals), isisd_signals); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 301 | cmd_init (1); |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 302 | vty_init (master); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 303 | memory_init (); |
hasso | c729c65 | 2004-10-13 08:36:47 +0000 | [diff] [blame] | 304 | access_list_init(); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 305 | isis_init (); |
| 306 | dyn_cache_init (); |
| 307 | sort_node (); |
| 308 | |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 309 | /* parse config file */ |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 310 | /* this is needed three times! because we have interfaces before the areas */ |
hasso | 320ec10 | 2004-06-20 19:54:37 +0000 | [diff] [blame] | 311 | vty_read_config (config_file, config_default); |
| 312 | vty_read_config (config_file, config_default); |
| 313 | vty_read_config (config_file, config_default); |
hasso | 00995cf | 2004-05-19 13:43:50 +0000 | [diff] [blame] | 314 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 315 | /* demonize */ |
| 316 | if (daemon_mode) |
| 317 | daemon (0, 0); |
| 318 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 319 | /* Process ID file creation. */ |
hasso | c3aac6f | 2004-02-20 18:44:21 +0000 | [diff] [blame] | 320 | pid_output (pid_file); |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 321 | |
| 322 | /* Make isis vty socket. */ |
jardin | 9e867fe | 2003-12-23 08:56:18 +0000 | [diff] [blame] | 323 | vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH); |
hasso | f390d2c | 2004-09-10 20:48:21 +0000 | [diff] [blame] | 324 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 325 | /* Print banner. */ |
ajs | 887c44a | 2004-12-03 16:36:46 +0000 | [diff] [blame] | 326 | zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port); |
hasso | c89c05d | 2005-09-04 21:36:36 +0000 | [diff] [blame] | 327 | |
jardin | eb5d44e | 2003-12-23 08:09:43 +0000 | [diff] [blame] | 328 | /* Start finite state machine. */ |
| 329 | while (thread_fetch (master, &thread)) |
| 330 | thread_call (&thread); |
| 331 | |
| 332 | /* Not reached. */ |
| 333 | exit (0); |
| 334 | } |