blob: b5e640335066ae28242a881cc3fb53d7b9cfa0b6 [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{
49 ZCAP_RAW,
50 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'},
77 { "version", no_argument, NULL, 'v'},
78 { "help", no_argument, NULL, 'h'},
79 { 0 }
80};
81
82/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000083char 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
hassoe42f5a32004-08-28 17:04:33 +0000123sighup (void)
paul718e3742002-12-13 20:15:29 +0000124{
125 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000126}
127
128/* SIGINT handler. */
129void
hassoe42f5a32004-08-28 17:04:33 +0000130sigint (void)
paul718e3742002-12-13 20:15:29 +0000131{
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
hassoe42f5a32004-08-28 17:04:33 +0000138sigterm (void)
paul718e3742002-12-13 20:15:29 +0000139{
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
hassoe42f5a32004-08-28 17:04:33 +0000146sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000147{
148 zlog_info ("SIGUSR1 received");
149 zlog_rotate (NULL);
150}
151
hassoe42f5a32004-08-28 17:04:33 +0000152struct quagga_signal_t ospf6_signals[] =
paul718e3742002-12-13 20:15:29 +0000153{
hassoe42f5a32004-08-28 17:04:33 +0000154 {
155 .signal = SIGHUP,
156 .handler = &sighup,
157 },
158 {
159 .signal = SIGINT,
160 .handler = &sigint,
161 },
162 {
163 .signal = SIGTERM,
164 .handler = &sigterm,
165 },
166 {
167 .signal = SIGUSR1,
168 .handler = &sigusr1,
169 },
170};
hasso508e53e2004-05-18 18:57:06 +0000171
172/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000173 state machine is handled here. */
174int
175main (int argc, char *argv[], char *envp[])
176{
177 char *p;
178 int opt;
179 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000180 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000181 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000182 struct thread thread;
183 int flag;
184
185 /* Set umask before anything for security */
186 umask (0027);
187
188 /* Preserve name of myself. */
189 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
190
paul718e3742002-12-13 20:15:29 +0000191 /* Command line argument treatment. */
192 while (1)
193 {
paul96735ee2003-08-10 02:51:22 +0000194 opt = getopt_long (argc, argv, "df:i:hp:A:P:u:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000195
196 if (opt == EOF)
197 break;
198
199 switch (opt)
200 {
201 case 0:
202 break;
203 case 'd':
204 daemon_mode = 1;
205 break;
206 case 'f':
207 config_file = optarg;
208 break;
209 case 'A':
210 vty_addr = optarg;
211 break;
212 case 'i':
213 pid_file = optarg;
214 break;
215 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000216 /* Deal with atoi() returning 0 on failure, and ospf6d not
217 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000218 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000219 {
220 vty_port = 0;
221 break;
hasso508e53e2004-05-18 18:57:06 +0000222 }
paul718e3742002-12-13 20:15:29 +0000223 vty_port = atoi (optarg);
paul4fc4e7a2003-01-22 19:47:09 +0000224 vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
hasso508e53e2004-05-18 18:57:06 +0000225 break;
pauledd7c242003-06-04 13:59:38 +0000226 case 'u':
227 ospf6d_privs.user = ospf6d_privs.group = optarg;
228 break;
paul718e3742002-12-13 20:15:29 +0000229 case 'v':
230 print_version (progname);
231 exit (0);
232 break;
233 case 'h':
234 usage (progname, 0);
235 break;
236 default:
237 usage (progname, 1);
238 break;
239 }
240 }
241
242 /* thread master */
243 master = thread_master_create ();
244
245 /* Initializations. */
246 if (! daemon_mode)
247 flag = ZLOG_STDOUT;
248 else
hasso508e53e2004-05-18 18:57:06 +0000249 flag = 0;
paul718e3742002-12-13 20:15:29 +0000250
251 zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
paul79dc3732004-07-23 15:17:45 +0000252 LOG_CONS|LOG_NDELAY|LOG_PID,
hasso508e53e2004-05-18 18:57:06 +0000253 LOG_DAEMON);
254 zprivs_init (&ospf6d_privs);
255 /* initialize zebra libraries */
hassoe42f5a32004-08-28 17:04:33 +0000256 signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
paul718e3742002-12-13 20:15:29 +0000257 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000258 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000259 memory_init ();
hasso508e53e2004-05-18 18:57:06 +0000260 if_init ();
261 access_list_init ();
262 prefix_list_init ();
263
264 /* initialize ospf6 */
265 ospf6_init ();
266
267 /* sort command vector */
paul718e3742002-12-13 20:15:29 +0000268 sort_node ();
269
270 /* parse config file */
hasso320ec102004-06-20 19:54:37 +0000271 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000272
273 if (daemon_mode)
274 daemon (0, 0);
275
276 /* pid file create */
277#if 0
278 pid_output_lock (pid_file);
279#else
280 pid_output (pid_file);
281#endif
282
hasso508e53e2004-05-18 18:57:06 +0000283 /* Make ospf6 vty socket. */
284 vty_serv_sock (vty_addr,
285 vty_port ? vty_port : OSPF6_VTY_PORT, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000286
paul718e3742002-12-13 20:15:29 +0000287 /* Print start message */
hasso508e53e2004-05-18 18:57:06 +0000288 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts",
paule8f29842003-08-12 13:08:31 +0000289 QUAGGA_VERSION, OSPF6_DAEMON_VERSION);
paul718e3742002-12-13 20:15:29 +0000290
291 /* Start finite state machine, here we go! */
292 while (thread_fetch (master, &thread))
293 thread_call (&thread);
294
295 /* Log in case thread failed */
296 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000297
298 /* Not reached. */
299 exit (0);
300}
301
hasso508e53e2004-05-18 18:57:06 +0000302