blob: f560547ff4ef5f1c32b572585b94d699c777dab7 [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"
40#include "debug.h"
paul2d75d052004-01-19 21:31:15 +000041#include "sigevent.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{
56 ZCAP_RAW,
57 ZCAP_BIND,
58 ZCAP_BROADCAST,
59 ZCAP_ADMIN,
60};
61
62struct zebra_privs_t ospfd_privs =
63{
pauld81fadf2003-08-14 05:32:12 +000064#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
65 .user = QUAGGA_USER,
66 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +000067#endif
68#if defined(VTY_GROUP)
69 .vty_group = VTY_GROUP,
70#endif
71 .caps_p = _caps_p,
72 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
73 .cap_num_i = 0
74};
75
paul718e3742002-12-13 20:15:29 +000076/* Configuration filename and directory. */
paul718e3742002-12-13 20:15:29 +000077char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
78
79/* OSPFd options. */
80struct option longopts[] =
81{
82 { "daemon", no_argument, NULL, 'd'},
83 { "config_file", required_argument, NULL, 'f'},
84 { "pid_file", required_argument, NULL, 'i'},
85 { "log_mode", no_argument, NULL, 'l'},
86 { "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. */
109static void
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\
121-A, --vty_addr Set vty's bind address\n\
122-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000123-u, --user User to run as\n\
124-g, --group Group to run as\n\
hassoc3abdb72004-10-11 16:27:03 +0000125-a. --apiserver Enable OSPF apiserver\n\
paul718e3742002-12-13 20:15:29 +0000126-v, --version Print program version\n\
127-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. */
135void
paul2d75d052004-01-19 21:31:15 +0000136sighup (void)
paul718e3742002-12-13 20:15:29 +0000137{
138 zlog (NULL, LOG_INFO, "SIGHUP received");
139}
140
141/* SIGINT handler. */
142void
paul2d75d052004-01-19 21:31:15 +0000143sigint (void)
paul718e3742002-12-13 20:15:29 +0000144{
145 zlog (NULL, LOG_INFO, "Terminating on signal");
146
147 ospf_terminate ();
148
149 exit (0);
150}
151
152/* SIGUSR1 handler. */
153void
paul2d75d052004-01-19 21:31:15 +0000154sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000155{
156 zlog_rotate (NULL);
157}
158
paul2d75d052004-01-19 21:31:15 +0000159struct quagga_signal_t ospf_signals[] =
paul718e3742002-12-13 20:15:29 +0000160{
paul2d75d052004-01-19 21:31:15 +0000161 {
162 .signal = SIGHUP,
163 .handler = &sighup,
164 },
165 {
166 .signal = SIGUSR1,
167 .handler = &sigusr1,
168 },
169 {
170 .signal = SIGINT,
171 .handler = &sigint,
172 },
hassof571dab2004-03-22 08:55:25 +0000173 {
174 .signal = SIGTERM,
175 .handler = &sigint,
176 },
paul2d75d052004-01-19 21:31:15 +0000177};
paul718e3742002-12-13 20:15:29 +0000178
179/* OSPFd main routine. */
180int
181main (int argc, char **argv)
182{
183 char *p;
184 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000185 int vty_port = OSPF_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000186 int daemon_mode = 0;
187 char *config_file = NULL;
188 char *progname;
189 struct thread thread;
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
197 /* Invoked by a priviledged user? -- endo. */
hassob3516a72003-05-25 22:11:22 +0000198 if (geteuid () != 0)
paul718e3742002-12-13 20:15:29 +0000199 {
200 errno = EPERM;
201 perror (progname);
202 exit (1);
203 }
204
205 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_OSPF,
206 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
207
paul020709f2003-04-04 02:44:16 +0000208 /* OSPF master init. */
209 ospf_master_init ();
210
hassod68614d2004-10-13 09:32:48 +0000211#ifdef SUPPORT_OSPF_API
hassof4d58ce2004-10-12 06:13:54 +0000212 /* OSPF apiserver is disabled by default. */
213 ospf_apiserver_enable = 0;
hassod68614d2004-10-13 09:32:48 +0000214#endif /* SUPPORT_OSPF_API */
hassof4d58ce2004-10-12 06:13:54 +0000215
paul718e3742002-12-13 20:15:29 +0000216 while (1)
217 {
218 int opt;
219
hassoc0652302004-11-25 19:33:48 +0000220 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:av", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000221
222 if (opt == EOF)
223 break;
224
225 switch (opt)
226 {
227 case 0:
228 break;
229 case 'd':
230 daemon_mode = 1;
231 break;
232 case 'f':
233 config_file = optarg;
234 break;
235 case 'A':
236 vty_addr = optarg;
237 break;
238 case 'i':
239 pid_file = optarg;
240 break;
241 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000242 /* Deal with atoi() returning 0 on failure, and ospfd not
243 listening on ospfd port... */
244 if (strcmp(optarg, "0") == 0)
245 {
246 vty_port = 0;
247 break;
248 }
249 vty_port = atoi (optarg);
250 vty_port = (vty_port ? vty_port : OSPF_VTY_PORT);
251 break;
hassoc3abdb72004-10-11 16:27:03 +0000252 case 'u':
hassoc0652302004-11-25 19:33:48 +0000253 ospfd_privs.user = optarg;
254 break;
255 case 'g':
256 ospfd_privs.group = optarg;
hassoc3abdb72004-10-11 16:27:03 +0000257 break;
hassod68614d2004-10-13 09:32:48 +0000258#ifdef SUPPORT_OSPF_API
hassoc3abdb72004-10-11 16:27:03 +0000259 case 'a':
260 ospf_apiserver_enable = 1;
261 break;
hassod68614d2004-10-13 09:32:48 +0000262#endif /* SUPPORT_OSPF_API */
paul718e3742002-12-13 20:15:29 +0000263 case 'v':
264 print_version (progname);
265 exit (0);
266 break;
267 case 'h':
268 usage (progname, 0);
269 break;
270 default:
271 usage (progname, 1);
272 break;
273 }
274 }
275
276 /* Initializations. */
paul020709f2003-04-04 02:44:16 +0000277 master = om->master;
paul718e3742002-12-13 20:15:29 +0000278
279 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000280 zprivs_init (&ospfd_privs);
paul2d75d052004-01-19 21:31:15 +0000281 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
paul718e3742002-12-13 20:15:29 +0000282 cmd_init (1);
283 debug_init ();
paulb21b19c2003-06-15 01:28:29 +0000284 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000285 memory_init ();
286
287 access_list_init ();
288 prefix_list_init ();
289
290 /* OSPFd inits. */
paul718e3742002-12-13 20:15:29 +0000291 ospf_if_init ();
292 ospf_zebra_init ();
293
294 /* OSPF vty inits. */
295 ospf_vty_init ();
296 ospf_vty_show_init ();
297
298 ospf_route_map_init ();
299#ifdef HAVE_SNMP
300 ospf_snmp_init ();
301#endif /* HAVE_SNMP */
302#ifdef HAVE_OPAQUE_LSA
303 ospf_opaque_init ();
304#endif /* HAVE_OPAQUE_LSA */
305
306 sort_node ();
307
308 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000309 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000310
311 /* Change to the daemon program. */
312 if (daemon_mode)
313 daemon (0, 0);
314
315 /* Process id file create. */
316 pid_output (pid_file);
317
318 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000319 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000320
hassob3516a72003-05-25 22:11:22 +0000321#ifdef DEBUG
paul718e3742002-12-13 20:15:29 +0000322 /* Print banner. */
paule8f29842003-08-12 13:08:31 +0000323 zlog (NULL, LOG_INFO, "OSPFd (%s) starts", QUAGGA_VERSION);
hassob3516a72003-05-25 22:11:22 +0000324#endif
paul718e3742002-12-13 20:15:29 +0000325
326 /* Fetch next active thread. */
327 while (thread_fetch (master, &thread))
328 thread_call (&thread);
329
330 /* Not reached. */
331 exit (0);
332}
333