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
34 #include "zebra/rib.h"
35 #include "zebra/zserv.h"
36 #include "zebra/debug.h"
37 #include "zebra/rib.h"
39 /* Master of threads. */
40 struct thread_master
*master
;
46 /* Route retain mode flag. */
49 /* Don't delete kernel route. */
50 int keep_kernel_mode
= 0;
52 /* Command line options. */
53 struct option longopts
[] =
55 { "batch", no_argument
, NULL
, 'b'},
56 { "daemon", no_argument
, NULL
, 'd'},
57 { "keep_kernel", no_argument
, NULL
, 'k'},
58 { "log_mode", no_argument
, NULL
, 'l'},
59 { "config_file", required_argument
, NULL
, 'f'},
60 { "pid_file", required_argument
, NULL
, 'i'},
61 { "help", no_argument
, NULL
, 'h'},
62 { "vty_addr", required_argument
, NULL
, 'A'},
63 { "vty_port", required_argument
, NULL
, 'P'},
64 { "retain", no_argument
, NULL
, 'r'},
65 { "version", no_argument
, NULL
, 'v'},
69 /* Default configuration file path. */
70 char config_current
[] = DEFAULT_CONFIG_FILE
;
71 char config_default
[] = SYSCONFDIR DEFAULT_CONFIG_FILE
;
73 /* Process ID saved for use by init system */
74 char *pid_file
= PATH_ZEBRA_PID
;
76 /* Help information display. */
78 usage (char *progname
, int status
)
81 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
84 printf ("Usage : %s [OPTION...]\n\n\
85 Daemon which manages kernel routing table management and \
86 redistribution between different routing protocols.\n\n\
87 -b, --batch Runs in batch mode\n\
88 -d, --daemon Runs in daemon mode\n\
89 -f, --config_file Set configuration file name\n\
90 -i, --pid_file Set process identifier file name\n\
91 -k, --keep_kernel Don't delete old routes which installed by zebra.\n\
92 -l, --log_mode Set verbose log mode flag\n\
93 -A, --vty_addr Set vty's bind address\n\
94 -P, --vty_port Set vty's port number\n\
95 -r, --retain When program terminates, retain added route by zebra.\n\
96 -v, --version Print program version\n\
97 -h, --help Display this help and exit\n\
99 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
105 /* SIGHUP handler. */
109 zlog_info ("SIGHUP received");
111 /* Reload of config file. */
115 /* SIGINT handler. */
119 /* Decrared in rib.c */
122 zlog_info ("Terminating on signal");
130 /* SIGUSR1 handler. */
137 /* Signale wrapper. */
139 signal_set (int signo
, void (*func
)(int))
142 struct sigaction sig
;
143 struct sigaction osig
;
145 sig
.sa_handler
= func
;
146 sigemptyset (&sig
.sa_mask
);
149 sig
.sa_flags
|= SA_RESTART
;
150 #endif /* SA_RESTART */
152 ret
= sigaction (signo
, &sig
, &osig
);
157 return (osig
.sa_handler
);
160 /* Initialization of signal handles. */
164 signal_set (SIGHUP
, sighup
);
165 signal_set (SIGINT
, sigint
);
166 signal_set (SIGTERM
, sigint
);
167 signal_set (SIGPIPE
, SIG_IGN
);
168 signal_set (SIGUSR1
, sigusr1
);
171 /* Main startup routine. */
173 main (int argc
, char **argv
)
176 char *vty_addr
= NULL
;
180 char *config_file
= NULL
;
182 struct thread thread
;
183 void rib_weed_tables ();
184 void zebra_vty_init ();
186 /* Set umask before anything for security */
189 /* preserve my name */
190 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
192 zlog_default
= openzlog (progname
, ZLOG_STDOUT
, ZLOG_ZEBRA
,
193 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
199 opt
= getopt_long (argc
, argv
, "bdklf:hA:P:rv", longopts
, 0);
214 keep_kernel_mode
= 1;
220 config_file
= optarg
;
229 vty_port
= atoi (optarg
);
235 print_version (progname
);
247 /* Make master thread emulator. */
248 master
= thread_master_create ();
250 /* Vty related initialize. */
256 /* Zebra related initialize. */
265 /* For debug purpose. */
266 /* SET_FLAG (zebra_debug_event, ZEBRA_DEBUG_EVENT); */
268 /* Make kernel routing socket. */
273 /* Sort VTY commands. */
278 #endif /* HAVE_SNMP */
280 /* Clean up self inserted route. */
281 if (! keep_kernel_mode
)
284 /* Configuration file read*/
285 vty_read_config (config_file
, config_current
, config_default
);
290 /* Exit when zebra is working in batch mode. */
294 /* Needed for BSD routing socket. */
301 /* Output pid of zebra. */
302 pid_output (pid_file
);
304 /* Needed for BSD routing socket. */
307 /* Make vty server socket. */
308 vty_serv_sock (vty_addr
,
309 vty_port
? vty_port
: ZEBRA_VTY_PORT
, ZEBRA_VTYSH_PATH
);
311 while (thread_fetch (master
, &thread
))
312 thread_call (&thread
);