]> git.proxmox.com Git - mirror_frr.git/blob - zebra/main.c
Initial revision
[mirror_frr.git] / zebra / main.c
1 /*
2 * zebra daemon main routine.
3 * Copyright (C) 1997, 98 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 "command.h"
28 #include "thread.h"
29 #include "filter.h"
30 #include "memory.h"
31 #include "prefix.h"
32 #include "log.h"
33
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/debug.h"
37 #include "zebra/rib.h"
38
39 /* Master of threads. */
40 struct thread_master *master;
41
42 /* process id. */
43 pid_t old_pid;
44 pid_t pid;
45
46 /* Route retain mode flag. */
47 int retain_mode = 0;
48
49 /* Don't delete kernel route. */
50 int keep_kernel_mode = 0;
51
52 /* Command line options. */
53 struct option longopts[] =
54 {
55 { "batch", no_argument, NULL, 'b'},
56 { "daemon", no_argument, NULL, 'd'},
57 { "keep_kernel", no_argument, NULL, 'k'},
58 { "log_mode", no_argument, NULL, 'l'},
59 { "config_file", required_argument, NULL, 'f'},
60 { "pid_file", required_argument, NULL, 'i'},
61 { "help", no_argument, NULL, 'h'},
62 { "vty_addr", required_argument, NULL, 'A'},
63 { "vty_port", required_argument, NULL, 'P'},
64 { "retain", no_argument, NULL, 'r'},
65 { "version", no_argument, NULL, 'v'},
66 { 0 }
67 };
68
69 /* Default configuration file path. */
70 char config_current[] = DEFAULT_CONFIG_FILE;
71 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
72
73 /* Process ID saved for use by init system */
74 char *pid_file = PATH_ZEBRA_PID;
75
76 /* Help information display. */
77 static void
78 usage (char *progname, int status)
79 {
80 if (status != 0)
81 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
82 else
83 {
84 printf ("Usage : %s [OPTION...]\n\n\
85 Daemon which manages kernel routing table management and \
86 redistribution between different routing protocols.\n\n\
87 -b, --batch Runs in batch mode\n\
88 -d, --daemon Runs in daemon mode\n\
89 -f, --config_file Set configuration file name\n\
90 -i, --pid_file Set process identifier file name\n\
91 -k, --keep_kernel Don't delete old routes which installed by zebra.\n\
92 -l, --log_mode Set verbose log mode flag\n\
93 -A, --vty_addr Set vty's bind address\n\
94 -P, --vty_port Set vty's port number\n\
95 -r, --retain When program terminates, retain added route by zebra.\n\
96 -v, --version Print program version\n\
97 -h, --help Display this help and exit\n\
98 \n\
99 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
100 }
101
102 exit (status);
103 }
104 \f
105 /* SIGHUP handler. */
106 void
107 sighup (int sig)
108 {
109 zlog_info ("SIGHUP received");
110
111 /* Reload of config file. */
112 ;
113 }
114
115 /* SIGINT handler. */
116 void
117 sigint (int sig)
118 {
119 /* Decrared in rib.c */
120 void rib_close ();
121
122 zlog_info ("Terminating on signal");
123
124 if (!retain_mode)
125 rib_close ();
126
127 exit (0);
128 }
129
130 /* SIGUSR1 handler. */
131 void
132 sigusr1 (int sig)
133 {
134 zlog_rotate (NULL);
135 }
136
137 /* Signale wrapper. */
138 RETSIGTYPE *
139 signal_set (int signo, void (*func)(int))
140 {
141 int ret;
142 struct sigaction sig;
143 struct sigaction osig;
144
145 sig.sa_handler = func;
146 sigemptyset (&sig.sa_mask);
147 sig.sa_flags = 0;
148 #ifdef SA_RESTART
149 sig.sa_flags |= SA_RESTART;
150 #endif /* SA_RESTART */
151
152 ret = sigaction (signo, &sig, &osig);
153
154 if (ret < 0)
155 return (SIG_ERR);
156 else
157 return (osig.sa_handler);
158 }
159
160 /* Initialization of signal handles. */
161 void
162 signal_init ()
163 {
164 signal_set (SIGHUP, sighup);
165 signal_set (SIGINT, sigint);
166 signal_set (SIGTERM, sigint);
167 signal_set (SIGPIPE, SIG_IGN);
168 signal_set (SIGUSR1, sigusr1);
169 }
170 \f
171 /* Main startup routine. */
172 int
173 main (int argc, char **argv)
174 {
175 char *p;
176 char *vty_addr = NULL;
177 int vty_port = 0;
178 int batch_mode = 0;
179 int daemon_mode = 0;
180 char *config_file = NULL;
181 char *progname;
182 struct thread thread;
183 void rib_weed_tables ();
184 void zebra_vty_init ();
185
186 /* Set umask before anything for security */
187 umask (0027);
188
189 /* preserve my name */
190 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
191
192 zlog_default = openzlog (progname, ZLOG_STDOUT, ZLOG_ZEBRA,
193 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
194
195 while (1)
196 {
197 int opt;
198
199 opt = getopt_long (argc, argv, "bdklf:hA:P:rv", longopts, 0);
200
201 if (opt == EOF)
202 break;
203
204 switch (opt)
205 {
206 case 0:
207 break;
208 case 'b':
209 batch_mode = 1;
210 case 'd':
211 daemon_mode = 1;
212 break;
213 case 'k':
214 keep_kernel_mode = 1;
215 break;
216 case 'l':
217 /* log_mode = 1; */
218 break;
219 case 'f':
220 config_file = optarg;
221 break;
222 case 'A':
223 vty_addr = optarg;
224 break;
225 case 'i':
226 pid_file = optarg;
227 break;
228 case 'P':
229 vty_port = atoi (optarg);
230 break;
231 case 'r':
232 retain_mode = 1;
233 break;
234 case 'v':
235 print_version (progname);
236 exit (0);
237 break;
238 case 'h':
239 usage (progname, 0);
240 break;
241 default:
242 usage (progname, 1);
243 break;
244 }
245 }
246
247 /* Make master thread emulator. */
248 master = thread_master_create ();
249
250 /* Vty related initialize. */
251 signal_init ();
252 cmd_init (1);
253 vty_init ();
254 memory_init ();
255
256 /* Zebra related initialize. */
257 zebra_init ();
258 rib_init ();
259 zebra_if_init ();
260 zebra_debug_init ();
261 zebra_vty_init ();
262 access_list_init ();
263 rtadv_init ();
264
265 /* For debug purpose. */
266 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
267
268 /* Make kernel routing socket. */
269 kernel_init ();
270 interface_list ();
271 route_read ();
272
273 /* Sort VTY commands. */
274 sort_node ();
275
276 #ifdef HAVE_SNMP
277 zebra_snmp_init ();
278 #endif /* HAVE_SNMP */
279
280 /* Clean up self inserted route. */
281 if (! keep_kernel_mode)
282 rib_sweep_route ();
283
284 /* Configuration file read*/
285 vty_read_config (config_file, config_current, config_default);
286
287 /* Clean up rib. */
288 rib_weed_tables ();
289
290 /* Exit when zebra is working in batch mode. */
291 if (batch_mode)
292 exit (0);
293
294 /* Needed for BSD routing socket. */
295 old_pid = getpid ();
296
297 /* Daemonize. */
298 if (daemon_mode)
299 daemon (0, 0);
300
301 /* Output pid of zebra. */
302 pid_output (pid_file);
303
304 /* Needed for BSD routing socket. */
305 pid = getpid ();
306
307 /* Make vty server socket. */
308 vty_serv_sock (vty_addr,
309 vty_port ? vty_port : ZEBRA_VTY_PORT, ZEBRA_VTYSH_PATH);
310
311 while (thread_fetch (master, &thread))
312 thread_call (&thread);
313
314 /* Not reached... */
315 exit (0);
316 }