2 * zebra daemon main routine.
3 * Copyright (C) 1997, 98 Kunihiro Ishiguro
5 * This file is part of GNU Zebra.
7 * GNU Zebra is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by the
9 * Free Software Foundation; either version 2, or (at your option) any
12 * GNU Zebra is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with GNU Zebra; see the file COPYING. If not, write to the Free
19 * Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
35 #include "zebra/rib.h"
36 #include "zebra/zserv.h"
38 #include "zebra/debug.h"
40 #include "zebra/rib.h"
42 /* Master of threads. */
43 struct thread_master *master;
49 /* Route retain mode flag. */
52 /* Don't delete kernel route. */
53 int keep_kernel_mode = 0;
55 /* Command line options. */
56 struct option longopts[] =
58 { "batch", no_argument, NULL, 'b'},
59 { "daemon", no_argument, NULL, 'd'},
60 { "keep_kernel", no_argument, NULL, 'k'},
61 { "log_mode", no_argument, NULL, 'l'},
62 { "config_file", required_argument, NULL, 'f'},
63 { "pid_file", required_argument, NULL, 'i'},
64 { "help", no_argument, NULL, 'h'},
65 { "vty_addr", required_argument, NULL, 'A'},
66 { "vty_port", required_argument, NULL, 'P'},
67 { "retain", no_argument, NULL, 'r'},
68 { "version", no_argument, NULL, 'v'},
72 /* Default configuration file path. */
73 char config_current[] = DEFAULT_CONFIG_FILE;
74 char config_default[] = SYSCONFDIR DEFAULT_CONFIG_FILE;
76 /* Process ID saved for use by init system */
77 char *pid_file = PATH_ZEBRA_PID;
79 /* Help information display. */
81 usage (char *progname, int status)
84 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
87 printf ("Usage : %s [OPTION...]\n\n\
88 Daemon which manages kernel routing table management and \
89 redistribution between different routing protocols.\n\n\
90 -b, --batch Runs in batch mode\n\
91 -d, --daemon Runs in daemon mode\n\
92 -f, --config_file Set configuration file name\n\
93 -i, --pid_file Set process identifier file name\n\
94 -k, --keep_kernel Don't delete old routes which installed by zebra.\n\
95 -l, --log_mode Set verbose log mode flag\n\
96 -A, --vty_addr Set vty's bind address\n\
97 -P, --vty_port Set vty's port number\n\
98 -r, --retain When program terminates, retain added route by zebra.\n\
99 -v, --version Print program version\n\
100 -h, --help Display this help and exit\n\
102 Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
109 /* SIGHUP handler. */
113 #ifdef BRCM_RIP_DEBUG
114 zlog_info ("SIGHUP received");
116 /* Reload of config file. */
120 /* SIGINT handler. */
124 /* Decrared in rib.c */
126 #ifdef BRCM_RIP_DEBUG
127 zlog_info ("Terminating on signal");
135 /* SIGUSR1 handler. */
139 #ifdef BRCM_RIP_DEBUG
144 /* Signale wrapper. */
146 signal_set (int signo, void (*func)(int))
149 struct sigaction sig;
150 struct sigaction osig;
152 sig.sa_handler = func;
153 sigemptyset (&sig.sa_mask);
156 sig.sa_flags |= SA_RESTART;
157 #endif /* SA_RESTART */
159 ret = sigaction (signo, &sig, &osig);
164 return (osig.sa_handler);
167 /* Initialization of signal handles. */
171 signal_set (SIGHUP, sighup);
172 signal_set (SIGINT, sigint);
173 signal_set (SIGTERM, sigint);
174 signal_set (SIGPIPE, SIG_IGN);
175 signal_set (SIGUSR1, sigusr1);
178 /* Main startup routine. */
181 zebra_main (int argc, char **argv)
184 main (int argc, char **argv)
188 char *vty_addr = NULL;
192 char *config_file = NULL;
194 struct thread thread;
195 void rib_weed_tables ();
197 /* Set umask before anything for security */
200 /* preserve my name */
201 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
203 #ifdef BRCM_RIP_DEBUG
204 zlog_default = openzlog (progname, ZLOG_STDOUT, ZLOG_ZEBRA,
205 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
211 opt = getopt_long (argc, argv, "bdklf:hA:P:rv", longopts, 0);
226 keep_kernel_mode = 1;
232 config_file = optarg;
241 vty_port = atoi (optarg);
247 #ifdef BRCM_CMD_SUPPORT
248 print_version (progname);
261 /* Make master thread emulator. */
262 master = thread_master_create ();
264 /* Vty related initialize. */
269 #ifdef BRCM_CMD_SUPPORT
273 /* Zebra related initialize. */
278 #ifdef BRCM_RIP_DEBUG
282 #ifdef BRCM_LIST_SUPPORT
290 /* For debug purpose. */
291 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
293 /* Make kernel routing socket. */
298 /* Sort VTY commands. */
303 #endif /* HAVE_SNMP */
305 /* Clean up self inserted route. */
306 if (! keep_kernel_mode)
309 /* Configuration file read*/
310 vty_read_config (config_file, config_current, config_default);
315 /* Exit when zebra is working in batch mode. */
319 /* Needed for BSD routing socket. */
326 /* Output pid of zebra. */
327 pid_output (pid_file);
329 /* Needed for BSD routing socket. */
332 #ifdef BRCM_CMD_SUPPORT
333 /* Make vty server socket. */
334 vty_serv_sock (vty_addr,
335 vty_port ? vty_port : ZEBRA_VTY_PORT, ZEBRA_VTYSH_PATH);
338 while (thread_fetch (master, &thread))
339 thread_call (&thread);