]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_main.c
Merge branch 'quagga' into google-bgp-multipath
[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"
edd7c245 39#include "privs.h"
2d75d052 40#include "sigevent.h"
b5114685 41#include "zclient.h"
718e3744 42
43#include "ospfd/ospfd.h"
44#include "ospfd/ospf_interface.h"
45#include "ospfd/ospf_asbr.h"
46#include "ospfd/ospf_lsa.h"
47#include "ospfd/ospf_lsdb.h"
48#include "ospfd/ospf_neighbor.h"
49#include "ospfd/ospf_dump.h"
50#include "ospfd/ospf_zebra.h"
51#include "ospfd/ospf_vty.h"
52
edd7c245 53/* ospfd privileges */
54zebra_capabilities_t _caps_p [] =
55{
ceacedba 56 ZCAP_NET_RAW,
edd7c245 57 ZCAP_BIND,
ceacedba 58 ZCAP_NET_ADMIN,
edd7c245 59};
60
61struct zebra_privs_t ospfd_privs =
62{
d81fadfd 63#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
64 .user = QUAGGA_USER,
65 .group = QUAGGA_GROUP,
edd7c245 66#endif
67#if defined(VTY_GROUP)
68 .vty_group = VTY_GROUP,
69#endif
70 .caps_p = _caps_p,
71 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
72 .cap_num_i = 0
73};
74
718e3744 75/* Configuration filename and directory. */
718e3744 76char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
77
78/* OSPFd options. */
79struct option longopts[] =
80{
81 { "daemon", no_argument, NULL, 'd'},
82 { "config_file", required_argument, NULL, 'f'},
83 { "pid_file", required_argument, NULL, 'i'},
b5114685 84 { "socket", required_argument, NULL, 'z'},
876b8be0 85 { "dryrun", no_argument, NULL, 'C'},
718e3744 86 { "help", no_argument, NULL, 'h'},
87 { "vty_addr", required_argument, NULL, 'A'},
88 { "vty_port", required_argument, NULL, 'P'},
edd7c245 89 { "user", required_argument, NULL, 'u'},
c065230a 90 { "group", required_argument, NULL, 'g'},
c3abdb72 91 { "apiserver", no_argument, NULL, 'a'},
718e3744 92 { "version", no_argument, NULL, 'v'},
93 { 0 }
94};
95
96/* OSPFd program name */
97
98/* Master of threads. */
99struct thread_master *master;
100
101/* Process ID saved for use by init system */
eb1ce605 102const char *pid_file = PATH_OSPFD_PID;
718e3744 103
d68614db 104#ifdef SUPPORT_OSPF_API
f4d58ce5 105extern int ospf_apiserver_enable;
d68614db 106#endif /* SUPPORT_OSPF_API */
c3abdb72 107
718e3744 108/* Help information display. */
4dadc291 109static void __attribute__ ((noreturn))
718e3744 110usage (char *progname, int status)
111{
112 if (status != 0)
113 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
114 else
115 {
116 printf ("Usage : %s [OPTION...]\n\
117Daemon which manages OSPF.\n\n\
118-d, --daemon Runs in daemon mode\n\
119-f, --config_file Set configuration file name\n\
120-i, --pid_file Set process identifier file name\n\
b5114685 121-z, --socket Set path of zebra socket\n\
718e3744 122-A, --vty_addr Set vty's bind address\n\
123-P, --vty_port Set vty's port number\n\
c065230a 124-u, --user User to run as\n\
125-g, --group Group to run as\n\
c3abdb72 126-a. --apiserver Enable OSPF apiserver\n\
718e3744 127-v, --version Print program version\n\
876b8be0 128-C, --dryrun Check configuration for validity and exit\n\
718e3744 129-h, --help Display this help and exit\n\
130\n\
131Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
132 }
133 exit (status);
134}
135\f
136/* SIGHUP handler. */
4dadc291 137static void
2d75d052 138sighup (void)
718e3744 139{
140 zlog (NULL, LOG_INFO, "SIGHUP received");
141}
142
88d6cf37 143/* SIGINT / SIGTERM handler. */
144static void
2d75d052 145sigint (void)
718e3744 146{
887c44a4 147 zlog_notice ("Terminating on signal");
718e3744 148 ospf_terminate ();
718e3744 149}
150
151/* SIGUSR1 handler. */
4dadc291 152static void
2d75d052 153sigusr1 (void)
718e3744 154{
155 zlog_rotate (NULL);
156}
157
2d75d052 158struct quagga_signal_t ospf_signals[] =
718e3744 159{
2d75d052 160 {
161 .signal = SIGHUP,
162 .handler = &sighup,
163 },
164 {
165 .signal = SIGUSR1,
166 .handler = &sigusr1,
167 },
168 {
169 .signal = SIGINT,
170 .handler = &sigint,
171 },
f571dab0 172 {
173 .signal = SIGTERM,
174 .handler = &sigint,
175 },
2d75d052 176};
718e3744 177\f
178/* OSPFd main routine. */
179int
180main (int argc, char **argv)
181{
182 char *p;
183 char *vty_addr = NULL;
4fc4e7ab 184 int vty_port = OSPF_VTY_PORT;
718e3744 185 int daemon_mode = 0;
186 char *config_file = NULL;
187 char *progname;
188 struct thread thread;
876b8be0 189 int dryrun = 0;
718e3744 190
191 /* Set umask before anything for security */
192 umask (0027);
193
194 /* get program name */
195 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
196
718e3744 197 while (1)
198 {
199 int opt;
200
b5114685 201 opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:avC", longopts, 0);
718e3744 202
203 if (opt == EOF)
204 break;
205
206 switch (opt)
207 {
208 case 0:
209 break;
210 case 'd':
211 daemon_mode = 1;
212 break;
213 case 'f':
214 config_file = optarg;
215 break;
216 case 'A':
217 vty_addr = optarg;
218 break;
219 case 'i':
220 pid_file = optarg;
221 break;
b5114685
VT
222 case 'z':
223 zclient_serv_path_set (optarg);
224 break;
718e3744 225 case 'P':
4fc4e7ab 226 /* Deal with atoi() returning 0 on failure, and ospfd not
227 listening on ospfd port... */
228 if (strcmp(optarg, "0") == 0)
229 {
230 vty_port = 0;
231 break;
232 }
233 vty_port = atoi (optarg);
0d6b2ee2
PJ
234 if (vty_port <= 0 || vty_port > 0xffff)
235 vty_port = OSPF_VTY_PORT;
4fc4e7ab 236 break;
c3abdb72 237 case 'u':
c065230a 238 ospfd_privs.user = optarg;
239 break;
240 case 'g':
241 ospfd_privs.group = optarg;
c3abdb72 242 break;
d68614db 243#ifdef SUPPORT_OSPF_API
c3abdb72 244 case 'a':
245 ospf_apiserver_enable = 1;
246 break;
d68614db 247#endif /* SUPPORT_OSPF_API */
718e3744 248 case 'v':
249 print_version (progname);
250 exit (0);
251 break;
876b8be0
PJ
252 case 'C':
253 dryrun = 1;
254 break;
718e3744 255 case 'h':
256 usage (progname, 0);
257 break;
258 default:
259 usage (progname, 1);
260 break;
261 }
262 }
263
b5114685
VT
264 /* Invoked by a priviledged user? -- endo. */
265 if (geteuid () != 0)
266 {
267 errno = EPERM;
268 perror (progname);
269 exit (1);
270 }
271
272 zlog_default = openzlog (progname, ZLOG_OSPF,
273 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
274
275 /* OSPF master init. */
276 ospf_master_init ();
277
278#ifdef SUPPORT_OSPF_API
279 /* OSPF apiserver is disabled by default. */
280 ospf_apiserver_enable = 0;
281#endif /* SUPPORT_OSPF_API */
282
718e3744 283 /* Initializations. */
020709f9 284 master = om->master;
718e3744 285
286 /* Library inits. */
edd7c245 287 zprivs_init (&ospfd_privs);
2d75d052 288 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
718e3744 289 cmd_init (1);
290 debug_init ();
b21b19c5 291 vty_init (master);
718e3744 292 memory_init ();
293
294 access_list_init ();
295 prefix_list_init ();
296
297 /* OSPFd inits. */
718e3744 298 ospf_if_init ();
299 ospf_zebra_init ();
300
301 /* OSPF vty inits. */
302 ospf_vty_init ();
303 ospf_vty_show_init ();
304
305 ospf_route_map_init ();
306#ifdef HAVE_SNMP
307 ospf_snmp_init ();
308#endif /* HAVE_SNMP */
309#ifdef HAVE_OPAQUE_LSA
310 ospf_opaque_init ();
311#endif /* HAVE_OPAQUE_LSA */
312
313 sort_node ();
314
315 /* Get configuration file. */
320ec10a 316 vty_read_config (config_file, config_default);
718e3744 317
876b8be0
PJ
318 /* Start execution only if not in dry-run mode */
319 if (dryrun)
320 return(0);
321
718e3744 322 /* Change to the daemon program. */
065de903
SH
323 if (daemon_mode && daemon (0, 0) < 0)
324 {
325 zlog_err("OSPFd daemon failed: %s", strerror(errno));
326 exit (1);
327 }
718e3744 328
329 /* Process id file create. */
330 pid_output (pid_file);
331
332 /* Create VTY socket */
4fc4e7ab 333 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
718e3744 334
335 /* Print banner. */
887c44a4 336 zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
718e3744 337
338 /* Fetch next active thread. */
339 while (thread_fetch (master, &thread))
340 thread_call (&thread);
341
342 /* Not reached. */
e8e1946e 343 return (0);
718e3744 344}
345