]> git.ipfire.org Git - thirdparty/bird.git/blob - conf/conf.c
Event handlers no longer return re-queue flag. Instead of using it, just
[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 #include <setjmp.h>
10 #include <stdarg.h>
11
12 #undef LOCAL_DEBUG
13
14 #include "nest/bird.h"
15 #include "nest/route.h"
16 #include "nest/protocol.h"
17 #include "nest/iface.h"
18 #include "lib/resource.h"
19 #include "lib/string.h"
20 #include "lib/event.h"
21 #include "lib/timer.h"
22 #include "conf/conf.h"
23 #include "filter/filter.h"
24
25 static jmp_buf conf_jmpbuf;
26
27 struct config *config, *new_config, *old_config, *future_config;
28 static event *config_event;
29 int shutting_down;
30 bird_clock_t boot_time;
31
32 struct config *
33 config_alloc(byte *name)
34 {
35 pool *p = rp_new(&root_pool, "Config");
36 linpool *l = lp_new(p, 4080);
37 struct config *c = lp_allocz(l, sizeof(struct config));
38
39 c->pool = p;
40 cfg_mem = c->mem = l;
41 c->file_name = cfg_strdup(name);
42 c->load_time = now;
43 if (!boot_time)
44 boot_time = now;
45 return c;
46 }
47
48 int
49 config_parse(struct config *c)
50 {
51 DBG("Parsing configuration file `%s'\n", c->file_name);
52 new_config = c;
53 cfg_mem = c->mem;
54 if (setjmp(conf_jmpbuf))
55 return 0;
56 cf_lex_init(0);
57 sysdep_preconfig(c);
58 protos_preconfig(c);
59 rt_preconfig(c);
60 cf_parse();
61 filters_postconfig(); /* FIXME: Do we really need this? */
62 protos_postconfig(c);
63 #ifdef IPV6
64 if (!c->router_id)
65 cf_error("Router ID must be configured manually on IPv6 routers");
66 #endif
67 return 1;
68 }
69
70 int
71 cli_parse(struct config *c)
72 {
73 new_config = c;
74 c->sym_fallback = config->sym_hash;
75 cfg_mem = c->mem;
76 if (setjmp(conf_jmpbuf))
77 return 0;
78 cf_lex_init(1);
79 cf_parse();
80 return 1;
81 }
82
83 void
84 config_free(struct config *c)
85 {
86 rfree(c->pool);
87 }
88
89 void
90 config_add_obstacle(struct config *c)
91 {
92 DBG("+++ adding obstacle %d\n", c->obstacle_count);
93 c->obstacle_count++;
94 }
95
96 void
97 config_del_obstacle(struct config *c)
98 {
99 DBG("+++ deleting obstacle %d\n", c->obstacle_count);
100 c->obstacle_count--;
101 if (!c->obstacle_count)
102 {
103 ASSERT(config_event);
104 ev_schedule(config_event);
105 }
106 }
107
108 static int
109 global_commit(struct config *new, struct config *old)
110 {
111 if (!old)
112 return 0;
113 if (!new->router_id)
114 new->router_id = old->router_id;
115 if (new->router_id != old->router_id)
116 return 1;
117 return 0;
118 }
119
120 static int
121 config_do_commit(struct config *c)
122 {
123 int force_restart, nobs;
124
125 DBG("do_commit\n");
126 old_config = config;
127 config = new_config = c;
128 if (old_config)
129 old_config->obstacle_count++;
130 DBG("sysdep_commit\n");
131 force_restart = sysdep_commit(c, old_config);
132 DBG("global_commit\n");
133 force_restart |= global_commit(c, old_config);
134 DBG("rt_commit\n");
135 rt_commit(c, old_config);
136 DBG("protos_commit\n");
137 protos_commit(c, old_config, force_restart);
138 new_config = NULL; /* Just to be sure nobody uses that now */
139 if (old_config)
140 nobs = --old_config->obstacle_count;
141 else
142 nobs = 0;
143 DBG("do_commit finished with %d obstacles remaining\n", nobs);
144 return !nobs;
145 }
146
147 static void
148 config_done(void *unused)
149 {
150 struct config *c;
151
152 DBG("config_done\n");
153 for(;;)
154 {
155 if (config->shutdown)
156 sysdep_shutdown_done();
157 log(L_INFO "Reconfigured");
158 if (old_config)
159 {
160 config_free(old_config);
161 old_config = NULL;
162 }
163 if (!future_config)
164 break;
165 c = future_config;
166 future_config = NULL;
167 log(L_INFO "Switching to queued configuration...");
168 if (!config_do_commit(c))
169 break;
170 }
171 }
172
173 int
174 config_commit(struct config *c)
175 {
176 if (!config) /* First-time configuration */
177 {
178 config_do_commit(c);
179 return CONF_DONE;
180 }
181 if (old_config) /* Reconfiguration already in progress */
182 {
183 if (shutting_down)
184 {
185 log(L_INFO "New configuration discarded due to shutdown");
186 config_free(c);
187 return CONF_SHUTDOWN;
188 }
189 if (future_config)
190 {
191 log(L_INFO "Queueing new configuration, ignoring the one already queued");
192 config_free(future_config);
193 }
194 else
195 log(L_INFO "Queued new configuration");
196 future_config = c;
197 return CONF_QUEUED;
198 }
199 if (config_do_commit(c))
200 {
201 config_done(NULL);
202 return CONF_DONE;
203 }
204 if (!config_event)
205 {
206 config_event = ev_new(&root_pool);
207 config_event->hook = config_done;
208 }
209 return CONF_PROGRESS;
210 }
211
212 void
213 order_shutdown(void)
214 {
215 struct config *c;
216
217 if (shutting_down)
218 return;
219 log(L_INFO "Shutting down");
220 c = lp_alloc(config->mem, sizeof(struct config));
221 memcpy(c, config, sizeof(struct config));
222 init_list(&c->protos);
223 init_list(&c->tables);
224 c->shutdown = 1;
225 config_commit(c);
226 shutting_down = 1;
227 }
228
229 void
230 cf_error(char *msg, ...)
231 {
232 char buf[1024];
233 va_list args;
234
235 va_start(args, msg);
236 if (bvsnprintf(buf, sizeof(buf), msg, args) < 0)
237 strcpy(buf, "<bug: error message too long>");
238 new_config->err_msg = cfg_strdup(buf);
239 new_config->err_lino = conf_lino;
240 longjmp(conf_jmpbuf, 1);
241 }
242
243 char *
244 cfg_strdup(char *c)
245 {
246 int l = strlen(c) + 1;
247 char *z = cfg_allocu(l);
248 memcpy(z, c, l);
249 return z;
250 }