]> git.proxmox.com Git - mirror_frr.git/blame - ripd/rip_main.c
build: rename (1 of ?): configure.ac + preproc
[mirror_frr.git] / ripd / rip_main.c
CommitLineData
718e3744 1/* 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
5e4fa164 24#include <lib/version.h>
718e3744 25#include "getopt.h"
26#include "thread.h"
27#include "command.h"
28#include "memory.h"
fc7948fa 29#include "memory_vty.h"
718e3744 30#include "prefix.h"
31#include "filter.h"
32#include "keychain.h"
33#include "log.h"
edd7c245 34#include "privs.h"
2d75d052 35#include "sigevent.h"
b5114685 36#include "zclient.h"
6a69b354 37#include "vrf.h"
718e3744 38
39#include "ripd/ripd.h"
40
41/* ripd options. */
42static struct option longopts[] =
43{
44 { "daemon", no_argument, NULL, 'd'},
45 { "config_file", required_argument, NULL, 'f'},
46 { "pid_file", required_argument, NULL, 'i'},
b5114685 47 { "socket", required_argument, NULL, 'z'},
718e3744 48 { "help", no_argument, NULL, 'h'},
876b8be0 49 { "dryrun", no_argument, NULL, 'C'},
718e3744 50 { "vty_addr", required_argument, NULL, 'A'},
51 { "vty_port", required_argument, NULL, 'P'},
52 { "retain", no_argument, NULL, 'r'},
edd7c245 53 { "user", required_argument, NULL, 'u'},
c065230a 54 { "group", required_argument, NULL, 'g'},
718e3744 55 { "version", no_argument, NULL, 'v'},
56 { 0 }
57};
58
edd7c245 59/* ripd privileges */
60zebra_capabilities_t _caps_p [] =
61{
ceacedba 62 ZCAP_NET_RAW,
edd7c245 63 ZCAP_BIND
64};
65
66struct zebra_privs_t ripd_privs =
67{
b2f36157
DL
68#if defined(FRR_USER)
69 .user = FRR_USER,
edd7c245 70#endif
b2f36157
DL
71#if defined FRR_GROUP
72 .group = FRR_GROUP,
d81fadfd 73#endif
74#ifdef VTY_GROUP
75 .vty_group = VTY_GROUP,
edd7c245 76#endif
77 .caps_p = _caps_p,
78 .cap_num_p = 2,
79 .cap_num_i = 0
80};
81
718e3744 82/* Configuration file and directory. */
718e3744 83char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
84char *config_file = NULL;
85
86/* ripd program name */
87
88/* Route retain mode flag. */
89int retain_mode = 0;
90
91/* RIP VTY bind address. */
92char *vty_addr = NULL;
93
94/* RIP VTY connection port. */
95int vty_port = RIP_VTY_PORT;
96
97/* Master of threads. */
98struct thread_master *master;
99
100/* Process ID saved for use by init system */
8a676be3 101const char *pid_file = PATH_RIPD_PID;
718e3744 102
103/* Help information display. */
104static void
105usage (char *progname, int status)
106{
107 if (status != 0)
108 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
109 else
110 {
111 printf ("Usage : %s [OPTION...]\n\
112Daemon which manages RIP version 1 and 2.\n\n\
113-d, --daemon Runs in daemon mode\n\
114-f, --config_file Set configuration file name\n\
115-i, --pid_file Set process identifier file name\n\
b5114685 116-z, --socket Set path of zebra socket\n\
718e3744 117-A, --vty_addr Set vty's bind address\n\
118-P, --vty_port Set vty's port number\n\
876b8be0 119-C, --dryrun Check configuration for validity and exit\n\
718e3744 120-r, --retain When program terminates, retain added route by ripd.\n\
c065230a 121-u, --user User to run as\n\
122-g, --group Group to run as\n\
718e3744 123-v, --version Print program version\n\
124-h, --help Display this help and exit\n\
125\n\
b2f36157 126Report bugs to %s\n", progname, FRR_BUG_ADDRESS);
718e3744 127 }
128
129 exit (status);
130}
6b0655a2 131
718e3744 132/* SIGHUP handler. */
dc63bfd4 133static void
2d75d052 134sighup (void)
718e3744 135{
136 zlog_info ("SIGHUP received");
137 rip_clean ();
138 rip_reset ();
139 zlog_info ("ripd restarting!");
140
141 /* Reload config file. */
320ec10a 142 vty_read_config (config_file, config_default);
718e3744 143
144 /* Create VTY's socket */
145 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
146
147 /* Try to return to normal operation. */
148}
149
150/* SIGINT handler. */
dc63bfd4 151static void
2d75d052 152sigint (void)
718e3744 153{
887c44a4 154 zlog_notice ("Terminating on signal");
718e3744 155
156 if (! retain_mode)
157 rip_clean ();
158
159 exit (0);
160}
161
162/* SIGUSR1 handler. */
dc63bfd4 163static void
2d75d052 164sigusr1 (void)
718e3744 165{
166 zlog_rotate (NULL);
167}
168
dc63bfd4 169static struct quagga_signal_t ripd_signals[] =
718e3744 170{
2d75d052 171 {
172 .signal = SIGHUP,
173 .handler = &sighup,
174 },
175 {
176 .signal = SIGUSR1,
177 .handler = &sigusr1,
178 },
179 {
180 .signal = SIGINT,
8c903fbb 181 .handler = &sigint,
2d75d052 182 },
f571dab0 183 {
184 .signal = SIGTERM,
185 .handler = &sigint,
186 },
2d75d052 187};
6b0655a2 188
718e3744 189/* Main routine of ripd. */
190int
191main (int argc, char **argv)
192{
193 char *p;
194 int daemon_mode = 0;
876b8be0 195 int dryrun = 0;
718e3744 196 char *progname;
197 struct thread thread;
198
199 /* Set umask before anything for security */
200 umask (0027);
201
202 /* Get program name. */
203 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
204
205 /* First of all we need logging init. */
7c8ff89e 206 zlog_default = openzlog (progname, ZLOG_RIP, 0,
718e3744 207 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
2caa9b39 208 zprivs_init (&ripd_privs);
c05795b1
SK
209#if defined(HAVE_CUMULUS)
210 zlog_set_level (NULL, ZLOG_DEST_SYSLOG, zlog_default->default_lvl);
211#endif
718e3744 212
213 /* Command line option parse. */
214 while (1)
215 {
216 int opt;
217
b5114685 218 opt = getopt_long (argc, argv, "df:i:z:hA:P:u:g:rvC", longopts, 0);
718e3744 219
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;
b5114685
VT
239 case 'z':
240 zclient_serv_path_set (optarg);
241 break;
718e3744 242 case 'P':
4fc4e7ab 243 /* Deal with atoi() returning 0 on failure, and ripd not
244 listening on rip port... */
245 if (strcmp(optarg, "0") == 0)
246 {
247 vty_port = 0;
248 break;
249 }
250 vty_port = atoi (optarg);
0d6b2ee2
PJ
251 if (vty_port <= 0 || vty_port > 0xffff)
252 vty_port = RIP_VTY_PORT;
718e3744 253 break;
254 case 'r':
255 retain_mode = 1;
256 break;
876b8be0
PJ
257 case 'C':
258 dryrun = 1;
259 break;
c065230a 260 case 'u':
261 ripd_privs.user = optarg;
262 break;
263 case 'g':
264 ripd_privs.group = optarg;
265 break;
718e3744 266 case 'v':
267 print_version (progname);
268 exit (0);
269 break;
270 case 'h':
271 usage (progname, 0);
272 break;
273 default:
274 usage (progname, 1);
275 break;
276 }
277 }
278
279 /* Prepare master thread. */
280 master = thread_master_create ();
281
282 /* Library initialization. */
837d16cc 283 signal_init (master, array_size(ripd_signals), ripd_signals);
718e3744 284 cmd_init (1);
b21b19c5 285 vty_init (master);
718e3744 286 memory_init ();
287 keychain_init ();
6a69b354 288 vrf_init ();
718e3744 289
290 /* RIP related initialization. */
291 rip_init ();
292 rip_if_init ();
4140ca4d 293 rip_zclient_init(master);
718e3744 294 rip_peer_init ();
295
718e3744 296 /* Get configuration file. */
320ec10a 297 vty_read_config (config_file, config_default);
718e3744 298
876b8be0
PJ
299 /* Start execution only if not in dry-run mode */
300 if(dryrun)
301 return (0);
302
718e3744 303 /* Change to the daemon program. */
065de903
SH
304 if (daemon_mode && daemon (0, 0) < 0)
305 {
306 zlog_err("RIPd daemon failed: %s", strerror(errno));
307 exit (1);
308 }
718e3744 309
310 /* Pid file create. */
311 pid_output (pid_file);
312
313 /* Create VTY's socket */
314 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
315
887c44a4 316 /* Print banner. */
b2f36157 317 zlog_notice ("RIPd %s starting: vty@%d", FRR_VERSION, vty_port);
887c44a4 318
718e3744 319 /* Execute each thread. */
320 while (thread_fetch (master, &thread))
321 thread_call (&thread);
322
323 /* Not reached. */
e8e1946e 324 return (0);
718e3744 325}