blob: 530b13c95965466ac8516a524d8429082b0a925a [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'},
paul718e3742002-12-13 20:15:29 +000049 { "version", no_argument, NULL, 'v'},
50 { 0 }
51};
52
pauledd7c242003-06-04 13:59:38 +000053/* ripd privileges */
54zebra_capabilities_t _caps_p [] =
55{
56 ZCAP_RAW,
57 ZCAP_BIND
58};
59
60struct zebra_privs_t ripd_privs =
61{
pauld81fadf2003-08-14 05:32:12 +000062#if defined(QUAGGA_USER)
63 .user = QUAGGA_USER,
pauledd7c242003-06-04 13:59:38 +000064#endif
pauld81fadf2003-08-14 05:32:12 +000065#if defined QUAGGA_GROUP
66 .group = QUAGGA_GROUP,
67#endif
68#ifdef VTY_GROUP
69 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000070#endif
71 .caps_p = _caps_p,
72 .cap_num_p = 2,
73 .cap_num_i = 0
74};
75
paul718e3742002-12-13 20:15:29 +000076/* Configuration file and directory. */
77char config_current[] = RIPD_DEFAULT_CONFIG;
78char 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 */
96char *pid_file = PATH_RIPD_PID;
97
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\
pauledd7c242003-06-04 13:59:38 +0000114-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000115-v, --version Print program version\n\
116-h, --help Display this help and exit\n\
117\n\
118Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
119 }
120
121 exit (status);
122}
123
paul718e3742002-12-13 20:15:29 +0000124/* SIGHUP handler. */
125void
paul2d75d052004-01-19 21:31:15 +0000126sighup (void)
paul718e3742002-12-13 20:15:29 +0000127{
128 zlog_info ("SIGHUP received");
129 rip_clean ();
130 rip_reset ();
131 zlog_info ("ripd restarting!");
132
133 /* Reload config file. */
134 vty_read_config (config_file, config_current, config_default);
135
136 /* Create VTY's socket */
137 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
138
139 /* Try to return to normal operation. */
140}
141
142/* SIGINT handler. */
143void
paul2d75d052004-01-19 21:31:15 +0000144sigint (void)
paul718e3742002-12-13 20:15:29 +0000145{
146 zlog (NULL, LOG_INFO, "Terminating on signal");
147
148 if (! retain_mode)
149 rip_clean ();
150
151 exit (0);
152}
153
154/* SIGUSR1 handler. */
155void
paul2d75d052004-01-19 21:31:15 +0000156sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000157{
158 zlog_rotate (NULL);
159}
160
paul2d75d052004-01-19 21:31:15 +0000161struct quagga_signal_t ripd_signals[] =
paul718e3742002-12-13 20:15:29 +0000162{
paul2d75d052004-01-19 21:31:15 +0000163 {
164 .signal = SIGHUP,
165 .handler = &sighup,
166 },
167 {
168 .signal = SIGUSR1,
169 .handler = &sigusr1,
170 },
171 {
172 .signal = SIGINT,
hasso8c903fb2004-03-17 20:39:18 +0000173 .handler = &sigint,
paul2d75d052004-01-19 21:31:15 +0000174 },
hassof571dab2004-03-22 08:55:25 +0000175 {
176 .signal = SIGTERM,
177 .handler = &sigint,
178 },
paul2d75d052004-01-19 21:31:15 +0000179};
paul718e3742002-12-13 20:15:29 +0000180
181/* Main routine of ripd. */
182int
183main (int argc, char **argv)
184{
185 char *p;
186 int daemon_mode = 0;
187 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 /* First of all we need logging init. */
197 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_RIP,
198 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
199
200 /* Command line option parse. */
201 while (1)
202 {
203 int opt;
204
paul96735ee2003-08-10 02:51:22 +0000205 opt = getopt_long (argc, argv, "df:i:hA:P:u:rv", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000206
207 if (opt == EOF)
208 break;
209
210 switch (opt)
211 {
212 case 0:
213 break;
214 case 'd':
215 daemon_mode = 1;
216 break;
217 case 'f':
218 config_file = optarg;
219 break;
220 case 'A':
221 vty_addr = optarg;
222 break;
223 case 'i':
224 pid_file = optarg;
225 break;
226 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000227 /* Deal with atoi() returning 0 on failure, and ripd not
228 listening on rip port... */
229 if (strcmp(optarg, "0") == 0)
230 {
231 vty_port = 0;
232 break;
233 }
234 vty_port = atoi (optarg);
235 vty_port = (vty_port ? vty_port : RIP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000236 break;
237 case 'r':
238 retain_mode = 1;
239 break;
pauledd7c242003-06-04 13:59:38 +0000240 case 'u':
241 ripd_privs.group = ripd_privs.user = optarg;
242 break;
paul718e3742002-12-13 20:15:29 +0000243 case 'v':
244 print_version (progname);
245 exit (0);
246 break;
247 case 'h':
248 usage (progname, 0);
249 break;
250 default:
251 usage (progname, 1);
252 break;
253 }
254 }
255
256 /* Prepare master thread. */
257 master = thread_master_create ();
258
259 /* Library initialization. */
pauledd7c242003-06-04 13:59:38 +0000260 zprivs_init (&ripd_privs);
paul2d75d052004-01-19 21:31:15 +0000261 signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
paul718e3742002-12-13 20:15:29 +0000262 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000263 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000264 memory_init ();
265 keychain_init ();
266
267 /* RIP related initialization. */
268 rip_init ();
269 rip_if_init ();
270 rip_zclient_init ();
271 rip_peer_init ();
272
273 /* Sort all installed commands. */
274 sort_node ();
275
276 /* Get configuration file. */
277 vty_read_config (config_file, config_current, config_default);
278
279 /* Change to the daemon program. */
280 if (daemon_mode)
281 daemon (0, 0);
282
283 /* Pid file create. */
284 pid_output (pid_file);
285
286 /* Create VTY's socket */
287 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
288
289 /* Execute each thread. */
290 while (thread_fetch (master, &thread))
291 thread_call (&thread);
292
293 /* Not reached. */
294 exit (0);
295}