blob: 0ba19c2ff7c52254d88e8e4d45b36eca2baf7e8c [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 },
75};
76
paul718e3742002-12-13 20:15:29 +000077/* Configuration file and directory. */
78char config_current[] = BGP_DEFAULT_CONFIG;
79char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG;
80
81/* Route retain mode flag. */
82int retain_mode = 0;
83
84/* Master of threads. */
85struct thread_master *master;
86
87/* Manually specified configuration file name. */
88char *config_file = NULL;
89
90/* Process ID saved for use by init system */
91char *pid_file = PATH_BGPD_PID;
92
93/* VTY port number and address. */
94int vty_port = BGP_VTY_PORT;
95char *vty_addr = NULL;
96
pauledd7c242003-06-04 13:59:38 +000097/* privileges */
98zebra_capabilities_t _caps_p [] =
99{
100 ZCAP_BIND,
101};
102
103struct zebra_privs_t bgpd_privs =
104{
pauld81fadf2003-08-14 05:32:12 +0000105#if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
106 .user = QUAGGA_USER,
107 .group = QUAGGA_GROUP,
108#endif
109#ifdef VTY_GROUP
110 .vty_group = VTY_GROUP,
pauledd7c242003-06-04 13:59:38 +0000111#endif
112 .caps_p = _caps_p,
113 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
114 .cap_num_i = 0,
115};
116
paul718e3742002-12-13 20:15:29 +0000117/* Help information display. */
118static void
119usage (char *progname, int status)
120{
121 if (status != 0)
122 fprintf (stderr, "Try `%s --help' for more information.\n", progname);
123 else
124 {
125 printf ("Usage : %s [OPTION...]\n\n\
126Daemon which manages kernel routing table management and \
127redistribution between different routing protocols.\n\n\
128-d, --daemon Runs in daemon mode\n\
129-f, --config_file Set configuration file name\n\
130-i, --pid_file Set process identifier file name\n\
131-p, --bgp_port Set bgp protocol's port number\n\
132-A, --vty_addr Set vty's bind address\n\
133-P, --vty_port Set vty's port number\n\
134-r, --retain When program terminates, retain added route by bgpd.\n\
135-n, --no_kernel Do not install route to kernel.\n\
pauledd7c242003-06-04 13:59:38 +0000136-u, --user User and group to run as\n\
paul718e3742002-12-13 20:15:29 +0000137-v, --version Print program version\n\
138-h, --help Display this help and exit\n\
139\n\
140Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS);
141 }
142
143 exit (status);
144}
145
146/* SIGHUP handler. */
147void
paul2d75d052004-01-19 21:31:15 +0000148sighup (void)
paul718e3742002-12-13 20:15:29 +0000149{
150 zlog (NULL, LOG_INFO, "SIGHUP received");
151
152 /* Terminate all thread. */
153 bgp_terminate ();
154 bgp_reset ();
155 zlog_info ("bgpd restarting!");
156
157 /* Reload config file. */
158 vty_read_config (config_file, config_current, config_default);
159
160 /* Create VTY's socket */
paul4fc4e7a2003-01-22 19:47:09 +0000161 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
paul718e3742002-12-13 20:15:29 +0000162
163 /* Try to return to normal operation. */
164}
165
166/* SIGINT handler. */
167void
paul2d75d052004-01-19 21:31:15 +0000168sigint (void)
paul718e3742002-12-13 20:15:29 +0000169{
170 zlog (NULL, LOG_INFO, "Terminating on signal");
171
172 if (! retain_mode)
173 bgp_terminate ();
174
175 exit (0);
176}
177
178/* SIGUSR1 handler. */
179void
paul2d75d052004-01-19 21:31:15 +0000180sigusr1 (void)
paul718e3742002-12-13 20:15:29 +0000181{
182 zlog_rotate (NULL);
183}
paul718e3742002-12-13 20:15:29 +0000184
185/* Main routine of bgpd. Treatment of argument and start bgp finite
186 state machine is handled at here. */
187int
188main (int argc, char **argv)
189{
190 char *p;
191 int opt;
192 int daemon_mode = 0;
193 char *progname;
194 struct thread thread;
195
196 /* Set umask before anything for security */
197 umask (0027);
198
199 /* Preserve name of myself. */
200 progname = ((p = strrchr (argv[0], '/')) ? ++p : argv[0]);
201
202 zlog_default = openzlog (progname, ZLOG_NOLOG, ZLOG_BGP,
203 LOG_CONS|LOG_NDELAY|LOG_PID, LOG_DAEMON);
204
205 /* BGP master init. */
206 bgp_master_init ();
207
208 /* Command line argument treatment. */
209 while (1)
210 {
paul96735ee2003-08-10 02:51:22 +0000211 opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:v", longopts, 0);
paul718e3742002-12-13 20:15:29 +0000212
213 if (opt == EOF)
214 break;
215
216 switch (opt)
217 {
218 case 0:
219 break;
220 case 'd':
221 daemon_mode = 1;
222 break;
223 case 'f':
224 config_file = optarg;
225 break;
226 case 'i':
227 pid_file = optarg;
228 break;
229 case 'p':
230 bm->port = atoi (optarg);
231 break;
232 case 'A':
233 vty_addr = optarg;
234 break;
235 case 'P':
paul4fc4e7a2003-01-22 19:47:09 +0000236 /* Deal with atoi() returning 0 on failure, and bgpd not
237 listening on bgp port... */
238 if (strcmp(optarg, "0") == 0)
239 {
240 vty_port = 0;
241 break;
242 }
243 vty_port = atoi (optarg);
244 vty_port = (vty_port ? vty_port : BGP_VTY_PORT);
paul718e3742002-12-13 20:15:29 +0000245 break;
246 case 'r':
247 retain_mode = 1;
248 break;
249 case 'n':
250 bgp_option_set (BGP_OPT_NO_FIB);
251 break;
pauledd7c242003-06-04 13:59:38 +0000252 case 'u':
253 bgpd_privs.user = bgpd_privs.group = optarg;
254 break;
paul718e3742002-12-13 20:15:29 +0000255 case 'v':
256 print_version (progname);
257 exit (0);
258 break;
259 case 'h':
260 usage (progname, 0);
261 break;
262 default:
263 usage (progname, 1);
264 break;
265 }
266 }
267
268 /* Make thread master. */
269 master = bm->master;
270
271 /* Initializations. */
272 srand (time (NULL));
paul2d75d052004-01-19 21:31:15 +0000273 signal_init (master, Q_SIGC(bgp_signals), bgp_signals);
pauledd7c242003-06-04 13:59:38 +0000274 zprivs_init (&bgpd_privs);
paul718e3742002-12-13 20:15:29 +0000275 cmd_init (1);
paulb21b19c2003-06-15 01:28:29 +0000276 vty_init (master);
paul718e3742002-12-13 20:15:29 +0000277 memory_init ();
278
279 /* BGP related initialization. */
280 bgp_init ();
281
282 /* Sort CLI commands. */
283 sort_node ();
284
285 /* Parse config file. */
286 vty_read_config (config_file, config_current, config_default);
287
288 /* Turn into daemon if daemon_mode is set. */
289 if (daemon_mode)
290 daemon (0, 0);
291
292 /* Process ID file creation. */
293 pid_output (pid_file);
294
295 /* Make bgp vty socket. */
296 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
297
298 /* Print banner. */
paule8f29842003-08-12 13:08:31 +0000299 zlog_info ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION,
paul718e3742002-12-13 20:15:29 +0000300 vty_port, bm->port);
301
302 /* Start finite state machine, here we go! */
303 while (thread_fetch (master, &thread))
304 thread_call (&thread);
305
306 /* Not reached. */
307 exit (0);
308}