blob: 54086e5b2bb0d48bb4444f00362c6c4d5b6bb989 [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"
gdt5e4fa162004-03-16 14:38:36 +000028#include <lib/version.h>
paul718e3742002-12-13 20:15:29 +000029#include "memory.h"
30#include "prefix.h"
31#include "log.h"
pauledd7c242003-06-04 13:59:38 +000032#include "privs.h"
paul2d75d052004-01-19 21:31:15 +000033#include "sigevent.h"
paul718e3742002-12-13 20:15:29 +000034
35#include "bgpd/bgpd.h"
36#include "bgpd/bgp_attr.h"
37#include "bgpd/bgp_mplsvpn.h"
38
39/* bgpd options, we use GNU getopt library. */
40struct option longopts[] =
41{
42 { "daemon", no_argument, NULL, 'd'},
43 { "config_file", required_argument, NULL, 'f'},
44 { "pid_file", required_argument, NULL, 'i'},
45 { "bgp_port", required_argument, NULL, 'p'},
46 { "vty_addr", required_argument, NULL, 'A'},
47 { "vty_port", required_argument, NULL, 'P'},
48 { "retain", no_argument, NULL, 'r'},
49 { "no_kernel", no_argument, NULL, 'n'},
pauledd7c242003-06-04 13:59:38 +000050 { "user", required_argument, NULL, 'u'},
paul718e3742002-12-13 20:15:29 +000051 { "version", no_argument, NULL, 'v'},
52 { "help", no_argument, NULL, 'h'},
53 { 0 }
54};
55
paul2d75d052004-01-19 21:31:15 +000056/* signal definitions */
57void sighup (void);
58void sigint (void);
59void sigusr1 (void);
60
61struct quagga_signal_t bgp_signals[] =
62{
63 {
64 .signal = SIGHUP,
65 .handler = &sighup,
66 },
67 {
68 .signal = SIGUSR1,
69 .handler = &sigusr1,
70 },
71 {
72 .signal = SIGINT,
73 .handler = &sigint,
74 },
hassof571dab2004-03-22 08:55:25 +000075 {
76 .signal = SIGTERM,
77 .handler = &sigint,
78 },
paul2d75d052004-01-19 21:31:15 +000079};
80
paul718e3742002-12-13 20:15:29 +000081/* Configuration file and directory. */
82char config_current[] = BGP_DEFAULT_CONFIG;
83char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
84
85/* Route retain mode flag. */
86int retain_mode = 0;
87
88/* Master of threads. */
89struct thread_master *master;
90
91/* Manually specified configuration file name. */
92char *config_file = NULL;
93
94/* Process ID saved for use by init system */
95char *pid_file = PATH_BGPD_PID;
96
97/* VTY port number and address. */
98int vty_port = BGP_VTY_PORT;
99char *vty_addr = NULL;
100
pauledd7c242003-06-04 13:59:38 +0000101/* privileges */
102zebra_capabilities_t _caps_p [] =
103{
104 ZCAP_BIND,
105};
106
107struct zebra_privs_t bgpd_privs =
108{
pauld81fadf2003-08-14 05:32:12 +0000109#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
110 .user = QUAGGA_USER,
111 .group = QUAGGA_GROUP,
112#endif
113#ifdef VTY_GROUP
114 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +0000115#endif
116 .caps_p = _caps_p,
117 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
118 .cap_num_i = 0,
119};
120
paul718e3742002-12-13 20:15:29 +0000121/* Help information display. */
122static void
123usage (char *progname, int status)
124{
125 if (status != 0)
126 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
127 else
128 {
129 printf ("Usage : %s [OPTION...]\n\n\
130Daemon which manages kernel routing table management and \
131redistribution between different routing protocols.\n\n\
132-d, --daemon Runs in daemon mode\n\
133-f, --config_file Set configuration file name\n\
134-i, --pid_file Set process identifier file name\n\
135-p, --bgp_port Set bgp protocol's port number\n\
136-A, --vty_addr Set vty's bind address\n\
137-P, --vty_port Set vty's port number\n\
138-r, --retain When program terminates, retain added route by bgpd.\n\
139-n, --no_kernel Do not install route to kernel.\n\
pauledd7c242003-06-04 13:59:38 +0000140-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000141-v, --version Print program version\n\
142-h, --help Display this help and exit\n\
143\n\
144Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
145 }
146
147 exit (status);
148}
149
150/* SIGHUP handler. */
151void
paul2d75d052004-01-19 21:31:15 +0000152sighup (void)
paul718e3742002-12-13 20:15:29 +0000153{
154 zlog (NULL, LOG_INFO, "SIGHUP received");
155
156 /* Terminate all thread. */
157 bgp_terminate ();
158 bgp_reset ();
159 zlog_info ("bgpd restarting!");
160
161 /* Reload config file. */
162 vty_read_config (config_file, config_current, config_default);
163
164 /* Create VTY's socket */
paul4fc4e7a2003-01-22 19:47:09 +0000165 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000166
167 /* Try to return to normal operation. */
168}
169
170/* SIGINT handler. */
171void
paul2d75d052004-01-19 21:31:15 +0000172sigint (void)
paul718e3742002-12-13 20:15:29 +0000173{
174 zlog (NULL, LOG_INFO, "Terminating on signal");
175
176 if (! retain_mode)
177 bgp_terminate ();
178
179 exit (0);
180}
181
182/* SIGUSR1 handler. */
183void
paul2d75d052004-01-19 21:31:15 +0000184sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000185{
186 zlog_rotate (NULL);
187}
paul718e3742002-12-13 20:15:29 +0000188
189/* Main routine of bgpd. Treatment of argument and start bgp finite
190 state machine is handled at here. */
191int
192main (int argc, char **argv)
193{
194 char *p;
195 int opt;
196 int daemon_mode = 0;
197 char *progname;
198 struct thread thread;
199
200 /* Set umask before anything for security */
201 umask (0027);
202
203 /* Preserve name of myself. */
204 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
205
206 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP,
207 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
208
209 /* BGP master init. */
210 bgp_master_init ();
211
212 /* Command line argument treatment. */
213 while (1)
214 {
paul96735ee2003-08-10 02:51:22 +0000215 opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000216
217 if (opt == EOF)
218 break;
219
220 switch (opt)
221 {
222 case 0:
223 break;
224 case 'd':
225 daemon_mode = 1;
226 break;
227 case 'f':
228 config_file = optarg;
229 break;
230 case 'i':
231 pid_file = optarg;
232 break;
233 case 'p':
234 bm->port = atoi (optarg);
235 break;
236 case 'A':
237 vty_addr = optarg;
238 break;
239 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000240 /* Deal with atoi() returning 0 on failure, and bgpd not
241 listening on bgp port... */
242 if (strcmp(optarg, "0") == 0)
243 {
244 vty_port = 0;
245 break;
246 }
247 vty_port = atoi (optarg);
248 vty_port = (vty_port ? vty_port : BGP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000249 break;
250 case 'r':
251 retain_mode = 1;
252 break;
253 case 'n':
254 bgp_option_set (BGP_OPT_NO_FIB);
255 break;
pauledd7c242003-06-04 13:59:38 +0000256 case 'u':
257 bgpd_privs.user = bgpd_privs.group = optarg;
258 break;
paul718e3742002-12-13 20:15:29 +0000259 case 'v':
260 print_version (progname);
261 exit (0);
262 break;
263 case 'h':
264 usage (progname, 0);
265 break;
266 default:
267 usage (progname, 1);
268 break;
269 }
270 }
271
272 /* Make thread master. */
273 master = bm->master;
274
275 /* Initializations. */
276 srand (time (NULL));
paul2d75d052004-01-19 21:31:15 +0000277 signal_init (master, Q_SIGC(bgp_signals), bgp_signals);
pauledd7c242003-06-04 13:59:38 +0000278 zprivs_init (&bgpd_privs);
paul718e3742002-12-13 20:15:29 +0000279 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000280 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000281 memory_init ();
282
283 /* BGP related initialization. */
284 bgp_init ();
285
286 /* Sort CLI commands. */
287 sort_node ();
288
289 /* Parse config file. */
290 vty_read_config (config_file, config_current, config_default);
291
292 /* Turn into daemon if daemon_mode is set. */
293 if (daemon_mode)
294 daemon (0, 0);
295
296 /* Process ID file creation. */
297 pid_output (pid_file);
298
299 /* Make bgp vty socket. */
300 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
301
302 /* Print banner. */
paule8f29842003-08-12 13:08:31 +0000303 zlog_info ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION,
paul718e3742002-12-13 20:15:29 +0000304 vty_port, bm->port);
305
306 /* Start finite state machine, here we go! */
307 while (thread_fetch (master, &thread))
308 thread_call (&thread);
309
310 /* Not reached. */
311 exit (0);
312}