blob: 74fc647542914e78db69c8107cfdd294050ab282 [file] [log] [blame]
jardineb5d44e2003-12-23 08:09:43 +00001/*
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
jardineb5d44e2003-12-23 08:09:43 +000023#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000024
25#include "getopt.h"
26#include "thread.h"
27#include "log.h"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
jardineb5d44e2003-12-23 08:09:43 +000029#include "command.h"
30#include "vty.h"
31#include "memory.h"
32#include "stream.h"
33#include "if.h"
jardin9e867fe2003-12-23 08:56:18 +000034#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000035#include "sigevent.h"
hassoc729c652004-10-13 08:36:47 +000036#include "filter.h"
jardineb5d44e2003-12-23 08:09:43 +000037
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 */
jardinfc58e872003-12-23 10:42:45 +000050#define ISISD_VTY_PORT 2608
jardineb5d44e2003-12-23 08:09:43 +000051
jardin9e867fe2003-12-23 08:56:18 +000052/* isisd privileges */
hassof390d2c2004-09-10 20:48:21 +000053zebra_capabilities_t _caps_p[] = {
paulceacedb2005-09-29 14:39:32 +000054 ZCAP_NET_RAW,
jardin9e867fe2003-12-23 08:56:18 +000055 ZCAP_BIND
56};
57
hassof390d2c2004-09-10 20:48:21 +000058struct zebra_privs_t isisd_privs = {
jardin9e867fe2003-12-23 08:56:18 +000059#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
jardineb5d44e2003-12-23 08:09:43 +000073/* isisd options */
hassof390d2c2004-09-10 20:48:21 +000074struct 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'},
hassoc0652302004-11-25 19:33:48 +000081 {"group", required_argument, NULL, 'g'},
hassof390d2c2004-09-10 20:48:21 +000082 {"version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000083 {"dryrun", no_argument, NULL, 'C'},
hassof390d2c2004-09-10 20:48:21 +000084 {"help", no_argument, NULL, 'h'},
85 {0}
jardineb5d44e2003-12-23 08:09:43 +000086};
87
88/* Configuration file and directory. */
jardineb5d44e2003-12-23 08:09:43 +000089char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
90char *config_file = NULL;
91
92/* isisd program name. */
93char *progname;
94
95int daemon_mode = 0;
96
97/* Master of threads. */
98struct thread_master *master;
99
hassoc3aac6f2004-02-20 18:44:21 +0000100/* Process ID saved for use by init system */
hasso1cd80842004-10-07 20:07:40 +0000101const char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000102
103/* for reload */
hasso37da8c02004-05-19 11:38:40 +0000104char _cwd[MAXPATHLEN];
105char _progpath[MAXPATHLEN];
jardineb5d44e2003-12-23 08:09:43 +0000106int _argc;
107char **_argv;
108char **_envp;
109
jardineb5d44e2003-12-23 08:09:43 +0000110/* Help information display. */
111static void
112usage (int status)
113{
114 if (status != 0)
115 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
116 else
hassof390d2c2004-09-10 20:48:21 +0000117 {
jardineb5d44e2003-12-23 08:09:43 +0000118 printf ("Usage : %s [OPTION...]\n\n\
119Daemon which manages IS-IS routing\n\n\
120-d, --daemon Runs in daemon mode\n\
121-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000122-i, --pid_file Set process identifier file name\n\
123-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000124-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000125-u, --user User to run as\n\
126-g, --group Group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000127-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000128-C, --dryrun Check configuration for validity and exit\n\
jardineb5d44e2003-12-23 08:09:43 +0000129-h, --help Display this help and exit\n\
130\n\
hassoaa0b9f92004-09-28 15:05:56 +0000131Report bugs to http://bugzilla.quagga.net\n", progname);
jardineb5d44e2003-12-23 08:09:43 +0000132 }
133
134 exit (status);
135}
136
137
138void
139reload ()
140{
hasso529d65b2004-12-24 00:14:50 +0000141 zlog_debug ("Reload");
jardineb5d44e2003-12-23 08:09:43 +0000142 /* FIXME: Clean up func call here */
ajscdb6ee92005-02-23 15:48:32 +0000143 vty_reset ();
jardineb5d44e2003-12-23 08:09:43 +0000144 execve (_progpath, _argv, _envp);
145}
146
ajs887c44a2004-12-03 16:36:46 +0000147static void
jardineb5d44e2003-12-23 08:09:43 +0000148terminate (int i)
149{
150 exit (i);
151}
152
153/*
154 * Signal handlers
155 */
paul2d75d052004-01-19 21:31:15 +0000156
hassof390d2c2004-09-10 20:48:21 +0000157void
paul2d75d052004-01-19 21:31:15 +0000158sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000159{
hasso529d65b2004-12-24 00:14:50 +0000160 zlog_debug ("SIGHUP received");
jardineb5d44e2003-12-23 08:09:43 +0000161 reload ();
162
163 return;
164}
165
166void
paul2d75d052004-01-19 21:31:15 +0000167sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000168{
ajs887c44a2004-12-03 16:36:46 +0000169 zlog_notice ("Terminating on signal SIGINT");
jardineb5d44e2003-12-23 08:09:43 +0000170 terminate (0);
jardineb5d44e2003-12-23 08:09:43 +0000171}
172
173void
paul2d75d052004-01-19 21:31:15 +0000174sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000175{
ajs887c44a2004-12-03 16:36:46 +0000176 zlog_notice ("Terminating on signal SIGTERM");
jardineb5d44e2003-12-23 08:09:43 +0000177 terminate (0);
178}
179
180void
paul2d75d052004-01-19 21:31:15 +0000181sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000182{
hasso529d65b2004-12-24 00:14:50 +0000183 zlog_debug ("SIGUSR1 received");
jardineb5d44e2003-12-23 08:09:43 +0000184 zlog_rotate (NULL);
185}
186
paul2d75d052004-01-19 21:31:15 +0000187struct quagga_signal_t isisd_signals[] =
hassof390d2c2004-09-10 20:48:21 +0000188{
paul2d75d052004-01-19 21:31:15 +0000189 {
hassof390d2c2004-09-10 20:48:21 +0000190 .signal = SIGHUP,
191 .handler = &sighup,
192 },
paul2d75d052004-01-19 21:31:15 +0000193 {
hassof390d2c2004-09-10 20:48:21 +0000194 .signal = SIGUSR1,
195 .handler = &sigusr1,
196 },
paul2d75d052004-01-19 21:31:15 +0000197 {
hassof390d2c2004-09-10 20:48:21 +0000198 .signal = SIGINT,
199 .handler = &sigint,
200 },
201 {
202 .signal = SIGTERM,
203 .handler = &sigterm,
204 },
paul2d75d052004-01-19 21:31:15 +0000205};
jardineb5d44e2003-12-23 08:09:43 +0000206
207/*
208 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
209 */
hassof390d2c2004-09-10 20:48:21 +0000210int
jardineb5d44e2003-12-23 08:09:43 +0000211main (int argc, char **argv, char **envp)
212{
213 char *p;
214 int opt, vty_port = ISISD_VTY_PORT;
215 struct thread thread;
216 char *config_file = NULL;
217 char *vty_addr = NULL;
Paul Jakma876b8be2006-10-15 23:35:57 +0000218 int dryrun = 0;
jardineb5d44e2003-12-23 08:09:43 +0000219
220 /* Get the programname without the preceding path. */
221 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
222
ajs274a4a42004-12-07 15:39:31 +0000223 zlog_default = openzlog (progname, ZLOG_ISIS,
hassof390d2c2004-09-10 20:48:21 +0000224 LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
jardineb5d44e2003-12-23 08:09:43 +0000225
jardineb5d44e2003-12-23 08:09:43 +0000226 /* for reload */
227 _argc = argc;
228 _argv = argv;
229 _envp = envp;
230 getcwd (_cwd, sizeof (_cwd));
231 if (*argv[0] == '.')
232 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
233 else
234 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
hassof390d2c2004-09-10 20:48:21 +0000235
jardineb5d44e2003-12-23 08:09:43 +0000236 /* Command line argument treatment. */
hassof390d2c2004-09-10 20:48:21 +0000237 while (1)
jardineb5d44e2003-12-23 08:09:43 +0000238 {
Paul Jakma876b8be2006-10-15 23:35:57 +0000239 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:g:vC", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000240
hassof390d2c2004-09-10 20:48:21 +0000241 if (opt == EOF)
242 break;
243
244 switch (opt)
245 {
246 case 0:
jardin9e867fe2003-12-23 08:56:18 +0000247 break;
hassof390d2c2004-09-10 20:48:21 +0000248 case 'd':
249 daemon_mode = 1;
250 break;
251 case 'f':
252 config_file = optarg;
253 break;
254 case 'i':
255 pid_file = optarg;
256 break;
257 case 'A':
258 vty_addr = optarg;
259 break;
260 case 'P':
261 /* Deal with atoi() returning 0 on failure, and isisd not
262 listening on isisd port... */
263 if (strcmp (optarg, "0") == 0)
264 {
265 vty_port = 0;
266 break;
267 }
268 vty_port = atoi (optarg);
269 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
270 break;
271 case 'u':
hassoc0652302004-11-25 19:33:48 +0000272 isisd_privs.user = optarg;
hassof390d2c2004-09-10 20:48:21 +0000273 break;
hassoc0652302004-11-25 19:33:48 +0000274 case 'g':
275 isisd_privs.group = optarg;
hassof390d2c2004-09-10 20:48:21 +0000276 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;
Paul Jakma876b8be2006-10-15 23:35:57 +0000284 case 'C':
285 dryrun = 1;
286 break;
hassof390d2c2004-09-10 20:48:21 +0000287 case 'h':
288 usage (0);
289 break;
290 default:
291 usage (1);
292 break;
293 }
jardineb5d44e2003-12-23 08:09:43 +0000294 }
hassof390d2c2004-09-10 20:48:21 +0000295
jardineb5d44e2003-12-23 08:09:43 +0000296 /* thread master */
297 master = thread_master_create ();
298
299 /* random seed from time */
hassof390d2c2004-09-10 20:48:21 +0000300 srand (time (NULL));
jardineb5d44e2003-12-23 08:09:43 +0000301
302 /*
303 * initializations
304 */
jardin9e867fe2003-12-23 08:56:18 +0000305 zprivs_init (&isisd_privs);
hassof390d2c2004-09-10 20:48:21 +0000306 signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000307 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000308 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000309 memory_init ();
hassoc729c652004-10-13 08:36:47 +0000310 access_list_init();
jardineb5d44e2003-12-23 08:09:43 +0000311 isis_init ();
312 dyn_cache_init ();
313 sort_node ();
314
hassof390d2c2004-09-10 20:48:21 +0000315 /* parse config file */
jardineb5d44e2003-12-23 08:09:43 +0000316 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000317 vty_read_config (config_file, config_default);
318 vty_read_config (config_file, config_default);
319 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000320
Paul Jakma876b8be2006-10-15 23:35:57 +0000321 /* Start execution only if not in dry-run mode */
322 if (dryrun)
323 return(0);
324
jardineb5d44e2003-12-23 08:09:43 +0000325 /* demonize */
326 if (daemon_mode)
327 daemon (0, 0);
328
jardineb5d44e2003-12-23 08:09:43 +0000329 /* Process ID file creation. */
hassoc3aac6f2004-02-20 18:44:21 +0000330 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000331
332 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000333 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
hassof390d2c2004-09-10 20:48:21 +0000334
jardineb5d44e2003-12-23 08:09:43 +0000335 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000336 zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
hassoc89c05d2005-09-04 21:36:36 +0000337
jardineb5d44e2003-12-23 08:09:43 +0000338 /* Start finite state machine. */
339 while (thread_fetch (master, &thread))
340 thread_call (&thread);
341
342 /* Not reached. */
343 exit (0);
344}