1 /* Main routine of bgpd.
2 Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro
4 This file is part of GNU Zebra.
6 GNU Zebra is free software; you can redistribute it and/or modify it
7 under the terms of the GNU General Public License as published by the
8 Free Software Foundation; either version 2, or (at your option) any
11 GNU Zebra is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with GNU Zebra; see the file COPYING. If not, write to the Free
18 Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
28 #include <lib/version.h>
35 #include "bgpd/bgpd.h"
36 #include "bgpd/bgp_attr.h"
37 #include "bgpd/bgp_mplsvpn.h"
39 /* bgpd options, we use GNU getopt library. */
40 struct option longopts
[] =
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 { "listenon", required_argument
, NULL
, 'l'},
47 { "vty_addr", required_argument
, NULL
, 'A'},
48 { "vty_port", required_argument
, NULL
, 'P'},
49 { "retain", no_argument
, NULL
, 'r'},
50 { "no_kernel", no_argument
, NULL
, 'n'},
51 { "user", required_argument
, NULL
, 'u'},
52 { "group", required_argument
, NULL
, 'g'},
53 { "version", no_argument
, NULL
, 'v'},
54 { "dryrun", no_argument
, NULL
, 'C'},
55 { "help", no_argument
, NULL
, 'h'},
59 /* signal definitions */
64 struct quagga_signal_t bgp_signals
[] =
84 /* Configuration file and directory. */
85 char config_default
[] = SYSCONFDIR BGP_DEFAULT_CONFIG
;
87 /* Route retain mode flag. */
90 /* Master of threads. */
91 struct thread_master
*master
;
93 /* Manually specified configuration file name. */
94 char *config_file
= NULL
;
96 /* Process ID saved for use by init system */
97 const char *pid_file
= PATH_BGPD_PID
;
99 /* VTY port number and address. */
100 int vty_port
= BGP_VTY_PORT
;
101 char *vty_addr
= NULL
;
104 zebra_capabilities_t _caps_p
[] =
110 struct zebra_privs_t bgpd_privs
=
112 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
114 .group
= QUAGGA_GROUP
,
117 .vty_group
= VTY_GROUP
,
120 .cap_num_p
= sizeof(_caps_p
)/sizeof(_caps_p
[0]),
124 /* Help information display. */
126 usage (char *progname
, int status
)
129 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
132 printf ("Usage : %s [OPTION...]\n\n\
133 Daemon which manages kernel routing table management and \
134 redistribution between different routing protocols.\n\n\
135 -d, --daemon Runs in daemon mode\n\
136 -f, --config_file Set configuration file name\n\
137 -i, --pid_file Set process identifier file name\n\
138 -p, --bgp_port Set bgp protocol's port number\n\
139 -l, --listenon Listen on specified address (implies -n)\n\
140 -A, --vty_addr Set vty's bind address\n\
141 -P, --vty_port Set vty's port number\n\
142 -r, --retain When program terminates, retain added route by bgpd.\n\
143 -n, --no_kernel Do not install route to kernel.\n\
144 -u, --user User to run as\n\
145 -g, --group Group to run as\n\
146 -v, --version Print program version\n\
147 -C, --dryrun Check configuration for validity and exit\n\
148 -h, --help Display this help and exit\n\
150 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
156 /* SIGHUP handler. */
160 zlog (NULL
, LOG_INFO
, "SIGHUP received");
162 /* Terminate all thread. */
165 zlog_info ("bgpd restarting!");
167 /* Reload config file. */
168 vty_read_config (config_file
, config_default
);
170 /* Create VTY's socket */
171 vty_serv_sock (vty_addr
, vty_port
, BGP_VTYSH_PATH
);
173 /* Try to return to normal operation. */
176 /* SIGINT handler. */
180 zlog_notice ("Terminating on signal");
188 /* SIGUSR1 handler. */
195 /* Main routine of bgpd. Treatment of argument and start bgp finite
196 state machine is handled at here. */
198 main (int argc
, char **argv
)
205 struct thread thread
;
208 /* Set umask before anything for security */
211 /* Preserve name of myself. */
212 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
214 zlog_default
= openzlog (progname
, ZLOG_BGP
,
215 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
217 /* BGP master init. */
220 /* Command line argument treatment. */
223 opt
= getopt_long (argc
, argv
, "df:i:hp:l:A:P:rnu:g:vC", longopts
, 0);
236 config_file
= optarg
;
242 tmp_port
= atoi (optarg
);
243 if (tmp_port
<= 0 || tmp_port
> 0xffff)
244 bm
->port
= BGP_PORT_DEFAULT
;
252 /* Deal with atoi() returning 0 on failure, and bgpd not
253 listening on bgp port... */
254 if (strcmp(optarg
, "0") == 0)
259 vty_port
= atoi (optarg
);
260 if (vty_port
<= 0 || vty_port
> 0xffff)
261 vty_port
= BGP_VTY_PORT
;
267 bm
->address
= optarg
;
268 /* listenon implies -n */
270 bgp_option_set (BGP_OPT_NO_FIB
);
273 bgpd_privs
.user
= optarg
;
276 bgpd_privs
.group
= optarg
;
279 print_version (progname
);
294 /* Make thread master. */
297 /* Initializations. */
299 signal_init (master
, Q_SIGC(bgp_signals
), bgp_signals
);
300 zprivs_init (&bgpd_privs
);
305 /* BGP related initialization. */
308 /* Sort CLI commands. */
311 /* Parse config file. */
312 vty_read_config (config_file
, config_default
);
314 /* Start execution only if not in dry-run mode */
318 /* Turn into daemon if daemon_mode is set. */
322 /* Process ID file creation. */
323 pid_output (pid_file
);
325 /* Make bgp vty socket. */
326 vty_serv_sock (vty_addr
, vty_port
, BGP_VTYSH_PATH
);
329 zlog_notice ("BGPd %s starting: vty@%d, bgp@%s:%d", QUAGGA_VERSION
,
331 (bm
->address
? bm
->address
: "<all>"),
334 /* Start finite state machine, here we go! */
335 while (thread_fetch (master
, &thread
))
336 thread_call (&thread
);