]> git.proxmox.com Git - mirror_frr.git/blame - ospfd/ospf_main.c
[ospfd/zserv] adjust to new format
[mirror_frr.git] / ospfd / ospf_main.c
CommitLineData
718e3744 1/*
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
5e4fa164 25#include <lib/version.h>
718e3744 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"
edd7c245 39#include "privs.h"
2d75d052 40#include "sigevent.h"
718e3744 41
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
edd7c245 52/* ospfd privileges */
53zebra_capabilities_t _caps_p [] =
54{
ceacedba 55 ZCAP_NET_RAW,
edd7c245 56 ZCAP_BIND,
ceacedba 57 ZCAP_NET_ADMIN,
edd7c245 58};
59
60struct zebra_privs_t ospfd_privs =
61{
d81fadfd 62#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
63 .user = QUAGGA_USER,
64 .group = QUAGGA_GROUP,
edd7c245 65#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
718e3744 74/* Configuration filename and directory. */
718e3744 75char 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'},
83 { "log_mode", no_argument, NULL, 'l'},
84 { "help", no_argument, NULL, 'h'},
85 { "vty_addr", required_argument, NULL, 'A'},
86 { "vty_port", required_argument, NULL, 'P'},
edd7c245 87 { "user", required_argument, NULL, 'u'},
c065230a 88 { "group", required_argument, NULL, 'g'},
c3abdb72 89 { "apiserver", no_argument, NULL, 'a'},
718e3744 90 { "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 */
eb1ce605 100const char *pid_file = PATH_OSPFD_PID;
718e3744 101
d68614db 102#ifdef SUPPORT_OSPF_API
f4d58ce5 103extern int ospf_apiserver_enable;
d68614db 104#endif /* SUPPORT_OSPF_API */
c3abdb72 105
718e3744 106/* Help information display. */
4dadc291 107static void __attribute__ ((noreturn))
718e3744 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\
c065230a 121-u, --user User to run as\n\
122-g, --group Group to run as\n\
c3abdb72 123-a. --apiserver Enable OSPF apiserver\n\
718e3744 124-v, --version Print program version\n\
125-h, --help Display this help and exit\n\
126\n\
127Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
128 }
129 exit (status);
130}
131\f
132/* SIGHUP handler. */
4dadc291 133static void
2d75d052 134sighup (void)
718e3744 135{
136 zlog (NULL, LOG_INFO, "SIGHUP received");
137}
138
88d6cf37 139/* SIGINT / SIGTERM handler. */
140static void
2d75d052 141sigint (void)
718e3744 142{
887c44a4 143 zlog_notice ("Terminating on signal");
718e3744 144 ospf_terminate ();
718e3744 145}
146
147/* SIGUSR1 handler. */
4dadc291 148static void
2d75d052 149sigusr1 (void)
718e3744 150{
151 zlog_rotate (NULL);
152}
153
2d75d052 154struct quagga_signal_t ospf_signals[] =
718e3744 155{
2d75d052 156 {
157 .signal = SIGHUP,
158 .handler = &sighup,
159 },
160 {
161 .signal = SIGUSR1,
162 .handler = &sigusr1,
163 },
164 {
165 .signal = SIGINT,
166 .handler = &sigint,
167 },
f571dab0 168 {
169 .signal = SIGTERM,
170 .handler = &sigint,
171 },
2d75d052 172};
718e3744 173\f
174/* OSPFd main routine. */
175int
176main (int argc, char **argv)
177{
178 char *p;
179 char *vty_addr = NULL;
4fc4e7ab 180 int vty_port = OSPF_VTY_PORT;
718e3744 181 int daemon_mode = 0;
182 char *config_file = NULL;
183 char *progname;
184 struct thread thread;
185
186 /* Set umask before anything for security */
187 umask (0027);
188
189 /* get program name */
190 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
191
192 /* Invoked by a priviledged user? -- endo. */
b3516a79 193 if (geteuid () != 0)
718e3744 194 {
195 errno = EPERM;
196 perror (progname);
197 exit (1);
198 }
199
274a4a44 200 zlog_default = openzlog (progname, ZLOG_OSPF,
718e3744 201 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
202
020709f9 203 /* OSPF master init. */
204 ospf_master_init ();
205
d68614db 206#ifdef SUPPORT_OSPF_API
f4d58ce5 207 /* OSPF apiserver is disabled by default. */
208 ospf_apiserver_enable = 0;
d68614db 209#endif /* SUPPORT_OSPF_API */
f4d58ce5 210
718e3744 211 while (1)
212 {
213 int opt;
214
c065230a 215 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:av", longopts, 0);
718e3744 216
217 if (opt == EOF)
218 break;
219
220 switch (opt)
221 {
222 case 0:
223 break;
224 case 'd':
225 daemon_mode = 1;
226 break;
227 case 'f':
228 config_file = optarg;
229 break;
230 case 'A':
231 vty_addr = optarg;
232 break;
233 case 'i':
234 pid_file = optarg;
235 break;
236 case 'P':
4fc4e7ab 237 /* Deal with atoi() returning 0 on failure, and ospfd not
238 listening on ospfd port... */
239 if (strcmp(optarg, "0") == 0)
240 {
241 vty_port = 0;
242 break;
243 }
244 vty_port = atoi (optarg);
245 vty_port = (vty_port ? vty_port : OSPF_VTY_PORT);
246 break;
c3abdb72 247 case 'u':
c065230a 248 ospfd_privs.user = optarg;
249 break;
250 case 'g':
251 ospfd_privs.group = optarg;
c3abdb72 252 break;
d68614db 253#ifdef SUPPORT_OSPF_API
c3abdb72 254 case 'a':
255 ospf_apiserver_enable = 1;
256 break;
d68614db 257#endif /* SUPPORT_OSPF_API */
718e3744 258 case 'v':
259 print_version (progname);
260 exit (0);
261 break;
262 case 'h':
263 usage (progname, 0);
264 break;
265 default:
266 usage (progname, 1);
267 break;
268 }
269 }
270
271 /* Initializations. */
020709f9 272 master = om->master;
718e3744 273
274 /* Library inits. */
edd7c245 275 zprivs_init (&ospfd_privs);
2d75d052 276 signal_init (master, Q_SIGC(ospf_signals), ospf_signals);
718e3744 277 cmd_init (1);
278 debug_init ();
b21b19c5 279 vty_init (master);
718e3744 280 memory_init ();
281
282 access_list_init ();
283 prefix_list_init ();
284
285 /* OSPFd inits. */
718e3744 286 ospf_if_init ();
287 ospf_zebra_init ();
288
289 /* OSPF vty inits. */
290 ospf_vty_init ();
291 ospf_vty_show_init ();
292
293 ospf_route_map_init ();
294#ifdef HAVE_SNMP
295 ospf_snmp_init ();
296#endif /* HAVE_SNMP */
297#ifdef HAVE_OPAQUE_LSA
298 ospf_opaque_init ();
299#endif /* HAVE_OPAQUE_LSA */
300
301 sort_node ();
302
303 /* Get configuration file. */
320ec10a 304 vty_read_config (config_file, config_default);
718e3744 305
306 /* Change to the daemon program. */
307 if (daemon_mode)
308 daemon (0, 0);
309
310 /* Process id file create. */
311 pid_output (pid_file);
312
313 /* Create VTY socket */
4fc4e7ab 314 vty_serv_sock (vty_addr, vty_port, OSPF_VTYSH_PATH);
718e3744 315
316 /* Print banner. */
887c44a4 317 zlog_notice ("OSPFd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
718e3744 318
319 /* Fetch next active thread. */
320 while (thread_fetch (master, &thread))
321 thread_call (&thread);
322
323 /* Not reached. */
324 exit (0);
325}
326