blob: 921937882922f62edaa5c5289b6f8687a0d8f3b3 [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'},
51 { "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'},
paul718e3742002-12-13 20:15:29 +000056 { "version", no_argument, NULL, 'v'},
57 { 0 }
58};
59
pauledd7c242003-06-04 13:59:38 +000060/* ripngd privileges */
61zebra_capabilities_t _caps_p [] =
62{
63 ZCAP_RAW,
64 ZCAP_BIND
65};
66
67struct zebra_privs_t ripngd_privs =
68{
pauld81fadf2003-08-14 05:32:12 +000069#if defined(QUAGGA_USER)
70 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000071#endif
pauld81fadf2003-08-14 05:32:12 +000072#if defined QUAGGA_GROUP
73 .group = QUAGGA_GROUP,
74#endif
75#ifdef VTY_GROUP
76 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000077#endif
78 .caps_p = _caps_p,
79 .cap_num_p = 2,
80 .cap_num_i = 0
81};
82
83
paul718e3742002-12-13 20:15:29 +000084/* RIPngd program name */
85
86/* Route retain mode flag. */
87int retain_mode = 0;
88
hassoa94434b2003-05-25 17:10:12 +000089/* RIPng VTY bind address. */
90char *vty_addr = NULL;
91
92/* RIPng VTY connection port. */
93int vty_port = RIPNG_VTY_PORT;
94
paul718e3742002-12-13 20:15:29 +000095/* Master of threads. */
96struct thread_master *master;
97
98/* Process ID saved for use by init system */
99char *pid_file = PATH_RIPNGD_PID;
100
101/* Help information display. */
102static void
103usage (char *progname, int status)
104{
105 if (status != 0)
106 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
107 else
108 {
109 printf ("Usage : %s [OPTION...]\n\
110Daemon which manages RIPng.\n\n\
111-d, --daemon Runs in daemon mode\n\
112-f, --config_file Set configuration file name\n\
113-i, --pid_file Set process identifier file name\n\
114-l. --log_mode Set verbose log mode flag\n\
115-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\
pauledd7c242003-06-04 13:59:38 +0000118-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000119-v, --version Print program version\n\
120-h, --help Display this help and exit\n\
121\n\
122Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
123 }
124 exit (status);
125}
126
127/* SIGHUP handler. */
128void
paul2d75d052004-01-19 21:31:15 +0000129sighup (void)
paul718e3742002-12-13 20:15:29 +0000130{
hassoa94434b2003-05-25 17:10:12 +0000131 zlog_info ("SIGHUP received");
132 ripng_clean ();
133 ripng_reset ();
134 zlog_info ("Terminating on signal");
135
136 /* Reload config file. */
hasso320ec102004-06-20 19:54:37 +0000137 vty_read_config (config_file, config_default);
hassoa94434b2003-05-25 17:10:12 +0000138 /* Create VTY's socket */
139 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
140
141 /* Try to return to normal operation. */
paul718e3742002-12-13 20:15:29 +0000142}
143
144/* SIGINT handler. */
145void
paul2d75d052004-01-19 21:31:15 +0000146sigint (void)
paul718e3742002-12-13 20:15:29 +0000147{
hassoa94434b2003-05-25 17:10:12 +0000148 zlog_info ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000149
150 if (! retain_mode)
hassoa94434b2003-05-25 17:10:12 +0000151 ripng_clean ();
paul718e3742002-12-13 20:15:29 +0000152
153 exit (0);
154}
155
156/* SIGUSR1 handler. */
157void
paul2d75d052004-01-19 21:31:15 +0000158sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000159{
160 zlog_rotate (NULL);
161}
162
paul2d75d052004-01-19 21:31:15 +0000163struct quagga_signal_t ripng_signals[] =
paul718e3742002-12-13 20:15:29 +0000164{
paul2d75d052004-01-19 21:31:15 +0000165 {
166 .signal = SIGHUP,
167 .handler = &sighup,
168 },
169 {
170 .signal = SIGUSR1,
171 .handler = &sigusr1,
172 },
173 {
174 .signal = SIGINT,
175 .handler = &sigint,
176 },
hassof571dab2004-03-22 08:55:25 +0000177 {
178 .signal = SIGTERM,
179 .handler = &sigint,
180 },
paul2d75d052004-01-19 21:31:15 +0000181};
paul718e3742002-12-13 20:15:29 +0000182
183/* RIPngd main routine. */
184int
185main (int argc, char **argv)
186{
187 char *p;
paul4fc4e7a2003-01-22 19:47:09 +0000188 int vty_port = RIPNG_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000189 int daemon_mode = 0;
paul718e3742002-12-13 20:15:29 +0000190 char *progname;
191 struct thread thread;
192
193 /* Set umask before anything for security */
194 umask (0027);
195
196 /* get program name */
197 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
198
199 zlog_default = openzlog(progname, ZLOG_NOLOG, ZLOG_RIPNG,
200 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
201
202 while (1)
203 {
204 int opt;
205
paul96735ee2003-08-10 02:51:22 +0000206 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000207
208 if (opt == EOF)
209 break;
210
211 switch (opt)
212 {
213 case 0:
214 break;
215 case 'd':
216 daemon_mode = 1;
217 break;
218 case 'l':
219 /* log_mode = 1; */
220 break;
221 case 'f':
222 config_file = optarg;
223 break;
224 case 'A':
225 vty_addr = optarg;
226 break;
227 case 'i':
228 pid_file = optarg;
paul4fc4e7a2003-01-22 19:47:09 +0000229 break;
paul718e3742002-12-13 20:15:29 +0000230 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000231 /* Deal with atoi() returning 0 on failure, and ripngd not
232 listening on ripngd port... */
233 if (strcmp(optarg, "0") == 0)
234 {
235 vty_port = 0;
236 break;
237 }
238 vty_port = atoi (optarg);
239 vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT);
240 break;
paul718e3742002-12-13 20:15:29 +0000241 case 'r':
242 retain_mode = 1;
243 break;
pauledd7c242003-06-04 13:59:38 +0000244 case 'u':
245 ripngd_privs.group = ripngd_privs.user = optarg;
246 break;
paul718e3742002-12-13 20:15:29 +0000247 case 'v':
248 print_version (progname);
249 exit (0);
250 break;
251 case 'h':
252 usage (progname, 0);
253 break;
254 default:
255 usage (progname, 1);
256 break;
257 }
258 }
259
260 master = thread_master_create ();
261
262 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000263 zprivs_init (&ripngd_privs);
paul2d75d052004-01-19 21:31:15 +0000264 signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
paul718e3742002-12-13 20:15:29 +0000265 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000266 vty_init (master);
hassoa94434b2003-05-25 17:10:12 +0000267 memory_init ();
paul718e3742002-12-13 20:15:29 +0000268
269 /* RIPngd inits. */
270 ripng_init ();
271 zebra_init ();
hassoa94434b2003-05-25 17:10:12 +0000272 ripng_peer_init ();
273
274 /* Sort all installed commands. */
paul718e3742002-12-13 20:15:29 +0000275 sort_node ();
276
277 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000278 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000279
280 /* Change to the daemon program. */
281 if (daemon_mode)
282 daemon (0, 0);
283
284 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000285 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000286
287 /* Process id file create. */
288 pid_output (pid_file);
289
290 /* Fetch next active thread. */
291 while (thread_fetch (master, &thread))
292 thread_call (&thread);
293
294 /* Not reached. */
295 exit (0);
296}