]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_main.c
Merge pull request #257 from opensourcerouting/nhrpd
[mirror_frr.git] / ospfd / ospf_main.c
CommitLineData
718e3744 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
5e4fa164 25#include <lib/version.h>
718e3744 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"
fc7948fa 39#include "memory_vty.h"
edd7c245 40#include "privs.h"
2d75d052 41#include "sigevent.h"
b5114685 42#include "zclient.h"
6a69b354 43#include "vrf.h"
ff1c42fb 44#include "sockopt.h"
718e3744 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"
7f342629 55#include "ospfd/ospf_bfd.h"
718e3744 56
edd7c245 57/* ospfd privileges */
58zebra_capabilities_t _caps_p [] =
59{
ceacedba 60 ZCAP_NET_RAW,
edd7c245 61 ZCAP_BIND,
ceacedba 62 ZCAP_NET_ADMIN,
edd7c245 63};
64
65struct zebra_privs_t ospfd_privs =
66{
b2f36157
DL
67#if defined(FRR_USER) && defined(FRR_GROUP)
68 .user = FRR_USER,
69 .group = FRR_GROUP,
edd7c245 70#endif
71#if defined(VTY_GROUP)
72 .vty_group = VTY_GROUP,
73#endif
74 .caps_p = _caps_p,
837d16cc 75 .cap_num_p = array_size(_caps_p),
edd7c245 76 .cap_num_i = 0
77};
78
718e3744 79/* Configuration filename and directory. */
7c8ff89e 80char config_default[100];
718e3744 81
82/* OSPFd options. */
9cbb9630 83#define OPTION_VTYSOCK 1000
718e3744 84struct option longopts[] =
85{
86 { "daemon", no_argument, NULL, 'd'},
7c8ff89e 87 { "instance", required_argument, NULL, 'n'},
718e3744 88 { "config_file", required_argument, NULL, 'f'},
89 { "pid_file", required_argument, NULL, 'i'},
b5114685 90 { "socket", required_argument, NULL, 'z'},
876b8be0 91 { "dryrun", no_argument, NULL, 'C'},
718e3744 92 { "help", no_argument, NULL, 'h'},
93 { "vty_addr", required_argument, NULL, 'A'},
94 { "vty_port", required_argument, NULL, 'P'},
9cbb9630 95 { "vty_socket", required_argument, NULL, OPTION_VTYSOCK},
edd7c245 96 { "user", required_argument, NULL, 'u'},
c065230a 97 { "group", required_argument, NULL, 'g'},
c3abdb72 98 { "apiserver", no_argument, NULL, 'a'},
718e3744 99 { "version", no_argument, NULL, 'v'},
100 { 0 }
101};
102
103/* OSPFd program name */
104
9cbb9630
MW
105/* VTY Socket prefix */
106char vty_sock_path[MAXPATHLEN] = OSPF_VTYSH_PATH;
107
718e3744 108/* Master of threads. */
109struct thread_master *master;
110
111/* Process ID saved for use by init system */
7c8ff89e 112char pid_file[100];
718e3744 113
d68614db 114#ifdef SUPPORT_OSPF_API
f4d58ce5 115extern int ospf_apiserver_enable;
d68614db 116#endif /* SUPPORT_OSPF_API */
c3abdb72 117
718e3744 118/* Help information display. */
4dadc291 119static void __attribute__ ((noreturn))
718e3744 120usage (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\
127Daemon which manages OSPF.\n\n\
128-d, --daemon Runs in daemon mode\n\
7c8ff89e 129-n, --instance Set the instance id\n\
718e3744 130-f, --config_file Set configuration file name\n\
131-i, --pid_file Set process identifier file name\n\
b5114685 132-z, --socket Set path of zebra socket\n\
718e3744 133-A, --vty_addr Set vty's bind address\n\
134-P, --vty_port Set vty's port number\n\
9cbb9630 135 --vty_socket Override vty socket path\n\
c065230a 136-u, --user User to run as\n\
137-g, --group Group to run as\n\
c3abdb72 138-a. --apiserver Enable OSPF apiserver\n\
718e3744 139-v, --version Print program version\n\
876b8be0 140-C, --dryrun Check configuration for validity and exit\n\
718e3744 141-h, --help Display this help and exit\n\
142\n\
b2f36157 143Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
718e3744 144 }
145 exit (status);
146}
6b0655a2 147
718e3744 148/* SIGHUP handler. */
4dadc291 149static void
2d75d052 150sighup (void)
718e3744 151{
152 zlog (NULL, LOG_INFO, "SIGHUP received");
153}
154
88d6cf37 155/* SIGINT / SIGTERM handler. */
156static void
2d75d052 157sigint (void)
718e3744 158{
887c44a4 159 zlog_notice ("Terminating on signal");
718e3744 160 ospf_terminate ();
718e3744 161}
162
163/* SIGUSR1 handler. */
4dadc291 164static void
2d75d052 165sigusr1 (void)
718e3744 166{
167 zlog_rotate (NULL);
168}
169
2d75d052 170struct quagga_signal_t ospf_signals[] =
718e3744 171{
2d75d052 172 {
173 .signal = SIGHUP,
174 .handler = &sighup,
175 },
176 {
177 .signal = SIGUSR1,
178 .handler = &sigusr1,
179 },
180 {
181 .signal = SIGINT,
182 .handler = &sigint,
183 },
f571dab0 184 {
185 .signal = SIGTERM,
186 .handler = &sigint,
187 },
2d75d052 188};
6b0655a2 189
718e3744 190/* OSPFd main routine. */
191int
192main (int argc, char **argv)
193{
194 char *p;
195 char *vty_addr = NULL;
4fc4e7ab 196 int vty_port = OSPF_VTY_PORT;
7c8ff89e 197 char vty_path[100];
718e3744 198 int daemon_mode = 0;
199 char *config_file = NULL;
200 char *progname;
7c8ff89e 201 u_short instance = 0;
718e3744 202 struct thread thread;
876b8be0 203 int dryrun = 0;
718e3744 204
205 /* Set umask before anything for security */
206 umask (0027);
207
822d8f55
DL
208#ifdef SUPPORT_OSPF_API
209 /* OSPF apiserver is disabled by default. */
210 ospf_apiserver_enable = 0;
211#endif /* SUPPORT_OSPF_API */
212
bb5ea4a6 213 strcpy(pid_file, PATH_OSPFD_PID);
214
718e3744 215 /* get program name */
216 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
217
718e3744 218 while (1)
219 {
220 int opt;
221
7c8ff89e 222 opt = getopt_long (argc, argv, "df:i:n:z:hA:P:u:g:avC", longopts, 0);
718e3744 223
224 if (opt == EOF)
225 break;
226
227 switch (opt)
228 {
7c8ff89e
DS
229 case 'n':
230 instance = atoi(optarg);
988225dd 231 if (instance < 1)
7c8ff89e
DS
232 exit(0);
233 break;
718e3744 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':
7c8ff89e 246 strcpy(pid_file,optarg);
718e3744 247 break;
b5114685
VT
248 case 'z':
249 zclient_serv_path_set (optarg);
250 break;
718e3744 251 case 'P':
4fc4e7ab 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);
0d6b2ee2
PJ
260 if (vty_port <= 0 || vty_port > 0xffff)
261 vty_port = OSPF_VTY_PORT;
4fc4e7ab 262 break;
9cbb9630
MW
263 case OPTION_VTYSOCK:
264 set_socket_path(vty_sock_path, OSPF_VTYSH_PATH, optarg, sizeof (vty_sock_path));
265 break;
c3abdb72 266 case 'u':
c065230a 267 ospfd_privs.user = optarg;
268 break;
269 case 'g':
270 ospfd_privs.group = optarg;
c3abdb72 271 break;
d68614db 272#ifdef SUPPORT_OSPF_API
c3abdb72 273 case 'a':
274 ospf_apiserver_enable = 1;
275 break;
d68614db 276#endif /* SUPPORT_OSPF_API */
718e3744 277 case 'v':
278 print_version (progname);
279 exit (0);
280 break;
876b8be0
PJ
281 case 'C':
282 dryrun = 1;
283 break;
718e3744 284 case 'h':
285 usage (progname, 0);
286 break;
287 default:
288 usage (progname, 1);
289 break;
290 }
291 }
292
b5114685
VT
293 /* Invoked by a priviledged user? -- endo. */
294 if (geteuid () != 0)
295 {
296 errno = EPERM;
297 perror (progname);
298 exit (1);
299 }
300
7c8ff89e 301 zlog_default = openzlog (progname, ZLOG_OSPF, instance,
b5114685 302 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
2caa9b39 303 zprivs_init (&ospfd_privs);
c05795b1
SK
304#if defined(HAVE_CUMULUS)
305 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
306#endif
b5114685
VT
307
308 /* OSPF master init. */
309 ospf_master_init ();
310
718e3744 311 /* Initializations. */
020709f9 312 master = om->master;
718e3744 313
314 /* Library inits. */
837d16cc 315 signal_init (master, array_size(ospf_signals), ospf_signals);
718e3744 316 cmd_init (1);
317 debug_init ();
b21b19c5 318 vty_init (master);
718e3744 319 memory_init ();
6a69b354 320 vrf_init ();
718e3744 321
322 access_list_init ();
323 prefix_list_init ();
324
325 /* OSPFd inits. */
718e3744 326 ospf_if_init ();
4140ca4d 327 ospf_zebra_init(master, instance);
718e3744 328
329 /* OSPF vty inits. */
330 ospf_vty_init ();
331 ospf_vty_show_init ();
09f35f8c 332 ospf_vty_clear_init ();
718e3744 333
7f342629
DS
334 /* OSPF BFD init */
335 ospf_bfd_init();
336
718e3744 337 ospf_route_map_init ();
338#ifdef HAVE_SNMP
339 ospf_snmp_init ();
340#endif /* HAVE_SNMP */
718e3744 341 ospf_opaque_init ();
718e3744 342
6dae198f
DS
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 */
7c8ff89e 346 if (!ospf_get_instance(instance))
6dae198f
DS
347 {
348 zlog_err("OSPF instance init failed: %s", strerror(errno));
349 exit (1);
350 }
351
718e3744 352 /* Get configuration file. */
7c8ff89e
DS
353 if (instance)
354 sprintf(config_default, "%sospfd-%d.conf", SYSCONFDIR, instance);
355 else
356 sprintf(config_default, "%s%s", SYSCONFDIR, OSPF_DEFAULT_CONFIG);
320ec10a 357 vty_read_config (config_file, config_default);
718e3744 358
876b8be0
PJ
359 /* Start execution only if not in dry-run mode */
360 if (dryrun)
361 return(0);
362
718e3744 363 /* Change to the daemon program. */
065de903
SH
364 if (daemon_mode && daemon (0, 0) < 0)
365 {
366 zlog_err("OSPFd daemon failed: %s", strerror(errno));
367 exit (1);
368 }
718e3744 369
9cbb9630
MW
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 );
67e5d676 386 strlcpy(pid_file, pidfile_temp, sizeof(pid_file));
9cbb9630
MW
387 }
388 /* Process id file create. */
389 pid_output (pid_file);
390
7c8ff89e
DS
391 /* Create VTY socket */
392 if (instance)
393 {
9cbb9630
MW
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 );
7c8ff89e
DS
407 }
408 else
409 {
9cbad1d7 410 strlcpy(vty_path, vty_sock_path, sizeof(vty_path));
7c8ff89e 411 }
7c8ff89e 412 vty_serv_sock (vty_addr, vty_port, vty_path);
718e3744 413
414 /* Print banner. */
b2f36157 415 zlog_notice ("OSPFd %s starting: vty@%d, %s", FRR_VERSION, vty_port, vty_path);
718e3744 416
417 /* Fetch next active thread. */
418 while (thread_fetch (master, &thread))
419 thread_call (&thread);
420
421 /* Not reached. */
e8e1946e 422 return (0);
718e3744 423}
424