]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
Makes date/time formats configurable.
[thirdparty/bird.git] / conf / conf.c
1 /*
2 * BIRD Internet Routing Daemon -- Configuration File Handling
3 *
4 * (c) 1998--2000 Martin Mares <mj@ucw.cz>
5 *
6 * Can be freely distributed and used under the terms of the GNU GPL.
7 */
8
9 /**
10 * DOC: Configuration manager
11 *
12 * Configuration of BIRD is complex, yet straightforward. There are three
13 * modules taking care of the configuration: config manager (which takes care
14 * of storage of the config information and controls switching between configs),
15 * lexical analyzer and parser.
16 *
17 * The configuration manager stores each config as a &config structure
18 * accompanied by a linear pool from which all information associated
19 * with the config and pointed to by the &config structure is allocated.
20 *
21 * There can exist up to four different configurations at one time: an active
22 * one (pointed to by @config), configuration we are just switching from
23 * (@old_config), one queued for the next reconfiguration (@future_config;
24 * if it's non-%NULL and the user wants to reconfigure once again, we just
25 * free the previous queued config and replace it with the new one) and
26 * finally a config being parsed (@new_config).
27 *
28 * Loading of new configuration is very simple: just call config_alloc()
29 * to get a new &config structure, then use config_parse() to parse a
30 * configuration file and fill all fields of the structure
31 * and finally ask the config manager to switch to the new
32 * config by calling config_commit().
33 *
34 * CLI commands are parsed in a very similar way -- there is also a stripped-down
35 * &config structure associated with them and they are lex-ed and parsed by the
36 * same functions, only a special fake token is prepended before the command
37 * text to make the parser recognize only the rules corresponding to CLI commands.
38 */
39
40 #include <setjmp.h>
41 #include <stdarg.h>
42
43 #undef LOCAL_DEBUG
44
45 #include "nest/bird.h"
46 #include "nest/route.h"
47 #include "nest/protocol.h"
48 #include "nest/iface.h"
49 #include "lib/resource.h"
50 #include "lib/string.h"
51 #include "lib/event.h"
52 #include "lib/timer.h"
53 #include "conf/conf.h"
54 #include "filter/filter.h"
55
56 static jmp_buf conf_jmpbuf;
57
58 struct config *config, *new_config, *old_config, *future_config;
59 static event *config_event;
60 int shutting_down;
61 bird_clock_t boot_time;
62
63 /**
64 * config_alloc - allocate a new configuration
65 * @name: name of the config
66 *
67 * This function creates new &config structure, attaches a resource
68 * pool and a linear memory pool to it and makes it available for
69 * further use. Returns a pointer to the structure.
70 */
71 struct config *
72 config_alloc(byte *name)
73 {
74 pool *p = rp_new(&root_pool, "Config");
75 linpool *l = lp_new(p, 4080);
76 struct config *c = lp_allocz(l, sizeof(struct config));
77
78 c->mrtdump_file = -1; /* Hack, this should be sysdep-specific */
79 c->pool = p;
80 cfg_mem = c->mem = l;
81 c->file_name = cfg_strdup(name);
82 c->load_time = now;
83 c->tf_base.fmt1 = c->tf_log.fmt1 = "%d-%m-%Y %T";
84
85 if (!boot_time)
86 boot_time = now;
87 return c;
88 }
89
90 /**
91 * config_parse - parse a configuration
92 * @c: configuration
93 *
94 * config_parse() reads input by calling a hook function pointed to
95 * by @cf_read_hook and parses it according to the configuration
96 * grammar. It also calls all the preconfig and postconfig hooks
97 * before, resp. after parsing.
98 *
99 * Result: 1 if the config has been parsed successfully, 0 if any
100 * error has occurred (such as anybody calling cf_error()) and
101 * the @err_msg field has been set to the error message.
102 */
103 int
104 config_parse(struct config *c)
105 {
106 DBG("Parsing configuration file `%s'\n", c->file_name);
107 new_config = c;
108 cfg_mem = c->mem;
109 if (setjmp(conf_jmpbuf))
110 return 0;
111 cf_lex_init(0);
112 sysdep_preconfig(c);
113 protos_preconfig(c);
114 rt_preconfig(c);
115 cf_parse();
116 protos_postconfig(c);
117 #ifdef IPV6
118 if (!c->router_id)
119 cf_error("Router ID must be configured manually on IPv6 routers");
120 #endif
121 return 1;
122 }
123
124 /**
125 * cli_parse - parse a CLI command
126 * @c: temporary config structure
127 *
128 * cli_parse() is similar to config_parse(), but instead of a configuration,
129 * it parses a CLI command. See the CLI module for more information.
130 */
131 int
132 cli_parse(struct config *c)
133 {
134 new_config = c;
135 c->sym_fallback = config->sym_hash;
136 cfg_mem = c->mem;
137 if (setjmp(conf_jmpbuf))
138 return 0;
139 cf_lex_init(1);
140 cf_parse();
141 return 1;
142 }
143
144 /**
145 * config_free - free a configuration
146 * @c: configuration to be freed
147 *
148 * This function takes a &config structure and frees all resources
149 * associated with it.
150 */
151 void
152 config_free(struct config *c)
153 {
154 rfree(c->pool);
155 }
156
157 void
158 config_add_obstacle(struct config *c)
159 {
160 DBG("+++ adding obstacle %d\n", c->obstacle_count);
161 c->obstacle_count++;
162 }
163
164 void
165 config_del_obstacle(struct config *c)
166 {
167 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
168 c->obstacle_count--;
169 if (!c->obstacle_count)
170 {
171 ASSERT(config_event);
172 ev_schedule(config_event);
173 }
174 }
175
176 static int
177 global_commit(struct config *new, struct config *old)
178 {
179 if (!old)
180 return 0;
181
182 if (!ipa_equal(old->listen_bgp_addr, new->listen_bgp_addr) ||
183 (old->listen_bgp_port != new->listen_bgp_port) ||
184 (old->listen_bgp_flags != new->listen_bgp_flags))
185 log(L_WARN "Reconfiguration of BGP listening socket not implemented, please restart BIRD.");
186
187 if (!new->router_id)
188 new->router_id = old->router_id;
189 if (new->router_id != old->router_id)
190 return 1;
191 return 0;
192 }
193
194 static int
195 config_do_commit(struct config *c, int type)
196 {
197 int force_restart, nobs;
198
199 DBG("do_commit\n");
200 old_config = config;
201 config = new_config = c;
202 if (old_config)
203 old_config->obstacle_count++;
204 DBG("sysdep_commit\n");
205 force_restart = sysdep_commit(c, old_config);
206 DBG("global_commit\n");
207 force_restart |= global_commit(c, old_config);
208 DBG("rt_commit\n");
209 rt_commit(c, old_config);
210 DBG("protos_commit\n");
211 protos_commit(c, old_config, force_restart, type);
212 new_config = NULL; /* Just to be sure nobody uses that now */
213 if (old_config)
214 nobs = --old_config->obstacle_count;
215 else
216 nobs = 0;
217 DBG("do_commit finished with %d obstacles remaining\n", nobs);
218 return !nobs;
219 }
220
221 static void
222 config_done(void *unused UNUSED)
223 {
224 struct config *c;
225
226 DBG("config_done\n");
227 for(;;)
228 {
229 if (config->shutdown)
230 sysdep_shutdown_done();
231 log(L_INFO "Reconfigured");
232 if (old_config)
233 {
234 config_free(old_config);
235 old_config = NULL;
236 }
237 if (!future_config)
238 break;
239 c = future_config;
240 future_config = NULL;
241 log(L_INFO "Switching to queued configuration...");
242 if (!config_do_commit(c, RECONFIG_HARD))
243 break;
244 }
245 }
246
247 /**
248 * config_commit - commit a configuration
249 * @c: new configuration
250 * @type: type of reconfiguration (RECONFIG_SOFT or RECONFIG_HARD)
251 *
252 * When a configuration is parsed and prepared for use, the
253 * config_commit() function starts the process of reconfiguration.
254 * It checks whether there is already a reconfiguration in progress
255 * in which case it just queues the new config for later processing.
256 * Else it notifies all modules about the new configuration by calling
257 * their commit() functions which can either accept it immediately
258 * or call config_add_obstacle() to report that they need some time
259 * to complete the reconfiguration. After all such obstacles are removed
260 * using config_del_obstacle(), the old configuration is freed and
261 * everything runs according to the new one.
262 *
263 * Result: %CONF_DONE if the configuration has been accepted immediately,
264 * %CONF_PROGRESS if it will take some time to switch to it, %CONF_QUEUED
265 * if it's been queued due to another reconfiguration being in progress now
266 * or %CONF_SHUTDOWN if BIRD is in shutdown mode and no new configurations
267 * are accepted.
268 */
269 int
270 config_commit(struct config *c, int type)
271 {
272 if (!config) /* First-time configuration */
273 {
274 config_do_commit(c, RECONFIG_HARD);
275 return CONF_DONE;
276 }
277 if (old_config) /* Reconfiguration already in progress */
278 {
279 if (shutting_down == 2)
280 {
281 log(L_INFO "New configuration discarded due to shutdown");
282 config_free(c);
283 return CONF_SHUTDOWN;
284 }
285 if (future_config)
286 {
287 log(L_INFO "Queueing new configuration, ignoring the one already queued");
288 config_free(future_config);
289 }
290 else
291 log(L_INFO "Queued new configuration");
292 future_config = c;
293 return CONF_QUEUED;
294 }
295 if (config_do_commit(c, type))
296 {
297 config_done(NULL);
298 return CONF_DONE;
299 }
300 if (!config_event)
301 {
302 config_event = ev_new(&root_pool);
303 config_event->hook = config_done;
304 }
305 return CONF_PROGRESS;
306 }
307
308 /**
309 * order_shutdown - order BIRD shutdown
310 *
311 * This function initiates shutdown of BIRD. It's accomplished by asking
312 * for switching to an empty configuration.
313 */
314 void
315 order_shutdown(void)
316 {
317 struct config *c;
318
319 if (shutting_down)
320 return;
321 log(L_INFO "Shutting down");
322 c = lp_alloc(config->mem, sizeof(struct config));
323 memcpy(c, config, sizeof(struct config));
324 init_list(&c->protos);
325 init_list(&c->tables);
326 c->shutdown = 1;
327 shutting_down = 1;
328 config_commit(c, RECONFIG_HARD);
329 shutting_down = 2;
330 }
331
332 /**
333 * cf_error - report a configuration error
334 * @msg: printf-like format string
335 *
336 * cf_error() can be called during execution of config_parse(), that is
337 * from the parser, a preconfig hook or a postconfig hook, to report an
338 * error in the configuration.
339 */
340 void
341 cf_error(char *msg, ...)
342 {
343 char buf[1024];
344 va_list args;
345
346 va_start(args, msg);
347 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
348 strcpy(buf, "<bug: error message too long>");
349 new_config->err_msg = cfg_strdup(buf);
350 new_config->err_lino = conf_lino;
351 longjmp(conf_jmpbuf, 1);
352 }
353
354 /**
355 * cfg_strdup - copy a string to config memory
356 * @c: string to copy
357 *
358 * cfg_strdup() creates a new copy of the string in the memory
359 * pool associated with the configuration being currently parsed.
360 * It's often used when a string literal occurs in the configuration
361 * and we want to preserve it for further use.
362 */
363 char *
364 cfg_strdup(char *c)
365 {
366 int l = strlen(c) + 1;
367 char *z = cfg_allocu(l);
368 memcpy(z, c, l);
369 return z;
370 }