]> git.proxmox.com Git - mirror_frr.git/blob - ospf6d/ospf6_main.c
Merge remote-tracking branch 'origin/cmaster-next' into vtysh-grammar
[mirror_frr.git] / ospf6d / ospf6_main.c
1 /*
2 * Copyright (C) 1999 Yasuhiro Ohara
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22 #include <zebra.h>
23 #include <lib/version.h>
24 #include <stdlib.h>
25
26 #include "getopt.h"
27 #include "thread.h"
28 #include "log.h"
29 #include "command.h"
30 #include "vty.h"
31 #include "memory.h"
32 #include "memory_vty.h"
33 #include "if.h"
34 #include "filter.h"
35 #include "prefix.h"
36 #include "plist.h"
37 #include "privs.h"
38 #include "sigevent.h"
39 #include "zclient.h"
40 #include "vrf.h"
41 #include "bfd.h"
42
43 #include "ospf6d.h"
44 #include "ospf6_top.h"
45 #include "ospf6_message.h"
46 #include "ospf6_asbr.h"
47 #include "ospf6_lsa.h"
48 #include "ospf6_interface.h"
49 #include "ospf6_zebra.h"
50
51 /* Default configuration file name for ospf6d. */
52 #define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
53
54 /* Default port values. */
55 #define OSPF6_VTY_PORT 2606
56
57 /* ospf6d privileges */
58 zebra_capabilities_t _caps_p [] =
59 {
60 ZCAP_NET_RAW,
61 ZCAP_BIND
62 };
63
64 struct zebra_privs_t ospf6d_privs =
65 {
66 #if defined(QUAGGA_USER)
67 .user = QUAGGA_USER,
68 #endif
69 #if defined QUAGGA_GROUP
70 .group = QUAGGA_GROUP,
71 #endif
72 #ifdef VTY_GROUP
73 .vty_group = VTY_GROUP,
74 #endif
75 .caps_p = _caps_p,
76 .cap_num_p = 2,
77 .cap_num_i = 0
78 };
79
80 /* ospf6d options, we use GNU getopt library. */
81 struct option longopts[] =
82 {
83 { "daemon", no_argument, NULL, 'd'},
84 { "config_file", required_argument, NULL, 'f'},
85 { "pid_file", required_argument, NULL, 'i'},
86 { "socket", required_argument, NULL, 'z'},
87 { "vty_addr", required_argument, NULL, 'A'},
88 { "vty_port", required_argument, NULL, 'P'},
89 { "user", required_argument, NULL, 'u'},
90 { "group", required_argument, NULL, 'g'},
91 { "version", no_argument, NULL, 'v'},
92 { "dryrun", no_argument, NULL, 'C'},
93 { "help", no_argument, NULL, 'h'},
94 { 0 }
95 };
96
97 /* Configuration file and directory. */
98 char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
99
100 /* ospf6d program name. */
101 char *progname;
102
103 /* is daemon? */
104 int daemon_mode = 0;
105
106 /* Master of threads. */
107 struct thread_master *master;
108
109 /* Process ID saved for use by init system */
110 const char *pid_file = PATH_OSPF6D_PID;
111
112 /* Help information display. */
113 static void
114 usage (char *progname, 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\
121 Daemon which manages OSPF version 3.\n\n\
122 -d, --daemon Runs in daemon mode\n\
123 -f, --config_file Set configuration file name\n\
124 -i, --pid_file Set process identifier file name\n\
125 -z, --socket Set path of zebra socket\n\
126 -A, --vty_addr Set vty's bind address\n\
127 -P, --vty_port Set vty's port number\n\
128 -u, --user User to run as\n\
129 -g, --group Group to run as\n\
130 -v, --version Print program version\n\
131 -C, --dryrun Check configuration for validity and exit\n\
132 -h, --help Display this help and exit\n\
133 \n\
134 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
135 }
136
137 exit (status);
138 }
139
140 static void __attribute__ ((noreturn))
141 ospf6_exit (int status)
142 {
143 struct listnode *node;
144 struct interface *ifp;
145
146 if (ospf6)
147 ospf6_delete (ospf6);
148
149 bfd_gbl_exit();
150
151 for (ALL_LIST_ELEMENTS_RO (vrf_iflist (VRF_DEFAULT), node, ifp))
152 if (ifp->info != NULL)
153 ospf6_interface_delete(ifp->info);
154
155 ospf6_message_terminate ();
156 ospf6_asbr_terminate ();
157 ospf6_lsa_terminate ();
158
159 vrf_terminate ();
160 vty_terminate ();
161 cmd_terminate ();
162
163 if (zclient)
164 zclient_free (zclient);
165
166 if (master)
167 thread_master_free (master);
168
169 if (zlog_default)
170 closezlog (zlog_default);
171
172 exit (status);
173 }
174
175 /* SIGHUP handler. */
176 static void
177 sighup (void)
178 {
179 zlog_info ("SIGHUP received");
180 }
181
182 /* SIGINT handler. */
183 static void
184 sigint (void)
185 {
186 zlog_notice ("Terminating on signal SIGINT");
187 ospf6_exit (0);
188 }
189
190 /* SIGTERM handler. */
191 static void
192 sigterm (void)
193 {
194 zlog_notice ("Terminating on signal SIGTERM");
195 ospf6_clean();
196 ospf6_exit (0);
197 }
198
199 /* SIGUSR1 handler. */
200 static void
201 sigusr1 (void)
202 {
203 zlog_info ("SIGUSR1 received");
204 zlog_rotate (NULL);
205 }
206
207 struct quagga_signal_t ospf6_signals[] =
208 {
209 {
210 .signal = SIGHUP,
211 .handler = &sighup,
212 },
213 {
214 .signal = SIGINT,
215 .handler = &sigint,
216 },
217 {
218 .signal = SIGTERM,
219 .handler = &sigterm,
220 },
221 {
222 .signal = SIGUSR1,
223 .handler = &sigusr1,
224 },
225 };
226
227 /* Main routine of ospf6d. Treatment of argument and starting ospf finite
228 state machine is handled here. */
229 int
230 main (int argc, char *argv[], char *envp[])
231 {
232 char *p;
233 int opt;
234 char *vty_addr = NULL;
235 int vty_port = 0;
236 char *config_file = NULL;
237 struct thread thread;
238 int dryrun = 0;
239
240 /* Set umask before anything for security */
241 umask (0027);
242
243 /* Preserve name of myself. */
244 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
245
246 /* Seed random number for LSA ID */
247 srandom (time(NULL));
248
249 /* Command line argument treatment. */
250 while (1)
251 {
252 opt = getopt_long (argc, argv, "df:i:z:hp:A:P:u:g:vC", longopts, 0);
253
254 if (opt == EOF)
255 break;
256
257 switch (opt)
258 {
259 case 0:
260 break;
261 case 'd':
262 daemon_mode = 1;
263 break;
264 case 'f':
265 config_file = optarg;
266 break;
267 case 'A':
268 vty_addr = optarg;
269 break;
270 case 'i':
271 pid_file = optarg;
272 break;
273 case 'z':
274 zclient_serv_path_set (optarg);
275 break;
276 case 'P':
277 /* Deal with atoi() returning 0 on failure, and ospf6d not
278 listening on ospf6d port... */
279 if (strcmp(optarg, "0") == 0)
280 {
281 vty_port = 0;
282 break;
283 }
284 vty_port = atoi (optarg);
285 if (vty_port <= 0 || vty_port > 0xffff)
286 vty_port = OSPF6_VTY_PORT;
287 break;
288 case 'u':
289 ospf6d_privs.user = optarg;
290 break;
291 case 'g':
292 ospf6d_privs.group = optarg;
293 break;
294 case 'v':
295 print_version (progname);
296 exit (0);
297 break;
298 case 'C':
299 dryrun = 1;
300 break;
301 case 'h':
302 usage (progname, 0);
303 break;
304 default:
305 usage (progname, 1);
306 break;
307 }
308 }
309
310 if (geteuid () != 0)
311 {
312 errno = EPERM;
313 perror (progname);
314 exit (1);
315 }
316
317 /* thread master */
318 master = thread_master_create ();
319
320 /* Initializations. */
321 zlog_default = openzlog (progname, ZLOG_OSPF6, 0,
322 LOG_CONS|LOG_NDELAY|LOG_PID,
323 LOG_DAEMON);
324 zprivs_init (&ospf6d_privs);
325 #if defined(HAVE_CUMULUS)
326 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
327 #endif
328
329 /* initialize zebra libraries */
330 signal_init (master, array_size(ospf6_signals), ospf6_signals);
331 cmd_init (1);
332 vty_init (master);
333 memory_init ();
334 vrf_init ();
335 access_list_init ();
336 prefix_list_init ();
337
338 /* initialize ospf6 */
339 ospf6_init ();
340
341 /* parse config file */
342 vty_read_config (config_file, config_default);
343
344 /* Start execution only if not in dry-run mode */
345 if (dryrun)
346 return(0);
347
348 if (daemon_mode && daemon (0, 0) < 0)
349 {
350 zlog_err("OSPF6d daemon failed: %s", strerror(errno));
351 exit (1);
352 }
353
354 /* pid file create */
355 pid_output (pid_file);
356
357 /* Make ospf6 vty socket. */
358 if (!vty_port)
359 vty_port = OSPF6_VTY_PORT;
360 vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
361
362 /* Print start message */
363 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
364 QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
365
366 /* Start finite state machine, here we go! */
367 while (thread_fetch (master, &thread))
368 thread_call (&thread);
369
370 /* Log in case thread failed */
371 zlog_warn ("Thread failed");
372
373 /* Not reached. */
374 ospf6_exit (0);
375 }
376
377