blob: 6f6262aa6f4a97ef34776754c7b516f3939bee5b [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
25#include "version.h"
26#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"
39
40#include "ospfd/ospfd.h"
41#include "ospfd/ospf_interface.h"
42#include "ospfd/ospf_asbr.h"
43#include "ospfd/ospf_lsa.h"
44#include "ospfd/ospf_lsdb.h"
45#include "ospfd/ospf_neighbor.h"
46#include "ospfd/ospf_dump.h"
47#include "ospfd/ospf_zebra.h"
48#include "ospfd/ospf_vty.h"
49
50/* Configuration filename and directory. */
51char config_current[] = OSPF_DEFAULT_CONFIG;
52char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
53
54/* OSPFd options. */
55struct option longopts[] =
56{
57 { "daemon", no_argument, NULL, 'd'},
58 { "config_file", required_argument, NULL, 'f'},
59 { "pid_file", required_argument, NULL, 'i'},
60 { "log_mode", no_argument, NULL, 'l'},
61 { "help", no_argument, NULL, 'h'},
62 { "vty_addr", required_argument, NULL, 'A'},
63 { "vty_port", required_argument, NULL, 'P'},
64 { "version", no_argument, NULL, 'v'},
65 { 0 }
66};
67
68/* OSPFd program name */
69
70/* Master of threads. */
71struct thread_master *master;
72
73/* Process ID saved for use by init system */
74char *pid_file = PATH_OSPFD_PID;
75
76/* Help information display. */
77static void
78usage (char *progname, int status)
79{
80 if (status != 0)
81 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
82 else
83 {
84 printf ("Usage : %s [OPTION...]\n\
85Daemon which manages OSPF.\n\n\
86-d, --daemon Runs in daemon mode\n\
87-f, --config_file Set configuration file name\n\
88-i, --pid_file Set process identifier file name\n\
89-A, --vty_addr Set vty's bind address\n\
90-P, --vty_port Set vty's port number\n\
91-v, --version Print program version\n\
92-h, --help Display this help and exit\n\
93\n\
94Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
95 }
96 exit (status);
97}
98
99/* SIGHUP handler. */
100void
101sighup (int sig)
102{
103 zlog (NULL, LOG_INFO, "SIGHUP received");
104}
105
106/* SIGINT handler. */
107void
108sigint (int sig)
109{
110 zlog (NULL, LOG_INFO, "Terminating on signal");
111
112 ospf_terminate ();
113
114 exit (0);
115}
116
117/* SIGUSR1 handler. */
118void
119sigusr1 (int sig)
120{
121 zlog_rotate (NULL);
122}
123
124/* Signal wrapper. */
125RETSIGTYPE *
126signal_set (int signo, void (*func)(int))
127{
128 int ret;
129 struct sigaction sig;
130 struct sigaction osig;
131
132 sig.sa_handler = func;
133 sigemptyset (&sig.sa_mask);
134 sig.sa_flags = 0;
135#ifdef SA_RESTART
136 sig.sa_flags |= SA_RESTART;
137#endif /* SA_RESTART */
138
139 ret = sigaction (signo, &sig, &osig);
140
141 if (ret < 0)
142 return (SIG_ERR);
143 else
144 return (osig.sa_handler);
145}
146
147/* Initialization of signal handles. */
148void
149signal_init ()
150{
151 signal_set (SIGHUP, sighup);
152 signal_set (SIGINT, sigint);
153 signal_set (SIGTERM, sigint);
154 signal_set (SIGPIPE, SIG_IGN);
155#ifdef SIGTSTP
156 signal_set (SIGTSTP, SIG_IGN);
157#endif
158#ifdef SIGTTIN
159 signal_set (SIGTTIN, SIG_IGN);
160#endif
161#ifdef SIGTTOU
162 signal_set (SIGTTOU, SIG_IGN);
163#endif
164 signal_set (SIGUSR1, sigusr1);
165}
166
167/* OSPFd main routine. */
168int
169main (int argc, char **argv)
170{
171 char *p;
172 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000173 int vty_port = OSPF_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000174 int daemon_mode = 0;
175 char *config_file = NULL;
176 char *progname;
177 struct thread thread;
178
179 /* Set umask before anything for security */
180 umask (0027);
181
182 /* get program name */
183 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
184
185 /* Invoked by a priviledged user? -- endo. */
186 if (getuid () != 0)
187 {
188 errno = EPERM;
189 perror (progname);
190 exit (1);
191 }
192
193 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_OSPF,
194 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
195
196 while (1)
197 {
198 int opt;
199
200 opt = getopt_long (argc, argv, "dlf:hA:P:v", longopts, 0);
201
202 if (opt == EOF)
203 break;
204
205 switch (opt)
206 {
207 case 0:
208 break;
209 case 'd':
210 daemon_mode = 1;
211 break;
212 case 'f':
213 config_file = optarg;
214 break;
215 case 'A':
216 vty_addr = optarg;
217 break;
218 case 'i':
219 pid_file = optarg;
220 break;
221 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000222 /* Deal with atoi() returning 0 on failure, and ospfd not
223 listening on ospfd port... */
224 if (strcmp(optarg, "0") == 0)
225 {
226 vty_port = 0;
227 break;
228 }
229 vty_port = atoi (optarg);
230 vty_port = (vty_port ? vty_port : OSPF_VTY_PORT);
231 break;
paul718e3742002-12-13 20:15:29 +0000232 case 'v':
233 print_version (progname);
234 exit (0);
235 break;
236 case 'h':
237 usage (progname, 0);
238 break;
239 default:
240 usage (progname, 1);
241 break;
242 }
243 }
244
245 /* Initializations. */
246 master = thread_master_create ();
247
248 /* Library inits. */
249 signal_init ();
250 cmd_init (1);
251 debug_init ();
252 vty_init ();
253 memory_init ();
254
255 access_list_init ();
256 prefix_list_init ();
257
258 /* OSPFd inits. */
259 ospf_init ();
260 ospf_if_init ();
261 ospf_zebra_init ();
262
263 /* OSPF vty inits. */
264 ospf_vty_init ();
265 ospf_vty_show_init ();
266
267 ospf_route_map_init ();
268#ifdef HAVE_SNMP
269 ospf_snmp_init ();
270#endif /* HAVE_SNMP */
271#ifdef HAVE_OPAQUE_LSA
272 ospf_opaque_init ();
273#endif /* HAVE_OPAQUE_LSA */
274
275 sort_node ();
276
277 /* Get configuration file. */
278 vty_read_config (config_file, config_current, config_default);
279
280 /* Change to the daemon program. */
281 if (daemon_mode)
282 daemon (0, 0);
283
284 /* Process id file create. */
285 pid_output (pid_file);
286
287 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000288 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000289
290 /* Print banner. */
291 zlog (NULL, LOG_INFO, "OSPFd (%s) starts", ZEBRA_VERSION);
292
293 /* Fetch next active thread. */
294 while (thread_fetch (master, &thread))
295 thread_call (&thread);
296
297 /* Not reached. */
298 exit (0);
299}
300