blob: 1bf93f210c3a86f6670e84e40c4528a954536207 [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
23#include <stdio.h>
24#include <zebra.h>
jardineb5d44e2003-12-23 08:09:43 +000025
26#include "getopt.h"
27#include "thread.h"
28#include "log.h"
gdt5e4fa162004-03-16 14:38:36 +000029#include <lib/version.h>
jardineb5d44e2003-12-23 08:09:43 +000030#include "command.h"
31#include "vty.h"
32#include "memory.h"
33#include "stream.h"
34#include "if.h"
jardin9e867fe2003-12-23 08:56:18 +000035#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000036#include "sigevent.h"
hassoc729c652004-10-13 08:36:47 +000037#include "filter.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[] = {
jardin9e867fe2003-12-23 08:56:18 +000055 ZCAP_RAW,
56 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[] = {
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'},
hassoc0652302004-11-25 19:33:48 +000082 {"group", required_argument, NULL, 'g'},
hassof390d2c2004-09-10 20:48:21 +000083 {"version", no_argument, NULL, 'v'},
84 {"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\
128-h, --help Display this help and exit\n\
129\n\
hassoaa0b9f92004-09-28 15:05:56 +0000130Report bugs to http://bugzilla.quagga.net\n", progname);
jardineb5d44e2003-12-23 08:09:43 +0000131 }
132
133 exit (status);
134}
135
136
137void
138reload ()
139{
140 zlog_info ("Reload");
141 /* FIXME: Clean up func call here */
142 vty_finish ();
143 execve (_progpath, _argv, _envp);
144}
145
146void
147terminate (int i)
148{
149 exit (i);
150}
151
152/*
153 * Signal handlers
154 */
paul2d75d052004-01-19 21:31:15 +0000155
hassof390d2c2004-09-10 20:48:21 +0000156void
paul2d75d052004-01-19 21:31:15 +0000157sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000158{
159 zlog_info ("SIGHUP received");
160 reload ();
161
162 return;
163}
164
165void
paul2d75d052004-01-19 21:31:15 +0000166sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000167{
168 zlog_info ("SIGINT received");
169 terminate (0);
hassof390d2c2004-09-10 20:48:21 +0000170
jardineb5d44e2003-12-23 08:09:43 +0000171 return;
172}
173
174void
paul2d75d052004-01-19 21:31:15 +0000175sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000176{
177 zlog_info ("SIGTERM received");
178 terminate (0);
179}
180
181void
paul2d75d052004-01-19 21:31:15 +0000182sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000183{
184 zlog_info ("SIGUSR1 received");
185 zlog_rotate (NULL);
186}
187
paul2d75d052004-01-19 21:31:15 +0000188struct quagga_signal_t isisd_signals[] =
hassof390d2c2004-09-10 20:48:21 +0000189{
paul2d75d052004-01-19 21:31:15 +0000190 {
hassof390d2c2004-09-10 20:48:21 +0000191 .signal = SIGHUP,
192 .handler = &sighup,
193 },
paul2d75d052004-01-19 21:31:15 +0000194 {
hassof390d2c2004-09-10 20:48:21 +0000195 .signal = SIGUSR1,
196 .handler = &sigusr1,
197 },
paul2d75d052004-01-19 21:31:15 +0000198 {
hassof390d2c2004-09-10 20:48:21 +0000199 .signal = SIGINT,
200 .handler = &sigint,
201 },
202 {
203 .signal = SIGTERM,
204 .handler = &sigterm,
205 },
paul2d75d052004-01-19 21:31:15 +0000206};
jardineb5d44e2003-12-23 08:09:43 +0000207
208/*
209 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
210 */
hassof390d2c2004-09-10 20:48:21 +0000211int
jardineb5d44e2003-12-23 08:09:43 +0000212main (int argc, char **argv, char **envp)
213{
214 char *p;
215 int opt, vty_port = ISISD_VTY_PORT;
216 struct thread thread;
217 char *config_file = NULL;
218 char *vty_addr = NULL;
219
220 /* Get the programname without the preceding path. */
221 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
222
223 zlog_default = openzlog (progname, ZLOG_NOLOG, 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 {
hassoc0652302004-11-25 19:33:48 +0000239 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:g:v", 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;
284 case 'h':
285 usage (0);
286 break;
287 default:
288 usage (1);
289 break;
290 }
jardineb5d44e2003-12-23 08:09:43 +0000291 }
hassof390d2c2004-09-10 20:48:21 +0000292
jardineb5d44e2003-12-23 08:09:43 +0000293 /* thread master */
294 master = thread_master_create ();
295
296 /* random seed from time */
hassof390d2c2004-09-10 20:48:21 +0000297 srand (time (NULL));
jardineb5d44e2003-12-23 08:09:43 +0000298
299 /*
300 * initializations
301 */
jardin9e867fe2003-12-23 08:56:18 +0000302 zprivs_init (&isisd_privs);
hassof390d2c2004-09-10 20:48:21 +0000303 signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000304 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000305 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000306 memory_init ();
hassoc729c652004-10-13 08:36:47 +0000307 access_list_init();
jardineb5d44e2003-12-23 08:09:43 +0000308 isis_init ();
309 dyn_cache_init ();
310 sort_node ();
311
hassof390d2c2004-09-10 20:48:21 +0000312 /* parse config file */
jardineb5d44e2003-12-23 08:09:43 +0000313 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000314 vty_read_config (config_file, config_default);
315 vty_read_config (config_file, config_default);
316 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000317
jardineb5d44e2003-12-23 08:09:43 +0000318 /* demonize */
319 if (daemon_mode)
320 daemon (0, 0);
321
jardineb5d44e2003-12-23 08:09:43 +0000322 /* Process ID file creation. */
hassoc3aac6f2004-02-20 18:44:21 +0000323 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000324
325 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000326 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
hassof390d2c2004-09-10 20:48:21 +0000327
jardineb5d44e2003-12-23 08:09:43 +0000328 /* Print banner. */
jardin9e867fe2003-12-23 08:56:18 +0000329#if defined(ZEBRA_VERSION)
jardineb5d44e2003-12-23 08:09:43 +0000330 zlog_info ("ISISd %s starting: vty@%d", ZEBRA_VERSION, vty_port);
jardin9e867fe2003-12-23 08:56:18 +0000331#elif defined(QUAGGA_VERSION)
332 zlog_info ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
333#endif
jardineb5d44e2003-12-23 08:09:43 +0000334#ifdef HAVE_IPV6
335 zlog_info ("IPv6 enabled");
336#endif
337 /* Start finite state machine. */
338 while (thread_fetch (master, &thread))
339 thread_call (&thread);
340
341 /* Not reached. */
342 exit (0);
343}