blob: a7e56dcd43927b4e58b67410f14bd54ee7446de6 [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'},
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
hassoc3abdb72004-10-11 16:27:03 +0000103/* OSPF apiserver is disabled by default. */
104int ospf_apiserver_enable = 0;
105
paul718e3742002-12-13 20:15:29 +0000106/* Help information display. */
107static void
108usage (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\
pauledd7c242003-06-04 13:59:38 +0000121-u, --user User and group to run as\n\
hassoc3abdb72004-10-11 16:27:03 +0000122-a. --apiserver Enable OSPF apiserver\n\
paul718e3742002-12-13 20:15:29 +0000123-v, --version Print program version\n\
124-h, --help Display this help and exit\n\
125\n\
126Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
127 }
128 exit (status);
129}
130
131/* SIGHUP handler. */
132void
paul2d75d052004-01-19 21:31:15 +0000133sighup (void)
paul718e3742002-12-13 20:15:29 +0000134{
135 zlog (NULL, LOG_INFO, "SIGHUP received");
136}
137
138/* SIGINT handler. */
139void
paul2d75d052004-01-19 21:31:15 +0000140sigint (void)
paul718e3742002-12-13 20:15:29 +0000141{
142 zlog (NULL, LOG_INFO, "Terminating on signal");
143
144 ospf_terminate ();
145
146 exit (0);
147}
148
149/* SIGUSR1 handler. */
150void
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;
187
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
202 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_OSPF,
203 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
204
paul020709f2003-04-04 02:44:16 +0000205 /* OSPF master init. */
206 ospf_master_init ();
207
paul718e3742002-12-13 20:15:29 +0000208 while (1)
209 {
210 int opt;
211
hassoc3abdb72004-10-11 16:27:03 +0000212 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:av", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000213
214 if (opt == EOF)
215 break;
216
217 switch (opt)
218 {
219 case 0:
220 break;
221 case 'd':
222 daemon_mode = 1;
223 break;
224 case 'f':
225 config_file = optarg;
226 break;
227 case 'A':
228 vty_addr = optarg;
229 break;
230 case 'i':
231 pid_file = optarg;
232 break;
233 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000234 /* Deal with atoi() returning 0 on failure, and ospfd not
235 listening on ospfd port... */
236 if (strcmp(optarg, "0") == 0)
237 {
238 vty_port = 0;
239 break;
240 }
241 vty_port = atoi (optarg);
242 vty_port = (vty_port ? vty_port : OSPF_VTY_PORT);
243 break;
hassoc3abdb72004-10-11 16:27:03 +0000244 case 'u':
245 ospfd_privs.group = ospfd_privs.user = optarg;
246 break;
247 case 'a':
248 ospf_apiserver_enable = 1;
249 break;
paul718e3742002-12-13 20:15:29 +0000250 case 'v':
251 print_version (progname);
252 exit (0);
253 break;
254 case 'h':
255 usage (progname, 0);
256 break;
257 default:
258 usage (progname, 1);
259 break;
260 }
261 }
262
263 /* Initializations. */
paul020709f2003-04-04 02:44:16 +0000264 master = om->master;
paul718e3742002-12-13 20:15:29 +0000265
266 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000267 zprivs_init (&ospfd_privs);
paul2d75d052004-01-19 21:31:15 +0000268 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
paul718e3742002-12-13 20:15:29 +0000269 cmd_init (1);
270 debug_init ();
paulb21b19c2003-06-15 01:28:29 +0000271 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000272 memory_init ();
273
274 access_list_init ();
275 prefix_list_init ();
276
277 /* OSPFd inits. */
paul718e3742002-12-13 20:15:29 +0000278 ospf_if_init ();
279 ospf_zebra_init ();
280
281 /* OSPF vty inits. */
282 ospf_vty_init ();
283 ospf_vty_show_init ();
284
285 ospf_route_map_init ();
286#ifdef HAVE_SNMP
287 ospf_snmp_init ();
288#endif /* HAVE_SNMP */
289#ifdef HAVE_OPAQUE_LSA
290 ospf_opaque_init ();
291#endif /* HAVE_OPAQUE_LSA */
292
293 sort_node ();
294
295 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000296 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000297
298 /* Change to the daemon program. */
299 if (daemon_mode)
300 daemon (0, 0);
301
302 /* Process id file create. */
303 pid_output (pid_file);
304
305 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000306 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000307
hassob3516a72003-05-25 22:11:22 +0000308#ifdef DEBUG
paul718e3742002-12-13 20:15:29 +0000309 /* Print banner. */
paule8f29842003-08-12 13:08:31 +0000310 zlog (NULL, LOG_INFO, "OSPFd (%s) starts", QUAGGA_VERSION);
hassob3516a72003-05-25 22:11:22 +0000311#endif
paul718e3742002-12-13 20:15:29 +0000312
313 /* Fetch next active thread. */
314 while (thread_fetch (master, &thread))
315 thread_call (&thread);
316
317 /* Not reached. */
318 exit (0);
319}
320