blob: 7055391052485f7a4674a1c9b51ff51a77291cc3 [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'},
50 { "log_mode", no_argument, NULL, 'l'},
Paul Jakma876b8be2006-10-15 23:35:57 +000051 { "dryrun", no_argument, NULL, 'C'},
paul718e3742002-12-13 20:15:29 +000052 { "help", no_argument, NULL, 'h'},
53 { "vty_addr", required_argument, NULL, 'A'},
54 { "vty_port", required_argument, NULL, 'P'},
55 { "retain", no_argument, NULL, 'r'},
pauledd7c242003-06-04 13:59:38 +000056 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000057 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000058 { "version", no_argument, NULL, 'v'},
59 { 0 }
60};
61
pauledd7c242003-06-04 13:59:38 +000062/* ripngd privileges */
63zebra_capabilities_t _caps_p [] =
64{
paulceacedb2005-09-29 14:39:32 +000065 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000066 ZCAP_BIND
67};
68
69struct zebra_privs_t ripngd_privs =
70{
pauld81fadf2003-08-14 05:32:12 +000071#if defined(QUAGGA_USER)
72 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000073#endif
pauld81fadf2003-08-14 05:32:12 +000074#if defined QUAGGA_GROUP
75 .group = QUAGGA_GROUP,
76#endif
77#ifdef VTY_GROUP
78 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000079#endif
80 .caps_p = _caps_p,
81 .cap_num_p = 2,
82 .cap_num_i = 0
83};
84
85
paul718e3742002-12-13 20:15:29 +000086/* RIPngd program name */
87
88/* Route retain mode flag. */
89int retain_mode = 0;
90
hassoa94434b2003-05-25 17:10:12 +000091/* RIPng VTY bind address. */
92char *vty_addr = NULL;
93
94/* RIPng VTY connection port. */
95int vty_port = RIPNG_VTY_PORT;
96
paul718e3742002-12-13 20:15:29 +000097/* Master of threads. */
98struct thread_master *master;
99
100/* Process ID saved for use by init system */
hasso7a1d5832004-10-08 06:32:23 +0000101const char *pid_file = PATH_RIPNGD_PID;
paul718e3742002-12-13 20:15:29 +0000102
103/* Help information display. */
104static void
105usage (char *progname, int status)
106{
107 if (status != 0)
108 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
109 else
110 {
111 printf ("Usage : %s [OPTION...]\n\
112Daemon which manages RIPng.\n\n\
113-d, --daemon Runs in daemon mode\n\
114-f, --config_file Set configuration file name\n\
115-i, --pid_file Set process identifier file name\n\
116-l. --log_mode Set verbose log mode flag\n\
117-A, --vty_addr Set vty's bind address\n\
118-P, --vty_port Set vty's port number\n\
119-r, --retain When program terminates, retain added route by ripngd.\n\
hassoc0652302004-11-25 19:33:48 +0000120-u, --user User to run as\n\
121-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000122-v, --version Print program version\n\
Paul Jakma876b8be2006-10-15 23:35:57 +0000123-C, --dryrun Check configuration for validity and exit\n\
paul718e3742002-12-13 20:15:29 +0000124-h, --help Display this help and exit\n\
125\n\
126Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
127 }
128 exit (status);
129}
130
131/* SIGHUP handler. */
132void
paul2d75d052004-01-19 21:31:15 +0000133sighup (void)
paul718e3742002-12-13 20:15:29 +0000134{
hassoa94434b2003-05-25 17:10:12 +0000135 zlog_info ("SIGHUP received");
136 ripng_clean ();
137 ripng_reset ();
hassoa94434b2003-05-25 17:10:12 +0000138
139 /* Reload config file. */
hasso320ec102004-06-20 19:54:37 +0000140 vty_read_config (config_file, config_default);
hassoa94434b2003-05-25 17:10:12 +0000141 /* Create VTY's socket */
142 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
143
144 /* Try to return to normal operation. */
paul718e3742002-12-13 20:15:29 +0000145}
146
147/* SIGINT handler. */
148void
paul2d75d052004-01-19 21:31:15 +0000149sigint (void)
paul718e3742002-12-13 20:15:29 +0000150{
ajs887c44a2004-12-03 16:36:46 +0000151 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000152
153 if (! retain_mode)
hassoa94434b2003-05-25 17:10:12 +0000154 ripng_clean ();
paul718e3742002-12-13 20:15:29 +0000155
156 exit (0);
157}
158
159/* SIGUSR1 handler. */
160void
paul2d75d052004-01-19 21:31:15 +0000161sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000162{
163 zlog_rotate (NULL);
164}
165
paul2d75d052004-01-19 21:31:15 +0000166struct quagga_signal_t ripng_signals[] =
paul718e3742002-12-13 20:15:29 +0000167{
paul2d75d052004-01-19 21:31:15 +0000168 {
169 .signal = SIGHUP,
170 .handler = &sighup,
171 },
172 {
173 .signal = SIGUSR1,
174 .handler = &sigusr1,
175 },
176 {
177 .signal = SIGINT,
178 .handler = &sigint,
179 },
hassof571dab2004-03-22 08:55:25 +0000180 {
181 .signal = SIGTERM,
182 .handler = &sigint,
183 },
paul2d75d052004-01-19 21:31:15 +0000184};
paul718e3742002-12-13 20:15:29 +0000185
186/* RIPngd main routine. */
187int
188main (int argc, char **argv)
189{
190 char *p;
paul4fc4e7a2003-01-22 19:47:09 +0000191 int vty_port = RIPNG_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000192 int daemon_mode = 0;
paul718e3742002-12-13 20:15:29 +0000193 char *progname;
194 struct thread thread;
Paul Jakma876b8be2006-10-15 23:35:57 +0000195 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000196
197 /* Set umask before anything for security */
198 umask (0027);
199
200 /* get program name */
201 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
202
ajs274a4a42004-12-07 15:39:31 +0000203 zlog_default = openzlog(progname, ZLOG_RIPNG,
paul718e3742002-12-13 20:15:29 +0000204 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
205
206 while (1)
207 {
208 int opt;
209
Paul Jakma876b8be2006-10-15 23:35:57 +0000210 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:g:vC", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000211
212 if (opt == EOF)
213 break;
214
215 switch (opt)
216 {
217 case 0:
218 break;
219 case 'd':
220 daemon_mode = 1;
221 break;
222 case 'l':
223 /* log_mode = 1; */
224 break;
225 case 'f':
226 config_file = optarg;
227 break;
228 case 'A':
229 vty_addr = optarg;
230 break;
231 case 'i':
232 pid_file = optarg;
paul4fc4e7a2003-01-22 19:47:09 +0000233 break;
paul718e3742002-12-13 20:15:29 +0000234 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000235 /* Deal with atoi() returning 0 on failure, and ripngd not
236 listening on ripngd port... */
237 if (strcmp(optarg, "0") == 0)
238 {
239 vty_port = 0;
240 break;
241 }
242 vty_port = atoi (optarg);
243 vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT);
244 break;
paul718e3742002-12-13 20:15:29 +0000245 case 'r':
246 retain_mode = 1;
247 break;
hassoc0652302004-11-25 19:33:48 +0000248 case 'u':
249 ripngd_privs.user = optarg;
250 break;
251 case 'g':
252 ripngd_privs.group = optarg;
253 break;
paul718e3742002-12-13 20:15:29 +0000254 case 'v':
255 print_version (progname);
256 exit (0);
257 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000258 case 'C':
259 dryrun = 1;
260 break;
paul718e3742002-12-13 20:15:29 +0000261 case 'h':
262 usage (progname, 0);
263 break;
264 default:
265 usage (progname, 1);
266 break;
267 }
268 }
269
270 master = thread_master_create ();
271
272 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000273 zprivs_init (&ripngd_privs);
paul2d75d052004-01-19 21:31:15 +0000274 signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
paul718e3742002-12-13 20:15:29 +0000275 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000276 vty_init (master);
hassoa94434b2003-05-25 17:10:12 +0000277 memory_init ();
paul718e3742002-12-13 20:15:29 +0000278
279 /* RIPngd inits. */
280 ripng_init ();
281 zebra_init ();
hassoa94434b2003-05-25 17:10:12 +0000282 ripng_peer_init ();
283
284 /* Sort all installed commands. */
paul718e3742002-12-13 20:15:29 +0000285 sort_node ();
286
287 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000288 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000289
Paul Jakma876b8be2006-10-15 23:35:57 +0000290 /* Start execution only if not in dry-run mode */
291 if(dryrun)
292 return(0);
293
paul718e3742002-12-13 20:15:29 +0000294 /* Change to the daemon program. */
295 if (daemon_mode)
296 daemon (0, 0);
297
298 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000299 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000300
301 /* Process id file create. */
302 pid_output (pid_file);
303
ajs887c44a2004-12-03 16:36:46 +0000304 /* Print banner. */
305 zlog_notice ("RIPNGd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
306
paul718e3742002-12-13 20:15:29 +0000307 /* Fetch next active thread. */
308 while (thread_fetch (master, &thread))
309 thread_call (&thread);
310
311 /* Not reached. */
paule8e19462006-01-19 20:16:55 +0000312 return 0;
paul718e3742002-12-13 20:15:29 +0000313}