blob: ba60b36ff4e74cdf10c57cc040e5a6b1569281bc [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* RIPd main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
3 *
4 * This file is part of GNU Zebra.
5 *
6 * GNU Zebra is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU General Public License as published by the
8 * Free Software Foundation; either version 2, or (at your option) any
9 * later version.
10 *
11 * GNU Zebra is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with GNU Zebra; see the file COPYING. If not, write to the Free
18 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 * 02111-1307, USA.
20 */
21
22#include <zebra.h>
23
gdt5e4fa162004-03-16 14:38:36 +000024#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000025#include "getopt.h"
26#include "thread.h"
27#include "command.h"
28#include "memory.h"
29#include "prefix.h"
30#include "filter.h"
31#include "keychain.h"
32#include "log.h"
pauledd7c242003-06-04 13:59:38 +000033#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000034#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000035
36#include "ripd/ripd.h"
37
38/* ripd options. */
39static struct option longopts[] =
40{
41 { "daemon", no_argument, NULL, 'd'},
42 { "config_file", required_argument, NULL, 'f'},
43 { "pid_file", required_argument, NULL, 'i'},
44 { "help", no_argument, NULL, 'h'},
45 { "vty_addr", required_argument, NULL, 'A'},
46 { "vty_port", required_argument, NULL, 'P'},
47 { "retain", no_argument, NULL, 'r'},
pauledd7c242003-06-04 13:59:38 +000048 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000049 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000050 { "version", no_argument, NULL, 'v'},
51 { 0 }
52};
53
pauledd7c242003-06-04 13:59:38 +000054/* ripd privileges */
55zebra_capabilities_t _caps_p [] =
56{
57 ZCAP_RAW,
58 ZCAP_BIND
59};
60
61struct zebra_privs_t ripd_privs =
62{
pauld81fadf2003-08-14 05:32:12 +000063#if defined(QUAGGA_USER)
64 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000065#endif
pauld81fadf2003-08-14 05:32:12 +000066#if defined QUAGGA_GROUP
67 .group = QUAGGA_GROUP,
68#endif
69#ifdef VTY_GROUP
70 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000071#endif
72 .caps_p = _caps_p,
73 .cap_num_p = 2,
74 .cap_num_i = 0
75};
76
paul718e3742002-12-13 20:15:29 +000077/* Configuration file and directory. */
paul718e3742002-12-13 20:15:29 +000078char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
79char *config_file = NULL;
80
81/* ripd program name */
82
83/* Route retain mode flag. */
84int retain_mode = 0;
85
86/* RIP VTY bind address. */
87char *vty_addr = NULL;
88
89/* RIP VTY connection port. */
90int vty_port = RIP_VTY_PORT;
91
92/* Master of threads. */
93struct thread_master *master;
94
95/* Process ID saved for use by init system */
hasso8a676be2004-10-08 06:36:38 +000096const char *pid_file = PATH_RIPD_PID;
paul718e3742002-12-13 20:15:29 +000097
98/* Help information display. */
99static void
100usage (char *progname, int status)
101{
102 if (status != 0)
103 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
104 else
105 {
106 printf ("Usage : %s [OPTION...]\n\
107Daemon which manages RIP version 1 and 2.\n\n\
108-d, --daemon Runs in daemon mode\n\
109-f, --config_file Set configuration file name\n\
110-i, --pid_file Set process identifier file name\n\
111-A, --vty_addr Set vty's bind address\n\
112-P, --vty_port Set vty's port number\n\
113-r, --retain When program terminates, retain added route by ripd.\n\
hassoc0652302004-11-25 19:33:48 +0000114-u, --user User to run as\n\
115-g, --group Group to run as\n\
paul718e3742002-12-13 20:15:29 +0000116-v, --version Print program version\n\
117-h, --help Display this help and exit\n\
118\n\
119Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
120 }
121
122 exit (status);
123}
124
paul718e3742002-12-13 20:15:29 +0000125/* SIGHUP handler. */
126void
paul2d75d052004-01-19 21:31:15 +0000127sighup (void)
paul718e3742002-12-13 20:15:29 +0000128{
129 zlog_info ("SIGHUP received");
130 rip_clean ();
131 rip_reset ();
132 zlog_info ("ripd restarting!");
133
134 /* Reload config file. */
hasso320ec102004-06-20 19:54:37 +0000135 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000136
137 /* Create VTY's socket */
138 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
139
140 /* Try to return to normal operation. */
141}
142
143/* SIGINT handler. */
144void
paul2d75d052004-01-19 21:31:15 +0000145sigint (void)
paul718e3742002-12-13 20:15:29 +0000146{
ajs887c44a2004-12-03 16:36:46 +0000147 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000148
149 if (! retain_mode)
150 rip_clean ();
151
152 exit (0);
153}
154
155/* SIGUSR1 handler. */
156void
paul2d75d052004-01-19 21:31:15 +0000157sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000158{
159 zlog_rotate (NULL);
160}
161
paul2d75d052004-01-19 21:31:15 +0000162struct quagga_signal_t ripd_signals[] =
paul718e3742002-12-13 20:15:29 +0000163{
paul2d75d052004-01-19 21:31:15 +0000164 {
165 .signal = SIGHUP,
166 .handler = &sighup,
167 },
168 {
169 .signal = SIGUSR1,
170 .handler = &sigusr1,
171 },
172 {
173 .signal = SIGINT,
hasso8c903fb2004-03-17 20:39:18 +0000174 .handler = &sigint,
paul2d75d052004-01-19 21:31:15 +0000175 },
hassof571dab2004-03-22 08:55:25 +0000176 {
177 .signal = SIGTERM,
178 .handler = &sigint,
179 },
paul2d75d052004-01-19 21:31:15 +0000180};
paul718e3742002-12-13 20:15:29 +0000181
182/* Main routine of ripd. */
183int
184main (int argc, char **argv)
185{
186 char *p;
187 int daemon_mode = 0;
188 char *progname;
189 struct thread thread;
190
191 /* Set umask before anything for security */
192 umask (0027);
193
194 /* Get program name. */
195 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
196
197 /* First of all we need logging init. */
ajs274a4a42004-12-07 15:39:31 +0000198 zlog_default = openzlog (progname, ZLOG_RIP,
paul718e3742002-12-13 20:15:29 +0000199 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
200
201 /* Command line option parse. */
202 while (1)
203 {
204 int opt;
205
hassoc0652302004-11-25 19:33:48 +0000206 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rv", 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 'f':
219 config_file = optarg;
220 break;
221 case 'A':
222 vty_addr = optarg;
223 break;
224 case 'i':
225 pid_file = optarg;
226 break;
227 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000228 /* Deal with atoi() returning 0 on failure, and ripd not
229 listening on rip 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 : RIP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000237 break;
238 case 'r':
239 retain_mode = 1;
240 break;
hassoc0652302004-11-25 19:33:48 +0000241 case 'u':
242 ripd_privs.user = optarg;
243 break;
244 case 'g':
245 ripd_privs.group = 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 /* Prepare master thread. */
261 master = thread_master_create ();
262
263 /* Library initialization. */
pauledd7c242003-06-04 13:59:38 +0000264 zprivs_init (&ripd_privs);
paul2d75d052004-01-19 21:31:15 +0000265 signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
paul718e3742002-12-13 20:15:29 +0000266 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000267 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000268 memory_init ();
269 keychain_init ();
270
271 /* RIP related initialization. */
272 rip_init ();
273 rip_if_init ();
274 rip_zclient_init ();
275 rip_peer_init ();
276
277 /* Sort all installed commands. */
278 sort_node ();
279
280 /* Get configuration file. */
hasso320ec102004-06-20 19:54:37 +0000281 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000282
283 /* Change to the daemon program. */
284 if (daemon_mode)
285 daemon (0, 0);
286
287 /* Pid file create. */
288 pid_output (pid_file);
289
290 /* Create VTY's socket */
291 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
292
ajs887c44a2004-12-03 16:36:46 +0000293 /* Print banner. */
294 zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
295
paul718e3742002-12-13 20:15:29 +0000296 /* Execute each thread. */
297 while (thread_fetch (master, &thread))
298 thread_call (&thread);
299
300 /* Not reached. */
301 exit (0);
302}