blob: a7a96a1f0e08ba50b860da31996a49aa52a24552 [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"
hassoe42f5a32004-08-28 17:04:33 +000036#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000037
38#include "ospf6d.h"
paul718e3742002-12-13 20:15:29 +000039
40/* Default configuration file name for ospf6d. */
41#define OSPF6_DEFAULT_CONFIG "ospf6d.conf"
hasso508e53e2004-05-18 18:57:06 +000042
paul718e3742002-12-13 20:15:29 +000043/* Default port values. */
44#define OSPF6_VTY_PORT 2606
45
pauledd7c242003-06-04 13:59:38 +000046/* ospf6d privileges */
hasso508e53e2004-05-18 18:57:06 +000047zebra_capabilities_t _caps_p [] =
pauledd7c242003-06-04 13:59:38 +000048{
paulceacedb2005-09-29 14:39:32 +000049 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000050 ZCAP_BIND
51};
52
53struct zebra_privs_t ospf6d_privs =
54{
pauld81fadf2003-08-14 05:32:12 +000055#if defined(QUAGGA_USER)
56 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000057#endif
pauld81fadf2003-08-14 05:32:12 +000058#if defined QUAGGA_GROUP
59 .group = QUAGGA_GROUP,
60#endif
61#ifdef VTY_GROUP
62 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000063#endif
64 .caps_p = _caps_p,
65 .cap_num_p = 2,
66 .cap_num_i = 0
67};
68
paul718e3742002-12-13 20:15:29 +000069/* ospf6d options, we use GNU getopt library. */
70struct option longopts[] =
71{
72 { "daemon", no_argument, NULL, 'd'},
73 { "config_file", required_argument, NULL, 'f'},
74 { "pid_file", required_argument, NULL, 'i'},
75 { "vty_addr", required_argument, NULL, 'A'},
76 { "vty_port", required_argument, NULL, 'P'},
hassoc0652302004-11-25 19:33:48 +000077 { "user", required_argument, NULL, 'u'},
78 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000079 { "version", no_argument, NULL, 'v'},
Paul Jakma876b8be2006-10-15 23:35:57 +000080 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000081 { "help", no_argument, NULL, 'h'},
82 { 0 }
83};
84
85/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000086char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
87
88/* ospf6d program name. */
hasso508e53e2004-05-18 18:57:06 +000089char *progname;
paul718e3742002-12-13 20:15:29 +000090
91/* is daemon? */
92int daemon_mode = 0;
93
94/* Master of threads. */
95struct thread_master *master;
96
97/* Process ID saved for use by init system */
paul0c083ee2004-10-10 12:54:58 +000098const char *pid_file = PATH_OSPF6D_PID;
paul718e3742002-12-13 20:15:29 +000099
paul718e3742002-12-13 20:15:29 +0000100/* Help information display. */
101static void
102usage (char *progname, int status)
103{
104 if (status != 0)
105 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
106 else
107 {
108 printf ("Usage : %s [OPTION...]\n\n\
109Daemon which manages OSPF version 3.\n\n\
110-d, --daemon Runs in daemon mode\n\
111-f, --config_file Set configuration file name\n\
112-i, --pid_file Set process identifier file name\n\
113-A, --vty_addr Set vty's bind address\n\
114-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000115-u, --user User to run as\n\
116-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000117-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000118-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000119-h, --help Display this help and exit\n\
120\n\
hasso508e53e2004-05-18 18:57:06 +0000121Report bugs to zebra@zebra.org\n", progname);
paul718e3742002-12-13 20:15:29 +0000122 }
123
124 exit (status);
125}
paul718e3742002-12-13 20:15:29 +0000126
127/* SIGHUP handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100128static void
hassoe42f5a32004-08-28 17:04:33 +0000129sighup (void)
paul718e3742002-12-13 20:15:29 +0000130{
131 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000132}
133
134/* SIGINT handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100135static void
hassoe42f5a32004-08-28 17:04:33 +0000136sigint (void)
paul718e3742002-12-13 20:15:29 +0000137{
ajs887c44a2004-12-03 16:36:46 +0000138 zlog_notice ("Terminating on signal SIGINT");
hasso508e53e2004-05-18 18:57:06 +0000139 exit (0);
paul718e3742002-12-13 20:15:29 +0000140}
141
142/* SIGTERM handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100143static void
hassoe42f5a32004-08-28 17:04:33 +0000144sigterm (void)
paul718e3742002-12-13 20:15:29 +0000145{
ajs887c44a2004-12-03 16:36:46 +0000146 zlog_notice ("Terminating on signal SIGTERM");
hasso508e53e2004-05-18 18:57:06 +0000147 exit (0);
paul718e3742002-12-13 20:15:29 +0000148}
149
150/* SIGUSR1 handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100151static void
hassoe42f5a32004-08-28 17:04:33 +0000152sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000153{
154 zlog_info ("SIGUSR1 received");
155 zlog_rotate (NULL);
156}
157
hassoe42f5a32004-08-28 17:04:33 +0000158struct quagga_signal_t ospf6_signals[] =
paul718e3742002-12-13 20:15:29 +0000159{
hassoe42f5a32004-08-28 17:04:33 +0000160 {
161 .signal = SIGHUP,
162 .handler = &sighup,
163 },
164 {
165 .signal = SIGINT,
166 .handler = &sigint,
167 },
168 {
169 .signal = SIGTERM,
170 .handler = &sigterm,
171 },
172 {
173 .signal = SIGUSR1,
174 .handler = &sigusr1,
175 },
176};
hasso508e53e2004-05-18 18:57:06 +0000177
178/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000179 state machine is handled here. */
180int
181main (int argc, char *argv[], char *envp[])
182{
183 char *p;
184 int opt;
185 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000186 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000187 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000188 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000189 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000190
191 /* Set umask before anything for security */
192 umask (0027);
193
194 /* Preserve name of myself. */
195 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
196
paul718e3742002-12-13 20:15:29 +0000197 /* Command line argument treatment. */
198 while (1)
199 {
Paul Jakma876b8be2006-10-15 23:35:57 +0000200 opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000201
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 ospf6d not
223 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000224 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000225 {
226 vty_port = 0;
227 break;
hasso508e53e2004-05-18 18:57:06 +0000228 }
paul718e3742002-12-13 20:15:29 +0000229 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000230 if (vty_port <= 0 || vty_port > 0xffff)
231 vty_port = OSPF6_VTY_PORT;
hasso508e53e2004-05-18 18:57:06 +0000232 break;
pauledd7c242003-06-04 13:59:38 +0000233 case 'u':
hassoc0652302004-11-25 19:33:48 +0000234 ospf6d_privs.user = optarg;
pauledd7c242003-06-04 13:59:38 +0000235 break;
hassoc0652302004-11-25 19:33:48 +0000236 case 'g':
237 ospf6d_privs.group = optarg;
238 break;
paul718e3742002-12-13 20:15:29 +0000239 case 'v':
240 print_version (progname);
241 exit (0);
242 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000243 case 'C':
244 dryrun = 1;
245 break;
paul718e3742002-12-13 20:15:29 +0000246 case 'h':
247 usage (progname, 0);
248 break;
249 default:
250 usage (progname, 1);
251 break;
252 }
253 }
254
255 /* thread master */
256 master = thread_master_create ();
257
258 /* Initializations. */
ajs274a4a42004-12-07 15:39:31 +0000259 zlog_default = openzlog (progname, ZLOG_OSPF6,
paul79dc3732004-07-23 15:17:45 +0000260 LOG_CONS|LOG_NDELAY|LOG_PID,
hasso508e53e2004-05-18 18:57:06 +0000261 LOG_DAEMON);
262 zprivs_init (&ospf6d_privs);
263 /* initialize zebra libraries */
hassoe42f5a32004-08-28 17:04:33 +0000264 signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
paul718e3742002-12-13 20:15:29 +0000265 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000266 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000267 memory_init ();
hasso508e53e2004-05-18 18:57:06 +0000268 if_init ();
269 access_list_init ();
270 prefix_list_init ();
271
272 /* initialize ospf6 */
273 ospf6_init ();
274
275 /* sort command vector */
paul718e3742002-12-13 20:15:29 +0000276 sort_node ();
277
278 /* parse config file */
hasso320ec102004-06-20 19:54:37 +0000279 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000280
Paul Jakma876b8be2006-10-15 23:35:57 +0000281 /* Start execution only if not in dry-run mode */
282 if (dryrun)
283 return(0);
284
Stephen Hemminger065de902009-08-07 11:13:49 -0700285 if (daemon_mode && daemon (0, 0) < 0)
286 {
287 zlog_err("OSPF6d daemon failed: %s", strerror(errno));
288 exit (1);
289 }
paul718e3742002-12-13 20:15:29 +0000290
291 /* pid file create */
paul718e3742002-12-13 20:15:29 +0000292 pid_output (pid_file);
paul718e3742002-12-13 20:15:29 +0000293
hasso508e53e2004-05-18 18:57:06 +0000294 /* Make ospf6 vty socket. */
ajs887c44a2004-12-03 16:36:46 +0000295 if (!vty_port)
296 vty_port = OSPF6_VTY_PORT;
297 vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000298
paul718e3742002-12-13 20:15:29 +0000299 /* Print start message */
ajs887c44a2004-12-03 16:36:46 +0000300 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
301 QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
paul718e3742002-12-13 20:15:29 +0000302
303 /* Start finite state machine, here we go! */
304 while (thread_fetch (master, &thread))
305 thread_call (&thread);
306
307 /* Log in case thread failed */
308 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000309
310 /* Not reached. */
311 exit (0);
312}
313
hasso508e53e2004-05-18 18:57:06 +0000314