blob: 047292297cc3929051fd8c4e0f774de6c7b34441 [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"
pauledd7c242003-06-04 13:59:38 +000032#include "privs.h"
paul718e3742002-12-13 20:15:29 +000033
34#include "bgpd/bgpd.h"
35#include "bgpd/bgp_attr.h"
36#include "bgpd/bgp_mplsvpn.h"
37
38/* bgpd options, we use GNU getopt library. */
39struct option longopts[] =
40{
41 { "daemon", no_argument, NULL, 'd'},
42 { "config_file", required_argument, NULL, 'f'},
43 { "pid_file", required_argument, NULL, 'i'},
44 { "bgp_port", required_argument, NULL, 'p'},
45 { "vty_addr", required_argument, NULL, 'A'},
46 { "vty_port", required_argument, NULL, 'P'},
47 { "retain", no_argument, NULL, 'r'},
48 { "no_kernel", no_argument, NULL, 'n'},
pauledd7c242003-06-04 13:59:38 +000049 { "user", required_argument, NULL, 'u'},
paul718e3742002-12-13 20:15:29 +000050 { "version", no_argument, NULL, 'v'},
51 { "help", no_argument, NULL, 'h'},
52 { 0 }
53};
54
55/* Configuration file and directory. */
56char config_current[] = BGP_DEFAULT_CONFIG;
57char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
58
59/* Route retain mode flag. */
60int retain_mode = 0;
61
62/* Master of threads. */
63struct thread_master *master;
64
65/* Manually specified configuration file name. */
66char *config_file = NULL;
67
68/* Process ID saved for use by init system */
69char *pid_file = PATH_BGPD_PID;
70
71/* VTY port number and address. */
72int vty_port = BGP_VTY_PORT;
73char *vty_addr = NULL;
74
pauledd7c242003-06-04 13:59:38 +000075/* privileges */
76zebra_capabilities_t _caps_p [] =
77{
78 ZCAP_BIND,
79};
80
81struct zebra_privs_t bgpd_privs =
82{
pauld81fadf2003-08-14 05:32:12 +000083#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
84 .user = QUAGGA_USER,
85 .group = QUAGGA_GROUP,
86#endif
87#ifdef VTY_GROUP
88 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +000089#endif
90 .caps_p = _caps_p,
91 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
92 .cap_num_i = 0,
93};
94
paul718e3742002-12-13 20:15:29 +000095/* Help information display. */
96static void
97usage (char *progname, int status)
98{
99 if (status != 0)
100 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
101 else
102 {
103 printf ("Usage : %s [OPTION...]\n\n\
104Daemon which manages kernel routing table management and \
105redistribution between different routing protocols.\n\n\
106-d, --daemon Runs in daemon mode\n\
107-f, --config_file Set configuration file name\n\
108-i, --pid_file Set process identifier file name\n\
109-p, --bgp_port Set bgp protocol's port number\n\
110-A, --vty_addr Set vty's bind address\n\
111-P, --vty_port Set vty's port number\n\
112-r, --retain When program terminates, retain added route by bgpd.\n\
113-n, --no_kernel Do not install route to kernel.\n\
pauledd7c242003-06-04 13:59:38 +0000114-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000115-v, --version Print program version\n\
116-h, --help Display this help and exit\n\
117\n\
118Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
119 }
120
121 exit (status);
122}
123
124/* SIGHUP handler. */
125void
126sighup (int sig)
127{
128 zlog (NULL, LOG_INFO, "SIGHUP received");
129
130 /* Terminate all thread. */
131 bgp_terminate ();
132 bgp_reset ();
133 zlog_info ("bgpd restarting!");
134
135 /* Reload config file. */
136 vty_read_config (config_file, config_current, config_default);
137
138 /* Create VTY's socket */
paul4fc4e7a2003-01-22 19:47:09 +0000139 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000140
141 /* Try to return to normal operation. */
142}
143
144/* SIGINT handler. */
145void
146sigint (int sig)
147{
148 zlog (NULL, LOG_INFO, "Terminating on signal");
149
150 if (! retain_mode)
151 bgp_terminate ();
152
153 exit (0);
154}
155
156/* SIGUSR1 handler. */
157void
158sigusr1 (int sig)
159{
160 zlog_rotate (NULL);
161}
162
163/* Signale wrapper. */
164RETSIGTYPE *
165signal_set (int signo, void (*func)(int))
166{
167 int ret;
168 struct sigaction sig;
169 struct sigaction osig;
170
171 sig.sa_handler = func;
172 sigemptyset (&sig.sa_mask);
173 sig.sa_flags = 0;
174#ifdef SA_RESTART
175 sig.sa_flags |= SA_RESTART;
176#endif /* SA_RESTART */
177
178 ret = sigaction (signo, &sig, &osig);
179
180 if (ret < 0)
181 return (SIG_ERR);
182 else
183 return (osig.sa_handler);
184}
185
186/* Initialization of signal handles. */
187void
188signal_init ()
189{
190 signal_set (SIGHUP, sighup);
191 signal_set (SIGINT, sigint);
192 signal_set (SIGTERM, sigint);
193 signal_set (SIGPIPE, SIG_IGN);
194 signal_set (SIGUSR1, sigusr1);
195}
196
197/* Main routine of bgpd. Treatment of argument and start bgp finite
198 state machine is handled at here. */
199int
200main (int argc, char **argv)
201{
202 char *p;
203 int opt;
204 int daemon_mode = 0;
205 char *progname;
206 struct thread thread;
207
208 /* Set umask before anything for security */
209 umask (0027);
210
211 /* Preserve name of myself. */
212 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
213
214 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP,
215 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
216
217 /* BGP master init. */
218 bgp_master_init ();
219
220 /* Command line argument treatment. */
221 while (1)
222 {
paul96735ee2003-08-10 02:51:22 +0000223 opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000224
225 if (opt == EOF)
226 break;
227
228 switch (opt)
229 {
230 case 0:
231 break;
232 case 'd':
233 daemon_mode = 1;
234 break;
235 case 'f':
236 config_file = optarg;
237 break;
238 case 'i':
239 pid_file = optarg;
240 break;
241 case 'p':
242 bm->port = atoi (optarg);
243 break;
244 case 'A':
245 vty_addr = optarg;
246 break;
247 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000248 /* Deal with atoi() returning 0 on failure, and bgpd not
249 listening on bgp port... */
250 if (strcmp(optarg, "0") == 0)
251 {
252 vty_port = 0;
253 break;
254 }
255 vty_port = atoi (optarg);
256 vty_port = (vty_port ? vty_port : BGP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000257 break;
258 case 'r':
259 retain_mode = 1;
260 break;
261 case 'n':
262 bgp_option_set (BGP_OPT_NO_FIB);
263 break;
pauledd7c242003-06-04 13:59:38 +0000264 case 'u':
265 bgpd_privs.user = bgpd_privs.group = optarg;
266 break;
paul718e3742002-12-13 20:15:29 +0000267 case 'v':
268 print_version (progname);
269 exit (0);
270 break;
271 case 'h':
272 usage (progname, 0);
273 break;
274 default:
275 usage (progname, 1);
276 break;
277 }
278 }
279
280 /* Make thread master. */
281 master = bm->master;
282
283 /* Initializations. */
284 srand (time (NULL));
285 signal_init ();
pauledd7c242003-06-04 13:59:38 +0000286 zprivs_init (&bgpd_privs);
paul718e3742002-12-13 20:15:29 +0000287 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000288 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000289 memory_init ();
290
291 /* BGP related initialization. */
292 bgp_init ();
293
294 /* Sort CLI commands. */
295 sort_node ();
296
297 /* Parse config file. */
298 vty_read_config (config_file, config_current, config_default);
299
300 /* Turn into daemon if daemon_mode is set. */
301 if (daemon_mode)
302 daemon (0, 0);
303
304 /* Process ID file creation. */
305 pid_output (pid_file);
306
307 /* Make bgp vty socket. */
308 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
309
310 /* Print banner. */
paule8f29842003-08-12 13:08:31 +0000311 zlog_info ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION,
paul718e3742002-12-13 20:15:29 +0000312 vty_port, bm->port);
313
314 /* Start finite state machine, here we go! */
315 while (thread_fetch (master, &thread))
316 thread_call (&thread);
317
318 /* Not reached. */
319 exit (0);
320}