blob: 5f26ce243cd3d5c20f6976f88a7145fc9df3c07e [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"
Paul Jakma7514fb72007-05-02 16:05:35 +000032#include "plist.h"
pauledd7c242003-06-04 13:59:38 +000033#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000034#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000035
36#include "zebra/rib.h"
37#include "zebra/zserv.h"
38#include "zebra/debug.h"
hasso18a6dce2004-10-03 18:18:34 +000039#include "zebra/router-id.h"
hassoca776982004-06-12 14:33:05 +000040#include "zebra/irdp.h"
paula1ac18c2005-06-28 17:17:12 +000041#include "zebra/rtadv.h"
paul718e3742002-12-13 20:15:29 +000042
paulb21b19c2003-06-15 01:28:29 +000043/* Zebra instance */
44struct zebra_t zebrad =
45{
46 .rtm_table_default = 0,
47};
paul718e3742002-12-13 20:15:29 +000048
49/* process id. */
paul718e3742002-12-13 20:15:29 +000050pid_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'},
paul718e3742002-12-13 20:15:29 +000072 { "config_file", required_argument, NULL, 'f'},
73 { "pid_file", required_argument, NULL, 'i'},
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +040074 { "socket", required_argument, NULL, 'z'},
paul718e3742002-12-13 20:15:29 +000075 { "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'},
Paul Jakma876b8be2006-10-15 23:35:57 +000079 { "dryrun", no_argument, NULL, 'C'},
hassoc34b6b52004-08-31 13:41:49 +000080#ifdef HAVE_NETLINK
hasso583d8002005-01-16 23:34:02 +000081 { "nl-bufsize", required_argument, NULL, 's'},
hassoc34b6b52004-08-31 13:41:49 +000082#endif /* HAVE_NETLINK */
pauledd7c242003-06-04 13:59:38 +000083 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000084 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000085 { "version", no_argument, NULL, 'v'},
86 { 0 }
87};
88
pauledd7c242003-06-04 13:59:38 +000089zebra_capabilities_t _caps_p [] =
90{
paulceacedb2005-09-29 14:39:32 +000091 ZCAP_NET_ADMIN,
pauledd7c242003-06-04 13:59:38 +000092 ZCAP_SYS_ADMIN,
paulceacedb2005-09-29 14:39:32 +000093 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000094};
95
96/* zebra privileges to run with */
97struct zebra_privs_t zserv_privs =
98{
pauld81fadf2003-08-14 05:32:12 +000099#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
100 .user = QUAGGA_USER,
101 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +0000102#endif
103#ifdef VTY_GROUP
104 .vty_group = VTY_GROUP,
105#endif
106 .caps_p = _caps_p,
107 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
108 .cap_num_i = 0
109};
110
paul718e3742002-12-13 20:15:29 +0000111/* Default configuration file path. */
paul718e3742002-12-13 20:15:29 +0000112char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
113
114/* Process ID saved for use by init system */
hassofce954f2004-10-07 20:29:24 +0000115const char *pid_file = PATH_ZEBRA_PID;
paul718e3742002-12-13 20:15:29 +0000116
117/* Help information display. */
118static void
119usage (char *progname, int status)
120{
121 if (status != 0)
122 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
123 else
124 {
hassoc34b6b52004-08-31 13:41:49 +0000125 printf ("Usage : %s [OPTION...]\n\n"\
126 "Daemon which manages kernel routing table management and "\
127 "redistribution between different routing protocols.\n\n"\
128 "-b, --batch Runs in batch mode\n"\
129 "-d, --daemon Runs in daemon mode\n"\
130 "-f, --config_file Set configuration file name\n"\
131 "-i, --pid_file Set process identifier file name\n"\
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400132 "-z, --socket Set path of zebra socket\n"\
hassoc34b6b52004-08-31 13:41:49 +0000133 "-k, --keep_kernel Don't delete old routes which installed by "\
134 "zebra.\n"\
Paul Jakma876b8be2006-10-15 23:35:57 +0000135 "-C, --dryrun Check configuration for validity and exit\n"\
hassoc34b6b52004-08-31 13:41:49 +0000136 "-A, --vty_addr Set vty's bind address\n"\
137 "-P, --vty_port Set vty's port number\n"\
138 "-r, --retain When program terminates, retain added route "\
139 "by zebra.\n"\
hassoc0652302004-11-25 19:33:48 +0000140 "-u, --user User to run as\n"\
141 "-g, --group Group to run as\n", progname);
hassoc34b6b52004-08-31 13:41:49 +0000142#ifdef HAVE_NETLINK
143 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
144#endif /* HAVE_NETLINK */
145 printf ("-v, --version Print program version\n"\
146 "-h, --help Display this help and exit\n"\
147 "\n"\
148 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000149 }
150
151 exit (status);
152}
153
154/* SIGHUP handler. */
paula1ac18c2005-06-28 17:17:12 +0000155static void
paul2d75d052004-01-19 21:31:15 +0000156sighup (void)
paul718e3742002-12-13 20:15:29 +0000157{
158 zlog_info ("SIGHUP received");
159
160 /* Reload of config file. */
161 ;
162}
163
164/* SIGINT handler. */
paula1ac18c2005-06-28 17:17:12 +0000165static void
paul2d75d052004-01-19 21:31:15 +0000166sigint (void)
paul718e3742002-12-13 20:15:29 +0000167{
ajs887c44a2004-12-03 16:36:46 +0000168 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000169
170 if (!retain_mode)
171 rib_close ();
hassoca776982004-06-12 14:33:05 +0000172#ifdef HAVE_IRDP
173 irdp_finish();
174#endif
paul718e3742002-12-13 20:15:29 +0000175
176 exit (0);
177}
178
179/* SIGUSR1 handler. */
paula1ac18c2005-06-28 17:17:12 +0000180static void
paul2d75d052004-01-19 21:31:15 +0000181sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000182{
183 zlog_rotate (NULL);
184}
185
paul2d75d052004-01-19 21:31:15 +0000186struct quagga_signal_t zebra_signals[] =
paul718e3742002-12-13 20:15:29 +0000187{
paul2d75d052004-01-19 21:31:15 +0000188 {
189 .signal = SIGHUP,
190 .handler = &sighup,
191 },
192 {
193 .signal = SIGUSR1,
194 .handler = &sigusr1,
195 },
196 {
197 .signal = SIGINT,
hasso8c903fb2004-03-17 20:39:18 +0000198 .handler = &sigint,
paul2d75d052004-01-19 21:31:15 +0000199 },
hassof571dab2004-03-22 08:55:25 +0000200 {
201 .signal = SIGTERM,
202 .handler = &sigint,
203 },
paul2d75d052004-01-19 21:31:15 +0000204};
paul718e3742002-12-13 20:15:29 +0000205
206/* Main startup routine. */
207int
208main (int argc, char **argv)
209{
210 char *p;
211 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000212 int vty_port = ZEBRA_VTY_PORT;
Paul Jakma876b8be2006-10-15 23:35:57 +0000213 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000214 int batch_mode = 0;
215 int daemon_mode = 0;
216 char *config_file = NULL;
217 char *progname;
218 struct thread thread;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400219 char *zserv_path = NULL;
paul718e3742002-12-13 20:15:29 +0000220
221 /* Set umask before anything for security */
222 umask (0027);
223
224 /* preserve my name */
225 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
226
ajs274a4a42004-12-07 15:39:31 +0000227 zlog_default = openzlog (progname, ZLOG_ZEBRA,
paul718e3742002-12-13 20:15:29 +0000228 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
229
230 while (1)
231 {
232 int opt;
233
hassoc34b6b52004-08-31 13:41:49 +0000234#ifdef HAVE_NETLINK
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400235 opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vs:C", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000236#else
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400237 opt = getopt_long (argc, argv, "bdkf:i:z:hA:P:ru:g:vC", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000238#endif /* HAVE_NETLINK */
paul718e3742002-12-13 20:15:29 +0000239
240 if (opt == EOF)
241 break;
242
243 switch (opt)
244 {
245 case 0:
246 break;
247 case 'b':
248 batch_mode = 1;
249 case 'd':
250 daemon_mode = 1;
251 break;
252 case 'k':
253 keep_kernel_mode = 1;
254 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000255 case 'C':
256 dryrun = 1;
257 break;
paul718e3742002-12-13 20:15:29 +0000258 case 'f':
259 config_file = optarg;
260 break;
261 case 'A':
262 vty_addr = optarg;
263 break;
264 case 'i':
265 pid_file = optarg;
266 break;
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400267 case 'z':
268 zserv_path = optarg;
269 break;
paul718e3742002-12-13 20:15:29 +0000270 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000271 /* Deal with atoi() returning 0 on failure, and zebra not
272 listening on zebra port... */
273 if (strcmp(optarg, "0") == 0)
274 {
275 vty_port = 0;
276 break;
277 }
paul718e3742002-12-13 20:15:29 +0000278 vty_port = atoi (optarg);
Paul Jakma0d6b2ee2008-05-29 18:29:16 +0000279 if (vty_port <= 0 || vty_port > 0xffff)
280 vty_port = ZEBRA_VTY_PORT;
paul718e3742002-12-13 20:15:29 +0000281 break;
282 case 'r':
283 retain_mode = 1;
284 break;
hassoc34b6b52004-08-31 13:41:49 +0000285#ifdef HAVE_NETLINK
286 case 's':
287 nl_rcvbufsize = atoi (optarg);
288 break;
289#endif /* HAVE_NETLINK */
hassoc0652302004-11-25 19:33:48 +0000290 case 'u':
291 zserv_privs.user = optarg;
292 break;
293 case 'g':
294 zserv_privs.group = optarg;
295 break;
paul718e3742002-12-13 20:15:29 +0000296 case 'v':
297 print_version (progname);
298 exit (0);
299 break;
300 case 'h':
301 usage (progname, 0);
302 break;
303 default:
304 usage (progname, 1);
305 break;
306 }
307 }
308
309 /* Make master thread emulator. */
paulb21b19c2003-06-15 01:28:29 +0000310 zebrad.master = thread_master_create ();
paul718e3742002-12-13 20:15:29 +0000311
pauledd7c242003-06-04 13:59:38 +0000312 /* privs initialise */
313 zprivs_init (&zserv_privs);
314
paul718e3742002-12-13 20:15:29 +0000315 /* Vty related initialize. */
paul2d75d052004-01-19 21:31:15 +0000316 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
paul718e3742002-12-13 20:15:29 +0000317 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000318 vty_init (zebrad.master);
paul718e3742002-12-13 20:15:29 +0000319 memory_init ();
320
321 /* Zebra related initialize. */
322 zebra_init ();
323 rib_init ();
324 zebra_if_init ();
325 zebra_debug_init ();
hasso18a6dce2004-10-03 18:18:34 +0000326 router_id_init();
paul718e3742002-12-13 20:15:29 +0000327 zebra_vty_init ();
328 access_list_init ();
Paul Jakma7514fb72007-05-02 16:05:35 +0000329 prefix_list_init ();
paul718e3742002-12-13 20:15:29 +0000330 rtadv_init ();
hassoca776982004-06-12 14:33:05 +0000331#ifdef HAVE_IRDP
332 irdp_init();
333#endif
paul718e3742002-12-13 20:15:29 +0000334
335 /* For debug purpose. */
336 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
337
338 /* Make kernel routing socket. */
339 kernel_init ();
340 interface_list ();
341 route_read ();
342
343 /* Sort VTY commands. */
344 sort_node ();
345
346#ifdef HAVE_SNMP
347 zebra_snmp_init ();
348#endif /* HAVE_SNMP */
349
Denis Ovsienko91b73512007-09-14 13:31:52 +0000350 /* Process the configuration file. Among other configuration
351 * directives we can meet those installing static routes. Such
352 * requests will not be executed immediately, but queued in
353 * zebra->ribq structure until we enter the main execution loop.
354 * The notifications from kernel will show originating PID equal
355 * to that after daemon() completes (if ever called).
356 */
hasso320ec102004-06-20 19:54:37 +0000357 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000358
Paul Jakma876b8be2006-10-15 23:35:57 +0000359 /* Don't start execution if we are in dry-run mode */
360 if (dryrun)
361 return(0);
362
paul718e3742002-12-13 20:15:29 +0000363 /* Clean up rib. */
364 rib_weed_tables ();
365
366 /* Exit when zebra is working in batch mode. */
367 if (batch_mode)
368 exit (0);
369
paul718e3742002-12-13 20:15:29 +0000370 /* Daemonize. */
Stephen Hemminger065de902009-08-07 11:13:49 -0700371 if (daemon_mode && daemon (0, 0) < 0)
372 {
373 zlog_err("Zebra daemon failed: %s", strerror(errno));
374 exit (1);
375 }
paul718e3742002-12-13 20:15:29 +0000376
377 /* Output pid of zebra. */
378 pid_output (pid_file);
379
Denis Ovsienko91b73512007-09-14 13:31:52 +0000380 /* After we have successfully acquired the pidfile, we can be sure
381 * about being the only copy of zebra process, which is submitting
382 * changes to the FIB.
383 * Clean up zebra-originated routes. The requests will be sent to OS
384 * immediately, so originating PID in notifications from kernel
385 * will be equal to the current getpid(). To know about such routes,
386 * we have to have route_read() called before.
387 */
388 if (! keep_kernel_mode)
389 rib_sweep_route ();
390
paul718e3742002-12-13 20:15:29 +0000391 /* Needed for BSD routing socket. */
392 pid = getpid ();
393
Denis Ovsienko97be79f2009-07-24 20:45:31 +0400394 /* This must be done only after locking pidfile (bug #403). */
Vyacheslav Trushkinb5114682011-11-25 18:51:48 +0400395 zebra_zserv_socket_init (zserv_path);
Denis Ovsienko97be79f2009-07-24 20:45:31 +0400396
paul718e3742002-12-13 20:15:29 +0000397 /* Make vty server socket. */
paul4fc4e7a2003-01-22 19:47:09 +0000398 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000399
ajs887c44a2004-12-03 16:36:46 +0000400 /* Print banner. */
401 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
402
paulb21b19c2003-06-15 01:28:29 +0000403 while (thread_fetch (zebrad.master, &thread))
paul718e3742002-12-13 20:15:29 +0000404 thread_call (&thread);
405
406 /* Not reached... */
paule8e19462006-01-19 20:16:55 +0000407 return 0;
paul718e3742002-12-13 20:15:29 +0000408}