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