blob: ed45bd13f7a80e73553345c8df679b62071f51ec [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'},
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"\
132 "-k, --keep_kernel Don't delete old routes which installed by "\
133 "zebra.\n"\
134 "-l, --log_mode Set verbose log mode flag\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;
paul718e3742002-12-13 20:15:29 +0000219
220 /* Set umask before anything for security */
221 umask (0027);
222
223 /* preserve my name */
224 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
225
ajs274a4a42004-12-07 15:39:31 +0000226 zlog_default = openzlog (progname, ZLOG_ZEBRA,
paul718e3742002-12-13 20:15:29 +0000227 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
228
229 while (1)
230 {
231 int opt;
232
hassoc34b6b52004-08-31 13:41:49 +0000233#ifdef HAVE_NETLINK
Paul Jakma876b8be2006-10-15 23:35:57 +0000234 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vs:C", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000235#else
Paul Jakma876b8be2006-10-15 23:35:57 +0000236 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vC", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000237#endif /* HAVE_NETLINK */
paul718e3742002-12-13 20:15:29 +0000238
239 if (opt == EOF)
240 break;
241
242 switch (opt)
243 {
244 case 0:
245 break;
246 case 'b':
247 batch_mode = 1;
248 case 'd':
249 daemon_mode = 1;
250 break;
251 case 'k':
252 keep_kernel_mode = 1;
253 break;
Paul Jakma876b8be2006-10-15 23:35:57 +0000254 case 'C':
255 dryrun = 1;
256 break;
paul718e3742002-12-13 20:15:29 +0000257 case 'l':
258 /* log_mode = 1; */
259 break;
260 case 'f':
261 config_file = optarg;
262 break;
263 case 'A':
264 vty_addr = optarg;
265 break;
266 case 'i':
267 pid_file = optarg;
268 break;
269 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000270 /* Deal with atoi() returning 0 on failure, and zebra not
271 listening on zebra port... */
272 if (strcmp(optarg, "0") == 0)
273 {
274 vty_port = 0;
275 break;
276 }
paul718e3742002-12-13 20:15:29 +0000277 vty_port = atoi (optarg);
paul4fc4e7a2003-01-22 19:47:09 +0000278 vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000279 break;
280 case 'r':
281 retain_mode = 1;
282 break;
hassoc34b6b52004-08-31 13:41:49 +0000283#ifdef HAVE_NETLINK
284 case 's':
285 nl_rcvbufsize = atoi (optarg);
286 break;
287#endif /* HAVE_NETLINK */
hassoc0652302004-11-25 19:33:48 +0000288 case 'u':
289 zserv_privs.user = optarg;
290 break;
291 case 'g':
292 zserv_privs.group = optarg;
293 break;
paul718e3742002-12-13 20:15:29 +0000294 case 'v':
295 print_version (progname);
296 exit (0);
297 break;
298 case 'h':
299 usage (progname, 0);
300 break;
301 default:
302 usage (progname, 1);
303 break;
304 }
305 }
306
307 /* Make master thread emulator. */
paulb21b19c2003-06-15 01:28:29 +0000308 zebrad.master = thread_master_create ();
paul718e3742002-12-13 20:15:29 +0000309
pauledd7c242003-06-04 13:59:38 +0000310 /* privs initialise */
311 zprivs_init (&zserv_privs);
312
paul718e3742002-12-13 20:15:29 +0000313 /* Vty related initialize. */
paul2d75d052004-01-19 21:31:15 +0000314 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
paul718e3742002-12-13 20:15:29 +0000315 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000316 vty_init (zebrad.master);
paul718e3742002-12-13 20:15:29 +0000317 memory_init ();
318
319 /* Zebra related initialize. */
320 zebra_init ();
321 rib_init ();
322 zebra_if_init ();
323 zebra_debug_init ();
hasso18a6dce2004-10-03 18:18:34 +0000324 router_id_init();
paul718e3742002-12-13 20:15:29 +0000325 zebra_vty_init ();
326 access_list_init ();
327 rtadv_init ();
hassoca776982004-06-12 14:33:05 +0000328#ifdef HAVE_IRDP
329 irdp_init();
330#endif
paul718e3742002-12-13 20:15:29 +0000331
332 /* For debug purpose. */
333 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
334
335 /* Make kernel routing socket. */
336 kernel_init ();
337 interface_list ();
338 route_read ();
339
340 /* Sort VTY commands. */
341 sort_node ();
342
343#ifdef HAVE_SNMP
344 zebra_snmp_init ();
345#endif /* HAVE_SNMP */
346
347 /* Clean up self inserted route. */
348 if (! keep_kernel_mode)
349 rib_sweep_route ();
350
351 /* Configuration file read*/
hasso320ec102004-06-20 19:54:37 +0000352 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000353
Paul Jakma876b8be2006-10-15 23:35:57 +0000354 /* Don't start execution if we are in dry-run mode */
355 if (dryrun)
356 return(0);
357
paul718e3742002-12-13 20:15:29 +0000358 /* Clean up rib. */
359 rib_weed_tables ();
360
361 /* Exit when zebra is working in batch mode. */
362 if (batch_mode)
363 exit (0);
364
365 /* Needed for BSD routing socket. */
366 old_pid = getpid ();
367
368 /* Daemonize. */
369 if (daemon_mode)
370 daemon (0, 0);
371
372 /* Output pid of zebra. */
373 pid_output (pid_file);
374
375 /* Needed for BSD routing socket. */
376 pid = getpid ();
377
378 /* Make vty server socket. */
paul4fc4e7a2003-01-22 19:47:09 +0000379 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000380
ajs887c44a2004-12-03 16:36:46 +0000381 /* Print banner. */
382 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
383
paulb21b19c2003-06-15 01:28:29 +0000384 while (thread_fetch (zebrad.master, &thread))
paul718e3742002-12-13 20:15:29 +0000385 thread_call (&thread);
386
387 /* Not reached... */
paule8e19462006-01-19 20:16:55 +0000388 return 0;
paul718e3742002-12-13 20:15:29 +0000389}