blob: 85209a15842432085cf79296d545f8458dac991a [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * RIPngd main routine.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
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
gdt5e4fa162004-03-16 14:38:36 +000025#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000026#include "getopt.h"
27#include "vector.h"
28#include "vty.h"
29#include "command.h"
hassoa94434b2003-05-25 17:10:12 +000030#include "memory.h"
paul718e3742002-12-13 20:15:29 +000031#include "thread.h"
32#include "log.h"
33#include "prefix.h"
34#include "if.h"
pauledd7c242003-06-04 13:59:38 +000035#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000036#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000037
38#include "ripngd/ripngd.h"
39
40/* Configuration filename and directory. */
paul718e3742002-12-13 20:15:29 +000041char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
hassoa94434b2003-05-25 17:10:12 +000042char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +000043
44/* RIPngd options. */
45struct option longopts[] =
46{
47 { "daemon", no_argument, NULL, 'd'},
48 { "config_file", required_argument, NULL, 'f'},
49 { "pid_file", required_argument, NULL, 'i'},
Paul Jakma876b8be2006-10-15 23:35:57 +000050 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000051 { "help", no_argument, NULL, 'h'},
52 { "vty_addr", required_argument, NULL, 'A'},
53 { "vty_port", required_argument, NULL, 'P'},
54 { "retain", no_argument, NULL, 'r'},
pauledd7c242003-06-04 13:59:38 +000055 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000056 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000057 { "version", no_argument, NULL, 'v'},
58 { 0 }
59};
60
pauledd7c242003-06-04 13:59:38 +000061/* ripngd privileges */
62zebra_capabilities_t _caps_p [] =
63{
paulceacedb2005-09-29 14:39:32 +000064 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000065 ZCAP_BIND
66};
67
68struct zebra_privs_t ripngd_privs =
69{
pauld81fadf2003-08-14 05:32:12 +000070#if defined(QUAGGA_USER)
71 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000072#endif
pauld81fadf2003-08-14 05:32:12 +000073#if defined QUAGGA_GROUP
74 .group = QUAGGA_GROUP,
75#endif
76#ifdef VTY_GROUP
77 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000078#endif
79 .caps_p = _caps_p,
80 .cap_num_p = 2,
81 .cap_num_i = 0
82};
83
84
paul718e3742002-12-13 20:15:29 +000085/* RIPngd program name */
86
87/* Route retain mode flag. */
88int retain_mode = 0;
89
hassoa94434b2003-05-25 17:10:12 +000090/* RIPng VTY bind address. */
91char *vty_addr = NULL;
92
93/* RIPng VTY connection port. */
94int vty_port = RIPNG_VTY_PORT;
95
paul718e3742002-12-13 20:15:29 +000096/* Master of threads. */
97struct thread_master *master;
98
99/* Process ID saved for use by init system */
hasso7a1d5832004-10-08 06:32:23 +0000100const char *pid_file = PATH_RIPNGD_PID;
paul718e3742002-12-13 20:15:29 +0000101
102/* Help information display. */
103static void
104usage (char *progname, int status)
105{
106 if (status != 0)
107 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
108 else
109 {
110 printf ("Usage : %s [OPTION...]\n\
111Daemon which manages RIPng.\n\n\
112-d, --daemon Runs in daemon mode\n\
113-f, --config_file Set configuration file name\n\
114-i, --pid_file Set process identifier file name\n\
paul718e3742002-12-13 20:15:29 +0000115-A, --vty_addr Set vty's bind address\n\
116-P, --vty_port Set vty's port number\n\
117-r, --retain When program terminates, retain added route by ripngd.\n\
hassoc0652302004-11-25 19:33:48 +0000118-u, --user User to run as\n\
119-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000120-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000121-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000122-h, --help Display this help and exit\n\
123\n\
124Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
125 }
126 exit (status);
127}
128
129/* SIGHUP handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100130static void
paul2d75d052004-01-19 21:31:15 +0000131sighup (void)
paul718e3742002-12-13 20:15:29 +0000132{
hassoa94434b2003-05-25 17:10:12 +0000133 zlog_info ("SIGHUP received");
134 ripng_clean ();
135 ripng_reset ();
hassoa94434b2003-05-25 17:10:12 +0000136
137 /* Reload config file. */
hasso320ec102004-06-20 19:54:37 +0000138 vty_read_config (config_file, config_default);
hassoa94434b2003-05-25 17:10:12 +0000139 /* Create VTY's socket */
140 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
141
142 /* Try to return to normal operation. */
paul718e3742002-12-13 20:15:29 +0000143}
144
145/* SIGINT handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100146static void
paul2d75d052004-01-19 21:31:15 +0000147sigint (void)
paul718e3742002-12-13 20:15:29 +0000148{
ajs887c44a2004-12-03 16:36:46 +0000149 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000150
151 if (! retain_mode)
hassoa94434b2003-05-25 17:10:12 +0000152 ripng_clean ();
paul718e3742002-12-13 20:15:29 +0000153
154 exit (0);
155}
156
157/* SIGUSR1 handler. */
Paul Jakma6ac29a52008-08-15 13:45:30 +0100158static void
paul2d75d052004-01-19 21:31:15 +0000159sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000160{
161 zlog_rotate (NULL);
162}
163
paul2d75d052004-01-19 21:31:15 +0000164struct quagga_signal_t ripng_signals[] =
paul718e3742002-12-13 20:15:29 +0000165{
paul2d75d052004-01-19 21:31:15 +0000166 {
167 .signal = SIGHUP,
168 .handler = &sighup,
169 },
170 {
171 .signal = SIGUSR1,
172 .handler = &sigusr1,
173 },
174 {
175 .signal = SIGINT,
176 .handler = &sigint,
177 },
hassof571dab2004-03-22 08:55:25 +0000178 {
179 .signal = SIGTERM,
180 .handler = &sigint,
181 },
paul2d75d052004-01-19 21:31:15 +0000182};
paul718e3742002-12-13 20:15:29 +0000183
184/* RIPngd main routine. */
185int
186main (int argc, char **argv)
187{
188 char *p;
paul4fc4e7a2003-01-22 19:47:09 +0000189 int vty_port = RIPNG_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000190 int daemon_mode = 0;
paul718e3742002-12-13 20:15:29 +0000191 char *progname;
192 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000193 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000194
195 /* Set umask before anything for security */
196 umask (0027);
197
198 /* get program name */
199 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
200
ajs274a4a42004-12-07 15:39:31 +0000201 zlog_default = openzlog(progname, ZLOG_RIPNG,
paul718e3742002-12-13 20:15:29 +0000202 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
203
204 while (1)
205 {
206 int opt;
207
Jeremy Jacksonc77cffd2008-12-28 12:57:42 -0500208 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000209
210 if (opt == EOF)
211 break;
212
213 switch (opt)
214 {
215 case 0:
216 break;
217 case 'd':
218 daemon_mode = 1;
219 break;
paul718e3742002-12-13 20:15:29 +0000220 case 'f':
221 config_file = optarg;
222 break;
223 case 'A':
224 vty_addr = optarg;
225 break;
226 case 'i':
227 pid_file = optarg;
paul4fc4e7a2003-01-22 19:47:09 +0000228 break;
paul718e3742002-12-13 20:15:29 +0000229 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000230 /* Deal with atoi() returning 0 on failure, and ripngd not
231 listening on ripngd port... */
232 if (strcmp(optarg, "0") == 0)
233 {
234 vty_port = 0;
235 break;
236 }
237 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000238 if (vty_port <= 0 || vty_port > 0xffff)
239 vty_port = RIPNG_VTY_PORT;
paul4fc4e7a2003-01-22 19:47:09 +0000240 break;
paul718e3742002-12-13 20:15:29 +0000241 case 'r':
242 retain_mode = 1;
243 break;
hassoc0652302004-11-25 19:33:48 +0000244 case 'u':
245 ripngd_privs.user = optarg;
246 break;
247 case 'g':
248 ripngd_privs.group = optarg;
249 break;
paul718e3742002-12-13 20:15:29 +0000250 case 'v':
251 print_version (progname);
252 exit (0);
253 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000254 case 'C':
255 dryrun = 1;
256 break;
paul718e3742002-12-13 20:15:29 +0000257 case 'h':
258 usage (progname, 0);
259 break;
260 default:
261 usage (progname, 1);
262 break;
263 }
264 }
265
266 master = thread_master_create ();
267
268 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000269 zprivs_init (&ripngd_privs);
paul2d75d052004-01-19 21:31:15 +0000270 signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
paul718e3742002-12-13 20:15:29 +0000271 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000272 vty_init (master);
hassoa94434b2003-05-25 17:10:12 +0000273 memory_init ();
paul718e3742002-12-13 20:15:29 +0000274
275 /* RIPngd inits. */
276 ripng_init ();
277 zebra_init ();
hassoa94434b2003-05-25 17:10:12 +0000278 ripng_peer_init ();
279
280 /* Sort all installed commands. */
paul718e3742002-12-13 20:15:29 +0000281 sort_node ();
282
283 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000284 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000285
Paul Jakma876b8be2006-10-15 23:35:57 +0000286 /* Start execution only if not in dry-run mode */
287 if(dryrun)
288 return(0);
289
paul718e3742002-12-13 20:15:29 +0000290 /* Change to the daemon program. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700291 if (daemon_mode && daemon (0, 0) < 0)
292 {
293 zlog_err("RIPNGd daemon failed: %s", strerror(errno));
294 exit (1);
295 }
paul718e3742002-12-13 20:15:29 +0000296
297 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000298 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000299
300 /* Process id file create. */
301 pid_output (pid_file);
302
ajs887c44a2004-12-03 16:36:46 +0000303 /* Print banner. */
304 zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
305
paul718e3742002-12-13 20:15:29 +0000306 /* Fetch next active thread. */
307 while (thread_fetch (master, &thread))
308 thread_call (&thread);
309
310 /* Not reached. */
paule8e19462006-01-19 20:16:55 +0000311 return 0;
paul718e3742002-12-13 20:15:29 +0000312}