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