blob: 85707f42f2a90a8d4b16582ea39bf2767dd28568 [file] [log] [blame]
pauledd7c242003-06-04 13:59:38 +00001/* zebra daemon main routine.
paul718e3742002-12-13 20:15:29 +00002 * Copyright (C) 1997, 98 Kunihiro Ishiguro
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 "version.h"
25#include "getopt.h"
26#include "command.h"
27#include "thread.h"
28#include "filter.h"
29#include "memory.h"
30#include "prefix.h"
31#include "log.h"
pauledd7c242003-06-04 13:59:38 +000032#include "privs.h"
paul718e3742002-12-13 20:15:29 +000033
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. */
40struct thread_master *master;
41
42/* process id. */
43pid_t old_pid;
44pid_t pid;
45
46/* Route retain mode flag. */
47int retain_mode = 0;
48
49/* Don't delete kernel route. */
50int keep_kernel_mode = 0;
51
52/* Command line options. */
53struct 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'},
pauledd7c242003-06-04 13:59:38 +000065 { "user", required_argument, NULL, 'u'},
paul718e3742002-12-13 20:15:29 +000066 { "version", no_argument, NULL, 'v'},
67 { 0 }
68};
69
pauledd7c242003-06-04 13:59:38 +000070zebra_capabilities_t _caps_p [] =
71{
72 ZCAP_ADMIN,
73 ZCAP_SYS_ADMIN,
hasso41908812003-06-05 11:33:10 +000074 ZCAP_RAW,
pauledd7c242003-06-04 13:59:38 +000075};
76
77/* zebra privileges to run with */
78struct zebra_privs_t zserv_privs =
79{
80#if defined(ZEBRA_USER) && defined(ZEBRA_GROUP)
81 .user = ZEBRA_USER,
82 .group = ZEBRA_GROUP,
83#endif
84#ifdef VTY_GROUP
85 .vty_group = VTY_GROUP,
86#endif
87 .caps_p = _caps_p,
88 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
89 .cap_num_i = 0
90};
91
paul718e3742002-12-13 20:15:29 +000092/* Default configuration file path. */
93char config_current[] = DEFAULT_CONFIG_FILE;
94char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
95
96/* Process ID saved for use by init system */
97char *pid_file = PATH_ZEBRA_PID;
98
99/* Help information display. */
100static void
101usage (char *progname, int status)
102{
103 if (status != 0)
104 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
105 else
106 {
107 printf ("Usage : %s [OPTION...]\n\n\
108Daemon which manages kernel routing table management and \
109redistribution between different routing protocols.\n\n\
110-b, --batch Runs in batch mode\n\
111-d, --daemon Runs in daemon mode\n\
112-f, --config_file Set configuration file name\n\
113-i, --pid_file Set process identifier file name\n\
114-k, --keep_kernel Don't delete old routes which installed by zebra.\n\
115-l, --log_mode Set verbose log mode flag\n\
116-A, --vty_addr Set vty's bind address\n\
117-P, --vty_port Set vty's port number\n\
118-r, --retain When program terminates, retain added route by zebra.\n\
pauledd7c242003-06-04 13:59:38 +0000119-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000120-v, --version Print program version\n\
121-h, --help Display this help and exit\n\
122\n\
123Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
124 }
125
126 exit (status);
127}
128
129/* SIGHUP handler. */
130void
131sighup (int sig)
132{
133 zlog_info ("SIGHUP received");
134
135 /* Reload of config file. */
136 ;
137}
138
139/* SIGINT handler. */
140void
141sigint (int sig)
142{
143 /* Decrared in rib.c */
144 void rib_close ();
145
146 zlog_info ("Terminating on signal");
147
148 if (!retain_mode)
149 rib_close ();
150
151 exit (0);
152}
153
154/* SIGUSR1 handler. */
155void
156sigusr1 (int sig)
157{
158 zlog_rotate (NULL);
159}
160
161/* Signale wrapper. */
162RETSIGTYPE *
163signal_set (int signo, void (*func)(int))
164{
165 int ret;
166 struct sigaction sig;
167 struct sigaction osig;
168
169 sig.sa_handler = func;
170 sigemptyset (&sig.sa_mask);
171 sig.sa_flags = 0;
172#ifdef SA_RESTART
173 sig.sa_flags |= SA_RESTART;
174#endif /* SA_RESTART */
175
176 ret = sigaction (signo, &sig, &osig);
177
178 if (ret < 0)
179 return (SIG_ERR);
180 else
181 return (osig.sa_handler);
182}
183
184/* Initialization of signal handles. */
185void
186signal_init ()
187{
188 signal_set (SIGHUP, sighup);
189 signal_set (SIGINT, sigint);
190 signal_set (SIGTERM, sigint);
191 signal_set (SIGPIPE, SIG_IGN);
192 signal_set (SIGUSR1, sigusr1);
193}
194
195/* Main startup routine. */
196int
197main (int argc, char **argv)
198{
199 char *p;
200 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000201 int vty_port = ZEBRA_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000202 int batch_mode = 0;
203 int daemon_mode = 0;
204 char *config_file = NULL;
205 char *progname;
206 struct thread thread;
207 void rib_weed_tables ();
208 void zebra_vty_init ();
209
210 /* Set umask before anything for security */
211 umask (0027);
212
213 /* preserve my name */
214 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
215
216 zlog_default = openzlog (progname, ZLOG_STDOUT, ZLOG_ZEBRA,
217 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
218
219 while (1)
220 {
221 int opt;
222
pauledd7c242003-06-04 13:59:38 +0000223 opt = getopt_long (argc, argv, "bdklf:hA:P:ru:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000224
225 if (opt == EOF)
226 break;
227
228 switch (opt)
229 {
230 case 0:
231 break;
232 case 'b':
233 batch_mode = 1;
234 case 'd':
235 daemon_mode = 1;
236 break;
237 case 'k':
238 keep_kernel_mode = 1;
239 break;
240 case 'l':
241 /* log_mode = 1; */
242 break;
243 case 'f':
244 config_file = optarg;
245 break;
246 case 'A':
247 vty_addr = optarg;
248 break;
249 case 'i':
250 pid_file = optarg;
251 break;
252 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000253 /* Deal with atoi() returning 0 on failure, and zebra not
254 listening on zebra port... */
255 if (strcmp(optarg, "0") == 0)
256 {
257 vty_port = 0;
258 break;
259 }
paul718e3742002-12-13 20:15:29 +0000260 vty_port = atoi (optarg);
paul4fc4e7a2003-01-22 19:47:09 +0000261 vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000262 break;
263 case 'r':
264 retain_mode = 1;
265 break;
pauledd7c242003-06-04 13:59:38 +0000266 case 'u':
267 zserv_privs.user = zserv_privs.group = optarg;
268 break;
paul718e3742002-12-13 20:15:29 +0000269 case 'v':
270 print_version (progname);
271 exit (0);
272 break;
273 case 'h':
274 usage (progname, 0);
275 break;
276 default:
277 usage (progname, 1);
278 break;
279 }
280 }
281
282 /* Make master thread emulator. */
283 master = thread_master_create ();
284
pauledd7c242003-06-04 13:59:38 +0000285 /* privs initialise */
286 zprivs_init (&zserv_privs);
287
paul718e3742002-12-13 20:15:29 +0000288 /* Vty related initialize. */
289 signal_init ();
290 cmd_init (1);
291 vty_init ();
292 memory_init ();
293
294 /* Zebra related initialize. */
295 zebra_init ();
296 rib_init ();
297 zebra_if_init ();
298 zebra_debug_init ();
299 zebra_vty_init ();
300 access_list_init ();
301 rtadv_init ();
302
303 /* For debug purpose. */
304 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
305
306 /* Make kernel routing socket. */
307 kernel_init ();
308 interface_list ();
309 route_read ();
310
311 /* Sort VTY commands. */
312 sort_node ();
313
314#ifdef HAVE_SNMP
315 zebra_snmp_init ();
316#endif /* HAVE_SNMP */
317
318 /* Clean up self inserted route. */
319 if (! keep_kernel_mode)
320 rib_sweep_route ();
321
322 /* Configuration file read*/
323 vty_read_config (config_file, config_current, config_default);
324
325 /* Clean up rib. */
326 rib_weed_tables ();
327
328 /* Exit when zebra is working in batch mode. */
329 if (batch_mode)
330 exit (0);
331
332 /* Needed for BSD routing socket. */
333 old_pid = getpid ();
334
335 /* Daemonize. */
336 if (daemon_mode)
337 daemon (0, 0);
338
339 /* Output pid of zebra. */
340 pid_output (pid_file);
341
342 /* Needed for BSD routing socket. */
343 pid = getpid ();
344
345 /* Make vty server socket. */
paul4fc4e7a2003-01-22 19:47:09 +0000346 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000347
348 while (thread_fetch (master, &thread))
349 thread_call (&thread);
350
351 /* Not reached... */
352 exit (0);
353}