blob: 82735b731642cf1e90d1187d5257088ae63270d2 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
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
gdt5e4fa162004-03-16 14:38:36 +000025#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000026#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"
pauledd7c242003-06-04 13:59:38 +000039#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000040#include "sigevent.h"
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040041#include "zclient.h"
paul718e3742002-12-13 20:15:29 +000042
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
pauledd7c242003-06-04 13:59:38 +000053/* ospfd privileges */
54zebra_capabilities_t _caps_p [] =
55{
paulceacedb2005-09-29 14:39:32 +000056 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000057 ZCAP_BIND,
paulceacedb2005-09-29 14:39:32 +000058 ZCAP_NET_ADMIN,
pauledd7c242003-06-04 13:59:38 +000059};
60
61struct zebra_privs_t ospfd_privs =
62{
pauld81fadf2003-08-14 05:32:12 +000063#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
64 .user = QUAGGA_USER,
65 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +000066#endif
67#if defined(VTY_GROUP)
68 .vty_group = VTY_GROUP,
69#endif
70 .caps_p = _caps_p,
Balaji.G837d16c2012-09-26 14:09:10 +053071 .cap_num_p = array_size(_caps_p),
pauledd7c242003-06-04 13:59:38 +000072 .cap_num_i = 0
73};
74
paul718e3742002-12-13 20:15:29 +000075/* Configuration filename and directory. */
paul718e3742002-12-13 20:15:29 +000076char 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'},
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040084 { "socket", required_argument, NULL, 'z'},
Paul Jakma876b8be2006-10-15 23:35:57 +000085 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000086 { "help", no_argument, NULL, 'h'},
87 { "vty_addr", required_argument, NULL, 'A'},
88 { "vty_port", required_argument, NULL, 'P'},
pauledd7c242003-06-04 13:59:38 +000089 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000090 { "group", required_argument, NULL, 'g'},
hassoc3abdb72004-10-11 16:27:03 +000091 { "apiserver", no_argument, NULL, 'a'},
paul718e3742002-12-13 20:15:29 +000092 { "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 */
hassoeb1ce602004-10-08 08:17:22 +0000102const char *pid_file = PATH_OSPFD_PID;
paul718e3742002-12-13 20:15:29 +0000103
hassod68614d2004-10-13 09:32:48 +0000104#ifdef SUPPORT_OSPF_API
hassof4d58ce2004-10-12 06:13:54 +0000105extern int ospf_apiserver_enable;
hassod68614d2004-10-13 09:32:48 +0000106#endif /* SUPPORT_OSPF_API */
hassoc3abdb72004-10-11 16:27:03 +0000107
paul718e3742002-12-13 20:15:29 +0000108/* Help information display. */
paul4dadc292005-05-06 21:37:42 +0000109static void __attribute__ ((noreturn))
paul718e3742002-12-13 20:15:29 +0000110usage (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\
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400121-z, --socket Set path of zebra socket\n\
paul718e3742002-12-13 20:15:29 +0000122-A, --vty_addr Set vty's bind address\n\
123-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000124-u, --user User to run as\n\
125-g, --group Group to run as\n\
hassoc3abdb72004-10-11 16:27:03 +0000126-a. --apiserver Enable OSPF apiserver\n\
paul718e3742002-12-13 20:15:29 +0000127-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000128-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000129-h, --help Display this help and exit\n\
130\n\
131Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
132 }
133 exit (status);
134}
David Lamparter6b0655a2014-06-04 06:53:35 +0200135
paul718e3742002-12-13 20:15:29 +0000136/* SIGHUP handler. */
paul4dadc292005-05-06 21:37:42 +0000137static void
paul2d75d052004-01-19 21:31:15 +0000138sighup (void)
paul718e3742002-12-13 20:15:29 +0000139{
140 zlog (NULL, LOG_INFO, "SIGHUP received");
141}
142
paul88d6cf32005-10-29 12:50:09 +0000143/* SIGINT / SIGTERM handler. */
144static void
paul2d75d052004-01-19 21:31:15 +0000145sigint (void)
paul718e3742002-12-13 20:15:29 +0000146{
ajs887c44a2004-12-03 16:36:46 +0000147 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000148 ospf_terminate ();
paul718e3742002-12-13 20:15:29 +0000149}
150
151/* SIGUSR1 handler. */
paul4dadc292005-05-06 21:37:42 +0000152static void
paul2d75d052004-01-19 21:31:15 +0000153sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000154{
155 zlog_rotate (NULL);
156}
157
paul2d75d052004-01-19 21:31:15 +0000158struct quagga_signal_t ospf_signals[] =
paul718e3742002-12-13 20:15:29 +0000159{
paul2d75d052004-01-19 21:31:15 +0000160 {
161 .signal = SIGHUP,
162 .handler = &sighup,
163 },
164 {
165 .signal = SIGUSR1,
166 .handler = &sigusr1,
167 },
168 {
169 .signal = SIGINT,
170 .handler = &sigint,
171 },
hassof571dab2004-03-22 08:55:25 +0000172 {
173 .signal = SIGTERM,
174 .handler = &sigint,
175 },
paul2d75d052004-01-19 21:31:15 +0000176};
David Lamparter6b0655a2014-06-04 06:53:35 +0200177
paul718e3742002-12-13 20:15:29 +0000178/* OSPFd main routine. */
179int
180main (int argc, char **argv)
181{
182 char *p;
183 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000184 int vty_port = OSPF_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000185 int daemon_mode = 0;
186 char *config_file = NULL;
187 char *progname;
188 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000189 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000190
191 /* Set umask before anything for security */
192 umask (0027);
193
David Lamparter822d8f52012-12-04 11:11:41 -0800194#ifdef SUPPORT_OSPF_API
195 /* OSPF apiserver is disabled by default. */
196 ospf_apiserver_enable = 0;
197#endif /* SUPPORT_OSPF_API */
198
paul718e3742002-12-13 20:15:29 +0000199 /* get program name */
200 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
201
paul718e3742002-12-13 20:15:29 +0000202 while (1)
203 {
204 int opt;
205
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400206 opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:avC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000207
208 if (opt == EOF)
209 break;
210
211 switch (opt)
212 {
213 case 0:
214 break;
215 case 'd':
216 daemon_mode = 1;
217 break;
218 case 'f':
219 config_file = optarg;
220 break;
221 case 'A':
222 vty_addr = optarg;
223 break;
224 case 'i':
225 pid_file = optarg;
226 break;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400227 case 'z':
228 zclient_serv_path_set (optarg);
229 break;
paul718e3742002-12-13 20:15:29 +0000230 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000231 /* Deal with atoi() returning 0 on failure, and ospfd not
232 listening on ospfd port... */
233 if (strcmp(optarg, "0") == 0)
234 {
235 vty_port = 0;
236 break;
237 }
238 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000239 if (vty_port <= 0 || vty_port > 0xffff)
240 vty_port = OSPF_VTY_PORT;
paul4fc4e7a2003-01-22 19:47:09 +0000241 break;
hassoc3abdb72004-10-11 16:27:03 +0000242 case 'u':
hassoc0652302004-11-25 19:33:48 +0000243 ospfd_privs.user = optarg;
244 break;
245 case 'g':
246 ospfd_privs.group = optarg;
hassoc3abdb72004-10-11 16:27:03 +0000247 break;
hassod68614d2004-10-13 09:32:48 +0000248#ifdef SUPPORT_OSPF_API
hassoc3abdb72004-10-11 16:27:03 +0000249 case 'a':
250 ospf_apiserver_enable = 1;
251 break;
hassod68614d2004-10-13 09:32:48 +0000252#endif /* SUPPORT_OSPF_API */
paul718e3742002-12-13 20:15:29 +0000253 case 'v':
254 print_version (progname);
255 exit (0);
256 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000257 case 'C':
258 dryrun = 1;
259 break;
paul718e3742002-12-13 20:15:29 +0000260 case 'h':
261 usage (progname, 0);
262 break;
263 default:
264 usage (progname, 1);
265 break;
266 }
267 }
268
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400269 /* Invoked by a priviledged user? -- endo. */
270 if (geteuid () != 0)
271 {
272 errno = EPERM;
273 perror (progname);
274 exit (1);
275 }
276
277 zlog_default = openzlog (progname, ZLOG_OSPF,
278 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
279
280 /* OSPF master init. */
281 ospf_master_init ();
282
paul718e3742002-12-13 20:15:29 +0000283 /* Initializations. */
paul020709f2003-04-04 02:44:16 +0000284 master = om->master;
paul718e3742002-12-13 20:15:29 +0000285
286 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000287 zprivs_init (&ospfd_privs);
Balaji.G837d16c2012-09-26 14:09:10 +0530288 signal_init (master, array_size(ospf_signals), ospf_signals);
paul718e3742002-12-13 20:15:29 +0000289 cmd_init (1);
290 debug_init ();
paulb21b19c2003-06-15 01:28:29 +0000291 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000292 memory_init ();
293
294 access_list_init ();
295 prefix_list_init ();
296
297 /* OSPFd inits. */
paul718e3742002-12-13 20:15:29 +0000298 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
paul718e3742002-12-13 20:15:29 +0000313 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000314 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000315
Paul Jakma876b8be2006-10-15 23:35:57 +0000316 /* Start execution only if not in dry-run mode */
317 if (dryrun)
318 return(0);
319
paul718e3742002-12-13 20:15:29 +0000320 /* Change to the daemon program. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700321 if (daemon_mode && daemon (0, 0) < 0)
322 {
323 zlog_err("OSPFd daemon failed: %s", strerror(errno));
324 exit (1);
325 }
paul718e3742002-12-13 20:15:29 +0000326
327 /* Process id file create. */
328 pid_output (pid_file);
329
330 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000331 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000332
333 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000334 zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
paul718e3742002-12-13 20:15:29 +0000335
336 /* Fetch next active thread. */
337 while (thread_fetch (master, &thread))
338 thread_call (&thread);
339
340 /* Not reached. */
paule8e19462006-01-19 20:16:55 +0000341 return (0);
paul718e3742002-12-13 20:15:29 +0000342}
343