]> git.proxmox.com Git - mirror_frr.git/blob - isisd/isis_main.c
Merge branch '-rip' into stable/2.0
[mirror_frr.git] / isisd / isis_main.c
1 /*
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 <zebra.h>
24
25 #include "getopt.h"
26 #include "thread.h"
27 #include "log.h"
28 #include <lib/version.h>
29 #include "command.h"
30 #include "vty.h"
31 #include "memory.h"
32 #include "memory_vty.h"
33 #include "stream.h"
34 #include "if.h"
35 #include "privs.h"
36 #include "sigevent.h"
37 #include "filter.h"
38 #include "plist.h"
39 #include "zclient.h"
40 #include "vrf.h"
41 #include "qobj.h"
42
43 #include "isisd/dict.h"
44 #include "isisd/isis_constants.h"
45 #include "isisd/isis_common.h"
46 #include "isisd/isis_flags.h"
47 #include "isisd/isis_circuit.h"
48 #include "isisd/isisd.h"
49 #include "isisd/isis_dynhn.h"
50 #include "isisd/isis_spf.h"
51 #include "isisd/isis_route.h"
52 #include "isisd/isis_routemap.h"
53 #include "isisd/isis_zebra.h"
54 #include "isisd/isis_tlv.h"
55 #include "isisd/isis_te.h"
56
57 /* Default configuration file name */
58 #define ISISD_DEFAULT_CONFIG "isisd.conf"
59 /* Default vty port */
60 #define ISISD_VTY_PORT 2608
61
62 /* isisd privileges */
63 zebra_capabilities_t _caps_p[] = {
64 ZCAP_NET_RAW,
65 ZCAP_BIND
66 };
67
68 struct zebra_privs_t isisd_privs = {
69 #if defined(QUAGGA_USER)
70 .user = QUAGGA_USER,
71 #endif
72 #if defined QUAGGA_GROUP
73 .group = QUAGGA_GROUP,
74 #endif
75 #ifdef VTY_GROUP
76 .vty_group = VTY_GROUP,
77 #endif
78 .caps_p = _caps_p,
79 .cap_num_p = sizeof (_caps_p) / sizeof (*_caps_p),
80 .cap_num_i = 0
81 };
82
83 /* isisd options */
84 struct option longopts[] = {
85 {"daemon", no_argument, NULL, 'd'},
86 {"config_file", required_argument, NULL, 'f'},
87 {"pid_file", required_argument, NULL, 'i'},
88 {"socket", required_argument, NULL, 'z'},
89 {"vty_addr", required_argument, NULL, 'A'},
90 {"vty_port", required_argument, NULL, 'P'},
91 {"user", required_argument, NULL, 'u'},
92 {"group", required_argument, NULL, 'g'},
93 {"version", no_argument, NULL, 'v'},
94 {"dryrun", no_argument, NULL, 'C'},
95 {"help", no_argument, NULL, 'h'},
96 {0}
97 };
98
99 /* Configuration file and directory. */
100 char config_default[] = SYSCONFDIR ISISD_DEFAULT_CONFIG;
101 char *config_file = NULL;
102
103 /* isisd program name. */
104 char *progname;
105
106 int daemon_mode = 0;
107
108 /* Master of threads. */
109 struct thread_master *master;
110
111 /* Process ID saved for use by init system */
112 const char *pid_file = PATH_ISISD_PID;
113
114 /* for reload */
115 char _cwd[MAXPATHLEN];
116 char _progpath[MAXPATHLEN];
117 int _argc;
118 char **_argv;
119 char **_envp;
120
121 /*
122 * Prototypes.
123 */
124 void reload(void);
125 void sighup(void);
126 void sigint(void);
127 void sigterm(void);
128 void sigusr1(void);
129
130
131 /* Help information display. */
132 static void
133 usage (int status)
134 {
135 if (status != 0)
136 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
137 else
138 {
139 printf ("Usage : %s [OPTION...]\n\n\
140 Daemon which manages IS-IS routing\n\n\
141 -d, --daemon Runs in daemon mode\n\
142 -f, --config_file Set configuration file name\n\
143 -i, --pid_file Set process identifier file name\n\
144 -z, --socket Set path of zebra socket\n\
145 -A, --vty_addr Set vty's bind address\n\
146 -P, --vty_port Set vty's port number\n\
147 -u, --user User to run as\n\
148 -g, --group Group to run as\n\
149 -v, --version Print program version\n\
150 -C, --dryrun Check configuration for validity and exit\n\
151 -h, --help Display this help and exit\n\
152 \n\
153 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
154 }
155
156 exit (status);
157 }
158
159
160 void
161 reload ()
162 {
163 zlog_debug ("Reload");
164 /* FIXME: Clean up func call here */
165 vty_reset ();
166 (void) isisd_privs.change (ZPRIVS_RAISE);
167 execve (_progpath, _argv, _envp);
168 zlog_err ("Reload failed: cannot exec %s: %s", _progpath,
169 safe_strerror (errno));
170 }
171
172 static __attribute__((__noreturn__)) void
173 terminate (int i)
174 {
175 exit (i);
176 }
177
178 /*
179 * Signal handlers
180 */
181
182 void
183 sighup (void)
184 {
185 zlog_debug ("SIGHUP received");
186 reload ();
187
188 return;
189 }
190
191 __attribute__((__noreturn__)) void
192 sigint (void)
193 {
194 zlog_notice ("Terminating on signal SIGINT");
195 terminate (0);
196 }
197
198 __attribute__((__noreturn__)) void
199 sigterm (void)
200 {
201 zlog_notice ("Terminating on signal SIGTERM");
202 terminate (0);
203 }
204
205 void
206 sigusr1 (void)
207 {
208 zlog_debug ("SIGUSR1 received");
209 zlog_rotate (NULL);
210 }
211
212 struct quagga_signal_t isisd_signals[] =
213 {
214 {
215 .signal = SIGHUP,
216 .handler = &sighup,
217 },
218 {
219 .signal = SIGUSR1,
220 .handler = &sigusr1,
221 },
222 {
223 .signal = SIGINT,
224 .handler = &sigint,
225 },
226 {
227 .signal = SIGTERM,
228 .handler = &sigterm,
229 },
230 };
231
232 /*
233 * Main routine of isisd. Parse arguments and handle IS-IS state machine.
234 */
235 int
236 main (int argc, char **argv, char **envp)
237 {
238 char *p;
239 int opt, vty_port = ISISD_VTY_PORT;
240 struct thread thread;
241 char *config_file = NULL;
242 char *vty_addr = NULL;
243 int dryrun = 0;
244
245 /* Get the programname without the preceding path. */
246 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
247
248 zlog_default = openzlog (progname, ZLOG_ISIS, 0,
249 LOG_CONS | LOG_NDELAY | LOG_PID, LOG_DAEMON);
250 zprivs_init (&isisd_privs);
251 #if defined(HAVE_CUMULUS)
252 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
253 #endif
254
255 /* for reload */
256 _argc = argc;
257 _argv = argv;
258 _envp = envp;
259 if (getcwd (_cwd, sizeof (_cwd)) == NULL)
260 {
261 zlog_err ("ISISd: Unable to determine CWD: %d", errno);
262 exit (1);
263 }
264
265 if (*argv[0] == '.')
266 snprintf (_progpath, sizeof (_progpath), "%s/%s", _cwd, _argv[0]);
267 else
268 snprintf (_progpath, sizeof (_progpath), "%s", argv[0]);
269
270 /* Command line argument treatment. */
271 while (1)
272 {
273 opt = getopt_long (argc, argv, "df:i:z:hA:p:P:u:g:vC", longopts, 0);
274
275 if (opt == EOF)
276 break;
277
278 switch (opt)
279 {
280 case 0:
281 break;
282 case 'd':
283 daemon_mode = 1;
284 break;
285 case 'f':
286 config_file = optarg;
287 break;
288 case 'i':
289 pid_file = optarg;
290 break;
291 case 'z':
292 zclient_serv_path_set (optarg);
293 break;
294 case 'A':
295 vty_addr = optarg;
296 break;
297 case 'P':
298 /* Deal with atoi() returning 0 on failure, and isisd not
299 listening on isisd port... */
300 if (strcmp (optarg, "0") == 0)
301 {
302 vty_port = 0;
303 break;
304 }
305 vty_port = atoi (optarg);
306 vty_port = (vty_port ? vty_port : ISISD_VTY_PORT);
307 break;
308 case 'u':
309 isisd_privs.user = optarg;
310 break;
311 case 'g':
312 isisd_privs.group = optarg;
313 break;
314 case 'v':
315 printf ("ISISd version %s\n", ISISD_VERSION);
316 printf ("Copyright (c) 2001-2002 Sampo Saaristo,"
317 " Ofer Wald and Hannes Gredler\n");
318 print_version ("Zebra");
319 exit (0);
320 break;
321 case 'C':
322 dryrun = 1;
323 break;
324 case 'h':
325 usage (0);
326 break;
327 default:
328 usage (1);
329 break;
330 }
331 }
332
333 /* thread master */
334 master = thread_master_create ();
335
336 /* random seed from time */
337 srandom (time (NULL));
338
339 /*
340 * initializations
341 */
342 signal_init (master, array_size (isisd_signals), isisd_signals);
343 cmd_init (1);
344 vty_config_lockless ();
345 vty_init (master);
346 memory_init ();
347 access_list_init();
348 vrf_init ();
349 prefix_list_init();
350 isis_init ();
351 isis_circuit_init ();
352 isis_spf_cmds_init ();
353 isis_redist_init ();
354 isis_route_map_init();
355 isis_mpls_te_init();
356
357 /* create the global 'isis' instance */
358 isis_new (1);
359
360 isis_zebra_init(master);
361
362 /* parse config file */
363 /* this is needed three times! because we have interfaces before the areas */
364 vty_read_config (config_file, config_default);
365
366 /* Start execution only if not in dry-run mode */
367 if (dryrun)
368 return(0);
369
370 /* demonize */
371 if (daemon_mode && daemon (0, 0) < 0)
372 {
373 zlog_err("ISISd daemon failed: %s", strerror(errno));
374 return (1);
375 }
376
377 /* Process ID file creation. */
378 if (pid_file[0] != '\0')
379 pid_output (pid_file);
380
381 /* Make isis vty socket. */
382 vty_serv_sock (vty_addr, vty_port, ISIS_VTYSH_PATH);
383
384 /* Print banner. */
385 zlog_notice ("Quagga-ISISd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
386
387 /* Start finite state machine. */
388 while (thread_fetch (master, &thread))
389 thread_call (&thread);
390
391 /* Not reached. */
392 exit (0);
393 }