blob: 64905d304739872e4a8d111bb20e0d217b952db1 [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"
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[] = {
jardin9e867fe2003-12-23 08:56:18 +000054 ZCAP_RAW,
55 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'},
81 {"version", no_argument, NULL, 'v'},
82 {"help", no_argument, NULL, 'h'},
83 {0}
jardineb5d44e2003-12-23 08:09:43 +000084};
85
86/* Configuration file and directory. */
jardineb5d44e2003-12-23 08:09:43 +000087char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
88char *config_file = NULL;
89
90/* isisd program name. */
91char *progname;
92
93int daemon_mode = 0;
94
95/* Master of threads. */
96struct thread_master *master;
97
hassoc3aac6f2004-02-20 18:44:21 +000098/* Process ID saved for use by init system */
99char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000100
101/* for reload */
hasso37da8c02004-05-19 11:38:40 +0000102char _cwd[MAXPATHLEN];
103char _progpath[MAXPATHLEN];
jardineb5d44e2003-12-23 08:09:43 +0000104int _argc;
105char **_argv;
106char **_envp;
107
jardineb5d44e2003-12-23 08:09:43 +0000108/* Help information display. */
109static void
110usage (int status)
111{
112 if (status != 0)
113 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
114 else
hassof390d2c2004-09-10 20:48:21 +0000115 {
jardineb5d44e2003-12-23 08:09:43 +0000116 printf ("Usage : %s [OPTION...]\n\n\
117Daemon which manages IS-IS routing\n\n\
118-d, --daemon Runs in daemon mode\n\
119-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000120-i, --pid_file Set process identifier file name\n\
121-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000122-P, --vty_port Set vty's port number\n\
jardin9e867fe2003-12-23 08:56:18 +0000123-u, --user User and group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000124-v, --version Print program version\n\
125-h, --help Display this help and exit\n\
126\n\
hassoaa0b9f92004-09-28 15:05:56 +0000127Report bugs to http://bugzilla.quagga.net\n", progname);
jardineb5d44e2003-12-23 08:09:43 +0000128 }
129
130 exit (status);
131}
132
133
134void
135reload ()
136{
137 zlog_info ("Reload");
138 /* FIXME: Clean up func call here */
139 vty_finish ();
140 execve (_progpath, _argv, _envp);
141}
142
143void
144terminate (int i)
145{
146 exit (i);
147}
148
149/*
150 * Signal handlers
151 */
paul2d75d052004-01-19 21:31:15 +0000152
hassof390d2c2004-09-10 20:48:21 +0000153void
paul2d75d052004-01-19 21:31:15 +0000154sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000155{
156 zlog_info ("SIGHUP received");
157 reload ();
158
159 return;
160}
161
162void
paul2d75d052004-01-19 21:31:15 +0000163sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000164{
165 zlog_info ("SIGINT received");
166 terminate (0);
hassof390d2c2004-09-10 20:48:21 +0000167
jardineb5d44e2003-12-23 08:09:43 +0000168 return;
169}
170
171void
paul2d75d052004-01-19 21:31:15 +0000172sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000173{
174 zlog_info ("SIGTERM received");
175 terminate (0);
176}
177
178void
paul2d75d052004-01-19 21:31:15 +0000179sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000180{
181 zlog_info ("SIGUSR1 received");
182 zlog_rotate (NULL);
183}
184
paul2d75d052004-01-19 21:31:15 +0000185struct quagga_signal_t isisd_signals[] =
hassof390d2c2004-09-10 20:48:21 +0000186{
paul2d75d052004-01-19 21:31:15 +0000187 {
hassof390d2c2004-09-10 20:48:21 +0000188 .signal = SIGHUP,
189 .handler = &sighup,
190 },
paul2d75d052004-01-19 21:31:15 +0000191 {
hassof390d2c2004-09-10 20:48:21 +0000192 .signal = SIGUSR1,
193 .handler = &sigusr1,
194 },
paul2d75d052004-01-19 21:31:15 +0000195 {
hassof390d2c2004-09-10 20:48:21 +0000196 .signal = SIGINT,
197 .handler = &sigint,
198 },
199 {
200 .signal = SIGTERM,
201 .handler = &sigterm,
202 },
paul2d75d052004-01-19 21:31:15 +0000203};
jardineb5d44e2003-12-23 08:09:43 +0000204
205/*
206 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
207 */
hassof390d2c2004-09-10 20:48:21 +0000208int
jardineb5d44e2003-12-23 08:09:43 +0000209main (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
220 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_ISIS,
hassof390d2c2004-09-10 20:48:21 +0000221 LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
jardineb5d44e2003-12-23 08:09:43 +0000222
jardineb5d44e2003-12-23 08:09:43 +0000223 /* 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]);
hassof390d2c2004-09-10 20:48:21 +0000232
jardineb5d44e2003-12-23 08:09:43 +0000233 /* Command line argument treatment. */
hassof390d2c2004-09-10 20:48:21 +0000234 while (1)
jardineb5d44e2003-12-23 08:09:43 +0000235 {
hassoc3aac6f2004-02-20 18:44:21 +0000236 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:v", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000237
hassof390d2c2004-09-10 20:48:21 +0000238 if (opt == EOF)
239 break;
240
241 switch (opt)
242 {
243 case 0:
jardin9e867fe2003-12-23 08:56:18 +0000244 break;
hassof390d2c2004-09-10 20:48:21 +0000245 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':
269 isisd_privs.user = isisd_privs.group = optarg;
270 break;
271 break;
272 case 'v':
273 printf ("ISISd version %s\n", ISISD_VERSION);
274 printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
275 " Ofer Wald and Hannes Gredler\n");
276 print_version ("Zebra");
277 exit (0);
278 break;
279 case 'h':
280 usage (0);
281 break;
282 default:
283 usage (1);
284 break;
285 }
jardineb5d44e2003-12-23 08:09:43 +0000286 }
hassof390d2c2004-09-10 20:48:21 +0000287
jardineb5d44e2003-12-23 08:09:43 +0000288 /* thread master */
289 master = thread_master_create ();
290
291 /* random seed from time */
hassof390d2c2004-09-10 20:48:21 +0000292 srand (time (NULL));
jardineb5d44e2003-12-23 08:09:43 +0000293
294 /*
295 * initializations
296 */
jardin9e867fe2003-12-23 08:56:18 +0000297 zprivs_init (&isisd_privs);
hassof390d2c2004-09-10 20:48:21 +0000298 signal_init (master, Q_SIGC (isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000299 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000300 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000301 memory_init ();
302 isis_init ();
303 dyn_cache_init ();
304 sort_node ();
305
hassof390d2c2004-09-10 20:48:21 +0000306 /* parse config file */
jardineb5d44e2003-12-23 08:09:43 +0000307 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000308 vty_read_config (config_file, config_default);
309 vty_read_config (config_file, config_default);
310 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000311
jardineb5d44e2003-12-23 08:09:43 +0000312 /* demonize */
313 if (daemon_mode)
314 daemon (0, 0);
315
jardineb5d44e2003-12-23 08:09:43 +0000316 /* Process ID file creation. */
hassoc3aac6f2004-02-20 18:44:21 +0000317 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000318
319 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000320 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
hassof390d2c2004-09-10 20:48:21 +0000321
jardineb5d44e2003-12-23 08:09:43 +0000322 /* Print banner. */
jardin9e867fe2003-12-23 08:56:18 +0000323#if defined(ZEBRA_VERSION)
jardineb5d44e2003-12-23 08:09:43 +0000324 zlog_info ("ISISd %s starting: vty@%d", ZEBRA_VERSION, vty_port);
jardin9e867fe2003-12-23 08:56:18 +0000325#elif defined(QUAGGA_VERSION)
326 zlog_info ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
327#endif
jardineb5d44e2003-12-23 08:09:43 +0000328#ifdef HAVE_IPV6
329 zlog_info ("IPv6 enabled");
330#endif
331 /* Start finite state machine. */
332 while (thread_fetch (master, &thread))
333 thread_call (&thread);
334
335 /* Not reached. */
336 exit (0);
337}