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