]> git.proxmox.com Git - mirror_frr.git/blob - ospfd/ospf_main.c
Merge remote-tracking branch 'origin/cmaster' into cmaster-next
[mirror_frr.git] / ospfd / ospf_main.c
1 /*
2 * OSPFd main routine.
3 * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
4 *
5 * This file is part of GNU Zebra.
6 *
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.
11 *
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.
16 *
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.
21 */
22
23 #include <zebra.h>
24
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"
41 #include "zclient.h"
42 #include "vrf.h"
43
44 #include "ospfd/ospfd.h"
45 #include "ospfd/ospf_interface.h"
46 #include "ospfd/ospf_asbr.h"
47 #include "ospfd/ospf_lsa.h"
48 #include "ospfd/ospf_lsdb.h"
49 #include "ospfd/ospf_neighbor.h"
50 #include "ospfd/ospf_dump.h"
51 #include "ospfd/ospf_zebra.h"
52 #include "ospfd/ospf_vty.h"
53 #include "ospfd/ospf_bfd.h"
54
55 /* ospfd privileges */
56 zebra_capabilities_t _caps_p [] =
57 {
58 ZCAP_NET_RAW,
59 ZCAP_BIND,
60 ZCAP_NET_ADMIN,
61 };
62
63 struct zebra_privs_t ospfd_privs =
64 {
65 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
66 .user = QUAGGA_USER,
67 .group = QUAGGA_GROUP,
68 #endif
69 #if defined(VTY_GROUP)
70 .vty_group = VTY_GROUP,
71 #endif
72 .caps_p = _caps_p,
73 .cap_num_p = array_size(_caps_p),
74 .cap_num_i = 0
75 };
76
77 /* Configuration filename and directory. */
78 char config_default[100];
79
80 /* OSPFd options. */
81 struct option longopts[] =
82 {
83 { "daemon", no_argument, NULL, 'd'},
84 { "instance", required_argument, NULL, 'n'},
85 { "config_file", required_argument, NULL, 'f'},
86 { "pid_file", required_argument, NULL, 'i'},
87 { "socket", required_argument, NULL, 'z'},
88 { "dryrun", no_argument, NULL, 'C'},
89 { "help", no_argument, NULL, 'h'},
90 { "vty_addr", required_argument, NULL, 'A'},
91 { "vty_port", required_argument, NULL, 'P'},
92 { "user", required_argument, NULL, 'u'},
93 { "group", required_argument, NULL, 'g'},
94 { "apiserver", no_argument, NULL, 'a'},
95 { "version", no_argument, NULL, 'v'},
96 { 0 }
97 };
98
99 /* OSPFd program name */
100
101 /* Master of threads. */
102 struct thread_master *master;
103
104 /* Process ID saved for use by init system */
105 char pid_file[100];
106
107 #ifdef SUPPORT_OSPF_API
108 extern int ospf_apiserver_enable;
109 #endif /* SUPPORT_OSPF_API */
110
111 /* Help information display. */
112 static void __attribute__ ((noreturn))
113 usage (char *progname, int status)
114 {
115 if (status != 0)
116 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
117 else
118 {
119 printf ("Usage : %s [OPTION...]\n\
120 Daemon which manages OSPF.\n\n\
121 -d, --daemon Runs in daemon mode\n\
122 -n, --instance Set the instance id\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 -a. --apiserver Enable OSPF apiserver\n\
131 -v, --version Print program version\n\
132 -C, --dryrun Check configuration for validity and exit\n\
133 -h, --help Display this help and exit\n\
134 \n\
135 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
136 }
137 exit (status);
138 }
139
140 /* SIGHUP handler. */
141 static void
142 sighup (void)
143 {
144 zlog (NULL, LOG_INFO, "SIGHUP received");
145 }
146
147 /* SIGINT / SIGTERM handler. */
148 static void
149 sigint (void)
150 {
151 zlog_notice ("Terminating on signal");
152 ospf_terminate ();
153 }
154
155 /* SIGUSR1 handler. */
156 static void
157 sigusr1 (void)
158 {
159 zlog_rotate (NULL);
160 }
161
162 struct quagga_signal_t ospf_signals[] =
163 {
164 {
165 .signal = SIGHUP,
166 .handler = &sighup,
167 },
168 {
169 .signal = SIGUSR1,
170 .handler = &sigusr1,
171 },
172 {
173 .signal = SIGINT,
174 .handler = &sigint,
175 },
176 {
177 .signal = SIGTERM,
178 .handler = &sigint,
179 },
180 };
181
182 /* OSPFd main routine. */
183 int
184 main (int argc, char **argv)
185 {
186 char *p;
187 char *vty_addr = NULL;
188 int vty_port = OSPF_VTY_PORT;
189 char vty_path[100];
190 int daemon_mode = 0;
191 char *config_file = NULL;
192 char *progname;
193 u_short instance = 0;
194 struct thread thread;
195 int dryrun = 0;
196
197 /* Set umask before anything for security */
198 umask (0027);
199
200 #ifdef SUPPORT_OSPF_API
201 /* OSPF apiserver is disabled by default. */
202 ospf_apiserver_enable = 0;
203 #endif /* SUPPORT_OSPF_API */
204
205 /* get program name */
206 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
207
208 while (1)
209 {
210 int opt;
211
212 opt = getopt_long (argc, argv, "df:i:n:z:hA:P:u:g:avC", longopts, 0);
213
214 if (opt == EOF)
215 break;
216
217 switch (opt)
218 {
219 case 'n':
220 instance = atoi(optarg);
221 if (instance < 1)
222 exit(0);
223 break;
224 case 0:
225 break;
226 case 'd':
227 daemon_mode = 1;
228 break;
229 case 'f':
230 config_file = optarg;
231 break;
232 case 'A':
233 vty_addr = optarg;
234 break;
235 case 'i':
236 strcpy(pid_file,optarg);
237 break;
238 case 'z':
239 zclient_serv_path_set (optarg);
240 break;
241 case 'P':
242 /* Deal with atoi() returning 0 on failure, and ospfd not
243 listening on ospfd port... */
244 if (strcmp(optarg, "0") == 0)
245 {
246 vty_port = 0;
247 break;
248 }
249 vty_port = atoi (optarg);
250 if (vty_port <= 0 || vty_port > 0xffff)
251 vty_port = OSPF_VTY_PORT;
252 break;
253 case 'u':
254 ospfd_privs.user = optarg;
255 break;
256 case 'g':
257 ospfd_privs.group = optarg;
258 break;
259 #ifdef SUPPORT_OSPF_API
260 case 'a':
261 ospf_apiserver_enable = 1;
262 break;
263 #endif /* SUPPORT_OSPF_API */
264 case 'v':
265 print_version (progname);
266 exit (0);
267 break;
268 case 'C':
269 dryrun = 1;
270 break;
271 case 'h':
272 usage (progname, 0);
273 break;
274 default:
275 usage (progname, 1);
276 break;
277 }
278 }
279
280 /* Invoked by a priviledged user? -- endo. */
281 if (geteuid () != 0)
282 {
283 errno = EPERM;
284 perror (progname);
285 exit (1);
286 }
287
288 zlog_default = openzlog (progname, ZLOG_OSPF, instance,
289 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
290 zprivs_init (&ospfd_privs);
291 zlog_set_file (NULL, LOG_DEFAULT_FILENAME, zlog_default->default_lvl);
292
293 /* OSPF master init. */
294 ospf_master_init ();
295
296 /* Initializations. */
297 master = om->master;
298
299 /* Library inits. */
300 signal_init (master, array_size(ospf_signals), ospf_signals);
301 cmd_init (1);
302 debug_init ();
303 vty_init (master);
304 memory_init ();
305 vrf_init ();
306
307 access_list_init ();
308 prefix_list_init ();
309
310 /* OSPFd inits. */
311 ospf_if_init ();
312 ospf_zebra_init(master, instance);
313
314 /* OSPF vty inits. */
315 ospf_vty_init ();
316 ospf_vty_show_init ();
317 ospf_vty_clear_init ();
318
319 /* OSPF BFD init */
320 ospf_bfd_init();
321
322 ospf_route_map_init ();
323 #ifdef HAVE_SNMP
324 ospf_snmp_init ();
325 #endif /* HAVE_SNMP */
326 #ifdef HAVE_OPAQUE_LSA
327 ospf_opaque_init ();
328 #endif /* HAVE_OPAQUE_LSA */
329
330 /* Need to initialize the default ospf structure, so the interface mode
331 commands can be duly processed if they are received before 'router ospf',
332 when quagga(ospfd) is restarted */
333 if (!ospf_get_instance(instance))
334 {
335 zlog_err("OSPF instance init failed: %s", strerror(errno));
336 exit (1);
337 }
338
339 /* Get configuration file. */
340 if (instance)
341 sprintf(config_default, "%sospfd-%d.conf", SYSCONFDIR, instance);
342 else
343 sprintf(config_default, "%s%s", SYSCONFDIR, OSPF_DEFAULT_CONFIG);
344 vty_read_config (config_file, config_default);
345
346 /* Start execution only if not in dry-run mode */
347 if (dryrun)
348 return(0);
349
350 /* Change to the daemon program. */
351 if (daemon_mode && daemon (0, 0) < 0)
352 {
353 zlog_err("OSPFd daemon failed: %s", strerror(errno));
354 exit (1);
355 }
356
357 /* Create VTY socket */
358 if (instance)
359 {
360 sprintf(pid_file, "/var/run/quagga/ospfd-%d.pid", instance);
361 sprintf(vty_path, "/var/run/quagga/ospfd-%d.vty", instance);
362 }
363 else
364 {
365 strcpy(pid_file, PATH_OSPFD_PID);
366 strcpy(vty_path, OSPF_VTYSH_PATH);
367 }
368 /* Process id file create. */
369 pid_output (pid_file);
370
371 vty_serv_sock (vty_addr, vty_port, vty_path);
372
373 /* Print banner. */
374 zlog_notice ("OSPFd %s starting: vty@%d, %s", QUAGGA_VERSION, vty_port, vty_path);
375
376 /* Fetch next active thread. */
377 while (thread_fetch (master, &thread))
378 thread_call (&thread);
379
380 /* Not reached. */
381 return (0);
382 }
383