blob: b952c00cf6be7537e193faa1bd4be437fee78ba0 [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>
25#include <net/ethernet.h>
26
27#include "getopt.h"
28#include "thread.h"
29#include "log.h"
30#include "version.h"
31#include "command.h"
32#include "vty.h"
33#include "memory.h"
34#include "stream.h"
35#include "if.h"
jardin9e867fe2003-12-23 08:56:18 +000036#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000037#include "sigevent.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 */
54zebra_capabilities_t _caps_p [] =
55{
56 ZCAP_RAW,
57 ZCAP_BIND
58};
59
60struct zebra_privs_t isisd_privs =
61{
62#if defined(QUAGGA_USER)
63 .user = QUAGGA_USER,
64#endif
65#if defined QUAGGA_GROUP
66 .group = QUAGGA_GROUP,
67#endif
68#ifdef VTY_GROUP
69 .vty_group = VTY_GROUP,
70#endif
71 .caps_p = _caps_p,
72 .cap_num_p = 2,
73 .cap_num_i = 0
74};
75
jardineb5d44e2003-12-23 08:09:43 +000076/* isisd options */
77struct option longopts[] =
78{
79 { "daemon", no_argument, NULL, 'd'},
80 { "config_file", required_argument, NULL, 'f'},
hassoc3aac6f2004-02-20 18:44:21 +000081 { "pid_file", required_argument, NULL, 'i'},
82 { "vty_addr", required_argument, NULL, 'A'},
jardineb5d44e2003-12-23 08:09:43 +000083 { "vty_port", required_argument, NULL, 'P'},
jardin9e867fe2003-12-23 08:56:18 +000084 { "user", required_argument, NULL, 'u'},
jardineb5d44e2003-12-23 08:09:43 +000085 { "version", no_argument, NULL, 'v'},
86 { "help", no_argument, NULL, 'h'},
87 { 0 }
88};
89
90/* Configuration file and directory. */
91char config_current[] = ISISD_DEFAULT_CONFIG;
92char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
93char *config_file = NULL;
94
95/* isisd program name. */
96char *progname;
97
98int daemon_mode = 0;
99
100/* Master of threads. */
101struct thread_master *master;
102
hassoc3aac6f2004-02-20 18:44:21 +0000103/* Process ID saved for use by init system */
104char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000105
106/* for reload */
107char _cwd[64];
108char _progpath[64];
109int _argc;
110char **_argv;
111char **_envp;
112
113
114/* Help information display. */
115static void
116usage (int status)
117{
118 if (status != 0)
119 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
120 else
121 {
122 printf ("Usage : %s [OPTION...]\n\n\
123Daemon which manages IS-IS routing\n\n\
124-d, --daemon Runs in daemon mode\n\
125-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000126-i, --pid_file Set process identifier file name\n\
127-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000128-P, --vty_port Set vty's port number\n\
jardin9e867fe2003-12-23 08:56:18 +0000129-u, --user User and group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000130-v, --version Print program version\n\
131-h, --help Display this help and exit\n\
132\n\
133Report bugs to sambo@cs.tut.fi\n", progname);
134 }
135
136 exit (status);
137}
138
139
140void
141reload ()
142{
143 zlog_info ("Reload");
144 /* FIXME: Clean up func call here */
145 vty_finish ();
146 execve (_progpath, _argv, _envp);
147}
148
149void
150terminate (int i)
151{
152 exit (i);
153}
154
155/*
156 * Signal handlers
157 */
paul2d75d052004-01-19 21:31:15 +0000158
jardineb5d44e2003-12-23 08:09:43 +0000159void
paul2d75d052004-01-19 21:31:15 +0000160sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000161{
162 zlog_info ("SIGHUP received");
163 reload ();
164
165 return;
166}
167
168void
paul2d75d052004-01-19 21:31:15 +0000169sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000170{
171 zlog_info ("SIGINT received");
172 terminate (0);
173
174 return;
175}
176
177void
paul2d75d052004-01-19 21:31:15 +0000178sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000179{
180 zlog_info ("SIGTERM received");
181 terminate (0);
182}
183
184void
paul2d75d052004-01-19 21:31:15 +0000185sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000186{
187 zlog_info ("SIGUSR1 received");
188 zlog_rotate (NULL);
189}
190
paul2d75d052004-01-19 21:31:15 +0000191struct quagga_signal_t isisd_signals[] =
192{
193 {
194 .signal = SIGHUP,
195 .handler = &sighup,
196 },
197 {
198 .signal = SIGUSR1,
199 .handler = &sigusr1,
200 },
201 {
202 .signal = SIGINT,
203 .handler = &sigint,
204 },
205 {
206 .signal = SIGTERM,
207 .handler = &sigterm,
208 },
209};
jardineb5d44e2003-12-23 08:09:43 +0000210
211/*
212 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
213 */
214int
215main (int argc, char **argv, char **envp)
216{
217 char *p;
218 int opt, vty_port = ISISD_VTY_PORT;
219 struct thread thread;
220 char *config_file = NULL;
221 char *vty_addr = NULL;
222
223 /* Get the programname without the preceding path. */
224 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
225
226 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_ISIS,
227 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
228
229
230 /* for reload */
231 _argc = argc;
232 _argv = argv;
233 _envp = envp;
234 getcwd (_cwd, sizeof (_cwd));
235 if (*argv[0] == '.')
236 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
237 else
238 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
239
240 /* Command line argument treatment. */
241 while (1)
242 {
hassoc3aac6f2004-02-20 18:44:21 +0000243 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:v", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000244
245 if (opt == EOF)
246 break;
247
248 switch (opt)
249 {
250 case 0:
251 break;
252 case 'd':
253 daemon_mode = 1;
254 break;
255 case 'f':
256 config_file = optarg;
257 break;
hassoc3aac6f2004-02-20 18:44:21 +0000258 case 'i':
259 pid_file = optarg;
260 break;
jardineb5d44e2003-12-23 08:09:43 +0000261 case 'A':
262 vty_addr = optarg;
263 break;
264 case 'P':
jardin9e867fe2003-12-23 08:56:18 +0000265 /* Deal with atoi() returning 0 on failure, and isisd not
266 listening on isisd port... */
267 if (strcmp(optarg, "0") == 0)
268 {
269 vty_port = 0;
270 break;
271 }
jardineb5d44e2003-12-23 08:09:43 +0000272 vty_port = atoi (optarg);
jardin9e867fe2003-12-23 08:56:18 +0000273 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
274 break;
275 case 'u':
276 isisd_privs.user = isisd_privs.group = optarg;
277 break;
jardineb5d44e2003-12-23 08:09:43 +0000278 break;
279 case 'v':
280 printf("ISISd version %s\n", ISISD_VERSION);
281 printf("Copyright (c) 2001-2002 Sampo Saaristo,"
282 " Ofer Wald and Hannes Gredler\n");
283 print_version ("Zebra");
284 exit (0);
285 break;
286 case 'h':
287 usage (0);
288 break;
289 default:
290 usage (1);
291 break;
292 }
293 }
294
295 /* thread master */
296 master = thread_master_create ();
297
298 /* random seed from time */
299 srand(time(NULL));
300
301 /*
302 * initializations
303 */
jardin9e867fe2003-12-23 08:56:18 +0000304 zprivs_init (&isisd_privs);
paul2d75d052004-01-19 21:31:15 +0000305 signal_init (master, Q_SIGC(isisd_signals), isisd_signals);
jardineb5d44e2003-12-23 08:09:43 +0000306 cmd_init (1);
jardin9e867fe2003-12-23 08:56:18 +0000307 vty_init (master);
jardineb5d44e2003-12-23 08:09:43 +0000308 memory_init ();
309 isis_init ();
310 dyn_cache_init ();
311 sort_node ();
312
313 /* parse config file */
314 /* this is needed three times! because we have interfaces before the areas */
315 vty_read_config (config_file, config_current, config_default);
316#if 0
317 vty_read_config (config_file, config_current, config_default);
318 vty_read_config (config_file, config_current, config_default);
319#endif
320 /* demonize */
321 if (daemon_mode)
322 daemon (0, 0);
323
jardineb5d44e2003-12-23 08:09:43 +0000324 /* Process ID file creation. */
hassoc3aac6f2004-02-20 18:44:21 +0000325 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000326
327 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000328 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
jardineb5d44e2003-12-23 08:09:43 +0000329
330 /* Print banner. */
jardin9e867fe2003-12-23 08:56:18 +0000331#if defined(ZEBRA_VERSION)
jardineb5d44e2003-12-23 08:09:43 +0000332 zlog_info ("ISISd %s starting: vty@%d", ZEBRA_VERSION, vty_port);
jardin9e867fe2003-12-23 08:56:18 +0000333#elif defined(QUAGGA_VERSION)
334 zlog_info ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
335#endif
jardineb5d44e2003-12-23 08:09:43 +0000336#ifdef HAVE_IPV6
337 zlog_info ("IPv6 enabled");
338#endif
339 /* Start finite state machine. */
340 while (thread_fetch (master, &thread))
341 thread_call (&thread);
342
343 /* Not reached. */
344 exit (0);
345}
346
347
348
349
350
351
352
353
354
355