BadVPN – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /**
2 * @file depend.c
3 * @author Ambroz Bizjak <ambrop7@gmail.com>
4 *
5 * @section LICENSE
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are met:
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * 2. Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * 3. Neither the name of the author nor the
15 * names of its contributors may be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
22 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
24 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
25 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
27 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * @section DESCRIPTION
30 *
31 * Dependencies module.
32 *
33 * Synopsis: provide(string name)
34 * Description: Provides a resource. On initialization, transitions any depend()-s
35 * waiting for this resource to UP state. On deinitialization, transitions
36 * depend()-s using this resource to DOWN state, and waits for all of them to
37 * receive the clean signal (i.e. wait for all of the statements following them in
38 * their processes to terminate). Initialization fails if a provide() already
39 * exists for this resource (including if it is being deinitialized).
40 *
41 * Synopsis: provide_event(string name)
42 * Description: Like provide(), but if another provide() already exists for this
43 * resource, initialization does not fail, and the request is queued to the active
44 * provide() for this resource. When an active provide() disappears that has
45 * queued provide()-s, one of them is promoted to be the active provide() for this
46 * resource, and the remaining queue is transferred to it.
47 * (mentions of provide() in this text also apply to provide_event())
48 *
49 * Synopsis: depend(string name)
50 * Description: Depends on a resource. Is in UP state when a provide()
51 * for this resource is available, and in DOWN state when it is not (either
52 * it does not exist or is being terminated).
53 * Variables: Provides variables available from the corresponding provide,
54 * ("modname.varname" or "modname").
55 */
56  
57 #include <stdlib.h>
58 #include <string.h>
59  
60 #include <misc/offset.h>
61 #include <misc/debug.h>
62 #include <misc/balloc.h>
63 #include <structure/LinkedList1.h>
64 #include <structure/LinkedList3.h>
65  
66 #include <ncd/module_common.h>
67  
68 #include <generated/blog_channel_ncd_depend.h>
69  
70 struct provide {
71 NCDModuleInst *i;
72 MemRef name;
73 int is_queued;
74 union {
75 struct {
76 LinkedList3Node queued_node; // node in list which begins with provide.queued_provides_firstnode
77 };
78 struct {
79 LinkedList1Node provides_node; // node in provides
80 LinkedList1 depends;
81 LinkedList3Node queued_provides_firstnode;
82 int dying;
83 };
84 };
85 };
86  
87 struct depend {
88 NCDModuleInst *i;
89 MemRef name;
90 struct provide *p;
91 LinkedList1Node node;
92 };
93  
94 struct global {
95 LinkedList1 provides;
96 LinkedList1 free_depends;
97 };
98  
99 static struct provide * find_provide (struct global *g, MemRef name)
100 {
101 for (LinkedList1Node *n = LinkedList1_GetFirst(&g->provides); n; n = LinkedList1Node_Next(n)) {
102 struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
103 ASSERT(!p->is_queued)
104  
105 if (MemRef_Equal(p->name, name)) {
106 return p;
107 }
108 }
109  
110 return NULL;
111 }
112  
113 static void provide_promote (struct provide *o)
114 {
115 struct global *g = ModuleGlobal(o->i);
116 ASSERT(!find_provide(g, o->name))
117  
118 // set not queued
119 o->is_queued = 0;
120  
121 // insert to provides list
122 LinkedList1_Append(&g->provides, &o->provides_node);
123  
124 // init depends list
125 LinkedList1_Init(&o->depends);
126  
127 // set not dying
128 o->dying = 0;
129  
130 // attach free depends with this name
131 LinkedList1Node *n = LinkedList1_GetFirst(&g->free_depends);
132 while (n) {
133 LinkedList1Node *next = LinkedList1Node_Next(n);
134 struct depend *d = UPPER_OBJECT(n, struct depend, node);
135 ASSERT(!d->p)
136  
137 if (!MemRef_Equal(d->name, o->name)) {
138 n = next;
139 continue;
140 }
141  
142 // remove from free depends list
143 LinkedList1_Remove(&g->free_depends, &d->node);
144  
145 // insert to provide's list
146 LinkedList1_Append(&o->depends, &d->node);
147  
148 // set provide
149 d->p = o;
150  
151 // signal up
152 NCDModuleInst_Backend_Up(d->i);
153  
154 n = next;
155 }
156 }
157  
158 static int func_globalinit (struct NCDInterpModuleGroup *group, const struct NCDModuleInst_iparams *params)
159 {
160 // allocate global state structure
161 struct global *g = BAlloc(sizeof(*g));
162 if (!g) {
163 BLog(BLOG_ERROR, "BAlloc failed");
164 return 0;
165 }
166  
167 // set group state pointer
168 group->group_state = g;
169  
170 // init provides list
171 LinkedList1_Init(&g->provides);
172  
173 // init free depends list
174 LinkedList1_Init(&g->free_depends);
175  
176 return 1;
177 }
178  
179 static void func_globalfree (struct NCDInterpModuleGroup *group)
180 {
181 struct global *g = group->group_state;
182 ASSERT(LinkedList1_IsEmpty(&g->free_depends))
183 ASSERT(LinkedList1_IsEmpty(&g->provides))
184  
185 // free global state structure
186 BFree(g);
187 }
188  
189 static void provide_func_new_templ (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params, int event)
190 {
191 struct global *g = ModuleGlobal(i);
192 struct provide *o = vo;
193 o->i = i;
194  
195 // read arguments
196 NCDValRef name_arg;
197 if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
198 ModuleLog(o->i, BLOG_ERROR, "wrong arity");
199 goto fail0;
200 }
201 if (!NCDVal_IsString(name_arg)) {
202 ModuleLog(o->i, BLOG_ERROR, "wrong type");
203 goto fail0;
204 }
205 o->name = NCDVal_StringMemRef(name_arg);
206  
207 // signal up.
208 // This comes above provide_promote(), so that effects on related depend statements are
209 // computed before this process advances, avoiding problems like failed variable resolutions.
210 NCDModuleInst_Backend_Up(o->i);
211  
212 // check for existing provide with this name
213 struct provide *ep = find_provide(g, o->name);
214 if (ep) {
215 ASSERT(!ep->is_queued)
216  
217 if (!event) {
218 ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
219 goto fail0;
220 }
221  
222 // set queued
223 o->is_queued = 1;
224  
225 // insert to existing provide's queued provides list
226 LinkedList3Node_InitAfter(&o->queued_node, &ep->queued_provides_firstnode);
227 } else {
228 // init first node for queued provides list
229 LinkedList3Node_InitLonely(&o->queued_provides_firstnode);
230  
231 // promote provide
232 provide_promote(o);
233 }
234  
235 return;
236  
237 fail0:
238 NCDModuleInst_Backend_DeadError(i);
239 }
240  
241 static void provide_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
242 {
243 provide_func_new_templ(vo, i, params, 0);
244 }
245  
246 static void provide_event_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
247 {
248 provide_func_new_templ(vo, i, params, 1);
249 }
250  
251 static void provide_free (struct provide *o)
252 {
253 struct global *g = ModuleGlobal(o->i);
254 ASSERT(o->is_queued || LinkedList1_IsEmpty(&o->depends))
255  
256 if (o->is_queued) {
257 // remove from existing provide's queued provides list
258 LinkedList3Node_Free(&o->queued_node);
259 } else {
260 // remove from provides list
261 LinkedList1_Remove(&g->provides, &o->provides_node);
262  
263 // if we have provides queued, promote the first one
264 if (LinkedList3Node_Next(&o->queued_provides_firstnode)) {
265 // get first queued provide
266 struct provide *qp = UPPER_OBJECT(LinkedList3Node_Next(&o->queued_provides_firstnode), struct provide, queued_node);
267 ASSERT(qp->is_queued)
268  
269 // make it the head of the queued provides list
270 LinkedList3Node_Free(&qp->queued_node);
271 LinkedList3Node_InitAfter(&qp->queued_provides_firstnode, &o->queued_provides_firstnode);
272 LinkedList3Node_Free(&o->queued_provides_firstnode);
273  
274 // promote provide
275 provide_promote(qp);
276 }
277 }
278  
279 NCDModuleInst_Backend_Dead(o->i);
280 }
281  
282 static void provide_func_die (void *vo)
283 {
284 struct provide *o = vo;
285 ASSERT(o->is_queued || !o->dying)
286  
287 // if we are queued or have no depends, die immediately
288 if (o->is_queued || LinkedList1_IsEmpty(&o->depends)) {
289 provide_free(o);
290 return;
291 }
292  
293 // set dying
294 o->dying = 1;
295  
296 // signal our depends down
297 for (LinkedList1Node *n = LinkedList1_GetFirst(&o->depends); n; n = LinkedList1Node_Next(n)) {
298 struct depend *d = UPPER_OBJECT(n, struct depend, node);
299 ASSERT(d->p == o)
300  
301 // signal down
302 NCDModuleInst_Backend_Down(d->i);
303 }
304 }
305  
306 static void depend_func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
307 {
308 struct global *g = ModuleGlobal(i);
309 struct depend *o = vo;
310 o->i = i;
311  
312 // read arguments
313 NCDValRef name_arg;
314 if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
315 ModuleLog(o->i, BLOG_ERROR, "wrong arity");
316 goto fail0;
317 }
318 if (!NCDVal_IsString(name_arg)) {
319 ModuleLog(o->i, BLOG_ERROR, "wrong type");
320 goto fail0;
321 }
322 o->name = NCDVal_StringMemRef(name_arg);
323  
324 // find a provide with our name
325 struct provide *p = find_provide(g, o->name);
326 ASSERT(!p || !p->is_queued)
327  
328 if (p && !p->dying) {
329 // insert to provide's list
330 LinkedList1_Append(&p->depends, &o->node);
331  
332 // set provide
333 o->p = p;
334  
335 // signal up
336 NCDModuleInst_Backend_Up(o->i);
337 } else {
338 // insert to free depends list
339 LinkedList1_Append(&g->free_depends, &o->node);
340  
341 // set no provide
342 o->p = NULL;
343 }
344  
345 return;
346  
347 fail0:
348 NCDModuleInst_Backend_DeadError(i);
349 }
350  
351 static void depend_free (struct depend *o)
352 {
353 struct global *g = ModuleGlobal(o->i);
354 ASSERT(!o->p || !o->p->is_queued)
355  
356 if (o->p) {
357 // remove from provide's list
358 LinkedList1_Remove(&o->p->depends, &o->node);
359  
360 // if provide is dying and is empty, let it die
361 if (o->p->dying && LinkedList1_IsEmpty(&o->p->depends)) {
362 provide_free(o->p);
363 }
364 } else {
365 // remove free depends list
366 LinkedList1_Remove(&g->free_depends, &o->node);
367 }
368  
369 NCDModuleInst_Backend_Dead(o->i);
370 }
371  
372 static void depend_func_die (void *vo)
373 {
374 struct depend *o = vo;
375  
376 depend_free(o);
377 }
378  
379 static void depend_func_clean (void *vo)
380 {
381 struct depend *o = vo;
382 struct global *g = ModuleGlobal(o->i);
383 ASSERT(!o->p || !o->p->is_queued)
384  
385 if (!(o->p && o->p->dying)) {
386 return;
387 }
388  
389 struct provide *p = o->p;
390  
391 // remove from provide's list
392 LinkedList1_Remove(&p->depends, &o->node);
393  
394 // insert to free depends list
395 LinkedList1_Append(&g->free_depends, &o->node);
396  
397 // set no provide
398 o->p = NULL;
399  
400 // if provide is empty, let it die
401 if (LinkedList1_IsEmpty(&p->depends)) {
402 provide_free(p);
403 }
404 }
405  
406 static int depend_func_getobj (void *vo, NCD_string_id_t objname, NCDObject *out_object)
407 {
408 struct depend *o = vo;
409 ASSERT(!o->p || !o->p->is_queued)
410  
411 if (!o->p) {
412 return 0;
413 }
414  
415 return NCDModuleInst_Backend_GetObj(o->p->i, objname, out_object);
416 }
417  
418 static struct NCDModule modules[] = {
419 {
420 .type = "provide",
421 .func_new2 = provide_func_new,
422 .func_die = provide_func_die,
423 .alloc_size = sizeof(struct provide)
424 }, {
425 .type = "provide_event",
426 .func_new2 = provide_event_func_new,
427 .func_die = provide_func_die,
428 .alloc_size = sizeof(struct provide)
429 }, {
430 .type = "depend",
431 .func_new2 = depend_func_new,
432 .func_die = depend_func_die,
433 .func_clean = depend_func_clean,
434 .func_getobj = depend_func_getobj,
435 .flags = NCDMODULE_FLAG_CAN_RESOLVE_WHEN_DOWN,
436 .alloc_size = sizeof(struct depend)
437 }, {
438 .type = NULL
439 }
440 };
441  
442 const struct NCDModuleGroup ncdmodule_depend = {
443 .func_globalinit = func_globalinit,
444 .func_globalfree = func_globalfree,
445 .modules = modules
446 };