blob: dbe1b537df8c2aaeb3376867d53ae08551dbbb24 [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
gdt5e4fa162004-03-16 14:38:36 +000024#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000025#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"
paul2d75d052004-01-19 21:31:15 +000033#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000034
35#include "zebra/rib.h"
36#include "zebra/zserv.h"
37#include "zebra/debug.h"
hasso18a6dce2004-10-03 18:18:34 +000038#include "zebra/router-id.h"
hassoca776982004-06-12 14:33:05 +000039#include "zebra/irdp.h"
paula1ac18c2005-06-28 17:17:12 +000040#include "zebra/rtadv.h"
paul718e3742002-12-13 20:15:29 +000041
paulb21b19c2003-06-15 01:28:29 +000042/* Zebra instance */
43struct zebra_t zebrad =
44{
45 .rtm_table_default = 0,
46};
paul718e3742002-12-13 20:15:29 +000047
48/* process id. */
49pid_t old_pid;
50pid_t pid;
51
gdt87efd642004-06-30 17:36:11 +000052/* Pacify zclient.o in libzebra, which expects this variable. */
53struct thread_master *master;
54
paul718e3742002-12-13 20:15:29 +000055/* Route retain mode flag. */
56int retain_mode = 0;
57
58/* Don't delete kernel route. */
59int keep_kernel_mode = 0;
60
hassoc34b6b52004-08-31 13:41:49 +000061#ifdef HAVE_NETLINK
62/* Receive buffer size for netlink socket */
63u_int32_t nl_rcvbufsize = 0;
64#endif /* HAVE_NETLINK */
65
paul718e3742002-12-13 20:15:29 +000066/* Command line options. */
67struct option longopts[] =
68{
69 { "batch", no_argument, NULL, 'b'},
70 { "daemon", no_argument, NULL, 'd'},
71 { "keep_kernel", no_argument, NULL, 'k'},
72 { "log_mode", no_argument, NULL, 'l'},
73 { "config_file", required_argument, NULL, 'f'},
74 { "pid_file", required_argument, NULL, 'i'},
75 { "help", no_argument, NULL, 'h'},
76 { "vty_addr", required_argument, NULL, 'A'},
77 { "vty_port", required_argument, NULL, 'P'},
78 { "retain", no_argument, NULL, 'r'},
hassoc34b6b52004-08-31 13:41:49 +000079#ifdef HAVE_NETLINK
hasso583d8002005-01-16 23:34:02 +000080 { "nl-bufsize", required_argument, NULL, 's'},
hassoc34b6b52004-08-31 13:41:49 +000081#endif /* HAVE_NETLINK */
pauledd7c242003-06-04 13:59:38 +000082 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000083 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000084 { "version", no_argument, NULL, 'v'},
85 { 0 }
86};
87
pauledd7c242003-06-04 13:59:38 +000088zebra_capabilities_t _caps_p [] =
89{
paulceacedb2005-09-29 14:39:32 +000090 ZCAP_NET_ADMIN,
pauledd7c242003-06-04 13:59:38 +000091 ZCAP_SYS_ADMIN,
paulceacedb2005-09-29 14:39:32 +000092 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000093};
94
95/* zebra privileges to run with */
96struct zebra_privs_t zserv_privs =
97{
pauld81fadf2003-08-14 05:32:12 +000098#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
99 .user = QUAGGA_USER,
100 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +0000101#endif
102#ifdef VTY_GROUP
103 .vty_group = VTY_GROUP,
104#endif
105 .caps_p = _caps_p,
106 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
107 .cap_num_i = 0
108};
109
paul718e3742002-12-13 20:15:29 +0000110/* Default configuration file path. */
paul718e3742002-12-13 20:15:29 +0000111char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
112
113/* Process ID saved for use by init system */
hassofce954f2004-10-07 20:29:24 +0000114const char *pid_file = PATH_ZEBRA_PID;
paul718e3742002-12-13 20:15:29 +0000115
116/* Help information display. */
117static void
118usage (char *progname, int status)
119{
120 if (status != 0)
121 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
122 else
123 {
hassoc34b6b52004-08-31 13:41:49 +0000124 printf ("Usage : %s [OPTION...]\n\n"\
125 "Daemon which manages kernel routing table management and "\
126 "redistribution between different routing protocols.\n\n"\
127 "-b, --batch Runs in batch mode\n"\
128 "-d, --daemon Runs in daemon mode\n"\
129 "-f, --config_file Set configuration file name\n"\
130 "-i, --pid_file Set process identifier file name\n"\
131 "-k, --keep_kernel Don't delete old routes which installed by "\
132 "zebra.\n"\
133 "-l, --log_mode Set verbose log mode flag\n"\
134 "-A, --vty_addr Set vty's bind address\n"\
135 "-P, --vty_port Set vty's port number\n"\
136 "-r, --retain When program terminates, retain added route "\
137 "by zebra.\n"\
hassoc0652302004-11-25 19:33:48 +0000138 "-u, --user User to run as\n"\
139 "-g, --group Group to run as\n", progname);
hassoc34b6b52004-08-31 13:41:49 +0000140#ifdef HAVE_NETLINK
141 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
142#endif /* HAVE_NETLINK */
143 printf ("-v, --version Print program version\n"\
144 "-h, --help Display this help and exit\n"\
145 "\n"\
146 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000147 }
148
149 exit (status);
150}
151
152/* SIGHUP handler. */
paula1ac18c2005-06-28 17:17:12 +0000153static void
paul2d75d052004-01-19 21:31:15 +0000154sighup (void)
paul718e3742002-12-13 20:15:29 +0000155{
156 zlog_info ("SIGHUP received");
157
158 /* Reload of config file. */
159 ;
160}
161
162/* SIGINT handler. */
paula1ac18c2005-06-28 17:17:12 +0000163static void
paul2d75d052004-01-19 21:31:15 +0000164sigint (void)
paul718e3742002-12-13 20:15:29 +0000165{
ajs887c44a2004-12-03 16:36:46 +0000166 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000167
168 if (!retain_mode)
169 rib_close ();
hassoca776982004-06-12 14:33:05 +0000170#ifdef HAVE_IRDP
171 irdp_finish();
172#endif
paul718e3742002-12-13 20:15:29 +0000173
174 exit (0);
175}
176
177/* SIGUSR1 handler. */
paula1ac18c2005-06-28 17:17:12 +0000178static void
paul2d75d052004-01-19 21:31:15 +0000179sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000180{
181 zlog_rotate (NULL);
182}
183
paul2d75d052004-01-19 21:31:15 +0000184struct quagga_signal_t zebra_signals[] =
paul718e3742002-12-13 20:15:29 +0000185{
paul2d75d052004-01-19 21:31:15 +0000186 {
187 .signal = SIGHUP,
188 .handler = &sighup,
189 },
190 {
191 .signal = SIGUSR1,
192 .handler = &sigusr1,
193 },
194 {
195 .signal = SIGINT,
hasso8c903fb2004-03-17 20:39:18 +0000196 .handler = &sigint,
paul2d75d052004-01-19 21:31:15 +0000197 },
hassof571dab2004-03-22 08:55:25 +0000198 {
199 .signal = SIGTERM,
200 .handler = &sigint,
201 },
paul2d75d052004-01-19 21:31:15 +0000202};
paul718e3742002-12-13 20:15:29 +0000203
204/* Main startup routine. */
205int
206main (int argc, char **argv)
207{
208 char *p;
209 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000210 int vty_port = ZEBRA_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000211 int batch_mode = 0;
212 int daemon_mode = 0;
213 char *config_file = NULL;
214 char *progname;
215 struct thread thread;
paul718e3742002-12-13 20:15:29 +0000216
217 /* Set umask before anything for security */
218 umask (0027);
219
220 /* preserve my name */
221 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
222
ajs274a4a42004-12-07 15:39:31 +0000223 zlog_default = openzlog (progname, ZLOG_ZEBRA,
paul718e3742002-12-13 20:15:29 +0000224 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
225
226 while (1)
227 {
228 int opt;
229
hassoc34b6b52004-08-31 13:41:49 +0000230#ifdef HAVE_NETLINK
hassoc0652302004-11-25 19:33:48 +0000231 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vs:", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000232#else
hassoc0652302004-11-25 19:33:48 +0000233 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:v", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000234#endif /* HAVE_NETLINK */
paul718e3742002-12-13 20:15:29 +0000235
236 if (opt == EOF)
237 break;
238
239 switch (opt)
240 {
241 case 0:
242 break;
243 case 'b':
244 batch_mode = 1;
245 case 'd':
246 daemon_mode = 1;
247 break;
248 case 'k':
249 keep_kernel_mode = 1;
250 break;
251 case 'l':
252 /* log_mode = 1; */
253 break;
254 case 'f':
255 config_file = optarg;
256 break;
257 case 'A':
258 vty_addr = optarg;
259 break;
260 case 'i':
261 pid_file = optarg;
262 break;
263 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000264 /* Deal with atoi() returning 0 on failure, and zebra not
265 listening on zebra port... */
266 if (strcmp(optarg, "0") == 0)
267 {
268 vty_port = 0;
269 break;
270 }
paul718e3742002-12-13 20:15:29 +0000271 vty_port = atoi (optarg);
paul4fc4e7a2003-01-22 19:47:09 +0000272 vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000273 break;
274 case 'r':
275 retain_mode = 1;
276 break;
hassoc34b6b52004-08-31 13:41:49 +0000277#ifdef HAVE_NETLINK
278 case 's':
279 nl_rcvbufsize = atoi (optarg);
280 break;
281#endif /* HAVE_NETLINK */
hassoc0652302004-11-25 19:33:48 +0000282 case 'u':
283 zserv_privs.user = optarg;
284 break;
285 case 'g':
286 zserv_privs.group = optarg;
287 break;
paul718e3742002-12-13 20:15:29 +0000288 case 'v':
289 print_version (progname);
290 exit (0);
291 break;
292 case 'h':
293 usage (progname, 0);
294 break;
295 default:
296 usage (progname, 1);
297 break;
298 }
299 }
300
301 /* Make master thread emulator. */
paulb21b19c2003-06-15 01:28:29 +0000302 zebrad.master = thread_master_create ();
paul718e3742002-12-13 20:15:29 +0000303
pauledd7c242003-06-04 13:59:38 +0000304 /* privs initialise */
305 zprivs_init (&zserv_privs);
306
paul718e3742002-12-13 20:15:29 +0000307 /* Vty related initialize. */
paul2d75d052004-01-19 21:31:15 +0000308 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
paul718e3742002-12-13 20:15:29 +0000309 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000310 vty_init (zebrad.master);
paul718e3742002-12-13 20:15:29 +0000311 memory_init ();
312
313 /* Zebra related initialize. */
314 zebra_init ();
315 rib_init ();
316 zebra_if_init ();
317 zebra_debug_init ();
hasso18a6dce2004-10-03 18:18:34 +0000318 router_id_init();
paul718e3742002-12-13 20:15:29 +0000319 zebra_vty_init ();
320 access_list_init ();
321 rtadv_init ();
hassoca776982004-06-12 14:33:05 +0000322#ifdef HAVE_IRDP
323 irdp_init();
324#endif
paul718e3742002-12-13 20:15:29 +0000325
326 /* For debug purpose. */
327 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
328
329 /* Make kernel routing socket. */
330 kernel_init ();
331 interface_list ();
332 route_read ();
333
334 /* Sort VTY commands. */
335 sort_node ();
336
337#ifdef HAVE_SNMP
338 zebra_snmp_init ();
339#endif /* HAVE_SNMP */
340
341 /* Clean up self inserted route. */
342 if (! keep_kernel_mode)
343 rib_sweep_route ();
344
345 /* Configuration file read*/
hasso320ec102004-06-20 19:54:37 +0000346 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000347
348 /* Clean up rib. */
349 rib_weed_tables ();
350
351 /* Exit when zebra is working in batch mode. */
352 if (batch_mode)
353 exit (0);
354
355 /* Needed for BSD routing socket. */
356 old_pid = getpid ();
357
358 /* Daemonize. */
359 if (daemon_mode)
360 daemon (0, 0);
361
362 /* Output pid of zebra. */
363 pid_output (pid_file);
364
365 /* Needed for BSD routing socket. */
366 pid = getpid ();
367
368 /* Make vty server socket. */
paul4fc4e7a2003-01-22 19:47:09 +0000369 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000370
ajs887c44a2004-12-03 16:36:46 +0000371 /* Print banner. */
372 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
373
paulb21b19c2003-06-15 01:28:29 +0000374 while (thread_fetch (zebrad.master, &thread))
paul718e3742002-12-13 20:15:29 +0000375 thread_call (&thread);
376
377 /* Not reached... */
paule8e19462006-01-19 20:16:55 +0000378 return 0;
paul718e3742002-12-13 20:15:29 +0000379}