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 { "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'},
50 { "user", required_argument
, NULL
, 'u'},
51 { "group", required_argument
, NULL
, 'g'},
52 { "version", no_argument
, NULL
, 'v'},
53 { "dryrun", no_argument
, NULL
, 'C'},
54 { "help", no_argument
, NULL
, 'h'},
58 /* signal definitions */
63 struct quagga_signal_t bgp_signals
[] =
83 /* Configuration file and directory. */
84 char config_default
[] = SYSCONFDIR BGP_DEFAULT_CONFIG
;
86 /* Route retain mode flag. */
89 /* Master of threads. */
90 struct thread_master
*master
;
92 /* Manually specified configuration file name. */
93 char *config_file
= NULL
;
95 /* Process ID saved for use by init system */
96 const char *pid_file
= PATH_BGPD_PID
;
98 /* VTY port number and address. */
99 int vty_port
= BGP_VTY_PORT
;
100 char *vty_addr
= NULL
;
103 zebra_capabilities_t _caps_p
[] =
109 struct zebra_privs_t bgpd_privs
=
111 #if defined(QUAGGA_USER) && defined(QUAGGA_GROUP)
113 .group
= QUAGGA_GROUP
,
116 .vty_group
= VTY_GROUP
,
119 .cap_num_p
= sizeof(_caps_p
)/sizeof(_caps_p
[0]),
123 /* Help information display. */
125 usage (char *progname
, int status
)
128 fprintf (stderr
, "Try `%s --help' for more information.\n", progname
);
131 printf ("Usage : %s [OPTION...]\n\n\
132 Daemon which manages kernel routing table management and \
133 redistribution between different routing protocols.\n\n\
134 -d, --daemon Runs in daemon mode\n\
135 -f, --config_file Set configuration file name\n\
136 -i, --pid_file Set process identifier file name\n\
137 -p, --bgp_port Set bgp protocol's port number\n\
138 -A, --vty_addr Set vty's bind address\n\
139 -P, --vty_port Set vty's port number\n\
140 -r, --retain When program terminates, retain added route by bgpd.\n\
141 -n, --no_kernel Do not install route to kernel.\n\
142 -u, --user User to run as\n\
143 -g, --group Group to run as\n\
144 -v, --version Print program version\n\
145 -C, --dryrun Check configuration for validity and exit\n\
146 -h, --help Display this help and exit\n\
148 Report bugs to %s\n", progname
, ZEBRA_BUG_ADDRESS
);
154 /* SIGHUP handler. */
158 zlog (NULL
, LOG_INFO
, "SIGHUP received");
160 /* Terminate all thread. */
163 zlog_info ("bgpd restarting!");
165 /* Reload config file. */
166 vty_read_config (config_file
, config_default
);
168 /* Create VTY's socket */
169 vty_serv_sock (vty_addr
, vty_port
, BGP_VTYSH_PATH
);
171 /* Try to return to normal operation. */
174 /* SIGINT handler. */
178 zlog_notice ("Terminating on signal");
186 /* SIGUSR1 handler. */
193 /* Main routine of bgpd. Treatment of argument and start bgp finite
194 state machine is handled at here. */
196 main (int argc
, char **argv
)
203 struct thread thread
;
205 /* Set umask before anything for security */
208 /* Preserve name of myself. */
209 progname
= ((p
= strrchr (argv
[0], '/')) ? ++p
: argv
[0]);
211 zlog_default
= openzlog (progname
, ZLOG_BGP
,
212 LOG_CONS
|LOG_NDELAY
|LOG_PID
, LOG_DAEMON
);
214 /* BGP master init. */
217 /* Command line argument treatment. */
220 opt
= getopt_long (argc
, argv
, "df:i:hp:A:P:rnu:g:vC", longopts
, 0);
233 config_file
= optarg
;
239 bm
->port
= atoi (optarg
);
245 /* Deal with atoi() returning 0 on failure, and bgpd not
246 listening on bgp port... */
247 if (strcmp(optarg
, "0") == 0)
252 vty_port
= atoi (optarg
);
253 vty_port
= (vty_port
? vty_port
: BGP_VTY_PORT
);
259 bgp_option_set (BGP_OPT_NO_FIB
);
262 bgpd_privs
.user
= optarg
;
265 bgpd_privs
.group
= optarg
;
268 print_version (progname
);
283 /* Make thread master. */
286 /* Initializations. */
288 signal_init (master
, Q_SIGC(bgp_signals
), bgp_signals
);
289 zprivs_init (&bgpd_privs
);
294 /* BGP related initialization. */
297 /* Sort CLI commands. */
300 /* Parse config file. */
301 vty_read_config (config_file
, config_default
);
303 /* Start execution only if not in dry-run mode */
307 /* Turn into daemon if daemon_mode is set. */
311 /* Process ID file creation. */
312 pid_output (pid_file
);
314 /* Make bgp vty socket. */
315 vty_serv_sock (vty_addr
, vty_port
, BGP_VTYSH_PATH
);
318 zlog_notice ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION
,
321 /* Start finite state machine, here we go! */
322 while (thread_fetch (master
, &thread
))
323 thread_call (&thread
);