blob: c22e90bbcdb6e4e3e15a810942516d68d5cb03f6 [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * Copyright (C) 1999 Yasuhiro Ohara
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
19 * Boston, MA 02111-1307, USA.
20 */
21
22#include <zebra.h>
hasso3b4cd3a2004-05-18 19:28:32 +000023#include <lib/version.h>
hasso508e53e2004-05-18 18:57:06 +000024
paul718e3742002-12-13 20:15:29 +000025#include "getopt.h"
26#include "thread.h"
27#include "log.h"
paul718e3742002-12-13 20:15:29 +000028#include "command.h"
29#include "vty.h"
30#include "memory.h"
hasso508e53e2004-05-18 18:57:06 +000031#include "if.h"
32#include "filter.h"
33#include "prefix.h"
34#include "plist.h"
pauledd7c242003-06-04 13:59:38 +000035#include "privs.h"
paul718e3742002-12-13 20:15:29 +000036
37#include "ospf6d.h"
paul718e3742002-12-13 20:15:29 +000038
39/* Default configuration file name for ospf6d. */
40#define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
hasso508e53e2004-05-18 18:57:06 +000041
paul718e3742002-12-13 20:15:29 +000042/* Default port values. */
43#define OSPF6_VTY_PORT 2606
44
pauledd7c242003-06-04 13:59:38 +000045/* ospf6d privileges */
hasso508e53e2004-05-18 18:57:06 +000046zebra_capabilities_t _caps_p [] =
pauledd7c242003-06-04 13:59:38 +000047{
48 ZCAP_RAW,
49 ZCAP_BIND
50};
51
52struct zebra_privs_t ospf6d_privs =
53{
pauld81fadf2003-08-14 05:32:12 +000054#if defined(QUAGGA_USER)
55 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000056#endif
pauld81fadf2003-08-14 05:32:12 +000057#if defined QUAGGA_GROUP
58 .group = QUAGGA_GROUP,
59#endif
60#ifdef VTY_GROUP
61 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000062#endif
63 .caps_p = _caps_p,
64 .cap_num_p = 2,
65 .cap_num_i = 0
66};
67
paul718e3742002-12-13 20:15:29 +000068/* ospf6d options, we use GNU getopt library. */
69struct option longopts[] =
70{
71 { "daemon", no_argument, NULL, 'd'},
72 { "config_file", required_argument, NULL, 'f'},
73 { "pid_file", required_argument, NULL, 'i'},
74 { "vty_addr", required_argument, NULL, 'A'},
75 { "vty_port", required_argument, NULL, 'P'},
76 { "version", no_argument, NULL, 'v'},
77 { "help", no_argument, NULL, 'h'},
78 { 0 }
79};
80
81/* Configuration file and directory. */
82char config_current[] = OSPF6_DEFAULT_CONFIG;
83char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
84
85/* ospf6d program name. */
hasso508e53e2004-05-18 18:57:06 +000086char *progname;
paul718e3742002-12-13 20:15:29 +000087
88/* is daemon? */
89int daemon_mode = 0;
90
91/* Master of threads. */
92struct thread_master *master;
93
94/* Process ID saved for use by init system */
95char *pid_file = PATH_OSPF6D_PID;
96
paul718e3742002-12-13 20:15:29 +000097/* Help information display. */
98static void
99usage (char *progname, int status)
100{
101 if (status != 0)
102 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
103 else
104 {
105 printf ("Usage : %s [OPTION...]\n\n\
106Daemon which manages OSPF version 3.\n\n\
107-d, --daemon Runs in daemon mode\n\
108-f, --config_file Set configuration file name\n\
109-i, --pid_file Set process identifier file name\n\
110-A, --vty_addr Set vty's bind address\n\
111-P, --vty_port Set vty's port number\n\
112-v, --version Print program version\n\
113-h, --help Display this help and exit\n\
114\n\
hasso508e53e2004-05-18 18:57:06 +0000115Report bugs to zebra@zebra.org\n", progname);
paul718e3742002-12-13 20:15:29 +0000116 }
117
118 exit (status);
119}
paul718e3742002-12-13 20:15:29 +0000120
121/* SIGHUP handler. */
122void
123sighup (int sig)
124{
125 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000126}
127
128/* SIGINT handler. */
129void
130sigint (int sig)
131{
132 zlog_info ("SIGINT received");
hasso508e53e2004-05-18 18:57:06 +0000133 exit (0);
paul718e3742002-12-13 20:15:29 +0000134}
135
136/* SIGTERM handler. */
137void
138sigterm (int sig)
139{
140 zlog_info ("SIGTERM received");
hasso508e53e2004-05-18 18:57:06 +0000141 exit (0);
paul718e3742002-12-13 20:15:29 +0000142}
143
144/* SIGUSR1 handler. */
145void
146sigusr1 (int sig)
147{
148 zlog_info ("SIGUSR1 received");
149 zlog_rotate (NULL);
150}
151
152/* Signale wrapper. */
153RETSIGTYPE *
154signal_set (int signo, void (*func)(int))
155{
156 int ret;
157 struct sigaction sig;
158 struct sigaction osig;
159
160 sig.sa_handler = func;
161 sigemptyset (&sig.sa_mask);
162 sig.sa_flags = 0;
163#ifdef SA_RESTART
164 sig.sa_flags |= SA_RESTART;
165#endif /* SA_RESTART */
166
167 ret = sigaction (signo, &sig, &osig);
168
169 if (ret < 0)
170 return (SIG_ERR);
171 else
172 return (osig.sa_handler);
173}
174
175/* Initialization of signal handles. */
176void
177signal_init ()
178{
179 signal_set (SIGHUP, sighup);
180 signal_set (SIGINT, sigint);
181 signal_set (SIGTERM, sigterm);
182 signal_set (SIGPIPE, SIG_IGN);
183#ifdef SIGTSTP
184 signal_set (SIGTSTP, SIG_IGN);
185#endif
186#ifdef SIGTTIN
187 signal_set (SIGTTIN, SIG_IGN);
188#endif
189#ifdef SIGTTOU
190 signal_set (SIGTTOU, SIG_IGN);
191#endif
192 signal_set (SIGUSR1, sigusr1);
193}
hasso508e53e2004-05-18 18:57:06 +0000194
195/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000196 state machine is handled here. */
197int
198main (int argc, char *argv[], char *envp[])
199{
200 char *p;
201 int opt;
202 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000203 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000204 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000205 struct thread thread;
206 int flag;
207
208 /* Set umask before anything for security */
209 umask (0027);
210
211 /* Preserve name of myself. */
212 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
213
paul718e3742002-12-13 20:15:29 +0000214 /* Command line argument treatment. */
215 while (1)
216 {
paul96735ee2003-08-10 02:51:22 +0000217 opt = getopt_long (argc, argv, "df:i:hp:A:P:u:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000218
219 if (opt == EOF)
220 break;
221
222 switch (opt)
223 {
224 case 0:
225 break;
226 case 'd':
227 daemon_mode = 1;
228 break;
229 case 'f':
230 config_file = optarg;
231 break;
232 case 'A':
233 vty_addr = optarg;
234 break;
235 case 'i':
236 pid_file = optarg;
237 break;
238 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000239 /* Deal with atoi() returning 0 on failure, and ospf6d not
240 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000241 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000242 {
243 vty_port = 0;
244 break;
hasso508e53e2004-05-18 18:57:06 +0000245 }
paul718e3742002-12-13 20:15:29 +0000246 vty_port = atoi (optarg);
paul4fc4e7a2003-01-22 19:47:09 +0000247 vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
hasso508e53e2004-05-18 18:57:06 +0000248 break;
pauledd7c242003-06-04 13:59:38 +0000249 case 'u':
250 ospf6d_privs.user = ospf6d_privs.group = optarg;
251 break;
paul718e3742002-12-13 20:15:29 +0000252 case 'v':
253 print_version (progname);
254 exit (0);
255 break;
256 case 'h':
257 usage (progname, 0);
258 break;
259 default:
260 usage (progname, 1);
261 break;
262 }
263 }
264
265 /* thread master */
266 master = thread_master_create ();
267
268 /* Initializations. */
269 if (! daemon_mode)
270 flag = ZLOG_STDOUT;
271 else
hasso508e53e2004-05-18 18:57:06 +0000272 flag = 0;
paul718e3742002-12-13 20:15:29 +0000273
274 zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
hasso508e53e2004-05-18 18:57:06 +0000275 LOG_CONS|LOG_NDELAY|LOG_PERROR|LOG_PID,
276 LOG_DAEMON);
277 zprivs_init (&ospf6d_privs);
278 /* initialize zebra libraries */
paul718e3742002-12-13 20:15:29 +0000279 signal_init ();
280 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000281 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000282 memory_init ();
hasso508e53e2004-05-18 18:57:06 +0000283 if_init ();
284 access_list_init ();
285 prefix_list_init ();
286
287 /* initialize ospf6 */
288 ospf6_init ();
289
290 /* sort command vector */
paul718e3742002-12-13 20:15:29 +0000291 sort_node ();
292
293 /* parse config file */
294 vty_read_config (config_file, config_current, config_default);
295
296 if (daemon_mode)
297 daemon (0, 0);
298
299 /* pid file create */
300#if 0
301 pid_output_lock (pid_file);
302#else
303 pid_output (pid_file);
304#endif
305
hasso508e53e2004-05-18 18:57:06 +0000306 /* Make ospf6 vty socket. */
307 vty_serv_sock (vty_addr,
308 vty_port ? vty_port : OSPF6_VTY_PORT, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000309
paul718e3742002-12-13 20:15:29 +0000310 /* Print start message */
hasso508e53e2004-05-18 18:57:06 +0000311 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts",
paule8f29842003-08-12 13:08:31 +0000312 QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
paul718e3742002-12-13 20:15:29 +0000313
314 /* Start finite state machine, here we go! */
315 while (thread_fetch (master, &thread))
316 thread_call (&thread);
317
318 /* Log in case thread failed */
319 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000320
321 /* Not reached. */
322 exit (0);
323}
324
hasso508e53e2004-05-18 18:57:06 +0000325