blob: baf5f38d4ef7ade3fb582fb31189248de1428da6 [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"
36
37#include "isisd/dict.h"
38#include "include-netbsd/iso.h"
39#include "isisd/isis_constants.h"
40#include "isisd/isis_common.h"
41#include "isisd/isis_flags.h"
42#include "isisd/isis_circuit.h"
43#include "isisd/isisd.h"
44#include "isisd/isis_dynhn.h"
45
46/* Default configuration file name */
47#define ISISD_DEFAULT_CONFIG "isisd.conf"
48/* Default vty port */
49#define ISISD_VTY_PORT 2607
50
51/* isisd options */
52struct option longopts[] =
53{
54 { "daemon", no_argument, NULL, 'd'},
55 { "config_file", required_argument, NULL, 'f'},
56 { "vty_port", required_argument, NULL, 'P'},
57 { "version", no_argument, NULL, 'v'},
58 { "help", no_argument, NULL, 'h'},
59 { 0 }
60};
61
62/* Configuration file and directory. */
63char config_current[] = ISISD_DEFAULT_CONFIG;
64char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
65char *config_file = NULL;
66
67/* isisd program name. */
68char *progname;
69
70int daemon_mode = 0;
71
72/* Master of threads. */
73struct thread_master *master;
74
75
76/* for reload */
77char _cwd[64];
78char _progpath[64];
79int _argc;
80char **_argv;
81char **_envp;
82
83
84/* Help information display. */
85static void
86usage (int status)
87{
88 if (status != 0)
89 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
90 else
91 {
92 printf ("Usage : %s [OPTION...]\n\n\
93Daemon which manages IS-IS routing\n\n\
94-d, --daemon Runs in daemon mode\n\
95-f, --config_file Set configuration file name\n\
96-P, --vty_port Set vty's port number\n\
97-v, --version Print program version\n\
98-h, --help Display this help and exit\n\
99\n\
100Report bugs to sambo@cs.tut.fi\n", progname);
101 }
102
103 exit (status);
104}
105
106
107void
108reload ()
109{
110 zlog_info ("Reload");
111 /* FIXME: Clean up func call here */
112 vty_finish ();
113 execve (_progpath, _argv, _envp);
114}
115
116void
117terminate (int i)
118{
119 exit (i);
120}
121
122/*
123 * Signal handlers
124 */
125void
126sighup (int sig)
127{
128 zlog_info ("SIGHUP received");
129 reload ();
130
131 return;
132}
133
134void
135sigint (int sig)
136{
137 zlog_info ("SIGINT received");
138 terminate (0);
139
140 return;
141}
142
143void
144sigterm (int sig)
145{
146 zlog_info ("SIGTERM received");
147 terminate (0);
148}
149
150void
151sigusr1 (int sig)
152{
153 zlog_info ("SIGUSR1 received");
154 zlog_rotate (NULL);
155}
156
157/*
158 * Signal wrapper.
159 */
160RETSIGTYPE *
161signal_set (int signo, void (*func)(int))
162{
163 int ret;
164 struct sigaction sig;
165 struct sigaction osig;
166
167 sig.sa_handler = func;
168 sigemptyset (&sig.sa_mask);
169 sig.sa_flags = 0;
170#ifdef SA_RESTART
171 sig.sa_flags |= SA_RESTART;
172#endif /* SA_RESTART */
173
174 ret = sigaction (signo, &sig, &osig);
175
176 if (ret < 0)
177 return (SIG_ERR);
178 else
179 return (osig.sa_handler);
180}
181
182void
183signal_init ()
184{
185 signal_set (SIGHUP, sighup);
186 signal_set (SIGINT, sigint);
187 signal_set (SIGTERM, sigterm);
188 signal_set (SIGPIPE, SIG_IGN);
189#ifdef SIGTSTP
190 signal_set (SIGTSTP, SIG_IGN);
191#endif
192#ifdef SIGTTIN
193 signal_set (SIGTTIN, SIG_IGN);
194#endif
195#ifdef SIGTTOU
196 signal_set (SIGTTOU, SIG_IGN);
197#endif
198 signal_set (SIGUSR1, sigusr1);
199}
200
201/*
202 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
203 */
204int
205main (int argc, char **argv, char **envp)
206{
207 char *p;
208 int opt, vty_port = ISISD_VTY_PORT;
209 struct thread thread;
210 char *config_file = NULL;
211 char *vty_addr = NULL;
212
213 /* Get the programname without the preceding path. */
214 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
215
216 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_ISIS,
217 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
218
219
220 /* for reload */
221 _argc = argc;
222 _argv = argv;
223 _envp = envp;
224 getcwd (_cwd, sizeof (_cwd));
225 if (*argv[0] == '.')
226 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
227 else
228 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
229
230 /* Command line argument treatment. */
231 while (1)
232 {
233 opt = getopt_long (argc, argv, "df:hAp:P:v", longopts, 0);
234
235 if (opt == EOF)
236 break;
237
238 switch (opt)
239 {
240 case 0:
241 break;
242 case 'd':
243 daemon_mode = 1;
244 break;
245 case 'f':
246 config_file = optarg;
247 break;
248 case 'A':
249 vty_addr = optarg;
250 break;
251 case 'P':
252 vty_port = atoi (optarg);
253 break;
254 case 'v':
255 printf("ISISd version %s\n", ISISD_VERSION);
256 printf("Copyright (c) 2001-2002 Sampo Saaristo,"
257 " Ofer Wald and Hannes Gredler\n");
258 print_version ("Zebra");
259 exit (0);
260 break;
261 case 'h':
262 usage (0);
263 break;
264 default:
265 usage (1);
266 break;
267 }
268 }
269
270 /* thread master */
271 master = thread_master_create ();
272
273 /* random seed from time */
274 srand(time(NULL));
275
276 /*
277 * initializations
278 */
279 signal_init ();
280 cmd_init (1);
281 vty_init ();
282 memory_init ();
283 isis_init ();
284 dyn_cache_init ();
285 sort_node ();
286
287 /* parse config file */
288 /* this is needed three times! because we have interfaces before the areas */
289 vty_read_config (config_file, config_current, config_default);
290#if 0
291 vty_read_config (config_file, config_current, config_default);
292 vty_read_config (config_file, config_current, config_default);
293#endif
294 /* demonize */
295 if (daemon_mode)
296 daemon (0, 0);
297
298 /* Problems with the build env ?*/
299#ifndef PATH_ISISD_PID
300#define PATH_ISISD_PID "/var/run/isisd.pid"
301#endif
302 /* Process ID file creation. */
303 pid_output (PATH_ISISD_PID);
304
305 /* Make isis vty socket. */
306 vty_serv_sock (vty_addr, vty_port ? vty_port : ISISD_VTY_PORT,
307 ISIS_VTYSH_PATH);
308
309 /* Print banner. */
310 zlog_info ("ISISd %s starting: vty@%d", ZEBRA_VERSION, vty_port);
311#ifdef HAVE_IPV6
312 zlog_info ("IPv6 enabled");
313#endif
314 /* Start finite state machine. */
315 while (thread_fetch (master, &thread))
316 thread_call (&thread);
317
318 /* Not reached. */
319 exit (0);
320}
321
322
323
324
325
326
327
328
329
330