blob: 44c387623647808ce5025704ea0f8cea1ef7401a [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/*
2 * RIPngd main routine.
3 * Copyright (C) 1998, 1999 Kunihiro Ishiguro
4 *
5 * This file is part of GNU Zebra.
6 *
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
10 * later version.
11 *
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
20 * 02111-1307, USA.
21 */
22
23#include <zebra.h>
24
25#include "version.h"
26#include "getopt.h"
27#include "vector.h"
28#include "vty.h"
29#include "command.h"
hassoa94434b2003-05-25 17:10:12 +000030#include "memory.h"
paul718e3742002-12-13 20:15:29 +000031#include "thread.h"
32#include "log.h"
33#include "prefix.h"
34#include "if.h"
35
36#include "ripngd/ripngd.h"
37
38/* Configuration filename and directory. */
39char config_current[] = RIPNG_DEFAULT_CONFIG;
40char config_default[] = SYSCONFDIR RIPNG_DEFAULT_CONFIG;
hassoa94434b2003-05-25 17:10:12 +000041char *config_file = NULL;
paul718e3742002-12-13 20:15:29 +000042
43/* RIPngd options. */
44struct option longopts[] =
45{
46 { "daemon", no_argument, NULL, 'd'},
47 { "config_file", required_argument, NULL, 'f'},
48 { "pid_file", required_argument, NULL, 'i'},
49 { "log_mode", no_argument, NULL, 'l'},
50 { "help", no_argument, NULL, 'h'},
51 { "vty_addr", required_argument, NULL, 'A'},
52 { "vty_port", required_argument, NULL, 'P'},
53 { "retain", no_argument, NULL, 'r'},
54 { "version", no_argument, NULL, 'v'},
55 { 0 }
56};
57
58/* RIPngd program name */
59
60/* Route retain mode flag. */
61int retain_mode = 0;
62
hassoa94434b2003-05-25 17:10:12 +000063/* RIPng VTY bind address. */
64char *vty_addr = NULL;
65
66/* RIPng VTY connection port. */
67int vty_port = RIPNG_VTY_PORT;
68
paul718e3742002-12-13 20:15:29 +000069/* Master of threads. */
70struct thread_master *master;
71
72/* Process ID saved for use by init system */
73char *pid_file = PATH_RIPNGD_PID;
74
75/* Help information display. */
76static void
77usage (char *progname, int status)
78{
79 if (status != 0)
80 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
81 else
82 {
83 printf ("Usage : %s [OPTION...]\n\
84Daemon which manages RIPng.\n\n\
85-d, --daemon Runs in daemon mode\n\
86-f, --config_file Set configuration file name\n\
87-i, --pid_file Set process identifier file name\n\
88-l. --log_mode Set verbose log mode flag\n\
89-A, --vty_addr Set vty's bind address\n\
90-P, --vty_port Set vty's port number\n\
91-r, --retain When program terminates, retain added route by ripngd.\n\
92-v, --version Print program version\n\
93-h, --help Display this help and exit\n\
94\n\
95Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
96 }
97 exit (status);
98}
99
100/* SIGHUP handler. */
101void
102sighup (int sig)
103{
hassoa94434b2003-05-25 17:10:12 +0000104 zlog_info ("SIGHUP received");
105 ripng_clean ();
106 ripng_reset ();
107 zlog_info ("Terminating on signal");
108
109 /* Reload config file. */
110 vty_read_config (config_file, config_current, config_default);
111 /* Create VTY's socket */
112 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
113
114 /* Try to return to normal operation. */
paul718e3742002-12-13 20:15:29 +0000115}
116
117/* SIGINT handler. */
118void
119sigint (int sig)
120{
hassoa94434b2003-05-25 17:10:12 +0000121 zlog_info ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000122
123 if (! retain_mode)
hassoa94434b2003-05-25 17:10:12 +0000124 ripng_clean ();
paul718e3742002-12-13 20:15:29 +0000125
126 exit (0);
127}
128
129/* SIGUSR1 handler. */
130void
131sigusr1 (int sig)
132{
133 zlog_rotate (NULL);
134}
135
136/* Signale wrapper. */
137RETSIGTYPE *
138signal_set (int signo, void (*func)(int))
139{
140 int ret;
141 struct sigaction sig;
142 struct sigaction osig;
143
144 sig.sa_handler = func;
145 sigemptyset (&sig.sa_mask);
146 sig.sa_flags = 0;
147#ifdef SA_RESTART
148 sig.sa_flags |= SA_RESTART;
149#endif /* SA_RESTART */
150
151 ret = sigaction (signo, &sig, &osig);
152
153 if (ret < 0)
154 return (SIG_ERR);
155 else
156 return (osig.sa_handler);
157}
158
159/* Initialization of signal handles. */
160void
161signal_init ()
162{
163 signal_set (SIGHUP, sighup);
164 signal_set (SIGINT, sigint);
165 signal_set (SIGTERM, sigint);
166 signal_set (SIGPIPE, SIG_IGN);
167 signal_set (SIGUSR1, sigusr1);
168}
169
170/* RIPngd main routine. */
171int
172main (int argc, char **argv)
173{
174 char *p;
paul4fc4e7a2003-01-22 19:47:09 +0000175 int vty_port = RIPNG_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000176 int daemon_mode = 0;
paul718e3742002-12-13 20:15:29 +0000177 char *progname;
178 struct thread thread;
179
180 /* Set umask before anything for security */
181 umask (0027);
182
183 /* get program name */
184 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
185
186 zlog_default = openzlog(progname, ZLOG_NOLOG, ZLOG_RIPNG,
187 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
188
189 while (1)
190 {
191 int opt;
192
193 opt = getopt_long (argc, argv, "dlf:hA:P:v", longopts, 0);
194
195 if (opt == EOF)
196 break;
197
198 switch (opt)
199 {
200 case 0:
201 break;
202 case 'd':
203 daemon_mode = 1;
204 break;
205 case 'l':
206 /* log_mode = 1; */
207 break;
208 case 'f':
209 config_file = optarg;
210 break;
211 case 'A':
212 vty_addr = optarg;
213 break;
214 case 'i':
215 pid_file = optarg;
paul4fc4e7a2003-01-22 19:47:09 +0000216 break;
paul718e3742002-12-13 20:15:29 +0000217 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000218 /* Deal with atoi() returning 0 on failure, and ripngd not
219 listening on ripngd port... */
220 if (strcmp(optarg, "0") == 0)
221 {
222 vty_port = 0;
223 break;
224 }
225 vty_port = atoi (optarg);
226 vty_port = (vty_port ? vty_port : RIPNG_VTY_PORT);
227 break;
paul718e3742002-12-13 20:15:29 +0000228 case 'r':
229 retain_mode = 1;
230 break;
231 case 'v':
232 print_version (progname);
233 exit (0);
234 break;
235 case 'h':
236 usage (progname, 0);
237 break;
238 default:
239 usage (progname, 1);
240 break;
241 }
242 }
243
244 master = thread_master_create ();
245
246 /* Library inits. */
247 signal_init ();
248 cmd_init (1);
249 vty_init ();
hassoa94434b2003-05-25 17:10:12 +0000250 memory_init ();
paul718e3742002-12-13 20:15:29 +0000251
252 /* RIPngd inits. */
253 ripng_init ();
254 zebra_init ();
hassoa94434b2003-05-25 17:10:12 +0000255 ripng_peer_init ();
256
257 /* Sort all installed commands. */
paul718e3742002-12-13 20:15:29 +0000258 sort_node ();
259
260 /* Get configuration file. */
261 vty_read_config (config_file, config_current, config_default);
262
263 /* Change to the daemon program. */
264 if (daemon_mode)
265 daemon (0, 0);
266
267 /* Create VTY socket */
paul4fc4e7a2003-01-22 19:47:09 +0000268 vty_serv_sock (vty_addr, vty_port, RIPNG_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000269
270 /* Process id file create. */
271 pid_output (pid_file);
272
273 /* Fetch next active thread. */
274 while (thread_fetch (master, &thread))
275 thread_call (&thread);
276
277 /* Not reached. */
278 exit (0);
279}