blob: 703779147024831cec7e445c2f8cf7e13539351e [file] [log] [blame]
paul718e3742002-12-13 20:15:29 +00001/* Main routine of bgpd.
2 Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
3
4This file is part of GNU Zebra.
5
6GNU Zebra is free software; you can redistribute it and/or modify it
7under the terms of the GNU General Public License as published by the
8Free Software Foundation; either version 2, or (at your option) any
9later version.
10
11GNU Zebra is distributed in the hope that it will be useful, but
12WITHOUT ANY WARRANTY; without even the implied warranty of
13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14General Public License for more details.
15
16You should have received a copy of the GNU General Public License
17along with GNU Zebra; see the file COPYING. If not, write to the Free
18Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
1902111-1307, USA. */
20
21#include <zebra.h>
22
23#include "vector.h"
24#include "vty.h"
25#include "command.h"
26#include "getopt.h"
27#include "thread.h"
28#include "version.h"
29#include "memory.h"
30#include "prefix.h"
31#include "log.h"
32
33#include "bgpd/bgpd.h"
34#include "bgpd/bgp_attr.h"
35#include "bgpd/bgp_mplsvpn.h"
36
37/* bgpd options, we use GNU getopt library. */
38struct option longopts[] =
39{
40 { "daemon", no_argument, NULL, 'd'},
41 { "config_file", required_argument, NULL, 'f'},
42 { "pid_file", required_argument, NULL, 'i'},
43 { "bgp_port", required_argument, NULL, 'p'},
44 { "vty_addr", required_argument, NULL, 'A'},
45 { "vty_port", required_argument, NULL, 'P'},
46 { "retain", no_argument, NULL, 'r'},
47 { "no_kernel", no_argument, NULL, 'n'},
48 { "version", no_argument, NULL, 'v'},
49 { "help", no_argument, NULL, 'h'},
50 { 0 }
51};
52
53/* Configuration file and directory. */
54char config_current[] = BGP_DEFAULT_CONFIG;
55char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
56
57/* Route retain mode flag. */
58int retain_mode = 0;
59
60/* Master of threads. */
61struct thread_master *master;
62
63/* Manually specified configuration file name. */
64char *config_file = NULL;
65
66/* Process ID saved for use by init system */
67char *pid_file = PATH_BGPD_PID;
68
69/* VTY port number and address. */
70int vty_port = BGP_VTY_PORT;
71char *vty_addr = NULL;
72
73/* Help information display. */
74static void
75usage (char *progname, int status)
76{
77 if (status != 0)
78 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
79 else
80 {
81 printf ("Usage : %s [OPTION...]\n\n\
82Daemon which manages kernel routing table management and \
83redistribution between different routing protocols.\n\n\
84-d, --daemon Runs in daemon mode\n\
85-f, --config_file Set configuration file name\n\
86-i, --pid_file Set process identifier file name\n\
87-p, --bgp_port Set bgp protocol's port number\n\
88-A, --vty_addr Set vty's bind address\n\
89-P, --vty_port Set vty's port number\n\
90-r, --retain When program terminates, retain added route by bgpd.\n\
91-n, --no_kernel Do not install route to kernel.\n\
92-v, --version Print program version\n\
93-h, --help Display this help and exit\n\
94\n\
95Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
96 }
97
98 exit (status);
99}
100
101/* SIGHUP handler. */
102void
103sighup (int sig)
104{
105 zlog (NULL, LOG_INFO, "SIGHUP received");
106
107 /* Terminate all thread. */
108 bgp_terminate ();
109 bgp_reset ();
110 zlog_info ("bgpd restarting!");
111
112 /* Reload config file. */
113 vty_read_config (config_file, config_current, config_default);
114
115 /* Create VTY's socket */
paul4fc4e7a2003-01-22 19:47:09 +0000116 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000117
118 /* Try to return to normal operation. */
119}
120
121/* SIGINT handler. */
122void
123sigint (int sig)
124{
125 zlog (NULL, LOG_INFO, "Terminating on signal");
126
127 if (! retain_mode)
128 bgp_terminate ();
129
130 exit (0);
131}
132
133/* SIGUSR1 handler. */
134void
135sigusr1 (int sig)
136{
137 zlog_rotate (NULL);
138}
139
140/* Signale wrapper. */
141RETSIGTYPE *
142signal_set (int signo, void (*func)(int))
143{
144 int ret;
145 struct sigaction sig;
146 struct sigaction osig;
147
148 sig.sa_handler = func;
149 sigemptyset (&sig.sa_mask);
150 sig.sa_flags = 0;
151#ifdef SA_RESTART
152 sig.sa_flags |= SA_RESTART;
153#endif /* SA_RESTART */
154
155 ret = sigaction (signo, &sig, &osig);
156
157 if (ret < 0)
158 return (SIG_ERR);
159 else
160 return (osig.sa_handler);
161}
162
163/* Initialization of signal handles. */
164void
165signal_init ()
166{
167 signal_set (SIGHUP, sighup);
168 signal_set (SIGINT, sigint);
169 signal_set (SIGTERM, sigint);
170 signal_set (SIGPIPE, SIG_IGN);
171 signal_set (SIGUSR1, sigusr1);
172}
173
174/* Main routine of bgpd. Treatment of argument and start bgp finite
175 state machine is handled at here. */
176int
177main (int argc, char **argv)
178{
179 char *p;
180 int opt;
181 int daemon_mode = 0;
182 char *progname;
183 struct thread thread;
184
185 /* Set umask before anything for security */
186 umask (0027);
187
188 /* Preserve name of myself. */
189 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
190
191 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP,
192 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
193
194 /* BGP master init. */
195 bgp_master_init ();
196
197 /* Command line argument treatment. */
198 while (1)
199 {
200 opt = getopt_long (argc, argv, "df:hp:A:P:rnv", longopts, 0);
201
202 if (opt == EOF)
203 break;
204
205 switch (opt)
206 {
207 case 0:
208 break;
209 case 'd':
210 daemon_mode = 1;
211 break;
212 case 'f':
213 config_file = optarg;
214 break;
215 case 'i':
216 pid_file = optarg;
217 break;
218 case 'p':
219 bm->port = atoi (optarg);
220 break;
221 case 'A':
222 vty_addr = optarg;
223 break;
224 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000225 /* Deal with atoi() returning 0 on failure, and bgpd not
226 listening on bgp port... */
227 if (strcmp(optarg, "0") == 0)
228 {
229 vty_port = 0;
230 break;
231 }
232 vty_port = atoi (optarg);
233 vty_port = (vty_port ? vty_port : BGP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000234 break;
235 case 'r':
236 retain_mode = 1;
237 break;
238 case 'n':
239 bgp_option_set (BGP_OPT_NO_FIB);
240 break;
241 case 'v':
242 print_version (progname);
243 exit (0);
244 break;
245 case 'h':
246 usage (progname, 0);
247 break;
248 default:
249 usage (progname, 1);
250 break;
251 }
252 }
253
254 /* Make thread master. */
255 master = bm->master;
256
257 /* Initializations. */
258 srand (time (NULL));
259 signal_init ();
260 cmd_init (1);
261 vty_init ();
262 memory_init ();
263
264 /* BGP related initialization. */
265 bgp_init ();
266
267 /* Sort CLI commands. */
268 sort_node ();
269
270 /* Parse config file. */
271 vty_read_config (config_file, config_current, config_default);
272
273 /* Turn into daemon if daemon_mode is set. */
274 if (daemon_mode)
275 daemon (0, 0);
276
277 /* Process ID file creation. */
278 pid_output (pid_file);
279
280 /* Make bgp vty socket. */
281 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
282
283 /* Print banner. */
284 zlog_info ("BGPd %s starting: vty@%d, bgp@%d", ZEBRA_VERSION,
285 vty_port, bm->port);
286
287 /* Start finite state machine, here we go! */
288 while (thread_fetch (master, &thread))
289 thread_call (&thread);
290
291 /* Not reached. */
292 exit (0);
293}