1 /* zebra daemon main routine.
2 * Copyright (C) 1997, 98 Kunihiro Ishiguro
4 * This file is part of GNU Zebra.
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
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.
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
24 #include <lib/version.h>
30 #include "zebra_memory.h"
31 #include "memory_vty.h"
39 #include "zebra/rib.h"
40 #include "zebra/zserv.h"
41 #include "zebra/debug.h"
42 #include "zebra/router-id.h"
43 #include "zebra/irdp.h"
44 #include "zebra/rtadv.h"
45 #include "zebra/zebra_fpm.h"
46 #include "zebra/zebra_ptm.h"
47 #include "zebra/zebra_ns.h"
48 #include "zebra/redistribute.h"
49 #include "zebra/zebra_mpls.h"
51 #define ZEBRA_PTM_SUPPORT
54 struct zebra_t zebrad
=
56 .rtm_table_default
= 0,
62 /* Pacify zclient.o in libzebra, which expects this variable. */
63 struct thread_master
*master
;
65 /* Route retain mode flag. */
68 /* Allow non-quagga entities to delete quagga routes */
71 /* Don't delete kernel route. */
72 int keep_kernel_mode
= 0;
75 /* Receive buffer size for netlink socket */
76 u_int32_t nl_rcvbufsize
= 4194304;
77 #endif /* HAVE_NETLINK */
79 /* Command line options. */
80 struct option longopts
[] =
82 { "batch", no_argument
, NULL
, 'b'},
83 { "daemon", no_argument
, NULL
, 'd'},
84 { "allow_delete", no_argument
, NULL
, 'a'},
85 { "keep_kernel", no_argument
, NULL
, 'k'},
86 { "config_file", required_argument
, NULL
, 'f'},
87 { "pid_file", required_argument
, NULL
, 'i'},
88 { "socket", required_argument
, NULL
, 'z'},
89 { "help", no_argument
, NULL
, 'h'},
90 { "vty_addr", required_argument
, NULL
, 'A'},
91 { "vty_port", required_argument
, NULL
, 'P'},
92 { "retain", no_argument
, NULL
, 'r'},
93 { "dryrun", no_argument
, NULL
, 'C'},
95 { "nl-bufsize", required_argument
, NULL
, 's'},
96 #endif /* HAVE_NETLINK */
97 { "user", required_argument
, NULL
, 'u'},
98 { "group", required_argument
, NULL
, 'g'},
99 { "version", no_argument
, NULL
, 'v'},
103 zebra_capabilities_t _caps_p
[] =
110 /* zebra privileges to run with */
111 struct zebra_privs_t zserv_privs
=
113 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
115 .group
= QUAGGA_GROUP
,
118 .vty_group
= VTY_GROUP
,
121 .cap_num_p
= array_size(_caps_p
),
125 /* Default configuration file path. */
126 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
128 /* Process ID saved for use by init system */
129 const char *pid_file
= PATH_ZEBRA_PID
;
131 /* Help information display. */
133 usage (char *progname
, int status
)
136 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
139 printf ("Usage : %s [OPTION...]\n\n"\
140 "Daemon which manages kernel routing table management and "\
141 "redistribution between different routing protocols.\n\n"\
142 "-b, --batch Runs in batch mode\n"\
143 "-d, --daemon Runs in daemon mode\n"\
144 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
145 "-f, --config_file Set configuration file name\n"\
146 "-i, --pid_file Set process identifier file name\n"\
147 "-z, --socket Set path of zebra socket\n"\
148 "-k, --keep_kernel Don't delete old routes which installed by "\
150 "-C, --dryrun Check configuration for validity and exit\n"\
151 "-A, --vty_addr Set vty's bind address\n"\
152 "-P, --vty_port Set vty's port number\n"\
153 "-r, --retain When program terminates, retain added route "\
155 "-u, --user User to run as\n"\
156 "-g, --group Group to run as\n", progname
);
158 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
159 #endif /* HAVE_NETLINK */
160 printf ("-v, --version Print program version\n"\
161 "-h, --help Display this help and exit\n"\
163 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS
);
169 /* SIGHUP handler. */
173 zlog_info ("SIGHUP received");
175 /* Reload of config file. */
179 /* SIGINT handler. */
183 struct zebra_ns
*zns
;
185 zlog_notice ("Terminating on signal");
195 zns
= zebra_ns_lookup (NS_DEFAULT
);
196 zebra_ns_disable (0, (void **)&zns
);
200 /* SIGUSR1 handler. */
207 struct quagga_signal_t zebra_signals
[] =
227 /* Main startup routine. */
229 main (int argc
, char **argv
)
232 char *vty_addr
= NULL
;
233 int vty_port
= ZEBRA_VTY_PORT
;
237 char *config_file
= NULL
;
239 struct thread thread
;
240 char *zserv_path
= NULL
;
242 /* Set umask before anything for security */
245 /* preserve my name */
246 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
248 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
249 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
250 zprivs_init (&zserv_privs
);
251 #if defined(HAVE_CUMULUS)
252 zlog_set_level (NULL
, ZLOG_DEST_SYSLOG
, zlog_default
->default_lvl
);
260 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vs:C", longopts
, 0);
262 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vC", longopts
, 0);
263 #endif /* HAVE_NETLINK */
281 keep_kernel_mode
= 1;
287 config_file
= optarg
;
299 /* Deal with atoi() returning 0 on failure, and zebra not
300 listening on zebra port... */
301 if (strcmp(optarg
, "0") == 0)
306 vty_port
= atoi (optarg
);
307 if (vty_port
<= 0 || vty_port
> 0xffff)
308 vty_port
= ZEBRA_VTY_PORT
;
315 nl_rcvbufsize
= atoi (optarg
);
317 #endif /* HAVE_NETLINK */
319 zserv_privs
.user
= optarg
;
322 zserv_privs
.group
= optarg
;
325 print_version (progname
);
337 /* Make master thread emulator. */
338 zebrad
.master
= thread_master_create ();
340 /* Vty related initialize. */
341 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
343 vty_init (zebrad
.master
);
346 /* Zebra related initialize. */
351 router_id_cmd_init ();
353 #if defined(HAVE_MPLS)
354 zebra_mpls_vty_init ();
358 #if defined (HAVE_RTADV)
365 #ifdef ZEBRA_PTM_SUPPORT
371 /* For debug purpose. */
372 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
374 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
379 #endif /* HAVE_SNMP */
382 zfpm_init (zebrad
.master
, 1, 0);
384 zfpm_init (zebrad
.master
, 0, 0);
387 /* Process the configuration file. Among other configuration
388 * directives we can meet those installing static routes. Such
389 * requests will not be executed immediately, but queued in
390 * zebra->ribq structure until we enter the main execution loop.
391 * The notifications from kernel will show originating PID equal
392 * to that after daemon() completes (if ever called).
394 vty_read_config (config_file
, config_default
);
396 /* Don't start execution if we are in dry-run mode */
401 /* rib_weed_tables (); */
403 /* Exit when zebra is working in batch mode. */
408 if (daemon_mode
&& daemon (0, 0) < 0)
410 zlog_err("Zebra daemon failed: %s", strerror(errno
));
414 /* Output pid of zebra. */
415 pid_output (pid_file
);
417 /* After we have successfully acquired the pidfile, we can be sure
418 * about being the only copy of zebra process, which is submitting
419 * changes to the FIB.
420 * Clean up zebra-originated routes. The requests will be sent to OS
421 * immediately, so originating PID in notifications from kernel
422 * will be equal to the current getpid(). To know about such routes,
423 * we have to have route_read() called before.
425 if (! keep_kernel_mode
)
428 /* Needed for BSD routing socket. */
431 /* This must be done only after locking pidfile (bug #403). */
432 zebra_zserv_socket_init (zserv_path
);
434 /* Make vty server socket. */
435 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
438 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
440 while (thread_fetch (zebrad
.master
, &thread
))
441 thread_call (&thread
);