blob: a87e3b7eb12f01f5ba53aeb2587151559bd3f1bf [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
24#include "version.h"
25#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"
paul718e3742002-12-13 20:15:29 +000034
35#include "ripd/ripd.h"
36
37/* ripd options. */
38static struct option longopts[] =
39{
40 { "daemon", no_argument, NULL, 'd'},
41 { "config_file", required_argument, NULL, 'f'},
42 { "pid_file", required_argument, NULL, 'i'},
43 { "help", no_argument, NULL, 'h'},
44 { "vty_addr", required_argument, NULL, 'A'},
45 { "vty_port", required_argument, NULL, 'P'},
46 { "retain", no_argument, NULL, 'r'},
pauledd7c242003-06-04 13:59:38 +000047 { "user", required_argument, NULL, 'u'},
paul718e3742002-12-13 20:15:29 +000048 { "version", no_argument, NULL, 'v'},
49 { 0 }
50};
51
pauledd7c242003-06-04 13:59:38 +000052/* ripd privileges */
53zebra_capabilities_t _caps_p [] =
54{
55 ZCAP_RAW,
56 ZCAP_BIND
57};
58
59struct zebra_privs_t ripd_privs =
60{
pauld81fadf2003-08-14 05:32:12 +000061#if defined(QUAGGA_USER)
62 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000063#endif
pauld81fadf2003-08-14 05:32:12 +000064#if defined QUAGGA_GROUP
65 .group = QUAGGA_GROUP,
66#endif
67#ifdef VTY_GROUP
68 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000069#endif
70 .caps_p = _caps_p,
71 .cap_num_p = 2,
72 .cap_num_i = 0
73};
74
paul718e3742002-12-13 20:15:29 +000075/* Configuration file and directory. */
76char config_current[] = RIPD_DEFAULT_CONFIG;
77char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
78char *config_file = NULL;
79
80/* ripd program name */
81
82/* Route retain mode flag. */
83int retain_mode = 0;
84
85/* RIP VTY bind address. */
86char *vty_addr = NULL;
87
88/* RIP VTY connection port. */
89int vty_port = RIP_VTY_PORT;
90
91/* Master of threads. */
92struct thread_master *master;
93
94/* Process ID saved for use by init system */
95char *pid_file = PATH_RIPD_PID;
96
97/* Help information display. */
98static void
99usage (char *progname, int status)
100{
101 if (status != 0)
102 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
103 else
104 {
105 printf ("Usage : %s [OPTION...]\n\
106Daemon which manages RIP version 1 and 2.\n\n\
107-d, --daemon Runs in daemon mode\n\
108-f, --config_file Set configuration file name\n\
109-i, --pid_file Set process identifier file name\n\
110-A, --vty_addr Set vty's bind address\n\
111-P, --vty_port Set vty's port number\n\
112-r, --retain When program terminates, retain added route by ripd.\n\
pauledd7c242003-06-04 13:59:38 +0000113-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000114-v, --version Print program version\n\
115-h, --help Display this help and exit\n\
116\n\
117Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
118 }
119
120 exit (status);
121}
122
123/* Signale wrapper. */
124RETSIGTYPE *
125signal_set (int signo, void (*func)(int))
126{
127 int ret;
128 struct sigaction sig;
129 struct sigaction osig;
130
131 sig.sa_handler = func;
132 sigemptyset (&sig.sa_mask);
133 sig.sa_flags = 0;
134#ifdef SA_RESTART
135 sig.sa_flags |= SA_RESTART;
136#endif /* SA_RESTART */
137
138 ret = sigaction (signo, &sig, &osig);
139
140 if (ret < 0)
141 return (SIG_ERR);
142 else
143 return (osig.sa_handler);
144}
145
146/* SIGHUP handler. */
147void
148sighup (int sig)
149{
150 zlog_info ("SIGHUP received");
151 rip_clean ();
152 rip_reset ();
153 zlog_info ("ripd restarting!");
154
155 /* Reload config file. */
156 vty_read_config (config_file, config_current, config_default);
157
158 /* Create VTY's socket */
159 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
160
161 /* Try to return to normal operation. */
162}
163
164/* SIGINT handler. */
165void
166sigint (int sig)
167{
168 zlog (NULL, LOG_INFO, "Terminating on signal");
169
170 if (! retain_mode)
171 rip_clean ();
172
173 exit (0);
174}
175
176/* SIGUSR1 handler. */
177void
178sigusr1 (int sig)
179{
180 zlog_rotate (NULL);
181}
182
183/* Initialization of signal handles. */
184void
185signal_init ()
186{
187 signal_set (SIGHUP, sighup);
188 signal_set (SIGINT, sigint);
189 signal_set (SIGTERM, sigint);
190 signal_set (SIGPIPE, SIG_IGN);
191 signal_set (SIGUSR1, sigusr1);
192}
193
194/* Main routine of ripd. */
195int
196main (int argc, char **argv)
197{
198 char *p;
199 int daemon_mode = 0;
200 char *progname;
201 struct thread thread;
202
203 /* Set umask before anything for security */
204 umask (0027);
205
206 /* Get program name. */
207 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
208
209 /* First of all we need logging init. */
210 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_RIP,
211 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
212
213 /* Command line option parse. */
214 while (1)
215 {
216 int opt;
217
paul96735ee2003-08-10 02:51:22 +0000218 opt = getopt_long (argc, argv, "df:i:hA:P:u:rv", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000219
220 if (opt == EOF)
221 break;
222
223 switch (opt)
224 {
225 case 0:
226 break;
227 case 'd':
228 daemon_mode = 1;
229 break;
230 case 'f':
231 config_file = optarg;
232 break;
233 case 'A':
234 vty_addr = optarg;
235 break;
236 case 'i':
237 pid_file = optarg;
238 break;
239 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000240 /* Deal with atoi() returning 0 on failure, and ripd not
241 listening on rip port... */
242 if (strcmp(optarg, "0") == 0)
243 {
244 vty_port = 0;
245 break;
246 }
247 vty_port = atoi (optarg);
248 vty_port = (vty_port ? vty_port : RIP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000249 break;
250 case 'r':
251 retain_mode = 1;
252 break;
pauledd7c242003-06-04 13:59:38 +0000253 case 'u':
254 ripd_privs.group = ripd_privs.user = optarg;
255 break;
paul718e3742002-12-13 20:15:29 +0000256 case 'v':
257 print_version (progname);
258 exit (0);
259 break;
260 case 'h':
261 usage (progname, 0);
262 break;
263 default:
264 usage (progname, 1);
265 break;
266 }
267 }
268
269 /* Prepare master thread. */
270 master = thread_master_create ();
271
272 /* Library initialization. */
pauledd7c242003-06-04 13:59:38 +0000273 zprivs_init (&ripd_privs);
paul718e3742002-12-13 20:15:29 +0000274 signal_init ();
275 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000276 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000277 memory_init ();
278 keychain_init ();
279
280 /* RIP related initialization. */
281 rip_init ();
282 rip_if_init ();
283 rip_zclient_init ();
284 rip_peer_init ();
285
286 /* Sort all installed commands. */
287 sort_node ();
288
289 /* Get configuration file. */
290 vty_read_config (config_file, config_current, config_default);
291
292 /* Change to the daemon program. */
293 if (daemon_mode)
294 daemon (0, 0);
295
296 /* Pid file create. */
297 pid_output (pid_file);
298
299 /* Create VTY's socket */
300 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
301
302 /* Execute each thread. */
303 while (thread_fetch (master, &thread))
304 thread_call (&thread);
305
306 /* Not reached. */
307 exit (0);
308}