blob: 8b9a3458db4edd33178ea518f580b5447afc7f62 [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'},
Paul Jakma876b8be2006-10-15 23:35:57 +000083 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000084 { "help", no_argument, NULL, 'h'},
85 { "vty_addr", required_argument, NULL, 'A'},
86 { "vty_port", required_argument, NULL, 'P'},
pauledd7c242003-06-04 13:59:38 +000087 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000088 { "group", required_argument, NULL, 'g'},
hassoc3abdb72004-10-11 16:27:03 +000089 { "apiserver", no_argument, NULL, 'a'},
paul718e3742002-12-13 20:15:29 +000090 { "version", no_argument, NULL, 'v'},
91 { 0 }
92};
93
94/* OSPFd program name */
95
96/* Master of threads. */
97struct thread_master *master;
98
99/* Process ID saved for use by init system */
hassoeb1ce602004-10-08 08:17:22 +0000100const char *pid_file = PATH_OSPFD_PID;
paul718e3742002-12-13 20:15:29 +0000101
hassod68614d2004-10-13 09:32:48 +0000102#ifdef SUPPORT_OSPF_API
hassof4d58ce2004-10-12 06:13:54 +0000103extern int ospf_apiserver_enable;
hassod68614d2004-10-13 09:32:48 +0000104#endif /* SUPPORT_OSPF_API */
hassoc3abdb72004-10-11 16:27:03 +0000105
paul718e3742002-12-13 20:15:29 +0000106/* Help information display. */
paul4dadc292005-05-06 21:37:42 +0000107static void __attribute__ ((noreturn))
paul718e3742002-12-13 20:15:29 +0000108usage (char *progname, int status)
109{
110 if (status != 0)
111 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
112 else
113 {
114 printf ("Usage : %s [OPTION...]\n\
115Daemon which manages OSPF.\n\n\
116-d, --daemon Runs in daemon mode\n\
117-f, --config_file Set configuration file name\n\
118-i, --pid_file Set process identifier file name\n\
119-A, --vty_addr Set vty's bind address\n\
120-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000121-u, --user User to run as\n\
122-g, --group Group to run as\n\
hassoc3abdb72004-10-11 16:27:03 +0000123-a. --apiserver Enable OSPF apiserver\n\
paul718e3742002-12-13 20:15:29 +0000124-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000125-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000126-h, --help Display this help and exit\n\
127\n\
128Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
129 }
130 exit (status);
131}
132
133/* SIGHUP handler. */
paul4dadc292005-05-06 21:37:42 +0000134static void
paul2d75d052004-01-19 21:31:15 +0000135sighup (void)
paul718e3742002-12-13 20:15:29 +0000136{
137 zlog (NULL, LOG_INFO, "SIGHUP received");
138}
139
paul88d6cf32005-10-29 12:50:09 +0000140/* SIGINT / SIGTERM handler. */
141static void
paul2d75d052004-01-19 21:31:15 +0000142sigint (void)
paul718e3742002-12-13 20:15:29 +0000143{
ajs887c44a2004-12-03 16:36:46 +0000144 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000145 ospf_terminate ();
paul718e3742002-12-13 20:15:29 +0000146}
147
148/* SIGUSR1 handler. */
paul4dadc292005-05-06 21:37:42 +0000149static void
paul2d75d052004-01-19 21:31:15 +0000150sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000151{
152 zlog_rotate (NULL);
153}
154
paul2d75d052004-01-19 21:31:15 +0000155struct quagga_signal_t ospf_signals[] =
paul718e3742002-12-13 20:15:29 +0000156{
paul2d75d052004-01-19 21:31:15 +0000157 {
158 .signal = SIGHUP,
159 .handler = &sighup,
160 },
161 {
162 .signal = SIGUSR1,
163 .handler = &sigusr1,
164 },
165 {
166 .signal = SIGINT,
167 .handler = &sigint,
168 },
hassof571dab2004-03-22 08:55:25 +0000169 {
170 .signal = SIGTERM,
171 .handler = &sigint,
172 },
paul2d75d052004-01-19 21:31:15 +0000173};
paul718e3742002-12-13 20:15:29 +0000174
175/* OSPFd main routine. */
176int
177main (int argc, char **argv)
178{
179 char *p;
180 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000181 int vty_port = OSPF_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000182 int daemon_mode = 0;
183 char *config_file = NULL;
184 char *progname;
185 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000186 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000187
188 /* Set umask before anything for security */
189 umask (0027);
190
191 /* get program name */
192 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
193
194 /* Invoked by a priviledged user? -- endo. */
hassob3516a72003-05-25 22:11:22 +0000195 if (geteuid () != 0)
paul718e3742002-12-13 20:15:29 +0000196 {
197 errno = EPERM;
198 perror (progname);
199 exit (1);
200 }
201
ajs274a4a42004-12-07 15:39:31 +0000202 zlog_default = openzlog (progname, ZLOG_OSPF,
paul718e3742002-12-13 20:15:29 +0000203 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
204
paul020709f2003-04-04 02:44:16 +0000205 /* OSPF master init. */
206 ospf_master_init ();
207
hassod68614d2004-10-13 09:32:48 +0000208#ifdef SUPPORT_OSPF_API
hassof4d58ce2004-10-12 06:13:54 +0000209 /* OSPF apiserver is disabled by default. */
210 ospf_apiserver_enable = 0;
hassod68614d2004-10-13 09:32:48 +0000211#endif /* SUPPORT_OSPF_API */
hassof4d58ce2004-10-12 06:13:54 +0000212
paul718e3742002-12-13 20:15:29 +0000213 while (1)
214 {
215 int opt;
216
Jeremy Jacksonc77cffd2008-12-28 12:57:42 -0500217 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:avC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000218
219 if (opt == EOF)
220 break;
221
222 switch (opt)
223 {
224 case 0:
225 break;
226 case 'd':
227 daemon_mode = 1;
228 break;
229 case 'f':
230 config_file = optarg;
231 break;
232 case 'A':
233 vty_addr = optarg;
234 break;
235 case 'i':
236 pid_file = optarg;
237 break;
238 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000239 /* Deal with atoi() returning 0 on failure, and ospfd not
240 listening on ospfd port... */
241 if (strcmp(optarg, "0") == 0)
242 {
243 vty_port = 0;
244 break;
245 }
246 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000247 if (vty_port <= 0 || vty_port > 0xffff)
248 vty_port = OSPF_VTY_PORT;
paul4fc4e7a2003-01-22 19:47:09 +0000249 break;
hassoc3abdb72004-10-11 16:27:03 +0000250 case 'u':
hassoc0652302004-11-25 19:33:48 +0000251 ospfd_privs.user = optarg;
252 break;
253 case 'g':
254 ospfd_privs.group = optarg;
hassoc3abdb72004-10-11 16:27:03 +0000255 break;
hassod68614d2004-10-13 09:32:48 +0000256#ifdef SUPPORT_OSPF_API
hassoc3abdb72004-10-11 16:27:03 +0000257 case 'a':
258 ospf_apiserver_enable = 1;
259 break;
hassod68614d2004-10-13 09:32:48 +0000260#endif /* SUPPORT_OSPF_API */
paul718e3742002-12-13 20:15:29 +0000261 case 'v':
262 print_version (progname);
263 exit (0);
264 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000265 case 'C':
266 dryrun = 1;
267 break;
paul718e3742002-12-13 20:15:29 +0000268 case 'h':
269 usage (progname, 0);
270 break;
271 default:
272 usage (progname, 1);
273 break;
274 }
275 }
276
277 /* Initializations. */
paul020709f2003-04-04 02:44:16 +0000278 master = om->master;
paul718e3742002-12-13 20:15:29 +0000279
280 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000281 zprivs_init (&ospfd_privs);
paul2d75d052004-01-19 21:31:15 +0000282 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
paul718e3742002-12-13 20:15:29 +0000283 cmd_init (1);
284 debug_init ();
paulb21b19c2003-06-15 01:28:29 +0000285 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000286 memory_init ();
287
288 access_list_init ();
289 prefix_list_init ();
290
291 /* OSPFd inits. */
paul718e3742002-12-13 20:15:29 +0000292 ospf_if_init ();
293 ospf_zebra_init ();
294
295 /* OSPF vty inits. */
296 ospf_vty_init ();
297 ospf_vty_show_init ();
298
299 ospf_route_map_init ();
300#ifdef HAVE_SNMP
301 ospf_snmp_init ();
302#endif /* HAVE_SNMP */
303#ifdef HAVE_OPAQUE_LSA
304 ospf_opaque_init ();
305#endif /* HAVE_OPAQUE_LSA */
306
307 sort_node ();
308
309 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000310 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000311
Paul Jakma876b8be2006-10-15 23:35:57 +0000312 /* Start execution only if not in dry-run mode */
313 if (dryrun)
314 return(0);
315
paul718e3742002-12-13 20:15:29 +0000316 /* Change to the daemon program. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700317 if (daemon_mode && daemon (0, 0) < 0)
318 {
319 zlog_err("OSPFd daemon failed: %s", strerror(errno));
320 exit (1);
321 }
paul718e3742002-12-13 20:15:29 +0000322
323 /* Process id file create. */
324 pid_output (pid_file);
325
326 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000327 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000328
329 /* Print banner. */
ajs887c44a2004-12-03 16:36:46 +0000330 zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
paul718e3742002-12-13 20:15:29 +0000331
332 /* Fetch next active thread. */
333 while (thread_fetch (master, &thread))
334 thread_call (&thread);
335
336 /* Not reached. */
paule8e19462006-01-19 20:16:55 +0000337 return (0);
paul718e3742002-12-13 20:15:29 +0000338}
339