]> git.proxmox.com Git - mirror_frr.git/blob - ripd/rip_main.c
2005-09-29 Paul Jakma <paul.jakma@sun.com>
[mirror_frr.git] / ripd / rip_main.c
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
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"
35
36 #include "ripd/ripd.h"
37
38 /* ripd options. */
39 static 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'},
48 { "user", required_argument, NULL, 'u'},
49 { "group", required_argument, NULL, 'g'},
50 { "version", no_argument, NULL, 'v'},
51 { 0 }
52 };
53
54 /* ripd privileges */
55 zebra_capabilities_t _caps_p [] =
56 {
57 ZCAP_NET_RAW,
58 ZCAP_BIND
59 };
60
61 struct zebra_privs_t ripd_privs =
62 {
63 #if defined(QUAGGA_USER)
64 .user = QUAGGA_USER,
65 #endif
66 #if defined QUAGGA_GROUP
67 .group = QUAGGA_GROUP,
68 #endif
69 #ifdef VTY_GROUP
70 .vty_group = VTY_GROUP,
71 #endif
72 .caps_p = _caps_p,
73 .cap_num_p = 2,
74 .cap_num_i = 0
75 };
76
77 /* Configuration file and directory. */
78 char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
79 char *config_file = NULL;
80
81 /* ripd program name */
82
83 /* Route retain mode flag. */
84 int retain_mode = 0;
85
86 /* RIP VTY bind address. */
87 char *vty_addr = NULL;
88
89 /* RIP VTY connection port. */
90 int vty_port = RIP_VTY_PORT;
91
92 /* Master of threads. */
93 struct thread_master *master;
94
95 /* Process ID saved for use by init system */
96 const char *pid_file = PATH_RIPD_PID;
97
98 /* Help information display. */
99 static void
100 usage (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\
107 Daemon 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\
114 -u, --user User to run as\n\
115 -g, --group Group to run as\n\
116 -v, --version Print program version\n\
117 -h, --help Display this help and exit\n\
118 \n\
119 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
120 }
121
122 exit (status);
123 }
124 \f
125 /* SIGHUP handler. */
126 void
127 sighup (void)
128 {
129 zlog_info ("SIGHUP received");
130 rip_clean ();
131 rip_reset ();
132 zlog_info ("ripd restarting!");
133
134 /* Reload config file. */
135 vty_read_config (config_file, config_default);
136
137 /* Create VTY's socket */
138 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
139
140 /* Try to return to normal operation. */
141 }
142
143 /* SIGINT handler. */
144 void
145 sigint (void)
146 {
147 zlog_notice ("Terminating on signal");
148
149 if (! retain_mode)
150 rip_clean ();
151
152 exit (0);
153 }
154
155 /* SIGUSR1 handler. */
156 void
157 sigusr1 (void)
158 {
159 zlog_rotate (NULL);
160 }
161
162 struct quagga_signal_t ripd_signals[] =
163 {
164 {
165 .signal = SIGHUP,
166 .handler = &sighup,
167 },
168 {
169 .signal = SIGUSR1,
170 .handler = &sigusr1,
171 },
172 {
173 .signal = SIGINT,
174 .handler = &sigint,
175 },
176 {
177 .signal = SIGTERM,
178 .handler = &sigint,
179 },
180 };
181 \f
182 /* Main routine of ripd. */
183 int
184 main (int argc, char **argv)
185 {
186 char *p;
187 int daemon_mode = 0;
188 char *progname;
189 struct thread thread;
190
191 /* Set umask before anything for security */
192 umask (0027);
193
194 /* Get program name. */
195 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
196
197 /* First of all we need logging init. */
198 zlog_default = openzlog (progname, ZLOG_RIP,
199 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
200
201 /* Command line option parse. */
202 while (1)
203 {
204 int opt;
205
206 opt = getopt_long (argc, argv, "df:i:hA:P:u:g:rv", longopts, 0);
207
208 if (opt == EOF)
209 break;
210
211 switch (opt)
212 {
213 case 0:
214 break;
215 case 'd':
216 daemon_mode = 1;
217 break;
218 case 'f':
219 config_file = optarg;
220 break;
221 case 'A':
222 vty_addr = optarg;
223 break;
224 case 'i':
225 pid_file = optarg;
226 break;
227 case 'P':
228 /* Deal with atoi() returning 0 on failure, and ripd not
229 listening on rip port... */
230 if (strcmp(optarg, "0") == 0)
231 {
232 vty_port = 0;
233 break;
234 }
235 vty_port = atoi (optarg);
236 vty_port = (vty_port ? vty_port : RIP_VTY_PORT);
237 break;
238 case 'r':
239 retain_mode = 1;
240 break;
241 case 'u':
242 ripd_privs.user = optarg;
243 break;
244 case 'g':
245 ripd_privs.group = optarg;
246 break;
247 case 'v':
248 print_version (progname);
249 exit (0);
250 break;
251 case 'h':
252 usage (progname, 0);
253 break;
254 default:
255 usage (progname, 1);
256 break;
257 }
258 }
259
260 /* Prepare master thread. */
261 master = thread_master_create ();
262
263 /* Library initialization. */
264 zprivs_init (&ripd_privs);
265 signal_init (master, Q_SIGC(ripd_signals), ripd_signals);
266 cmd_init (1);
267 vty_init (master);
268 memory_init ();
269 keychain_init ();
270
271 /* RIP related initialization. */
272 rip_init ();
273 rip_if_init ();
274 rip_zclient_init ();
275 rip_peer_init ();
276
277 /* Sort all installed commands. */
278 sort_node ();
279
280 /* Get configuration file. */
281 vty_read_config (config_file, config_default);
282
283 /* Change to the daemon program. */
284 if (daemon_mode)
285 daemon (0, 0);
286
287 /* Pid file create. */
288 pid_output (pid_file);
289
290 /* Create VTY's socket */
291 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
292
293 /* Print banner. */
294 zlog_notice ("RIPd %s starting: vty@%d", QUAGGA_VERSION, vty_port);
295
296 /* Execute each thread. */
297 while (thread_fetch (master, &thread))
298 thread_call (&thread);
299
300 /* Not reached. */
301 exit (0);
302 }