blob: 7d895799b42e4648a0157b25e1f19dfb25ec83f1 [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. */
50pid_t old_pid;
51pid_t pid;
52
gdt87efd642004-06-30 17:36:11 +000053/* Pacify zclient.o in libzebra, which expects this variable. */
54struct thread_master *master;
55
paul718e3742002-12-13 20:15:29 +000056/* Route retain mode flag. */
57int retain_mode = 0;
58
59/* Don't delete kernel route. */
60int keep_kernel_mode = 0;
61
hassoc34b6b52004-08-31 13:41:49 +000062#ifdef HAVE_NETLINK
63/* Receive buffer size for netlink socket */
64u_int32_t nl_rcvbufsize = 0;
65#endif /* HAVE_NETLINK */
66
paul718e3742002-12-13 20:15:29 +000067/* Command line options. */
68struct option longopts[] =
69{
70 { "batch", no_argument, NULL, 'b'},
71 { "daemon", no_argument, NULL, 'd'},
72 { "keep_kernel", no_argument, NULL, 'k'},
73 { "log_mode", no_argument, NULL, 'l'},
74 { "config_file", required_argument, NULL, 'f'},
75 { "pid_file", required_argument, NULL, 'i'},
76 { "help", no_argument, NULL, 'h'},
77 { "vty_addr", required_argument, NULL, 'A'},
78 { "vty_port", required_argument, NULL, 'P'},
79 { "retain", no_argument, NULL, 'r'},
Paul Jakma876b8be2006-10-15 23:35:57 +000080 { "dryrun", no_argument, NULL, 'C'},
hassoc34b6b52004-08-31 13:41:49 +000081#ifdef HAVE_NETLINK
hasso583d8002005-01-16 23:34:02 +000082 { "nl-bufsize", required_argument, NULL, 's'},
hassoc34b6b52004-08-31 13:41:49 +000083#endif /* HAVE_NETLINK */
pauledd7c242003-06-04 13:59:38 +000084 { "user", required_argument, NULL, 'u'},
hassoc0652302004-11-25 19:33:48 +000085 { "group", required_argument, NULL, 'g'},
paul718e3742002-12-13 20:15:29 +000086 { "version", no_argument, NULL, 'v'},
87 { 0 }
88};
89
pauledd7c242003-06-04 13:59:38 +000090zebra_capabilities_t _caps_p [] =
91{
paulceacedb2005-09-29 14:39:32 +000092 ZCAP_NET_ADMIN,
pauledd7c242003-06-04 13:59:38 +000093 ZCAP_SYS_ADMIN,
paulceacedb2005-09-29 14:39:32 +000094 ZCAP_NET_RAW,
pauledd7c242003-06-04 13:59:38 +000095};
96
97/* zebra privileges to run with */
98struct zebra_privs_t zserv_privs =
99{
pauld81fadf2003-08-14 05:32:12 +0000100#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
101 .user = QUAGGA_USER,
102 .group = QUAGGA_GROUP,
pauledd7c242003-06-04 13:59:38 +0000103#endif
104#ifdef VTY_GROUP
105 .vty_group = VTY_GROUP,
106#endif
107 .caps_p = _caps_p,
108 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
109 .cap_num_i = 0
110};
111
paul718e3742002-12-13 20:15:29 +0000112/* Default configuration file path. */
paul718e3742002-12-13 20:15:29 +0000113char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
114
115/* Process ID saved for use by init system */
hassofce954f2004-10-07 20:29:24 +0000116const char *pid_file = PATH_ZEBRA_PID;
paul718e3742002-12-13 20:15:29 +0000117
118/* Help information display. */
119static void
120usage (char *progname, int status)
121{
122 if (status != 0)
123 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
124 else
125 {
hassoc34b6b52004-08-31 13:41:49 +0000126 printf ("Usage : %s [OPTION...]\n\n"\
127 "Daemon which manages kernel routing table management and "\
128 "redistribution between different routing protocols.\n\n"\
129 "-b, --batch Runs in batch mode\n"\
130 "-d, --daemon Runs in daemon mode\n"\
131 "-f, --config_file Set configuration file name\n"\
132 "-i, --pid_file Set process identifier file name\n"\
133 "-k, --keep_kernel Don't delete old routes which installed by "\
134 "zebra.\n"\
135 "-l, --log_mode Set verbose log mode flag\n"\
Paul Jakma876b8be2006-10-15 23:35:57 +0000136 "-C, --dryrun Check configuration for validity and exit\n"\
hassoc34b6b52004-08-31 13:41:49 +0000137 "-A, --vty_addr Set vty's bind address\n"\
138 "-P, --vty_port Set vty's port number\n"\
139 "-r, --retain When program terminates, retain added route "\
140 "by zebra.\n"\
hassoc0652302004-11-25 19:33:48 +0000141 "-u, --user User to run as\n"\
142 "-g, --group Group to run as\n", progname);
hassoc34b6b52004-08-31 13:41:49 +0000143#ifdef HAVE_NETLINK
144 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
145#endif /* HAVE_NETLINK */
146 printf ("-v, --version Print program version\n"\
147 "-h, --help Display this help and exit\n"\
148 "\n"\
149 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS);
paul718e3742002-12-13 20:15:29 +0000150 }
151
152 exit (status);
153}
154
155/* SIGHUP handler. */
paula1ac18c2005-06-28 17:17:12 +0000156static void
paul2d75d052004-01-19 21:31:15 +0000157sighup (void)
paul718e3742002-12-13 20:15:29 +0000158{
159 zlog_info ("SIGHUP received");
160
161 /* Reload of config file. */
162 ;
163}
164
165/* SIGINT handler. */
paula1ac18c2005-06-28 17:17:12 +0000166static void
paul2d75d052004-01-19 21:31:15 +0000167sigint (void)
paul718e3742002-12-13 20:15:29 +0000168{
ajs887c44a2004-12-03 16:36:46 +0000169 zlog_notice ("Terminating on signal");
paul718e3742002-12-13 20:15:29 +0000170
171 if (!retain_mode)
172 rib_close ();
hassoca776982004-06-12 14:33:05 +0000173#ifdef HAVE_IRDP
174 irdp_finish();
175#endif
paul718e3742002-12-13 20:15:29 +0000176
177 exit (0);
178}
179
180/* SIGUSR1 handler. */
paula1ac18c2005-06-28 17:17:12 +0000181static void
paul2d75d052004-01-19 21:31:15 +0000182sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000183{
184 zlog_rotate (NULL);
185}
186
paul2d75d052004-01-19 21:31:15 +0000187struct quagga_signal_t zebra_signals[] =
paul718e3742002-12-13 20:15:29 +0000188{
paul2d75d052004-01-19 21:31:15 +0000189 {
190 .signal = SIGHUP,
191 .handler = &sighup,
192 },
193 {
194 .signal = SIGUSR1,
195 .handler = &sigusr1,
196 },
197 {
198 .signal = SIGINT,
hasso8c903fb2004-03-17 20:39:18 +0000199 .handler = &sigint,
paul2d75d052004-01-19 21:31:15 +0000200 },
hassof571dab2004-03-22 08:55:25 +0000201 {
202 .signal = SIGTERM,
203 .handler = &sigint,
204 },
paul2d75d052004-01-19 21:31:15 +0000205};
paul718e3742002-12-13 20:15:29 +0000206
207/* Main startup routine. */
208int
209main (int argc, char **argv)
210{
211 char *p;
212 char *vty_addr = NULL;
paul4fc4e7a2003-01-22 19:47:09 +0000213 int vty_port = ZEBRA_VTY_PORT;
Paul Jakma876b8be2006-10-15 23:35:57 +0000214 int dryrun = 0;
paul718e3742002-12-13 20:15:29 +0000215 int batch_mode = 0;
216 int daemon_mode = 0;
217 char *config_file = NULL;
218 char *progname;
219 struct thread thread;
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
Paul Jakma876b8be2006-10-15 23:35:57 +0000235 opt = getopt_long (argc, argv, "bdklf:i:hA:P:ru:g:vs:C", longopts, 0);
hassoc34b6b52004-08-31 13:41:49 +0000236#else
Paul Jakma876b8be2006-10-15 23:35:57 +0000237 opt = getopt_long (argc, argv, "bdklf:i: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 'l':
259 /* log_mode = 1; */
260 break;
261 case 'f':
262 config_file = optarg;
263 break;
264 case 'A':
265 vty_addr = optarg;
266 break;
267 case 'i':
268 pid_file = optarg;
269 break;
270 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);
paul4fc4e7a2003-01-22 19:47:09 +0000279 vty_port = (vty_port ? vty_port : ZEBRA_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000280 break;
281 case 'r':
282 retain_mode = 1;
283 break;
hassoc34b6b52004-08-31 13:41:49 +0000284#ifdef HAVE_NETLINK
285 case 's':
286 nl_rcvbufsize = atoi (optarg);
287 break;
288#endif /* HAVE_NETLINK */
hassoc0652302004-11-25 19:33:48 +0000289 case 'u':
290 zserv_privs.user = optarg;
291 break;
292 case 'g':
293 zserv_privs.group = optarg;
294 break;
paul718e3742002-12-13 20:15:29 +0000295 case 'v':
296 print_version (progname);
297 exit (0);
298 break;
299 case 'h':
300 usage (progname, 0);
301 break;
302 default:
303 usage (progname, 1);
304 break;
305 }
306 }
307
308 /* Make master thread emulator. */
paulb21b19c2003-06-15 01:28:29 +0000309 zebrad.master = thread_master_create ();
paul718e3742002-12-13 20:15:29 +0000310
pauledd7c242003-06-04 13:59:38 +0000311 /* privs initialise */
312 zprivs_init (&zserv_privs);
313
paul718e3742002-12-13 20:15:29 +0000314 /* Vty related initialize. */
paul2d75d052004-01-19 21:31:15 +0000315 signal_init (zebrad.master, Q_SIGC(zebra_signals), zebra_signals);
paul718e3742002-12-13 20:15:29 +0000316 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000317 vty_init (zebrad.master);
paul718e3742002-12-13 20:15:29 +0000318 memory_init ();
319
320 /* Zebra related initialize. */
321 zebra_init ();
322 rib_init ();
323 zebra_if_init ();
324 zebra_debug_init ();
hasso18a6dce2004-10-03 18:18:34 +0000325 router_id_init();
paul718e3742002-12-13 20:15:29 +0000326 zebra_vty_init ();
327 access_list_init ();
Paul Jakma7514fb72007-05-02 16:05:35 +0000328 prefix_list_init ();
paul718e3742002-12-13 20:15:29 +0000329 rtadv_init ();
hassoca776982004-06-12 14:33:05 +0000330#ifdef HAVE_IRDP
331 irdp_init();
332#endif
paul718e3742002-12-13 20:15:29 +0000333
334 /* For debug purpose. */
335 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
336
337 /* Make kernel routing socket. */
338 kernel_init ();
339 interface_list ();
340 route_read ();
341
342 /* Sort VTY commands. */
343 sort_node ();
344
345#ifdef HAVE_SNMP
346 zebra_snmp_init ();
347#endif /* HAVE_SNMP */
348
349 /* Clean up self inserted route. */
350 if (! keep_kernel_mode)
351 rib_sweep_route ();
352
353 /* Configuration file read*/
hasso320ec102004-06-20 19:54:37 +0000354 vty_read_config (config_file, config_default);
paul718e3742002-12-13 20:15:29 +0000355
Paul Jakma876b8be2006-10-15 23:35:57 +0000356 /* Don't start execution if we are in dry-run mode */
357 if (dryrun)
358 return(0);
359
paul718e3742002-12-13 20:15:29 +0000360 /* Clean up rib. */
361 rib_weed_tables ();
362
363 /* Exit when zebra is working in batch mode. */
364 if (batch_mode)
365 exit (0);
366
367 /* Needed for BSD routing socket. */
368 old_pid = getpid ();
369
370 /* Daemonize. */
371 if (daemon_mode)
372 daemon (0, 0);
373
374 /* Output pid of zebra. */
375 pid_output (pid_file);
376
377 /* Needed for BSD routing socket. */
378 pid = getpid ();
379
380 /* Make vty server socket. */
paul4fc4e7a2003-01-22 19:47:09 +0000381 vty_serv_sock (vty_addr, vty_port, ZEBRA_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000382
ajs887c44a2004-12-03 16:36:46 +0000383 /* Print banner. */
384 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION, vty_port);
385
paulb21b19c2003-06-15 01:28:29 +0000386 while (thread_fetch (zebrad.master, &thread))
paul718e3742002-12-13 20:15:29 +0000387 thread_call (&thread);
388
389 /* Not reached... */
paule8e19462006-01-19 20:16:55 +0000390 return 0;
paul718e3742002-12-13 20:15:29 +0000391}