MiniDLNA update: 1.0.19.1 to 1.0.20
[tomato.git] / release / src / router / zebra / ripd / rip_main.c
blob1070fb456aad8c9a2b3e21c6e90b4289653819bf
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 "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"
34 #include "ripd/ripd.h"
36 /* ripd options. */
37 static struct option longopts[] =
39 { "daemon", no_argument, NULL, 'd'},
40 { "config_file", required_argument, NULL, 'f'},
41 { "pid_file", required_argument, NULL, 'i'},
42 { "help", no_argument, NULL, 'h'},
43 { "vty_addr", required_argument, NULL, 'A'},
44 { "vty_port", required_argument, NULL, 'P'},
45 { "retain", no_argument, NULL, 'r'},
46 { "version", no_argument, NULL, 'v'},
47 { 0 }
50 /* Configuration file and directory. */
51 char config_current[] = RIPD_DEFAULT_CONFIG;
52 char config_default[] = SYSCONFDIR RIPD_DEFAULT_CONFIG;
53 char *config_file = NULL;
55 /* ripd program name */
57 /* Route retain mode flag. */
58 int retain_mode = 0;
60 /* RIP VTY bind address. */
61 char *vty_addr = NULL;
63 /* RIP VTY connection port. */
64 int vty_port = RIP_VTY_PORT;
66 /* Master of threads. */
67 struct thread_master *master;
69 /* Process ID saved for use by init system */
70 char *pid_file = PATH_RIPD_PID;
72 /* Help information display. */
73 static void
74 usage (char *progname, int status)
76 if (status != 0)
77 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
78 else
80 printf ("Usage : %s [OPTION...]\n\
81 Daemon which manages RIP version 1 and 2.\n\n\
82 -d, --daemon Runs in daemon mode\n\
83 -f, --config_file Set configuration file name\n\
84 -i, --pid_file Set process identifier file name\n\
85 -A, --vty_addr Set vty's bind address\n\
86 -P, --vty_port Set vty's port number\n\
87 -r, --retain When program terminates, retain added route by ripd.\n\
88 -v, --version Print program version\n\
89 -h, --help Display this help and exit\n\
90 \n\
91 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
94 exit (status);
97 /* Signale wrapper. */
98 RETSIGTYPE *
99 signal_set (int signo, void (*func)(int))
101 int ret;
102 struct sigaction sig;
103 struct sigaction osig;
105 sig.sa_handler = func;
106 sigemptyset (&sig.sa_mask);
107 sig.sa_flags = 0;
108 #ifdef SA_RESTART
109 sig.sa_flags |= SA_RESTART;
110 #endif /* SA_RESTART */
112 ret = sigaction (signo, &sig, &osig);
114 if (ret < 0)
115 return (SIG_ERR);
116 else
117 return (osig.sa_handler);
120 /* SIGHUP handler. */
121 void
122 sighup (int sig)
124 zlog_info ("SIGHUP received");
125 rip_clean ();
126 rip_reset ();
127 zlog_info ("ripd restarting!");
129 /* Reload config file. */
130 vty_read_config (config_file, config_current, config_default);
132 /* Create VTY's socket */
133 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
135 /* Try to return to normal operation. */
138 /* SIGINT handler. */
139 void
140 sigint (int sig)
142 zlog (NULL, LOG_INFO, "Terminating on signal");
144 if (! retain_mode)
145 rip_clean ();
147 exit (0);
150 /* SIGUSR1 handler. */
151 void
152 sigusr1 (int sig)
154 zlog_rotate (NULL);
157 /* Initialization of signal handles. */
158 void
159 signal_init ()
161 signal_set (SIGHUP, sighup);
162 signal_set (SIGINT, sigint);
163 signal_set (SIGTERM, sigint);
164 signal_set (SIGPIPE, SIG_IGN);
165 signal_set (SIGUSR1, sigusr1);
168 /* Main routine of ripd. */
170 main (int argc, char **argv)
172 char *p;
173 int daemon_mode = 0;
174 char *progname;
175 struct thread thread;
177 /* Set umask before anything for security */
178 umask (0027);
180 /* Get program name. */
181 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
183 /* First of all we need logging init. */
184 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_RIP,
185 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
187 /* Command line option parse. */
188 while (1)
190 int opt;
192 opt = getopt_long (argc, argv, "df:hA:P:rv", longopts, 0);
194 if (opt == EOF)
195 break;
197 switch (opt)
199 case 0:
200 break;
201 case 'd':
202 daemon_mode = 1;
203 break;
204 case 'f':
205 config_file = optarg;
206 break;
207 case 'A':
208 vty_addr = optarg;
209 break;
210 case 'i':
211 pid_file = optarg;
212 break;
213 case 'P':
214 vty_port = atoi (optarg);
215 break;
216 case 'r':
217 retain_mode = 1;
218 break;
219 case 'v':
220 print_version (progname);
221 exit (0);
222 break;
223 case 'h':
224 usage (progname, 0);
225 break;
226 default:
227 usage (progname, 1);
228 break;
232 /* Prepare master thread. */
233 master = thread_master_create ();
235 /* Library initialization. */
236 signal_init ();
237 cmd_init (1);
238 vty_init ();
239 memory_init ();
240 keychain_init ();
242 /* RIP related initialization. */
243 rip_init ();
244 rip_if_init ();
245 rip_zclient_init ();
246 rip_peer_init ();
248 /* Sort all installed commands. */
249 sort_node ();
251 /* Get configuration file. */
252 vty_read_config (config_file, config_current, config_default);
254 /* Change to the daemon program. */
255 if (daemon_mode)
256 daemon (0, 0);
258 /* Pid file create. */
259 pid_output (pid_file);
261 /* Create VTY's socket */
262 vty_serv_sock (vty_addr, vty_port, RIP_VTYSH_PATH);
264 /* Execute each thread. */
265 while (thread_fetch (master, &thread))
266 thread_call (&thread);
268 /* Not reached. */
269 exit (0);