blob: e763a9e807335e9f16265ce91bbd523ffb2209ba [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 */
53zebra_capabilities_t _caps_p [] =
54{
55 ZCAP_RAW,
56 ZCAP_BIND
57};
58
59struct zebra_privs_t isisd_privs =
60{
61#if defined(QUAGGA_USER)
62 .user = QUAGGA_USER,
63#endif
64#if defined QUAGGA_GROUP
65 .group = QUAGGA_GROUP,
66#endif
67#ifdef VTY_GROUP
68 .vty_group = VTY_GROUP,
69#endif
70 .caps_p = _caps_p,
71 .cap_num_p = 2,
72 .cap_num_i = 0
73};
74
jardineb5d44e2003-12-23 08:09:43 +000075/* isisd options */
76struct option longopts[] =
77{
78 { "daemon", no_argument, NULL, 'd'},
79 { "config_file", required_argument, NULL, 'f'},
hassoc3aac6f2004-02-20 18:44:21 +000080 { "pid_file", required_argument, NULL, 'i'},
81 { "vty_addr", required_argument, NULL, 'A'},
jardineb5d44e2003-12-23 08:09:43 +000082 { "vty_port", required_argument, NULL, 'P'},
jardin9e867fe2003-12-23 08:56:18 +000083 { "user", required_argument, NULL, 'u'},
jardineb5d44e2003-12-23 08:09:43 +000084 { "version", no_argument, NULL, 'v'},
85 { "help", no_argument, NULL, 'h'},
86 { 0 }
87};
88
89/* Configuration file and directory. */
90char config_current[] = ISISD_DEFAULT_CONFIG;
91char 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 */
103char *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
112
113/* Help information display. */
114static void
115usage (int status)
116{
117 if (status != 0)
118 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
119 else
120 {
121 printf ("Usage : %s [OPTION...]\n\n\
122Daemon which manages IS-IS routing\n\n\
123-d, --daemon Runs in daemon mode\n\
124-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000125-i, --pid_file Set process identifier file name\n\
126-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000127-P, --vty_port Set vty's port number\n\
jardin9e867fe2003-12-23 08:56:18 +0000128-u, --user User and group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000129-v, --version Print program version\n\
130-h, --help Display this help and exit\n\
131\n\
132Report bugs to sambo@cs.tut.fi\n", progname);
133 }
134
135 exit (status);
136}
137
138
139void
140reload ()
141{
142 zlog_info ("Reload");
143 /* FIXME: Clean up func call here */
144 vty_finish ();
145 execve (_progpath, _argv, _envp);
146}
147
148void
149terminate (int i)
150{
151 exit (i);
152}
153
154/*
155 * Signal handlers
156 */
paul2d75d052004-01-19 21:31:15 +0000157
jardineb5d44e2003-12-23 08:09:43 +0000158void
paul2d75d052004-01-19 21:31:15 +0000159sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000160{
161 zlog_info ("SIGHUP received");
162 reload ();
163
164 return;
165}
166
167void
paul2d75d052004-01-19 21:31:15 +0000168sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000169{
170 zlog_info ("SIGINT received");
171 terminate (0);
172
173 return;
174}
175
176void
paul2d75d052004-01-19 21:31:15 +0000177sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000178{
179 zlog_info ("SIGTERM received");
180 terminate (0);
181}
182
183void
paul2d75d052004-01-19 21:31:15 +0000184sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000185{
186 zlog_info ("SIGUSR1 received");
187 zlog_rotate (NULL);
188}
189
paul2d75d052004-01-19 21:31:15 +0000190struct quagga_signal_t isisd_signals[] =
191{
192 {
193 .signal = SIGHUP,
194 .handler = &sighup,
195 },
196 {
197 .signal = SIGUSR1,
198 .handler = &sigusr1,
199 },
200 {
201 .signal = SIGINT,
202 .handler = &sigint,
203 },
204 {
205 .signal = SIGTERM,
206 .handler = &sigterm,
207 },
208};
jardineb5d44e2003-12-23 08:09:43 +0000209
210/*
211 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
212 */
213int
214main (int argc, char **argv, char **envp)
215{
216 char *p;
217 int opt, vty_port = ISISD_VTY_PORT;
218 struct thread thread;
219 char *config_file = NULL;
220 char *vty_addr = NULL;
221
222 /* Get the programname without the preceding path. */
223 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
224
225 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_ISIS,
226 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
227
228
229 /* for reload */
230 _argc = argc;
231 _argv = argv;
232 _envp = envp;
233 getcwd (_cwd, sizeof (_cwd));
234 if (*argv[0] == '.')
235 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
236 else
237 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
238
239 /* Command line argument treatment. */
240 while (1)
241 {
hassoc3aac6f2004-02-20 18:44:21 +0000242 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:v", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000243
244 if (opt == EOF)
245 break;
246
247 switch (opt)
248 {
249 case 0:
250 break;
251 case 'd':
252 daemon_mode = 1;
253 break;
254 case 'f':
255 config_file = optarg;
256 break;
hassoc3aac6f2004-02-20 18:44:21 +0000257 case 'i':
258 pid_file = optarg;
259 break;
jardineb5d44e2003-12-23 08:09:43 +0000260 case 'A':
261 vty_addr = optarg;
262 break;
263 case 'P':
jardin9e867fe2003-12-23 08:56:18 +0000264 /* Deal with atoi() returning 0 on failure, and isisd not
265 listening on isisd port... */
266 if (strcmp(optarg, "0") == 0)
267 {
268 vty_port = 0;
269 break;
270 }
jardineb5d44e2003-12-23 08:09:43 +0000271 vty_port = atoi (optarg);
jardin9e867fe2003-12-23 08:56:18 +0000272 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
273 break;
274 case 'u':
275 isisd_privs.user = isisd_privs.group = optarg;
276 break;
jardineb5d44e2003-12-23 08:09:43 +0000277 break;
278 case 'v':
279 printf("ISISd version %s\n", ISISD_VERSION);
280 printf("Copyright (c) 2001-2002 Sampo Saaristo,"
281 " Ofer Wald and Hannes Gredler\n");
282 print_version ("Zebra");
283 exit (0);
284 break;
285 case 'h':
286 usage (0);
287 break;
288 default:
289 usage (1);
290 break;
291 }
292 }
293
294 /* thread master */
295 master = thread_master_create ();
296
297 /* random seed from time */
298 srand(time(NULL));
299
300 /*
301 * initializations
302 */
jardin9e867fe2003-12-23 08:56:18 +0000303 zprivs_init (&isisd_privs);
paul2d75d052004-01-19 21:31:15 +0000304 signal_init (master, Q_SIGC(isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000305 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000306 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000307 memory_init ();
308 isis_init ();
309 dyn_cache_init ();
310 sort_node ();
311
312 /* parse config file */
313 /* this is needed three times! because we have interfaces before the areas */
314 vty_read_config (config_file, config_current, config_default);
jardineb5d44e2003-12-23 08:09:43 +0000315 vty_read_config (config_file, config_current, config_default);
316 vty_read_config (config_file, config_current, 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);
jardineb5d44e2003-12-23 08:09:43 +0000327
328 /* 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}
344
345
346
347
348
349
350
351
352
353