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>
38 #include "zebra/rib.h"
39 #include "zebra/zserv.h"
40 #include "zebra/debug.h"
41 #include "zebra/router-id.h"
42 #include "zebra/irdp.h"
43 #include "zebra/rtadv.h"
44 #include "zebra/zebra_fpm.h"
45 #include "zebra/zebra_ptm.h"
46 #include "zebra/zebra_ns.h"
47 #include "zebra/redistribute.h"
49 #define ZEBRA_PTM_SUPPORT
52 struct zebra_t zebrad
=
54 .rtm_table_default
= 0,
60 /* Pacify zclient.o in libzebra, which expects this variable. */
61 struct thread_master
*master
;
63 /* Route retain mode flag. */
66 /* Allow non-quagga entities to delete quagga routes */
69 /* Don't delete kernel route. */
70 int keep_kernel_mode
= 0;
73 /* Receive buffer size for netlink socket */
74 u_int32_t nl_rcvbufsize
= 4194304;
75 #endif /* HAVE_NETLINK */
77 /* Command line options. */
78 struct option longopts
[] =
80 { "batch", no_argument
, NULL
, 'b'},
81 { "daemon", no_argument
, NULL
, 'd'},
82 { "allow_delete", no_argument
, NULL
, 'a'},
83 { "keep_kernel", no_argument
, NULL
, 'k'},
84 { "config_file", required_argument
, NULL
, 'f'},
85 { "pid_file", required_argument
, NULL
, 'i'},
86 { "socket", required_argument
, NULL
, 'z'},
87 { "help", no_argument
, NULL
, 'h'},
88 { "vty_addr", required_argument
, NULL
, 'A'},
89 { "vty_port", required_argument
, NULL
, 'P'},
90 { "retain", no_argument
, NULL
, 'r'},
91 { "dryrun", no_argument
, NULL
, 'C'},
93 { "nl-bufsize", required_argument
, NULL
, 's'},
94 #endif /* HAVE_NETLINK */
95 { "user", required_argument
, NULL
, 'u'},
96 { "group", required_argument
, NULL
, 'g'},
97 { "version", no_argument
, NULL
, 'v'},
101 zebra_capabilities_t _caps_p
[] =
108 /* zebra privileges to run with */
109 struct zebra_privs_t zserv_privs
=
111 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
113 .group
= QUAGGA_GROUP
,
116 .vty_group
= VTY_GROUP
,
119 .cap_num_p
= array_size(_caps_p
),
123 /* Default configuration file path. */
124 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
126 /* Process ID saved for use by init system */
127 const char *pid_file
= PATH_ZEBRA_PID
;
129 /* Help information display. */
131 usage (char *progname
, int status
)
134 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
137 printf ("Usage : %s [OPTION...]\n\n"\
138 "Daemon which manages kernel routing table management and "\
139 "redistribution between different routing protocols.\n\n"\
140 "-b, --batch Runs in batch mode\n"\
141 "-d, --daemon Runs in daemon mode\n"\
142 "-a, --allow_delete Allow other processes to delete Quagga Routes\n" \
143 "-f, --config_file Set configuration file name\n"\
144 "-i, --pid_file Set process identifier file name\n"\
145 "-z, --socket Set path of zebra socket\n"\
146 "-k, --keep_kernel Don't delete old routes which installed by "\
148 "-C, --dryrun Check configuration for validity and exit\n"\
149 "-A, --vty_addr Set vty's bind address\n"\
150 "-P, --vty_port Set vty's port number\n"\
151 "-r, --retain When program terminates, retain added route "\
153 "-u, --user User to run as\n"\
154 "-g, --group Group to run as\n", progname
);
156 printf ("-s, --nl-bufsize Set netlink receive buffer size\n");
157 #endif /* HAVE_NETLINK */
158 printf ("-v, --version Print program version\n"\
159 "-h, --help Display this help and exit\n"\
161 "Report bugs to %s\n", ZEBRA_BUG_ADDRESS
);
167 /* SIGHUP handler. */
171 zlog_info ("SIGHUP received");
173 /* Reload of config file. */
177 /* SIGINT handler. */
181 struct zebra_ns
*zns
;
183 zlog_notice ("Terminating on signal");
193 zns
= zebra_ns_lookup (NS_DEFAULT
);
194 zebra_ns_disable (0, (void **)&zns
);
195 systemd_send_stopping();
199 /* SIGUSR1 handler. */
206 struct quagga_signal_t zebra_signals
[] =
226 /* Main startup routine. */
228 main (int argc
, char **argv
)
231 char *vty_addr
= NULL
;
232 int vty_port
= ZEBRA_VTY_PORT
;
236 char *config_file
= NULL
;
238 struct thread thread
;
239 char *zserv_path
= NULL
;
241 /* Set umask before anything for security */
244 /* preserve my name */
245 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
247 zlog_default
= openzlog (progname
, ZLOG_ZEBRA
, 0,
248 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
255 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vs:C", longopts
, 0);
257 opt
= getopt_long (argc
, argv
, "bdakf:i:z:hA:P:ru:g:vC", longopts
, 0);
258 #endif /* HAVE_NETLINK */
276 keep_kernel_mode
= 1;
282 config_file
= optarg
;
294 /* Deal with atoi() returning 0 on failure, and zebra not
295 listening on zebra port... */
296 if (strcmp(optarg
, "0") == 0)
301 vty_port
= atoi (optarg
);
302 if (vty_port
<= 0 || vty_port
> 0xffff)
303 vty_port
= ZEBRA_VTY_PORT
;
310 nl_rcvbufsize
= atoi (optarg
);
312 #endif /* HAVE_NETLINK */
314 zserv_privs
.user
= optarg
;
317 zserv_privs
.group
= optarg
;
320 print_version (progname
);
332 /* Make master thread emulator. */
333 zebrad
.master
= thread_master_create ();
335 /* privs initialise */
336 zprivs_init (&zserv_privs
);
338 /* Vty related initialize. */
339 signal_init (zebrad
.master
, array_size(zebra_signals
), zebra_signals
);
341 vty_init (zebrad
.master
);
344 /* Zebra related initialize. */
349 router_id_cmd_init ();
353 #if defined (HAVE_RTADV)
360 #ifdef ZEBRA_PTM_SUPPORT
364 /* For debug purpose. */
365 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
367 /* Initialize NS( and implicitly the VRF module), and make kernel routing socket. */
372 #endif /* HAVE_SNMP */
375 zfpm_init (zebrad
.master
, 1, 0);
377 zfpm_init (zebrad
.master
, 0, 0);
380 /* Process the configuration file. Among other configuration
381 * directives we can meet those installing static routes. Such
382 * requests will not be executed immediately, but queued in
383 * zebra->ribq structure until we enter the main execution loop.
384 * The notifications from kernel will show originating PID equal
385 * to that after daemon() completes (if ever called).
387 vty_read_config (config_file
, config_default
);
389 /* Don't start execution if we are in dry-run mode */
394 /* rib_weed_tables (); */
396 /* Exit when zebra is working in batch mode. */
401 if (daemon_mode
&& daemon (0, 0) < 0)
403 zlog_err("Zebra daemon failed: %s", strerror(errno
));
404 systemd_send_stopping ();
408 /* Output pid of zebra. */
409 pid_output (pid_file
);
411 systemd_send_started (zebrad
.master
);
412 /* After we have successfully acquired the pidfile, we can be sure
413 * about being the only copy of zebra process, which is submitting
414 * changes to the FIB.
415 * Clean up zebra-originated routes. The requests will be sent to OS
416 * immediately, so originating PID in notifications from kernel
417 * will be equal to the current getpid(). To know about such routes,
418 * we have to have route_read() called before.
420 if (! keep_kernel_mode
)
423 /* Needed for BSD routing socket. */
426 /* This must be done only after locking pidfile (bug #403). */
427 zebra_zserv_socket_init (zserv_path
);
429 /* Make vty server socket. */
430 vty_serv_sock (vty_addr
, vty_port
, ZEBRA_VTYSH_PATH
);
433 zlog_notice ("Zebra %s starting: vty@%d", QUAGGA_VERSION
, vty_port
);
435 while (thread_fetch (zebrad
.master
, &thread
))
436 thread_call (&thread
);