]>
Commit | Line | Data |
---|---|---|
1 | /* Main routine of bgpd. | |
2 | Copyright (C) 1996, 97, 98, 1999 Kunihiro Ishiguro | |
3 | ||
4 | This file is part of GNU Zebra. | |
5 | ||
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 | |
9 | later version. | |
10 | ||
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. | |
15 | ||
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 | |
19 | 02111-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" | |
32 | #include "privs.h" | |
33 | #include "sigevent.h" | |
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. */ | |
40 | struct 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'}, | |
50 | { "user", required_argument, NULL, 'u'}, | |
51 | { "version", no_argument, NULL, 'v'}, | |
52 | { "help", no_argument, NULL, 'h'}, | |
53 | { 0 } | |
54 | }; | |
55 | ||
56 | /* signal definitions */ | |
57 | void sighup (void); | |
58 | void sigint (void); | |
59 | void sigusr1 (void); | |
60 | ||
61 | struct 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 | ||
77 | /* Configuration file and directory. */ | |
78 | char config_current[] = BGP_DEFAULT_CONFIG; | |
79 | char config_default[] = SYSCONFDIR BGP_DEFAULT_CONFIG; | |
80 | ||
81 | /* Route retain mode flag. */ | |
82 | int retain_mode = 0; | |
83 | ||
84 | /* Master of threads. */ | |
85 | struct thread_master *master; | |
86 | ||
87 | /* Manually specified configuration file name. */ | |
88 | char *config_file = NULL; | |
89 | ||
90 | /* Process ID saved for use by init system */ | |
91 | char *pid_file = PATH_BGPD_PID; | |
92 | ||
93 | /* VTY port number and address. */ | |
94 | int vty_port = BGP_VTY_PORT; | |
95 | char *vty_addr = NULL; | |
96 | ||
97 | /* privileges */ | |
98 | zebra_capabilities_t _caps_p [] = | |
99 | { | |
100 | ZCAP_BIND, | |
101 | }; | |
102 | ||
103 | struct zebra_privs_t bgpd_privs = | |
104 | { | |
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, | |
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 | ||
117 | /* Help information display. */ | |
118 | static void | |
119 | usage (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\ | |
126 | Daemon which manages kernel routing table management and \ | |
127 | redistribution 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\ | |
136 | -u, --user User and group to run as\n\ | |
137 | -v, --version Print program version\n\ | |
138 | -h, --help Display this help and exit\n\ | |
139 | \n\ | |
140 | Report bugs to %s\n", progname, ZEBRA_BUG_ADDRESS); | |
141 | } | |
142 | ||
143 | exit (status); | |
144 | } | |
145 | \f | |
146 | /* SIGHUP handler. */ | |
147 | void | |
148 | sighup (void) | |
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 */ | |
161 | vty_serv_sock (vty_addr, vty_port, BGP_VTYSH_PATH); | |
162 | ||
163 | /* Try to return to normal operation. */ | |
164 | } | |
165 | ||
166 | /* SIGINT handler. */ | |
167 | void | |
168 | sigint (void) | |
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. */ | |
179 | void | |
180 | sigusr1 (void) | |
181 | { | |
182 | zlog_rotate (NULL); | |
183 | } | |
184 | \f | |
185 | /* Main routine of bgpd. Treatment of argument and start bgp finite | |
186 | state machine is handled at here. */ | |
187 | int | |
188 | main (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 | { | |
211 | opt = getopt_long (argc, argv, "df:i:hp:A:P:rnu:v", longopts, 0); | |
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': | |
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); | |
245 | break; | |
246 | case 'r': | |
247 | retain_mode = 1; | |
248 | break; | |
249 | case 'n': | |
250 | bgp_option_set (BGP_OPT_NO_FIB); | |
251 | break; | |
252 | case 'u': | |
253 | bgpd_privs.user = bgpd_privs.group = optarg; | |
254 | break; | |
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)); | |
273 | signal_init (master, Q_SIGC(bgp_signals), bgp_signals); | |
274 | zprivs_init (&bgpd_privs); | |
275 | cmd_init (1); | |
276 | vty_init (master); | |
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. */ | |
299 | zlog_info ("BGPd %s starting: vty@%d, bgp@%d", QUAGGA_VERSION, | |
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 | } |