blob: a2a7e51a96898a9be1d426b66b7479dd8bb3ff99 [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. */
jardineb5d44e2003-12-23 08:09:43 +000090char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
91char *config_file = NULL;
92
93/* isisd program name. */
94char *progname;
95
96int daemon_mode = 0;
97
98/* Master of threads. */
99struct thread_master *master;
100
hassoc3aac6f2004-02-20 18:44:21 +0000101/* Process ID saved for use by init system */
102char *pid_file = PATH_ISISD_PID;
jardineb5d44e2003-12-23 08:09:43 +0000103
104/* for reload */
hasso37da8c02004-05-19 11:38:40 +0000105char _cwd[MAXPATHLEN];
106char _progpath[MAXPATHLEN];
jardineb5d44e2003-12-23 08:09:43 +0000107int _argc;
108char **_argv;
109char **_envp;
110
111
112/* Help information display. */
113static void
114usage (int status)
115{
116 if (status != 0)
117 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
118 else
119 {
120 printf ("Usage : %s [OPTION...]\n\n\
121Daemon which manages IS-IS routing\n\n\
122-d, --daemon Runs in daemon mode\n\
123-f, --config_file Set configuration file name\n\
hassoc3aac6f2004-02-20 18:44:21 +0000124-i, --pid_file Set process identifier file name\n\
125-A, --vty_addr Set vty's bind address\n\
jardineb5d44e2003-12-23 08:09:43 +0000126-P, --vty_port Set vty's port number\n\
jardin9e867fe2003-12-23 08:56:18 +0000127-u, --user User and group to run as\n\
jardineb5d44e2003-12-23 08:09:43 +0000128-v, --version Print program version\n\
129-h, --help Display this help and exit\n\
130\n\
131Report bugs to sambo@cs.tut.fi\n", progname);
132 }
133
134 exit (status);
135}
136
137
138void
139reload ()
140{
141 zlog_info ("Reload");
142 /* FIXME: Clean up func call here */
143 vty_finish ();
144 execve (_progpath, _argv, _envp);
145}
146
147void
148terminate (int i)
149{
150 exit (i);
151}
152
153/*
154 * Signal handlers
155 */
paul2d75d052004-01-19 21:31:15 +0000156
jardineb5d44e2003-12-23 08:09:43 +0000157void
paul2d75d052004-01-19 21:31:15 +0000158sighup (void)
jardineb5d44e2003-12-23 08:09:43 +0000159{
160 zlog_info ("SIGHUP received");
161 reload ();
162
163 return;
164}
165
166void
paul2d75d052004-01-19 21:31:15 +0000167sigint (void)
jardineb5d44e2003-12-23 08:09:43 +0000168{
169 zlog_info ("SIGINT received");
170 terminate (0);
171
172 return;
173}
174
175void
paul2d75d052004-01-19 21:31:15 +0000176sigterm (void)
jardineb5d44e2003-12-23 08:09:43 +0000177{
178 zlog_info ("SIGTERM received");
179 terminate (0);
180}
181
182void
paul2d75d052004-01-19 21:31:15 +0000183sigusr1 (void)
jardineb5d44e2003-12-23 08:09:43 +0000184{
185 zlog_info ("SIGUSR1 received");
186 zlog_rotate (NULL);
187}
188
paul2d75d052004-01-19 21:31:15 +0000189struct quagga_signal_t isisd_signals[] =
190{
191 {
192 .signal = SIGHUP,
193 .handler = &sighup,
194 },
195 {
196 .signal = SIGUSR1,
197 .handler = &sigusr1,
198 },
199 {
200 .signal = SIGINT,
201 .handler = &sigint,
202 },
203 {
204 .signal = SIGTERM,
205 .handler = &sigterm,
206 },
207};
jardineb5d44e2003-12-23 08:09:43 +0000208
209/*
210 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
211 */
212int
213main (int argc, char **argv, char **envp)
214{
215 char *p;
216 int opt, vty_port = ISISD_VTY_PORT;
217 struct thread thread;
218 char *config_file = NULL;
219 char *vty_addr = NULL;
220
221 /* Get the programname without the preceding path. */
222 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
223
224 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_ISIS,
225 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
226
227
228 /* for reload */
229 _argc = argc;
230 _argv = argv;
231 _envp = envp;
232 getcwd (_cwd, sizeof (_cwd));
233 if (*argv[0] == '.')
234 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
235 else
236 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
237
238 /* Command line argument treatment. */
239 while (1)
240 {
hassoc3aac6f2004-02-20 18:44:21 +0000241 opt = getopt_long (argc, argv, "df:i:hA:p:P:u:v", longopts, 0);
jardineb5d44e2003-12-23 08:09:43 +0000242
243 if (opt == EOF)
244 break;
245
246 switch (opt)
247 {
248 case 0:
249 break;
250 case 'd':
251 daemon_mode = 1;
252 break;
253 case 'f':
254 config_file = optarg;
255 break;
hassoc3aac6f2004-02-20 18:44:21 +0000256 case 'i':
257 pid_file = optarg;
258 break;
jardineb5d44e2003-12-23 08:09:43 +0000259 case 'A':
260 vty_addr = optarg;
261 break;
262 case 'P':
jardin9e867fe2003-12-23 08:56:18 +0000263 /* Deal with atoi() returning 0 on failure, and isisd not
264 listening on isisd port... */
265 if (strcmp(optarg, "0") == 0)
266 {
267 vty_port = 0;
268 break;
269 }
jardineb5d44e2003-12-23 08:09:43 +0000270 vty_port = atoi (optarg);
jardin9e867fe2003-12-23 08:56:18 +0000271 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
272 break;
273 case 'u':
274 isisd_privs.user = isisd_privs.group = optarg;
275 break;
jardineb5d44e2003-12-23 08:09:43 +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 }
291 }
292
293 /* thread master */
294 master = thread_master_create ();
295
296 /* random seed from time */
297 srand(time(NULL));
298
299 /*
300 * initializations
301 */
jardin9e867fe2003-12-23 08:56:18 +0000302 zprivs_init (&isisd_privs);
paul2d75d052004-01-19 21:31:15 +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 ();
307 isis_init ();
308 dyn_cache_init ();
309 sort_node ();
310
311 /* parse config file */
312 /* this is needed three times! because we have interfaces before the areas */
hasso320ec102004-06-20 19:54:37 +0000313 vty_read_config (config_file, config_default);
314 vty_read_config (config_file, config_default);
315 vty_read_config (config_file, config_default);
hasso00995cf2004-05-19 13:43:50 +0000316
jardineb5d44e2003-12-23 08:09:43 +0000317 /* demonize */
318 if (daemon_mode)
319 daemon (0, 0);
320
jardineb5d44e2003-12-23 08:09:43 +0000321 /* Process ID file creation. */
hassoc3aac6f2004-02-20 18:44:21 +0000322 pid_output (pid_file);
jardineb5d44e2003-12-23 08:09:43 +0000323
324 /* Make isis vty socket. */
jardin9e867fe2003-12-23 08:56:18 +0000325 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
jardineb5d44e2003-12-23 08:09:43 +0000326
327 /* Print banner. */
jardin9e867fe2003-12-23 08:56:18 +0000328#if defined(ZEBRA_VERSION)
jardineb5d44e2003-12-23 08:09:43 +0000329 zlog_info ("ISISd %s starting: vty@%d", ZEBRA_VERSION, vty_port);
jardin9e867fe2003-12-23 08:56:18 +0000330#elif defined(QUAGGA_VERSION)
331 zlog_info ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
332#endif
jardineb5d44e2003-12-23 08:09:43 +0000333#ifdef HAVE_IPV6
334 zlog_info ("IPv6 enabled");
335#endif
336 /* Start finite state machine. */
337 while (thread_fetch (master, &thread))
338 thread_call (&thread);
339
340 /* Not reached. */
341 exit (0);
342}
343
344
345
346
347
348
349
350
351
352