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