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