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