Merge branch 'Teaman-ND' into Teaman-RT
[tomato.git] / release / src / router / zebra / ospfd / ospf_main.c
blobe649c17b3a477266d1b8c14d7ca6dc98d8270871
1 /*
2 * OSPFd main routine.
3 * Copyright (C) 1998, 99 Kunihiro Ishiguro, Toshiaki Takada
5 * This file is part of GNU Zebra.
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.
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.
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.
23 #include <zebra.h>
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"
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"
50 /* Configuration filename and directory. */
51 char config_current[] = OSPF_DEFAULT_CONFIG;
52 char config_default[] = SYSCONFDIR OSPF_DEFAULT_CONFIG;
54 /* OSPFd options. */
55 struct option longopts[] =
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 }
68 /* OSPFd program name */
70 /* Master of threads. */
71 struct thread_master *master;
73 /* Process ID saved for use by init system */
74 char *pid_file = PATH_OSPFD_PID;
76 /* Help information display. */
77 static void
78 usage (char *progname, int status)
80 if (status != 0)
81 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
82 else
84 printf ("Usage : %s [OPTION...]\n\
85 Daemon 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\
94 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
96 exit (status);
99 /* SIGHUP handler. */
100 void
101 sighup (int sig)
103 zlog (NULL, LOG_INFO, "SIGHUP received");
106 /* SIGINT handler. */
107 void
108 sigint (int sig)
110 zlog (NULL, LOG_INFO, "Terminating on signal");
112 ospf_terminate ();
114 exit (0);
117 /* SIGUSR1 handler. */
118 void
119 sigusr1 (int sig)
121 zlog_rotate (NULL);
124 /* Signal wrapper. */
125 RETSIGTYPE *
126 signal_set (int signo, void (*func)(int))
128 int ret;
129 struct sigaction sig;
130 struct sigaction osig;
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 */
139 ret = sigaction (signo, &sig, &osig);
141 if (ret < 0)
142 return (SIG_ERR);
143 else
144 return (osig.sa_handler);
147 /* Initialization of signal handles. */
148 void
149 signal_init ()
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);
167 /* OSPFd main routine. */
169 main (int argc, char **argv)
171 char *p;
172 char *vty_addr = NULL;
173 int vty_port = 0;
174 int daemon_mode = 0;
175 char *config_file = NULL;
176 char *progname;
177 struct thread thread;
179 /* Set umask before anything for security */
180 umask (0027);
182 /* get program name */
183 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
185 /* Invoked by a priviledged user? -- endo. */
186 if (getuid () != 0)
188 errno = EPERM;
189 perror (progname);
190 exit (1);
193 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_OSPF,
194 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
196 /* OSPF master init. */
197 ospf_master_init ();
199 while (1)
201 int opt;
203 opt = getopt_long (argc, argv, "dlf:hA:P:v", longopts, 0);
205 if (opt == EOF)
206 break;
208 switch (opt)
210 case 0:
211 break;
212 case 'd':
213 daemon_mode = 1;
214 break;
215 case 'f':
216 config_file = optarg;
217 break;
218 case 'A':
219 vty_addr = optarg;
220 break;
221 case 'i':
222 pid_file = optarg;
223 break;
224 case 'P':
225 vty_port = atoi (optarg);
226 break;
227 case 'v':
228 print_version (progname);
229 exit (0);
230 break;
231 case 'h':
232 usage (progname, 0);
233 break;
234 default:
235 usage (progname, 1);
236 break;
240 /* Initializations. */
241 master = om->master;
243 /* Library inits. */
244 signal_init ();
245 cmd_init (1);
246 debug_init ();
247 vty_init ();
248 memory_init ();
250 access_list_init ();
251 prefix_list_init ();
253 /* OSPFd inits. */
254 ospf_init ();
255 ospf_if_init ();
256 ospf_zebra_init ();
258 /* OSPF vty inits. */
259 ospf_vty_init ();
260 ospf_vty_show_init ();
262 ospf_route_map_init ();
263 #ifdef HAVE_SNMP
264 ospf_snmp_init ();
265 #endif /* HAVE_SNMP */
266 #ifdef HAVE_OPAQUE_LSA
267 ospf_opaque_init ();
268 #endif /* HAVE_OPAQUE_LSA */
270 sort_node ();
272 /* Get configuration file. */
273 vty_read_config (config_file, config_current, config_default);
275 /* Change to the daemon program. */
276 if (daemon_mode)
277 daemon (0, 0);
279 /* Process id file create. */
280 pid_output (pid_file);
282 /* Create VTY socket */
283 vty_serv_sock (vty_addr,
284 vty_port ? vty_port : OSPF_VTY_PORT, OSPF_VTYSH_PATH);
286 /* Print banner. */
287 zlog (NULL, LOG_INFO, "OSPFd (%s) starts", ZEBRA_VERSION);
289 /* Fetch next active thread. */
290 while (thread_fetch (master, &thread))
291 thread_call (&thread);
293 /* Not reached. */
294 exit (0);