blob: 15d85d6def45e4f4843ffebdaeb1a587cf72594d [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"
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040037#include "zclient.h"
jardineb5d44e2003-12-23 08:09:43 +000038
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 */
jardinfc58e872003-12-23 10:42:45 +000051#define ISISD_VTY_PORT 2608
jardineb5d44e2003-12-23 08:09:43 +000052
jardin9e867fe2003-12-23 08:56:18 +000053/* isisd privileges */
hassof390d2c2004-09-10 20:48:21 +000054zebra_capabilities_t _caps_p[] = {
paulceacedb2005-09-29 14:39:32 +000055 ZCAP_NET_RAW,
jardin9e867fe2003-12-23 08:56:18 +000056 ZCAP_BIND
57};
58
hassof390d2c2004-09-10 20:48:21 +000059struct zebra_privs_t isisd_privs = {
jardin9e867fe2003-12-23 08:56:18 +000060#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
jardineb5d44e2003-12-23 08:09:43 +000074/* isisd options */
hassof390d2c2004-09-10 20:48:21 +000075struct option longopts[] = {
Vyacheslav Trushkin2f658672011-11-25 17:56:21 +040076 {"daemon", no_argument, NULL, 'd'},
hassof390d2c2004-09-10 20:48:21 +000077 {"config_file", required_argument, NULL, 'f'},
Vyacheslav Trushkin2f658672011-11-25 17:56:21 +040078 {"pid_file", required_argument, NULL, 'i'},
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040079 {"socket", required_argument, NULL, 'z'},
Vyacheslav Trushkin2f658672011-11-25 17:56:21 +040080 {"vty_addr", required_argument, NULL, 'A'},
81 {"vty_port", required_argument, NULL, 'P'},
82 {"user", required_argument, NULL, 'u'},
83 {"group", required_argument, NULL, 'g'},
84 {"version", no_argument, NULL, 'v'},
85 {"dryrun", no_argument, NULL, 'C'},
86 {"help", no_argument, NULL, 'h'},
hassof390d2c2004-09-10 20:48:21 +000087 {0}
jardineb5d44e2003-12-23 08:09:43 +000088};
89
90/* Configuration file and directory. */
jardineb5d44e2003-12-23 08:09:43 +000091char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
92char *config_file = NULL;
93
94/* isisd program name. */
95char *progname;
96
97int daemon_mode = 0;
98
99/* Master of threads. */
100struct thread_master *master;
101
hassoc3aac6f2004-02-20 18:44:21 +0000102/* Process ID saved for use by init system */
hasso1cd80842004-10-07 20:07:40 +0000103const char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000104
105/* for reload */
hasso37da8c02004-05-19 11:38:40 +0000106char _cwd[MAXPATHLEN];
107char _progpath[MAXPATHLEN];
jardineb5d44e2003-12-23 08:09:43 +0000108int _argc;
109char **_argv;
110char **_envp;
111
Paul Jakma41b36e92006-12-08 01:09:50 +0000112/*
113 * Prototypes.
114 */
115void reload(void);
116void sighup(void);
117void sigint(void);
118void sigterm(void);
119void sigusr1(void);
120
121
jardineb5d44e2003-12-23 08:09:43 +0000122/* Help information display. */
123static void
124usage (int status)
125{
126 if (status != 0)
127 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
128 else
hassof390d2c2004-09-10 20:48:21 +0000129 {
jardineb5d44e2003-12-23 08:09:43 +0000130 printf ("Usage : %s [OPTION...]\n\n\
131Daemon which manages IS-IS routing\n\n\
132-d, --daemon Runs in daemon mode\n\
133-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000134-i, --pid_file Set process identifier file name\n\
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400135-z, --socket Set path of zebra socket\n\
hassoc3aac6f2004-02-20 18:44:21 +0000136-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000137-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000138-u, --user User to run as\n\
139-g, --group Group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000140-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000141-C, --dryrun Check configuration for validity and exit\n\
jardineb5d44e2003-12-23 08:09:43 +0000142-h, --help Display this help and exit\n\
143\n\
hassoaa0b9f92004-09-28 15:05:56 +0000144Report bugs to http://bugzilla.quagga.net\n", progname);
jardineb5d44e2003-12-23 08:09:43 +0000145 }
146
147 exit (status);
148}
149
150
151void
152reload ()
153{
hasso529d65b2004-12-24 00:14:50 +0000154 zlog_debug ("Reload");
jardineb5d44e2003-12-23 08:09:43 +0000155 /* FIXME: Clean up func call here */
ajscdb6ee92005-02-23 15:48:32 +0000156 vty_reset ();
jardineb5d44e2003-12-23 08:09:43 +0000157 execve (_progpath, _argv, _envp);
158}
159
ajs887c44a2004-12-03 16:36:46 +0000160static void
jardineb5d44e2003-12-23 08:09:43 +0000161terminate (int i)
162{
163 exit (i);
164}
165
166/*
167 * Signal handlers
168 */
paul2d75d052004-01-19 21:31:15 +0000169
hassof390d2c2004-09-10 20:48:21 +0000170void
paul2d75d052004-01-19 21:31:15 +0000171sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000172{
hasso529d65b2004-12-24 00:14:50 +0000173 zlog_debug ("SIGHUP received");
jardineb5d44e2003-12-23 08:09:43 +0000174 reload ();
175
176 return;
177}
178
179void
paul2d75d052004-01-19 21:31:15 +0000180sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000181{
ajs887c44a2004-12-03 16:36:46 +0000182 zlog_notice ("Terminating on signal SIGINT");
jardineb5d44e2003-12-23 08:09:43 +0000183 terminate (0);
jardineb5d44e2003-12-23 08:09:43 +0000184}
185
186void
paul2d75d052004-01-19 21:31:15 +0000187sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000188{
ajs887c44a2004-12-03 16:36:46 +0000189 zlog_notice ("Terminating on signal SIGTERM");
jardineb5d44e2003-12-23 08:09:43 +0000190 terminate (0);
191}
192
193void
paul2d75d052004-01-19 21:31:15 +0000194sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000195{
hasso529d65b2004-12-24 00:14:50 +0000196 zlog_debug ("SIGUSR1 received");
jardineb5d44e2003-12-23 08:09:43 +0000197 zlog_rotate (NULL);
198}
199
paul2d75d052004-01-19 21:31:15 +0000200struct quagga_signal_t isisd_signals[] =
hassof390d2c2004-09-10 20:48:21 +0000201{
paul2d75d052004-01-19 21:31:15 +0000202 {
hassof390d2c2004-09-10 20:48:21 +0000203 .signal = SIGHUP,
204 .handler = &sighup,
205 },
paul2d75d052004-01-19 21:31:15 +0000206 {
hassof390d2c2004-09-10 20:48:21 +0000207 .signal = SIGUSR1,
208 .handler = &sigusr1,
209 },
paul2d75d052004-01-19 21:31:15 +0000210 {
hassof390d2c2004-09-10 20:48:21 +0000211 .signal = SIGINT,
212 .handler = &sigint,
213 },
214 {
215 .signal = SIGTERM,
216 .handler = &sigterm,
217 },
paul2d75d052004-01-19 21:31:15 +0000218};
jardineb5d44e2003-12-23 08:09:43 +0000219
220/*
221 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
222 */
hassof390d2c2004-09-10 20:48:21 +0000223int
jardineb5d44e2003-12-23 08:09:43 +0000224main (int argc, char **argv, char **envp)
225{
226 char *p;
227 int opt, vty_port = ISISD_VTY_PORT;
228 struct thread thread;
229 char *config_file = NULL;
230 char *vty_addr = NULL;
Paul Jakma876b8be2006-10-15 23:35:57 +0000231 int dryrun = 0;
jardineb5d44e2003-12-23 08:09:43 +0000232
233 /* Get the programname without the preceding path. */
234 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
235
ajs274a4a42004-12-07 15:39:31 +0000236 zlog_default = openzlog (progname, ZLOG_ISIS,
hassof390d2c2004-09-10 20:48:21 +0000237 LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
jardineb5d44e2003-12-23 08:09:43 +0000238
jardineb5d44e2003-12-23 08:09:43 +0000239 /* for reload */
240 _argc = argc;
241 _argv = argv;
242 _envp = envp;
243 getcwd (_cwd, sizeof (_cwd));
244 if (*argv[0] == '.')
245 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
246 else
247 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
hassof390d2c2004-09-10 20:48:21 +0000248
jardineb5d44e2003-12-23 08:09:43 +0000249 /* Command line argument treatment. */
hassof390d2c2004-09-10 20:48:21 +0000250 while (1)
jardineb5d44e2003-12-23 08:09:43 +0000251 {
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400252 opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000253
hassof390d2c2004-09-10 20:48:21 +0000254 if (opt == EOF)
255 break;
256
257 switch (opt)
258 {
259 case 0:
jardin9e867fe2003-12-23 08:56:18 +0000260 break;
hassof390d2c2004-09-10 20:48:21 +0000261 case 'd':
262 daemon_mode = 1;
263 break;
264 case 'f':
265 config_file = optarg;
266 break;
267 case 'i':
268 pid_file = optarg;
269 break;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400270 case 'z':
271 zclient_serv_path_set (optarg);
272 break;
hassof390d2c2004-09-10 20:48:21 +0000273 case 'A':
274 vty_addr = optarg;
275 break;
276 case 'P':
277 /* Deal with atoi() returning 0 on failure, and isisd not
278 listening on isisd port... */
279 if (strcmp (optarg, "0") == 0)
280 {
281 vty_port = 0;
282 break;
283 }
284 vty_port = atoi (optarg);
285 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
286 break;
287 case 'u':
hassoc0652302004-11-25 19:33:48 +0000288 isisd_privs.user = optarg;
hassof390d2c2004-09-10 20:48:21 +0000289 break;
hassoc0652302004-11-25 19:33:48 +0000290 case 'g':
291 isisd_privs.group = optarg;
hassof390d2c2004-09-10 20:48:21 +0000292 break;
293 case 'v':
294 printf ("ISISd version %s\n", ISISD_VERSION);
295 printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
296 " Ofer Wald and Hannes Gredler\n");
297 print_version ("Zebra");
298 exit (0);
299 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000300 case 'C':
301 dryrun = 1;
302 break;
hassof390d2c2004-09-10 20:48:21 +0000303 case 'h':
304 usage (0);
305 break;
306 default:
307 usage (1);
308 break;
309 }
jardineb5d44e2003-12-23 08:09:43 +0000310 }
hassof390d2c2004-09-10 20:48:21 +0000311
jardineb5d44e2003-12-23 08:09:43 +0000312 /* thread master */
313 master = thread_master_create ();
314
315 /* random seed from time */
hassof390d2c2004-09-10 20:48:21 +0000316 srand (time (NULL));
jardineb5d44e2003-12-23 08:09:43 +0000317
318 /*
319 * initializations
320 */
jardin9e867fe2003-12-23 08:56:18 +0000321 zprivs_init (&isisd_privs);
hassof390d2c2004-09-10 20:48:21 +0000322 signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000323 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000324 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000325 memory_init ();
hassoc729c652004-10-13 08:36:47 +0000326 access_list_init();
jardineb5d44e2003-12-23 08:09:43 +0000327 isis_init ();
328 dyn_cache_init ();
329 sort_node ();
330
hassof390d2c2004-09-10 20:48:21 +0000331 /* parse config file */
jardineb5d44e2003-12-23 08:09:43 +0000332 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000333 vty_read_config (config_file, config_default);
334 vty_read_config (config_file, config_default);
335 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000336
Paul Jakma876b8be2006-10-15 23:35:57 +0000337 /* Start execution only if not in dry-run mode */
338 if (dryrun)
339 return(0);
340
jardineb5d44e2003-12-23 08:09:43 +0000341 /* demonize */
Stephen Hemminger065de902009-08-07 11:13:49 -0700342 if (daemon_mode && daemon (0, 0) < 0)
343 {
344 zlog_err("ISISd daemon failed: %s", strerror(errno));
345 exit (1);
346 }
jardineb5d44e2003-12-23 08:09:43 +0000347
jardineb5d44e2003-12-23 08:09:43 +0000348 /* Process ID file creation. */
hassoc3aac6f2004-02-20 18:44:21 +0000349 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000350
351 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000352 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
hassof390d2c2004-09-10 20:48:21 +0000353
jardineb5d44e2003-12-23 08:09:43 +0000354 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000355 zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
hassoc89c05d2005-09-04 21:36:36 +0000356
jardineb5d44e2003-12-23 08:09:43 +0000357 /* Start finite state machine. */
358 while (thread_fetch (master, &thread))
359 thread_call (&thread);
360
361 /* Not reached. */
362 exit (0);
363}