blob: 1a200a8f59d7807a37054bafc4445fb6eb2c3e23 [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"
paul718e3742002-12-13 20:15:29 +000041
42#include "ospfd/ospfd.h"
43#include "ospfd/ospf_interface.h"
44#include "ospfd/ospf_asbr.h"
45#include "ospfd/ospf_lsa.h"
46#include "ospfd/ospf_lsdb.h"
47#include "ospfd/ospf_neighbor.h"
48#include "ospfd/ospf_dump.h"
49#include "ospfd/ospf_zebra.h"
50#include "ospfd/ospf_vty.h"
51
pauledd7c242003-06-04 13:59:38 +000052/* ospfd privileges */
53zebra_capabilities_t _caps_p [] =
54{
paulceacedb2005-09-29 14:39:32 +000055 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000056 ZCAP_BIND,
paulceacedb2005-09-29 14:39:32 +000057 ZCAP_NET_ADMIN,
pauledd7c242003-06-04 13:59:38 +000058};
59
60struct zebra_privs_t ospfd_privs =
61{
pauld81fadf2003-08-14 05:32:12 +000062#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
63 .user = QUAGGA_USER,
64 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +000065#endif
66#if defined(VTY_GROUP)
67 .vty_group = VTY_GROUP,
68#endif
69 .caps_p = _caps_p,
70 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
71 .cap_num_i = 0
72};
73
paul718e3742002-12-13 20:15:29 +000074/* Configuration filename and directory. */
paul718e3742002-12-13 20:15:29 +000075char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
76
77/* OSPFd options. */
78struct option longopts[] =
79{
80 { "daemon", no_argument, NULL, 'd'},
81 { "config_file", required_argument, NULL, 'f'},
82 { "pid_file", required_argument, NULL, 'i'},
83 { "log_mode", no_argument, NULL, 'l'},
Paul Jakma876b8be2006-10-15 23:35:57 +000084 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000085 { "help", no_argument, NULL, 'h'},
86 { "vty_addr", required_argument, NULL, 'A'},
87 { "vty_port", required_argument, NULL, 'P'},
pauledd7c242003-06-04 13:59:38 +000088 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000089 { "group", required_argument, NULL, 'g'},
hassoc3abdb72004-10-11 16:27:03 +000090 { "apiserver", no_argument, NULL, 'a'},
paul718e3742002-12-13 20:15:29 +000091 { "version", no_argument, NULL, 'v'},
92 { 0 }
93};
94
95/* OSPFd program name */
96
97/* Master of threads. */
98struct thread_master *master;
99
100/* Process ID saved for use by init system */
hassoeb1ce602004-10-08 08:17:22 +0000101const char *pid_file = PATH_OSPFD_PID;
paul718e3742002-12-13 20:15:29 +0000102
hassod68614d2004-10-13 09:32:48 +0000103#ifdef SUPPORT_OSPF_API
hassof4d58ce2004-10-12 06:13:54 +0000104extern int ospf_apiserver_enable;
hassod68614d2004-10-13 09:32:48 +0000105#endif /* SUPPORT_OSPF_API */
hassoc3abdb72004-10-11 16:27:03 +0000106
paul718e3742002-12-13 20:15:29 +0000107/* Help information display. */
paul4dadc292005-05-06 21:37:42 +0000108static void __attribute__ ((noreturn))
paul718e3742002-12-13 20:15:29 +0000109usage (char *progname, int status)
110{
111 if (status != 0)
112 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
113 else
114 {
115 printf ("Usage : %s [OPTION...]\n\
116Daemon which manages OSPF.\n\n\
117-d, --daemon Runs in daemon mode\n\
118-f, --config_file Set configuration file name\n\
119-i, --pid_file Set process identifier file name\n\
120-A, --vty_addr Set vty's bind address\n\
121-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000122-u, --user User to run as\n\
123-g, --group Group to run as\n\
hassoc3abdb72004-10-11 16:27:03 +0000124-a. --apiserver Enable OSPF apiserver\n\
paul718e3742002-12-13 20:15:29 +0000125-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000126-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000127-h, --help Display this help and exit\n\
128\n\
129Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
130 }
131 exit (status);
132}
133
134/* SIGHUP handler. */
paul4dadc292005-05-06 21:37:42 +0000135static void
paul2d75d052004-01-19 21:31:15 +0000136sighup (void)
paul718e3742002-12-13 20:15:29 +0000137{
138 zlog (NULL, LOG_INFO, "SIGHUP received");
139}
140
paul88d6cf32005-10-29 12:50:09 +0000141/* SIGINT / SIGTERM handler. */
142static void
paul2d75d052004-01-19 21:31:15 +0000143sigint (void)
paul718e3742002-12-13 20:15:29 +0000144{
ajs887c44a2004-12-03 16:36:46 +0000145 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000146 ospf_terminate ();
paul718e3742002-12-13 20:15:29 +0000147}
148
149/* SIGUSR1 handler. */
paul4dadc292005-05-06 21:37:42 +0000150static void
paul2d75d052004-01-19 21:31:15 +0000151sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000152{
153 zlog_rotate (NULL);
154}
155
paul2d75d052004-01-19 21:31:15 +0000156struct quagga_signal_t ospf_signals[] =
paul718e3742002-12-13 20:15:29 +0000157{
paul2d75d052004-01-19 21:31:15 +0000158 {
159 .signal = SIGHUP,
160 .handler = &sighup,
161 },
162 {
163 .signal = SIGUSR1,
164 .handler = &sigusr1,
165 },
166 {
167 .signal = SIGINT,
168 .handler = &sigint,
169 },
hassof571dab2004-03-22 08:55:25 +0000170 {
171 .signal = SIGTERM,
172 .handler = &sigint,
173 },
paul2d75d052004-01-19 21:31:15 +0000174};
paul718e3742002-12-13 20:15:29 +0000175
176/* OSPFd main routine. */
177int
178main (int argc, char **argv)
179{
180 char *p;
181 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000182 int vty_port = OSPF_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000183 int daemon_mode = 0;
184 char *config_file = NULL;
185 char *progname;
186 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000187 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000188
189 /* Set umask before anything for security */
190 umask (0027);
191
192 /* get program name */
193 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
194
195 /* Invoked by a priviledged user? -- endo. */
hassob3516a72003-05-25 22:11:22 +0000196 if (geteuid () != 0)
paul718e3742002-12-13 20:15:29 +0000197 {
198 errno = EPERM;
199 perror (progname);
200 exit (1);
201 }
202
ajs274a4a42004-12-07 15:39:31 +0000203 zlog_default = openzlog (progname, ZLOG_OSPF,
paul718e3742002-12-13 20:15:29 +0000204 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
205
paul020709f2003-04-04 02:44:16 +0000206 /* OSPF master init. */
207 ospf_master_init ();
208
hassod68614d2004-10-13 09:32:48 +0000209#ifdef SUPPORT_OSPF_API
hassof4d58ce2004-10-12 06:13:54 +0000210 /* OSPF apiserver is disabled by default. */
211 ospf_apiserver_enable = 0;
hassod68614d2004-10-13 09:32:48 +0000212#endif /* SUPPORT_OSPF_API */
hassof4d58ce2004-10-12 06:13:54 +0000213
paul718e3742002-12-13 20:15:29 +0000214 while (1)
215 {
216 int opt;
217
Paul Jakma876b8be2006-10-15 23:35:57 +0000218 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:avC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000219
220 if (opt == EOF)
221 break;
222
223 switch (opt)
224 {
225 case 0:
226 break;
227 case 'd':
228 daemon_mode = 1;
229 break;
230 case 'f':
231 config_file = optarg;
232 break;
233 case 'A':
234 vty_addr = optarg;
235 break;
236 case 'i':
237 pid_file = optarg;
238 break;
239 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000240 /* Deal with atoi() returning 0 on failure, and ospfd not
241 listening on ospfd port... */
242 if (strcmp(optarg, "0") == 0)
243 {
244 vty_port = 0;
245 break;
246 }
247 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000248 if (vty_port <= 0 || vty_port > 0xffff)
249 vty_port = OSPF_VTY_PORT;
paul4fc4e7a2003-01-22 19:47:09 +0000250 break;
hassoc3abdb72004-10-11 16:27:03 +0000251 case 'u':
hassoc0652302004-11-25 19:33:48 +0000252 ospfd_privs.user = optarg;
253 break;
254 case 'g':
255 ospfd_privs.group = optarg;
hassoc3abdb72004-10-11 16:27:03 +0000256 break;
hassod68614d2004-10-13 09:32:48 +0000257#ifdef SUPPORT_OSPF_API
hassoc3abdb72004-10-11 16:27:03 +0000258 case 'a':
259 ospf_apiserver_enable = 1;
260 break;
hassod68614d2004-10-13 09:32:48 +0000261#endif /* SUPPORT_OSPF_API */
paul718e3742002-12-13 20:15:29 +0000262 case 'v':
263 print_version (progname);
264 exit (0);
265 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000266 case 'C':
267 dryrun = 1;
268 break;
paul718e3742002-12-13 20:15:29 +0000269 case 'h':
270 usage (progname, 0);
271 break;
272 default:
273 usage (progname, 1);
274 break;
275 }
276 }
277
278 /* Initializations. */
paul020709f2003-04-04 02:44:16 +0000279 master = om->master;
paul718e3742002-12-13 20:15:29 +0000280
281 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000282 zprivs_init (&ospfd_privs);
paul2d75d052004-01-19 21:31:15 +0000283 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
paul718e3742002-12-13 20:15:29 +0000284 cmd_init (1);
285 debug_init ();
paulb21b19c2003-06-15 01:28:29 +0000286 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000287 memory_init ();
288
289 access_list_init ();
290 prefix_list_init ();
291
292 /* OSPFd inits. */
paul718e3742002-12-13 20:15:29 +0000293 ospf_if_init ();
294 ospf_zebra_init ();
295
296 /* OSPF vty inits. */
297 ospf_vty_init ();
298 ospf_vty_show_init ();
299
300 ospf_route_map_init ();
301#ifdef HAVE_SNMP
302 ospf_snmp_init ();
303#endif /* HAVE_SNMP */
304#ifdef HAVE_OPAQUE_LSA
305 ospf_opaque_init ();
306#endif /* HAVE_OPAQUE_LSA */
307
308 sort_node ();
309
310 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000311 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000312
Paul Jakma876b8be2006-10-15 23:35:57 +0000313 /* Start execution only if not in dry-run mode */
314 if (dryrun)
315 return(0);
316
paul718e3742002-12-13 20:15:29 +0000317 /* Change to the daemon program. */
318 if (daemon_mode)
319 daemon (0, 0);
320
321 /* Process id file create. */
322 pid_output (pid_file);
323
324 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000325 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000326
327 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000328 zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
paul718e3742002-12-13 20:15:29 +0000329
330 /* Fetch next active thread. */
331 while (thread_fetch (master, &thread))
332 thread_call (&thread);
333
334 /* Not reached. */
paule8e19462006-01-19 20:16:55 +0000335 return (0);
paul718e3742002-12-13 20:15:29 +0000336}
337