blob: ba266d95120ac3f17b6beba8f263384cff2e78e4 [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'},
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'},
80 { "help", no_argument, NULL, 'h'},
81 { 0 }
82};
83
84/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000085char config_default[] = SYSCONFDIR OSPF6_DEFAULT_CONFIG;
86
87/* ospf6d program name. */
hasso508e53e2004-05-18 18:57:06 +000088char *progname;
paul718e3742002-12-13 20:15:29 +000089
90/* is daemon? */
91int daemon_mode = 0;
92
93/* Master of threads. */
94struct thread_master *master;
95
96/* Process ID saved for use by init system */
paul0c083ee2004-10-10 12:54:58 +000097const char *pid_file = PATH_OSPF6D_PID;
paul718e3742002-12-13 20:15:29 +000098
paul718e3742002-12-13 20:15:29 +000099/* Help information display. */
100static void
101usage (char *progname, int status)
102{
103 if (status != 0)
104 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
105 else
106 {
107 printf ("Usage : %s [OPTION...]\n\n\
108Daemon which manages OSPF version 3.\n\n\
109-d, --daemon Runs in daemon mode\n\
110-f, --config_file Set configuration file name\n\
111-i, --pid_file Set process identifier file name\n\
112-A, --vty_addr Set vty's bind address\n\
113-P, --vty_port Set vty's port number\n\
hassoc0652302004-11-25 19:33:48 +0000114-u, --user User to run as\n\
115-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000116-v, --version Print program version\n\
117-h, --help Display this help and exit\n\
118\n\
hasso508e53e2004-05-18 18:57:06 +0000119Report bugs to zebra@zebra.org\n", progname);
paul718e3742002-12-13 20:15:29 +0000120 }
121
122 exit (status);
123}
paul718e3742002-12-13 20:15:29 +0000124
125/* SIGHUP handler. */
126void
hassoe42f5a32004-08-28 17:04:33 +0000127sighup (void)
paul718e3742002-12-13 20:15:29 +0000128{
129 zlog_info ("SIGHUP received");
paul718e3742002-12-13 20:15:29 +0000130}
131
132/* SIGINT handler. */
133void
hassoe42f5a32004-08-28 17:04:33 +0000134sigint (void)
paul718e3742002-12-13 20:15:29 +0000135{
ajs887c44a2004-12-03 16:36:46 +0000136 zlog_notice ("Terminating on signal SIGINT");
hasso508e53e2004-05-18 18:57:06 +0000137 exit (0);
paul718e3742002-12-13 20:15:29 +0000138}
139
140/* SIGTERM handler. */
141void
hassoe42f5a32004-08-28 17:04:33 +0000142sigterm (void)
paul718e3742002-12-13 20:15:29 +0000143{
ajs887c44a2004-12-03 16:36:46 +0000144 zlog_notice ("Terminating on signal SIGTERM");
hasso508e53e2004-05-18 18:57:06 +0000145 exit (0);
paul718e3742002-12-13 20:15:29 +0000146}
147
148/* SIGUSR1 handler. */
149void
hassoe42f5a32004-08-28 17:04:33 +0000150sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000151{
152 zlog_info ("SIGUSR1 received");
153 zlog_rotate (NULL);
154}
155
hassoe42f5a32004-08-28 17:04:33 +0000156struct quagga_signal_t ospf6_signals[] =
paul718e3742002-12-13 20:15:29 +0000157{
hassoe42f5a32004-08-28 17:04:33 +0000158 {
159 .signal = SIGHUP,
160 .handler = &sighup,
161 },
162 {
163 .signal = SIGINT,
164 .handler = &sigint,
165 },
166 {
167 .signal = SIGTERM,
168 .handler = &sigterm,
169 },
170 {
171 .signal = SIGUSR1,
172 .handler = &sigusr1,
173 },
174};
hasso508e53e2004-05-18 18:57:06 +0000175
176/* Main routine of ospf6d. Treatment of argument and starting ospf finite
paul718e3742002-12-13 20:15:29 +0000177 state machine is handled here. */
178int
179main (int argc, char *argv[], char *envp[])
180{
181 char *p;
182 int opt;
183 char *vty_addr = NULL;
hasso508e53e2004-05-18 18:57:06 +0000184 int vty_port = 0;
paul718e3742002-12-13 20:15:29 +0000185 char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +0000186 struct thread thread;
187 int flag;
188
189 /* Set umask before anything for security */
190 umask (0027);
191
192 /* Preserve name of myself. */
193 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
194
paul718e3742002-12-13 20:15:29 +0000195 /* Command line argument treatment. */
196 while (1)
197 {
hassoc0652302004-11-25 19:33:48 +0000198 opt = getopt_long (argc, argv, "df:i:hp:A:P:u:g:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000199
200 if (opt == EOF)
201 break;
202
203 switch (opt)
204 {
205 case 0:
206 break;
207 case 'd':
208 daemon_mode = 1;
209 break;
210 case 'f':
211 config_file = optarg;
212 break;
213 case 'A':
214 vty_addr = optarg;
215 break;
216 case 'i':
217 pid_file = optarg;
218 break;
219 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000220 /* Deal with atoi() returning 0 on failure, and ospf6d not
221 listening on ospf6d port... */
hasso508e53e2004-05-18 18:57:06 +0000222 if (strcmp(optarg, "0") == 0)
paul4fc4e7a2003-01-22 19:47:09 +0000223 {
224 vty_port = 0;
225 break;
hasso508e53e2004-05-18 18:57:06 +0000226 }
paul718e3742002-12-13 20:15:29 +0000227 vty_port = atoi (optarg);
paul4fc4e7a2003-01-22 19:47:09 +0000228 vty_port = (vty_port ? vty_port : OSPF6_VTY_PORT);
hasso508e53e2004-05-18 18:57:06 +0000229 break;
pauledd7c242003-06-04 13:59:38 +0000230 case 'u':
hassoc0652302004-11-25 19:33:48 +0000231 ospf6d_privs.user = optarg;
pauledd7c242003-06-04 13:59:38 +0000232 break;
hassoc0652302004-11-25 19:33:48 +0000233 case 'g':
234 ospf6d_privs.group = optarg;
235 break;
paul718e3742002-12-13 20:15:29 +0000236 case 'v':
237 print_version (progname);
238 exit (0);
239 break;
240 case 'h':
241 usage (progname, 0);
242 break;
243 default:
244 usage (progname, 1);
245 break;
246 }
247 }
248
249 /* thread master */
250 master = thread_master_create ();
251
252 /* Initializations. */
253 if (! daemon_mode)
254 flag = ZLOG_STDOUT;
255 else
hasso508e53e2004-05-18 18:57:06 +0000256 flag = 0;
paul718e3742002-12-13 20:15:29 +0000257
258 zlog_default = openzlog (progname, flag, ZLOG_OSPF6,
paul79dc3732004-07-23 15:17:45 +0000259 LOG_CONS|LOG_NDELAY|LOG_PID,
hasso508e53e2004-05-18 18:57:06 +0000260 LOG_DAEMON);
261 zprivs_init (&ospf6d_privs);
262 /* initialize zebra libraries */
hassoe42f5a32004-08-28 17:04:33 +0000263 signal_init (master, Q_SIGC(ospf6_signals), ospf6_signals);
paul718e3742002-12-13 20:15:29 +0000264 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000265 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000266 memory_init ();
hasso508e53e2004-05-18 18:57:06 +0000267 if_init ();
268 access_list_init ();
269 prefix_list_init ();
270
271 /* initialize ospf6 */
272 ospf6_init ();
273
274 /* sort command vector */
paul718e3742002-12-13 20:15:29 +0000275 sort_node ();
276
277 /* parse config file */
hasso320ec102004-06-20 19:54:37 +0000278 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000279
280 if (daemon_mode)
281 daemon (0, 0);
282
283 /* pid file create */
paul718e3742002-12-13 20:15:29 +0000284 pid_output (pid_file);
paul718e3742002-12-13 20:15:29 +0000285
hasso508e53e2004-05-18 18:57:06 +0000286 /* Make ospf6 vty socket. */
ajs887c44a2004-12-03 16:36:46 +0000287 if (!vty_port)
288 vty_port = OSPF6_VTY_PORT;
289 vty_serv_sock (vty_addr, vty_port, OSPF6_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000290
paul718e3742002-12-13 20:15:29 +0000291 /* Print start message */
ajs887c44a2004-12-03 16:36:46 +0000292 zlog_notice ("OSPF6d (Quagga-%s ospf6d-%s) starts: vty@%d",
293 QUAGGA_VERSION, OSPF6_DAEMON_VERSION,vty_port);
paul718e3742002-12-13 20:15:29 +0000294
295 /* Start finite state machine, here we go! */
296 while (thread_fetch (master, &thread))
297 thread_call (&thread);
298
299 /* Log in case thread failed */
300 zlog_warn ("Thread failed");
paul718e3742002-12-13 20:15:29 +0000301
302 /* Not reached. */
303 exit (0);
304}
305
hasso508e53e2004-05-18 18:57:06 +0000306