Merge commit 'remotes/jocke/ospf_neighbour'; commit 'remotes/jocke/snmp_crosscompile'
[jleu-quagga.git] / ripd / rip_main.c
blob0b29107d30119887982d4b2d00584d792ceed492
1 /* RIPd main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro <kunihiro@zebra.org>
4 * This file is part of GNU Zebra.
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.
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.
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.
22 #include <zebra.h>
24 #include <lib/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 #include "privs.h"
34 #include "sigevent.h"
36 #include "ripd/ripd.h"
38 /* ripd options. */
39 static struct option longopts[] =
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 { "dryrun", no_argument, NULL, 'C'},
46 { "vty_addr", required_argument, NULL, 'A'},
47 { "vty_port", required_argument, NULL, 'P'},
48 { "retain", no_argument, NULL, 'r'},
49 { "user", required_argument, NULL, 'u'},
50 { "group", required_argument, NULL, 'g'},
51 { "version", no_argument, NULL, 'v'},
52 { 0 }
55 /* ripd privileges */
56 zebra_capabilities_t _caps_p [] =
58 ZCAP_NET_RAW,
59 ZCAP_BIND
62 struct zebra_privs_t ripd_privs =
64 #if defined(QUAGGA_USER)
65 .user = QUAGGA_USER,
66 #endif
67 #if defined QUAGGA_GROUP
68 .group = QUAGGA_GROUP,
69 #endif
70 #ifdef VTY_GROUP
71 .vty_group = VTY_GROUP,
72 #endif
73 .caps_p = _caps_p,
74 .cap_num_p = 2,
75 .cap_num_i = 0
78 /* Configuration file and directory. */
79 char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
80 char *config_file = NULL;
82 /* ripd program name */
84 /* Route retain mode flag. */
85 int retain_mode = 0;
87 /* RIP VTY bind address. */
88 char *vty_addr = NULL;
90 /* RIP VTY connection port. */
91 int vty_port = RIP_VTY_PORT;
93 /* Master of threads. */
94 struct thread_master *master;
96 /* Process ID saved for use by init system */
97 const char *pid_file = PATH_RIPD_PID;
99 /* Help information display. */
100 static void
101 usage (char *progname, int status)
103 if (status != 0)
104 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
105 else
107 printf ("Usage : %s [OPTION...]\n\
108 Daemon which manages RIP version 1 and 2.\n\n\
109 -d, --daemon Runs in daemon mode\n\
110 -f, --config_file Set configuration file name\n\
111 -i, --pid_file Set process identifier file name\n\
112 -A, --vty_addr Set vty's bind address\n\
113 -P, --vty_port Set vty's port number\n\
114 -C, --dryrun Check configuration for validity and exit\n\
115 -r, --retain When program terminates, retain added route by ripd.\n\
116 -u, --user User to run as\n\
117 -g, --group Group to run as\n\
118 -v, --version Print program version\n\
119 -h, --help Display this help and exit\n\
121 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
124 exit (status);
127 /* SIGHUP handler. */
128 static void
129 sighup (void)
131 zlog_info ("SIGHUP received");
132 rip_clean ();
133 rip_reset ();
134 zlog_info ("ripd restarting!");
136 /* Reload config file. */
137 vty_read_config (config_file, config_default);
139 /* Create VTY's socket */
140 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
142 /* Try to return to normal operation. */
145 /* SIGINT handler. */
146 static void
147 sigint (void)
149 zlog_notice ("Terminating on signal");
151 if (! retain_mode)
152 rip_clean ();
154 exit (0);
157 /* SIGUSR1 handler. */
158 static void
159 sigusr1 (void)
161 zlog_rotate (NULL);
164 static struct quagga_signal_t ripd_signals[] =
167 .signal = SIGHUP,
168 .handler = &sighup,
171 .signal = SIGUSR1,
172 .handler = &sigusr1,
175 .signal = SIGINT,
176 .handler = &sigint,
179 .signal = SIGTERM,
180 .handler = &sigint,
184 /* Main routine of ripd. */
186 main (int argc, char **argv)
188 char *p;
189 int daemon_mode = 0;
190 int dryrun = 0;
191 char *progname;
192 struct thread thread;
194 /* Set umask before anything for security */
195 umask (0027);
197 /* Get program name. */
198 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
200 /* First of all we need logging init. */
201 zlog_default = openzlog (progname, ZLOG_RIP,
202 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
204 /* Command line option parse. */
205 while (1)
207 int opt;
209 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rvC", longopts, 0);
211 if (opt == EOF)
212 break;
214 switch (opt)
216 case 0:
217 break;
218 case 'd':
219 daemon_mode = 1;
220 break;
221 case 'f':
222 config_file = optarg;
223 break;
224 case 'A':
225 vty_addr = optarg;
226 break;
227 case 'i':
228 pid_file = optarg;
229 break;
230 case 'P':
231 /* Deal with atoi() returning 0 on failure, and ripd not
232 listening on rip port... */
233 if (strcmp(optarg, "0") == 0)
235 vty_port = 0;
236 break;
238 vty_port = atoi (optarg);
239 if (vty_port <= 0 || vty_port > 0xffff)
240 vty_port = RIP_VTY_PORT;
241 break;
242 case 'r':
243 retain_mode = 1;
244 break;
245 case 'C':
246 dryrun = 1;
247 break;
248 case 'u':
249 ripd_privs.user = optarg;
250 break;
251 case 'g':
252 ripd_privs.group = optarg;
253 break;
254 case 'v':
255 print_version (progname);
256 exit (0);
257 break;
258 case 'h':
259 usage (progname, 0);
260 break;
261 default:
262 usage (progname, 1);
263 break;
267 /* Prepare master thread. */
268 master = thread_master_create ();
270 /* Library initialization. */
271 zprivs_init (&ripd_privs);
272 signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
273 cmd_init (1);
274 vty_init (master);
275 memory_init ();
276 keychain_init ();
278 /* RIP related initialization. */
279 rip_init ();
280 rip_if_init ();
281 rip_zclient_init ();
282 rip_peer_init ();
284 /* Sort all installed commands. */
285 sort_node ();
287 /* Get configuration file. */
288 vty_read_config (config_file, config_default);
290 /* Start execution only if not in dry-run mode */
291 if(dryrun)
292 return (0);
294 /* Change to the daemon program. */
295 if (daemon_mode)
296 daemon (0, 0);
298 /* Pid file create. */
299 pid_output (pid_file);
301 /* Create VTY's socket */
302 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
304 /* Print banner. */
305 zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
307 /* Execute each thread. */
308 while (thread_fetch (master, &thread))
309 thread_call (&thread);
311 /* Not reached. */
312 return (0);