]> git.proxmox.com Git - mirror_frr.git/blame - bgpd/bgp_main.c
2004-01-19 Paul Jakma <paul@dishone.st>
[mirror_frr.git] / bgpd / bgp_main.c
CommitLineData
718e3744 1/* 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"
edd7c245 32#include "privs.h"
2d75d052 33#include "sigevent.h"
718e3744 34
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'},
edd7c245 50 { "user", required_argument, NULL, 'u'},
718e3744 51 { "version", no_argument, NULL, 'v'},
52 { "help", no_argument, NULL, 'h'},
53 { 0 }
54};
55
2d75d052 56/* 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
718e3744 77/* 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
edd7c245 97/* privileges */
98zebra_capabilities_t _caps_p [] =
99{
100 ZCAP_BIND,
101};
102
103struct zebra_privs_t bgpd_privs =
104{
d81fadfd 105#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,
edd7c245 111#endif
112 .caps_p = _caps_p,
113 .cap_num_p = sizeof(_caps_p)/sizeof(_caps_p[0]),
114 .cap_num_i = 0,
115};
116
718e3744 117/* 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\
edd7c245 136-u, --user User and group to run as\n\
718e3744 137-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\f
146/* SIGHUP handler. */
147void
2d75d052 148sighup (void)
718e3744 149{
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 */
4fc4e7ab 161 vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH);
718e3744 162
163 /* Try to return to normal operation. */
164}
165
166/* SIGINT handler. */
167void
2d75d052 168sigint (void)
718e3744 169{
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
2d75d052 180sigusr1 (void)
718e3744 181{
182 zlog_rotate (NULL);
183}
718e3744 184\f
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 {
96735eea 211 opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:v", longopts, 0);
718e3744 212
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':
4fc4e7ab 236 /* 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);
718e3744 245 break;
246 case 'r':
247 retain_mode = 1;
248 break;
249 case 'n':
250 bgp_option_set (BGP_OPT_NO_FIB);
251 break;
edd7c245 252 case 'u':
253 bgpd_privs.user = bgpd_privs.group = optarg;
254 break;
718e3744 255 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));
2d75d052 273 signal_init (master, Q_SIGC(bgp_signals), bgp_signals);
edd7c245 274 zprivs_init (&bgpd_privs);
718e3744 275 cmd_init (1);
b21b19c5 276 vty_init (master);
718e3744 277 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. */
e8f2984c 299 zlog_info ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION,
718e3744 300 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}