blob: 1070fb456aad8c9a2b3e21c6e90b4289653819bf [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"
33
34#include "ripd/ripd.h"
35
36/* ripd options. */
37static struct option longopts[] =
38{
39 { "daemon", no_argument, NULL, 'd'},
40 { "config_file", required_argument, NULL, 'f'},
41 { "pid_file", required_argument, NULL, 'i'},
42 { "help", no_argument, NULL, 'h'},
43 { "vty_addr", required_argument, NULL, 'A'},
44 { "vty_port", required_argument, NULL, 'P'},
45 { "retain", no_argument, NULL, 'r'},
46 { "version", no_argument, NULL, 'v'},
47 { 0 }
48};
49
50/* Configuration file and directory. */
51char config_current[] = RIPD_DEFAULT_CONFIG;
52char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
53char *config_file = NULL;
54
55/* ripd program name */
56
57/* Route retain mode flag. */
58int retain_mode = 0;
59
60/* RIP VTY bind address. */
61char *vty_addr = NULL;
62
63/* RIP VTY connection port. */
64int vty_port = RIP_VTY_PORT;
65
66/* Master of threads. */
67struct thread_master *master;
68
69/* Process ID saved for use by init system */
70char *pid_file = PATH_RIPD_PID;
71
72/* Help information display. */
73static void
74usage (char *progname, int status)
75{
76 if (status != 0)
77 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
78 else
79 {
80 printf ("Usage : %s [OPTION...]\n\
81Daemon which manages RIP version 1 and 2.\n\n\
82-d, --daemon Runs in daemon mode\n\
83-f, --config_file Set configuration file name\n\
84-i, --pid_file Set process identifier file name\n\
85-A, --vty_addr Set vty's bind address\n\
86-P, --vty_port Set vty's port number\n\
87-r, --retain When program terminates, retain added route by ripd.\n\
88-v, --version Print program version\n\
89-h, --help Display this help and exit\n\
90\n\
91Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
92 }
93
94 exit (status);
95}
96
97/* Signale wrapper. */
98RETSIGTYPE *
99signal_set (int signo, void (*func)(int))
100{
101 int ret;
102 struct sigaction sig;
103 struct sigaction osig;
104
105 sig.sa_handler = func;
106 sigemptyset (&sig.sa_mask);
107 sig.sa_flags = 0;
108#ifdef SA_RESTART
109 sig.sa_flags |= SA_RESTART;
110#endif /* SA_RESTART */
111
112 ret = sigaction (signo, &sig, &osig);
113
114 if (ret < 0)
115 return (SIG_ERR);
116 else
117 return (osig.sa_handler);
118}
119
120/* SIGHUP handler. */
121void
122sighup (int sig)
123{
124 zlog_info ("SIGHUP received");
125 rip_clean ();
126 rip_reset ();
127 zlog_info ("ripd restarting!");
128
129 /* Reload config file. */
130 vty_read_config (config_file, config_current, config_default);
131
132 /* Create VTY's socket */
133 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
134
135 /* Try to return to normal operation. */
136}
137
138/* SIGINT handler. */
139void
140sigint (int sig)
141{
142 zlog (NULL, LOG_INFO, "Terminating on signal");
143
144 if (! retain_mode)
145 rip_clean ();
146
147 exit (0);
148}
149
150/* SIGUSR1 handler. */
151void
152sigusr1 (int sig)
153{
154 zlog_rotate (NULL);
155}
156
157/* Initialization of signal handles. */
158void
159signal_init ()
160{
161 signal_set (SIGHUP, sighup);
162 signal_set (SIGINT, sigint);
163 signal_set (SIGTERM, sigint);
164 signal_set (SIGPIPE, SIG_IGN);
165 signal_set (SIGUSR1, sigusr1);
166}
167
168/* Main routine of ripd. */
169int
170main (int argc, char **argv)
171{
172 char *p;
173 int daemon_mode = 0;
174 char *progname;
175 struct thread thread;
176
177 /* Set umask before anything for security */
178 umask (0027);
179
180 /* Get program name. */
181 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
182
183 /* First of all we need logging init. */
184 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_RIP,
185 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
186
187 /* Command line option parse. */
188 while (1)
189 {
190 int opt;
191
192 opt = getopt_long (argc, argv, "df:hA:P:rv", longopts, 0);
193
194 if (opt == EOF)
195 break;
196
197 switch (opt)
198 {
199 case 0:
200 break;
201 case 'd':
202 daemon_mode = 1;
203 break;
204 case 'f':
205 config_file = optarg;
206 break;
207 case 'A':
208 vty_addr = optarg;
209 break;
210 case 'i':
211 pid_file = optarg;
212 break;
213 case 'P':
214 vty_port = atoi (optarg);
215 break;
216 case 'r':
217 retain_mode = 1;
218 break;
219 case 'v':
220 print_version (progname);
221 exit (0);
222 break;
223 case 'h':
224 usage (progname, 0);
225 break;
226 default:
227 usage (progname, 1);
228 break;
229 }
230 }
231
232 /* Prepare master thread. */
233 master = thread_master_create ();
234
235 /* Library initialization. */
236 signal_init ();
237 cmd_init (1);
238 vty_init ();
239 memory_init ();
240 keychain_init ();
241
242 /* RIP related initialization. */
243 rip_init ();
244 rip_if_init ();
245 rip_zclient_init ();
246 rip_peer_init ();
247
248 /* Sort all installed commands. */
249 sort_node ();
250
251 /* Get configuration file. */
252 vty_read_config (config_file, config_current, config_default);
253
254 /* Change to the daemon program. */
255 if (daemon_mode)
256 daemon (0, 0);
257
258 /* Pid file create. */
259 pid_output (pid_file);
260
261 /* Create VTY's socket */
262 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
263
264 /* Execute each thread. */
265 while (thread_fetch (master, &thread))
266 thread_call (&thread);
267
268 /* Not reached. */
269 exit (0);
270}