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