blob: 3beb2279efe162ff35ec26a0bce8c672e2d44703 [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"
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. */
41char config_current[] = RIPNG_DEFAULT_CONFIG;
42char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
hassoa94434b2003-05-25 17:10:12 +000043char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +000044
45/* RIPngd options. */
46struct option longopts[] =
47{
48 { "daemon", no_argument, NULL, 'd'},
49 { "config_file", required_argument, NULL, 'f'},
50 { "pid_file", required_argument, NULL, 'i'},
51 { "log_mode", no_argument, NULL, 'l'},
52 { "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'},
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{
64 ZCAP_RAW,
65 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 */
100char *pid_file = PATH_RIPNGD_PID;
101
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\
115-l. --log_mode Set verbose log mode flag\n\
116-A, --vty_addr Set vty's bind address\n\
117-P, --vty_port Set vty's port number\n\
118-r, --retain When program terminates, retain added route by ripngd.\n\
pauledd7c242003-06-04 13:59:38 +0000119-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000120-v, --version Print program version\n\
121-h, --help Display this help and exit\n\
122\n\
123Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
124 }
125 exit (status);
126}
127
128/* SIGHUP handler. */
129void
paul2d75d052004-01-19 21:31:15 +0000130sighup (void)
paul718e3742002-12-13 20:15:29 +0000131{
hassoa94434b2003-05-25 17:10:12 +0000132 zlog_info ("SIGHUP received");
133 ripng_clean ();
134 ripng_reset ();
135 zlog_info ("Terminating on signal");
136
137 /* Reload config file. */
138 vty_read_config (config_file, config_current, config_default);
139 /* 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. */
146void
paul2d75d052004-01-19 21:31:15 +0000147sigint (void)
paul718e3742002-12-13 20:15:29 +0000148{
hassoa94434b2003-05-25 17:10:12 +0000149 zlog_info ("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. */
158void
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 },
178};
paul718e3742002-12-13 20:15:29 +0000179
180/* RIPngd main routine. */
181int
182main (int argc, char **argv)
183{
184 char *p;
paul4fc4e7a2003-01-22 19:47:09 +0000185 int vty_port = RIPNG_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000186 int daemon_mode = 0;
paul718e3742002-12-13 20:15:29 +0000187 char *progname;
188 struct thread thread;
189
190 /* Set umask before anything for security */
191 umask (0027);
192
193 /* get program name */
194 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
195
196 zlog_default = openzlog(progname, ZLOG_NOLOG, ZLOG_RIPNG,
197 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
198
199 while (1)
200 {
201 int opt;
202
paul96735ee2003-08-10 02:51:22 +0000203 opt = getopt_long (argc, argv, "dlf:i:hA:P:u:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000204
205 if (opt == EOF)
206 break;
207
208 switch (opt)
209 {
210 case 0:
211 break;
212 case 'd':
213 daemon_mode = 1;
214 break;
215 case 'l':
216 /* log_mode = 1; */
217 break;
218 case 'f':
219 config_file = optarg;
220 break;
221 case 'A':
222 vty_addr = optarg;
223 break;
224 case 'i':
225 pid_file = optarg;
paul4fc4e7a2003-01-22 19:47:09 +0000226 break;
paul718e3742002-12-13 20:15:29 +0000227 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000228 /* Deal with atoi() returning 0 on failure, and ripngd not
229 listening on ripngd port... */
230 if (strcmp(optarg, "0") == 0)
231 {
232 vty_port = 0;
233 break;
234 }
235 vty_port = atoi (optarg);
236 vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT);
237 break;
paul718e3742002-12-13 20:15:29 +0000238 case 'r':
239 retain_mode = 1;
240 break;
pauledd7c242003-06-04 13:59:38 +0000241 case 'u':
242 ripngd_privs.group = ripngd_privs.user = optarg;
243 break;
paul718e3742002-12-13 20:15:29 +0000244 case 'v':
245 print_version (progname);
246 exit (0);
247 break;
248 case 'h':
249 usage (progname, 0);
250 break;
251 default:
252 usage (progname, 1);
253 break;
254 }
255 }
256
257 master = thread_master_create ();
258
259 /* Library inits. */
pauledd7c242003-06-04 13:59:38 +0000260 zprivs_init (&ripngd_privs);
paul2d75d052004-01-19 21:31:15 +0000261 signal_init (master, Q_SIGC(ripng_signals), ripng_signals);
paul718e3742002-12-13 20:15:29 +0000262 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000263 vty_init (master);
hassoa94434b2003-05-25 17:10:12 +0000264 memory_init ();
paul718e3742002-12-13 20:15:29 +0000265
266 /* RIPngd inits. */
267 ripng_init ();
268 zebra_init ();
hassoa94434b2003-05-25 17:10:12 +0000269 ripng_peer_init ();
270
271 /* Sort all installed commands. */
paul718e3742002-12-13 20:15:29 +0000272 sort_node ();
273
274 /* Get configuration file. */
275 vty_read_config (config_file, config_current, config_default);
276
277 /* Change to the daemon program. */
278 if (daemon_mode)
279 daemon (0, 0);
280
281 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000282 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000283
284 /* Process id file create. */
285 pid_output (pid_file);
286
287 /* Fetch next active thread. */
288 while (thread_fetch (master, &thread))
289 thread_call (&thread);
290
291 /* Not reached. */
292 exit (0);
293}