blob: 9b762674b7ea8a87fffa01d6d5a5682227e1bc40 [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
25#include "version.h"
26#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"
paul718e3742002-12-13 20:15:29 +000036
37#include "ripngd/ripngd.h"
38
39/* Configuration filename and directory. */
40char config_current[] = RIPNG_DEFAULT_CONFIG;
41char 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
129sighup (int sig)
130{
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. */
137 vty_read_config (config_file, config_current, config_default);
138 /* 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
146sigint (int sig)
147{
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
158sigusr1 (int sig)
159{
160 zlog_rotate (NULL);
161}
162
163/* Signale wrapper. */
164RETSIGTYPE *
165signal_set (int signo, void (*func)(int))
166{
167 int ret;
168 struct sigaction sig;
169 struct sigaction osig;
170
171 sig.sa_handler = func;
172 sigemptyset (&sig.sa_mask);
173 sig.sa_flags = 0;
174#ifdef SA_RESTART
175 sig.sa_flags |= SA_RESTART;
176#endif /* SA_RESTART */
177
178 ret = sigaction (signo, &sig, &osig);
179
180 if (ret < 0)
181 return (SIG_ERR);
182 else
183 return (osig.sa_handler);
184}
185
186/* Initialization of signal handles. */
187void
188signal_init ()
189{
190 signal_set (SIGHUP, sighup);
191 signal_set (SIGINT, sigint);
192 signal_set (SIGTERM, sigint);
193 signal_set (SIGPIPE, SIG_IGN);
194 signal_set (SIGUSR1, sigusr1);
195}
196
197/* RIPngd main routine. */
198int
199main (int argc, char **argv)
200{
201 char *p;
paul4fc4e7a2003-01-22 19:47:09 +0000202 int vty_port = RIPNG_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000203 int daemon_mode = 0;
paul718e3742002-12-13 20:15:29 +0000204 char *progname;
205 struct thread thread;
206
207 /* Set umask before anything for security */
208 umask (0027);
209
210 /* get program name */
211 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
212
213 zlog_default = openzlog(progname, ZLOG_NOLOG, ZLOG_RIPNG,
214 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
215
216 while (1)
217 {
218 int opt;
219
paul96735ee2003-08-10 02:51:22 +0000220 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000221
222 if (opt == EOF)
223 break;
224
225 switch (opt)
226 {
227 case 0:
228 break;
229 case 'd':
230 daemon_mode = 1;
231 break;
232 case 'l':
233 /* log_mode = 1; */
234 break;
235 case 'f':
236 config_file = optarg;
237 break;
238 case 'A':
239 vty_addr = optarg;
240 break;
241 case 'i':
242 pid_file = optarg;
paul4fc4e7a2003-01-22 19:47:09 +0000243 break;
paul718e3742002-12-13 20:15:29 +0000244 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000245 /* Deal with atoi() returning 0 on failure, and ripngd not
246 listening on ripngd port... */
247 if (strcmp(optarg, "0") == 0)
248 {
249 vty_port = 0;
250 break;
251 }
252 vty_port = atoi (optarg);
253 vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT);
254 break;
paul718e3742002-12-13 20:15:29 +0000255 case 'r':
256 retain_mode = 1;
257 break;
pauledd7c242003-06-04 13:59:38 +0000258 case 'u':
259 ripngd_privs.group = ripngd_privs.user = optarg;
260 break;
paul718e3742002-12-13 20:15:29 +0000261 case 'v':
262 print_version (progname);
263 exit (0);
264 break;
265 case 'h':
266 usage (progname, 0);
267 break;
268 default:
269 usage (progname, 1);
270 break;
271 }
272 }
273
274 master = thread_master_create ();
275
276 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000277 zprivs_init (&ripngd_privs);
paul718e3742002-12-13 20:15:29 +0000278 signal_init ();
279 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000280 vty_init (master);
hassoa94434b2003-05-25 17:10:12 +0000281 memory_init ();
paul718e3742002-12-13 20:15:29 +0000282
283 /* RIPngd inits. */
284 ripng_init ();
285 zebra_init ();
hassoa94434b2003-05-25 17:10:12 +0000286 ripng_peer_init ();
287
288 /* Sort all installed commands. */
paul718e3742002-12-13 20:15:29 +0000289 sort_node ();
290
291 /* Get configuration file. */
292 vty_read_config (config_file, config_current, config_default);
293
294 /* 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
304 /* Fetch next active thread. */
305 while (thread_fetch (master, &thread))
306 thread_call (&thread);
307
308 /* Not reached. */
309 exit (0);
310}