+ fix the bug reported by Milan Kocian (IPv6 route handling was broken by the RIB...
[jleu-quagga.git] / ospfd / ospf_main.c
blob27a12dd04a18ffc9096ccb5448d3bb42b6216235
1 /*
2 * OSPFd main routine.
3 * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
23 #include <zebra.h>
25 #include <lib/version.h>
26 #include "getopt.h"
27 #include "thread.h"
28 #include "prefix.h"
29 #include "linklist.h"
30 #include "if.h"
31 #include "vector.h"
32 #include "vty.h"
33 #include "command.h"
34 #include "filter.h"
35 #include "plist.h"
36 #include "stream.h"
37 #include "log.h"
38 #include "memory.h"
39 #include "privs.h"
40 #include "sigevent.h"
42 #include "ospfd/ospfd.h"
43 #include "ospfd/ospf_interface.h"
44 #include "ospfd/ospf_asbr.h"
45 #include "ospfd/ospf_lsa.h"
46 #include "ospfd/ospf_lsdb.h"
47 #include "ospfd/ospf_neighbor.h"
48 #include "ospfd/ospf_dump.h"
49 #include "ospfd/ospf_zebra.h"
50 #include "ospfd/ospf_vty.h"
52 /* ospfd privileges */
53 zebra_capabilities_t _caps_p [] =
55 ZCAP_NET_RAW,
56 ZCAP_BIND,
57 ZCAP_NET_ADMIN,
60 struct zebra_privs_t ospfd_privs =
62 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
63 .user = QUAGGA_USER,
64 .group = QUAGGA_GROUP,
65 #endif
66 #if defined(VTY_GROUP)
67 .vty_group = VTY_GROUP,
68 #endif
69 .caps_p = _caps_p,
70 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
71 .cap_num_i = 0
74 /* Configuration filename and directory. */
75 char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
77 /* OSPFd options. */
78 struct option longopts[] =
80 { "daemon", no_argument, NULL, 'd'},
81 { "config_file", required_argument, NULL, 'f'},
82 { "pid_file", required_argument, NULL, 'i'},
83 { "log_mode", no_argument, NULL, 'l'},
84 { "dryrun", no_argument, NULL, 'C'},
85 { "help", no_argument, NULL, 'h'},
86 { "vty_addr", required_argument, NULL, 'A'},
87 { "vty_port", required_argument, NULL, 'P'},
88 { "user", required_argument, NULL, 'u'},
89 { "group", required_argument, NULL, 'g'},
90 { "apiserver", no_argument, NULL, 'a'},
91 { "version", no_argument, NULL, 'v'},
92 { 0 }
95 /* OSPFd program name */
97 /* Master of threads. */
98 struct thread_master *master;
100 /* Process ID saved for use by init system */
101 const char *pid_file = PATH_OSPFD_PID;
103 #ifdef SUPPORT_OSPF_API
104 extern int ospf_apiserver_enable;
105 #endif /* SUPPORT_OSPF_API */
107 /* Help information display. */
108 static void __attribute__ ((noreturn))
109 usage (char *progname, int status)
111 if (status != 0)
112 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
113 else
115 printf ("Usage : %s [OPTION...]\n\
116 Daemon which manages OSPF.\n\n\
117 -d, --daemon Runs in daemon mode\n\
118 -f, --config_file Set configuration file name\n\
119 -i, --pid_file Set process identifier file name\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 -a. --apiserver Enable OSPF apiserver\n\
125 -v, --version Print program version\n\
126 -C, --dryrun Check configuration for validity and exit\n\
127 -h, --help Display this help and exit\n\
129 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
131 exit (status);
134 /* SIGHUP handler. */
135 static void
136 sighup (void)
138 zlog (NULL, LOG_INFO, "SIGHUP received");
141 /* SIGINT / SIGTERM handler. */
142 static void
143 sigint (void)
145 zlog_notice ("Terminating on signal");
146 ospf_terminate ();
149 /* SIGUSR1 handler. */
150 static void
151 sigusr1 (void)
153 zlog_rotate (NULL);
156 struct quagga_signal_t ospf_signals[] =
159 .signal = SIGHUP,
160 .handler = &sighup,
163 .signal = SIGUSR1,
164 .handler = &sigusr1,
167 .signal = SIGINT,
168 .handler = &sigint,
171 .signal = SIGTERM,
172 .handler = &sigint,
176 /* OSPFd main routine. */
178 main (int argc, char **argv)
180 char *p;
181 char *vty_addr = NULL;
182 int vty_port = OSPF_VTY_PORT;
183 int daemon_mode = 0;
184 char *config_file = NULL;
185 char *progname;
186 struct thread thread;
187 int dryrun = 0;
189 /* Set umask before anything for security */
190 umask (0027);
192 /* get program name */
193 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
195 /* Invoked by a priviledged user? -- endo. */
196 if (geteuid () != 0)
198 errno = EPERM;
199 perror (progname);
200 exit (1);
203 zlog_default = openzlog (progname, ZLOG_OSPF,
204 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
206 /* OSPF master init. */
207 ospf_master_init ();
209 #ifdef SUPPORT_OSPF_API
210 /* OSPF apiserver is disabled by default. */
211 ospf_apiserver_enable = 0;
212 #endif /* SUPPORT_OSPF_API */
214 while (1)
216 int opt;
218 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:avC", longopts, 0);
220 if (opt == EOF)
221 break;
223 switch (opt)
225 case 0:
226 break;
227 case 'd':
228 daemon_mode = 1;
229 break;
230 case 'f':
231 config_file = optarg;
232 break;
233 case 'A':
234 vty_addr = optarg;
235 break;
236 case 'i':
237 pid_file = optarg;
238 break;
239 case 'P':
240 /* Deal with atoi() returning 0 on failure, and ospfd not
241 listening on ospfd port... */
242 if (strcmp(optarg, "0") == 0)
244 vty_port = 0;
245 break;
247 vty_port = atoi (optarg);
248 vty_port = (vty_port ? vty_port : OSPF_VTY_PORT);
249 break;
250 case 'u':
251 ospfd_privs.user = optarg;
252 break;
253 case 'g':
254 ospfd_privs.group = optarg;
255 break;
256 #ifdef SUPPORT_OSPF_API
257 case 'a':
258 ospf_apiserver_enable = 1;
259 break;
260 #endif /* SUPPORT_OSPF_API */
261 case 'v':
262 print_version (progname);
263 exit (0);
264 break;
265 case 'C':
266 dryrun = 1;
267 break;
268 case 'h':
269 usage (progname, 0);
270 break;
271 default:
272 usage (progname, 1);
273 break;
277 /* Initializations. */
278 master = om->master;
280 /* Library inits. */
281 zprivs_init (&ospfd_privs);
282 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
283 cmd_init (1);
284 debug_init ();
285 vty_init (master);
286 memory_init ();
288 access_list_init ();
289 prefix_list_init ();
291 /* OSPFd inits. */
292 ospf_if_init ();
293 ospf_zebra_init ();
295 /* OSPF vty inits. */
296 ospf_vty_init ();
297 ospf_vty_show_init ();
299 ospf_route_map_init ();
300 #ifdef HAVE_SNMP
301 ospf_snmp_init ();
302 #endif /* HAVE_SNMP */
303 #ifdef HAVE_OPAQUE_LSA
304 ospf_opaque_init ();
305 #endif /* HAVE_OPAQUE_LSA */
307 sort_node ();
309 /* Get configuration file. */
310 vty_read_config (config_file, config_default);
312 /* Start execution only if not in dry-run mode */
313 if (dryrun)
314 return(0);
316 /* Change to the daemon program. */
317 if (daemon_mode)
318 daemon (0, 0);
320 /* Process id file create. */
321 pid_output (pid_file);
323 /* Create VTY socket */
324 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
326 /* Print banner. */
327 zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
329 /* Fetch next active thread. */
330 while (thread_fetch (master, &thread))
331 thread_call (&thread);
333 /* Not reached. */
334 return (0);