dynamic_depend.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539
  1. /**
  2. * @file dynamic_depend.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Dynamic dependencies module.
  25. *
  26. * Synopsis: dynamic_provide(string name, order_value)
  27. * Synopsis: dynamic_depend(string name)
  28. */
  29. #include <stdlib.h>
  30. #include <string.h>
  31. #include <misc/offset.h>
  32. #include <misc/debug.h>
  33. #include <structure/LinkedList0.h>
  34. #include <structure/BAVL.h>
  35. #include <ncd/NCDModule.h>
  36. #include <generated/blog_channel_ncd_dynamic_depend.h>
  37. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  38. struct provide;
  39. struct name {
  40. char *name;
  41. BAVLNode names_tree_node;
  42. BAVL provides_tree;
  43. LinkedList0 waiting_depends_list;
  44. struct provide *cur_p;
  45. LinkedList0 cur_bound_depends_list;
  46. int cur_resetting;
  47. };
  48. struct provide {
  49. NCDModuleInst *i;
  50. struct name *n;
  51. NCDValue *order_value;
  52. BAVLNode provides_tree_node;
  53. int dying;
  54. };
  55. struct depend {
  56. NCDModuleInst *i;
  57. struct name *n;
  58. int is_bound;
  59. LinkedList0Node depends_list_node;
  60. };
  61. static BAVL names_tree;
  62. static void provide_free (struct provide *o);
  63. static void depend_free (struct depend *o);
  64. static int stringptr_comparator (void *user, char **v1, char **v2)
  65. {
  66. int cmp = strcmp(*v1, *v2);
  67. if (cmp < 0) {
  68. return -1;
  69. }
  70. if (cmp > 0) {
  71. return 1;
  72. }
  73. return 0;
  74. }
  75. static int valueptr_comparator (void *user, NCDValue **v1, NCDValue **v2)
  76. {
  77. return NCDValue_Compare(*v1, *v2);
  78. }
  79. static struct name * find_name (const char *name)
  80. {
  81. BAVLNode *tn = BAVL_LookupExact(&names_tree, &name);
  82. if (!tn) {
  83. return NULL;
  84. }
  85. struct name *n = UPPER_OBJECT(tn, struct name, names_tree_node);
  86. ASSERT(!strcmp(n->name, name))
  87. return n;
  88. }
  89. static struct name * name_init (NCDModuleInst *i, const char *name)
  90. {
  91. ASSERT(!find_name(name))
  92. // allocate structure
  93. struct name *o = malloc(sizeof(*o));
  94. if (!o) {
  95. ModuleLog(i, BLOG_ERROR, "malloc failed");
  96. goto fail0;
  97. }
  98. // copy name
  99. if (!(o->name = strdup(name))) {
  100. ModuleLog(i, BLOG_ERROR, "strdup failed");
  101. goto fail1;
  102. }
  103. // insert to names tree
  104. ASSERT_EXECUTE(BAVL_Insert(&names_tree, &o->names_tree_node, NULL))
  105. // init provides tree
  106. BAVL_Init(&o->provides_tree, OFFSET_DIFF(struct provide, order_value, provides_tree_node), (BAVL_comparator)valueptr_comparator, NULL);
  107. // init waiting depends list
  108. LinkedList0_Init(&o->waiting_depends_list);
  109. // set no current provide
  110. o->cur_p = NULL;
  111. return o;
  112. fail1:
  113. free(o);
  114. fail0:
  115. return NULL;
  116. }
  117. static void name_free (struct name *o)
  118. {
  119. ASSERT(BAVL_IsEmpty(&o->provides_tree))
  120. ASSERT(LinkedList0_IsEmpty(&o->waiting_depends_list))
  121. ASSERT(!o->cur_p)
  122. // remove from names tree
  123. BAVL_Remove(&names_tree, &o->names_tree_node);
  124. // free name
  125. free(o->name);
  126. // free structure
  127. free(o);
  128. }
  129. static struct provide * name_get_first_provide (struct name *o)
  130. {
  131. BAVLNode *tn = BAVL_GetFirst(&o->provides_tree);
  132. if (!tn) {
  133. return NULL;
  134. }
  135. struct provide *p = UPPER_OBJECT(tn, struct provide, provides_tree_node);
  136. ASSERT(p->n == o)
  137. return p;
  138. }
  139. static void name_new_current (struct name *o)
  140. {
  141. ASSERT(!o->cur_p)
  142. ASSERT(!BAVL_IsEmpty(&o->provides_tree))
  143. // set current provide
  144. o->cur_p = name_get_first_provide(o);
  145. // init bound depends list
  146. LinkedList0_Init(&o->cur_bound_depends_list);
  147. // set not resetting
  148. o->cur_resetting = 0;
  149. // bind waiting depends
  150. while (!LinkedList0_IsEmpty(&o->waiting_depends_list)) {
  151. struct depend *d = UPPER_OBJECT(LinkedList0_GetFirst(&o->waiting_depends_list), struct depend, depends_list_node);
  152. ASSERT(d->n == o)
  153. ASSERT(!d->is_bound)
  154. // remove from waiting depends list
  155. LinkedList0_Remove(&o->waiting_depends_list, &d->depends_list_node);
  156. // set bound
  157. d->is_bound = 1;
  158. // add to bound depends list
  159. LinkedList0_Prepend(&o->cur_bound_depends_list, &d->depends_list_node);
  160. // signal up
  161. NCDModuleInst_Backend_Up(d->i);
  162. }
  163. }
  164. static void name_free_if_unused (struct name *o)
  165. {
  166. if (BAVL_IsEmpty(&o->provides_tree) && LinkedList0_IsEmpty(&o->waiting_depends_list)) {
  167. name_free(o);
  168. }
  169. }
  170. static void name_continue_resetting (struct name *o)
  171. {
  172. ASSERT(o->cur_p)
  173. ASSERT(o->cur_resetting)
  174. // still have bound depends?
  175. if (!LinkedList0_IsEmpty(&o->cur_bound_depends_list)) {
  176. return;
  177. }
  178. struct provide *old_p = o->cur_p;
  179. // set no current provide
  180. o->cur_p = NULL;
  181. // free old current provide if it's dying
  182. if (old_p->dying) {
  183. provide_free(old_p);
  184. }
  185. if (!BAVL_IsEmpty(&o->provides_tree)) {
  186. // get new current provide
  187. name_new_current(o);
  188. } else {
  189. // free name if unused
  190. name_free_if_unused(o);
  191. }
  192. }
  193. static void name_start_resetting (struct name *o)
  194. {
  195. ASSERT(o->cur_p)
  196. ASSERT(!o->cur_resetting)
  197. // set resetting
  198. o->cur_resetting = 1;
  199. // signal bound depends down
  200. for (LinkedList0Node *ln = LinkedList0_GetFirst(&o->cur_bound_depends_list); ln; ln = LinkedList0Node_Next(ln)) {
  201. struct depend *d = UPPER_OBJECT(ln, struct depend, depends_list_node);
  202. ASSERT(d->n == o)
  203. ASSERT(d->is_bound)
  204. NCDModuleInst_Backend_Down(d->i);
  205. }
  206. // if there were no bound depends, continue right away
  207. name_continue_resetting(o);
  208. }
  209. static int func_globalinit (struct NCDModuleInitParams params)
  210. {
  211. // init names tree
  212. BAVL_Init(&names_tree, OFFSET_DIFF(struct name, name, names_tree_node), (BAVL_comparator)stringptr_comparator, NULL);
  213. return 1;
  214. }
  215. static void provide_func_new (NCDModuleInst *i)
  216. {
  217. // allocate instance
  218. struct provide *o = malloc(sizeof(*o));
  219. if (!o) {
  220. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  221. goto fail0;
  222. }
  223. NCDModuleInst_Backend_SetUser(i, o);
  224. // init arguments
  225. o->i = i;
  226. // read arguments
  227. NCDValue *name_arg;
  228. if (!NCDValue_ListRead(i->args, 2, &name_arg, &o->order_value)) {
  229. ModuleLog(i, BLOG_ERROR, "wrong arity");
  230. goto fail1;
  231. }
  232. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  233. ModuleLog(i, BLOG_ERROR, "wrong type");
  234. goto fail1;
  235. }
  236. char *name_str = NCDValue_StringValue(name_arg);
  237. // find name, create new if needed
  238. struct name *n = find_name(name_str);
  239. if (!n && !(n = name_init(i, name_str))) {
  240. goto fail1;
  241. }
  242. // set name
  243. o->n = n;
  244. // check for order value conflict
  245. if (BAVL_LookupExact(&n->provides_tree, &o->order_value)) {
  246. ModuleLog(i, BLOG_ERROR, "order value already exists");
  247. goto fail1;
  248. }
  249. // add to name's provides tree
  250. ASSERT_EXECUTE(BAVL_Insert(&n->provides_tree, &o->provides_tree_node, NULL))
  251. // set not dying
  252. o->dying = 0;
  253. // signal up
  254. NCDModuleInst_Backend_Up(i);
  255. // should this be the current provide?
  256. if (o == name_get_first_provide(n)) {
  257. if (!n->cur_p) {
  258. name_new_current(n);
  259. }
  260. else if (!n->cur_resetting) {
  261. name_start_resetting(n);
  262. }
  263. }
  264. return;
  265. fail1:
  266. free(o);
  267. fail0:
  268. NCDModuleInst_Backend_SetError(i);
  269. NCDModuleInst_Backend_Dead(i);
  270. }
  271. static void provide_free (struct provide *o)
  272. {
  273. struct name *n = o->n;
  274. ASSERT(o->dying)
  275. ASSERT(o != n->cur_p)
  276. NCDModuleInst *i = o->i;
  277. // remove from name's provides tree
  278. BAVL_Remove(&n->provides_tree, &o->provides_tree_node);
  279. // free instance
  280. free(o);
  281. NCDModuleInst_Backend_Dead(i);
  282. }
  283. static void provide_func_die (void *vo)
  284. {
  285. struct provide *o = vo;
  286. struct name *n = o->n;
  287. ASSERT(!o->dying)
  288. // set dying
  289. o->dying = 1;
  290. // if this is not the current provide, die right away
  291. if (o != n->cur_p) {
  292. // free provide
  293. provide_free(o);
  294. // free name if unused
  295. name_free_if_unused(n);
  296. return;
  297. }
  298. ASSERT(!n->cur_resetting)
  299. // start resetting
  300. name_start_resetting(n);
  301. }
  302. static void depend_func_new (NCDModuleInst *i)
  303. {
  304. // allocate instance
  305. struct depend *o = malloc(sizeof(*o));
  306. if (!o) {
  307. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  308. goto fail0;
  309. }
  310. NCDModuleInst_Backend_SetUser(i, o);
  311. // init arguments
  312. o->i = i;
  313. // read arguments
  314. NCDValue *name_arg;
  315. if (!NCDValue_ListRead(i->args, 1, &name_arg)) {
  316. ModuleLog(i, BLOG_ERROR, "wrong arity");
  317. goto fail1;
  318. }
  319. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  320. ModuleLog(i, BLOG_ERROR, "wrong type");
  321. goto fail1;
  322. }
  323. char *name_str = NCDValue_StringValue(name_arg);
  324. // find name, create new if needed
  325. struct name *n = find_name(name_str);
  326. if (!n && !(n = name_init(i, name_str))) {
  327. goto fail1;
  328. }
  329. // set name
  330. o->n = n;
  331. if (n->cur_p && !n->cur_resetting) {
  332. // set bound
  333. o->is_bound = 1;
  334. // add to bound depends list
  335. LinkedList0_Prepend(&n->cur_bound_depends_list, &o->depends_list_node);
  336. // signal up
  337. NCDModuleInst_Backend_Up(i);
  338. } else {
  339. // set not bound
  340. o->is_bound = 0;
  341. // add to waiting depends list
  342. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  343. }
  344. return;
  345. fail1:
  346. free(o);
  347. fail0:
  348. NCDModuleInst_Backend_SetError(i);
  349. NCDModuleInst_Backend_Dead(i);
  350. }
  351. static void depend_func_die (void *vo)
  352. {
  353. struct depend *o = vo;
  354. NCDModuleInst *i = o->i;
  355. struct name *n = o->n;
  356. if (o->is_bound) {
  357. ASSERT(n->cur_p)
  358. // remove from bound depends list
  359. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  360. // continue resetting
  361. if (n->cur_resetting) {
  362. name_continue_resetting(n);
  363. }
  364. } else {
  365. // remove from waiting depends list
  366. LinkedList0_Remove(&n->waiting_depends_list, &o->depends_list_node);
  367. // free name if unused
  368. name_free_if_unused(n);
  369. }
  370. // free instance
  371. free(o);
  372. NCDModuleInst_Backend_Dead(i);
  373. }
  374. static void depend_func_clean (void *vo)
  375. {
  376. struct depend *o = vo;
  377. struct name *n = o->n;
  378. ASSERT(!o->is_bound || n->cur_p)
  379. if (!(o->is_bound && n->cur_resetting)) {
  380. return;
  381. }
  382. // remove from bound depends list
  383. LinkedList0_Remove(&n->cur_bound_depends_list, &o->depends_list_node);
  384. // set not bound
  385. o->is_bound = 0;
  386. // add to waiting depends list
  387. LinkedList0_Prepend(&n->waiting_depends_list, &o->depends_list_node);
  388. // continue resetting
  389. name_continue_resetting(n);
  390. }
  391. static int depend_func_getvar (void *vo, const char *varname, NCDValue *out)
  392. {
  393. struct depend *o = vo;
  394. struct name *n = o->n;
  395. ASSERT(!o->is_bound || n->cur_p)
  396. if (!o->is_bound) {
  397. return 0;
  398. }
  399. return NCDModuleInst_Backend_GetVar(n->cur_p->i, varname, out);
  400. }
  401. static NCDModuleInst * depend_func_getobj (void *vo, const char *objname)
  402. {
  403. struct depend *o = vo;
  404. struct name *n = o->n;
  405. ASSERT(!o->is_bound || n->cur_p)
  406. if (!o->is_bound) {
  407. return NULL;
  408. }
  409. return NCDModuleInst_Backend_GetObj(n->cur_p->i, objname);
  410. }
  411. static const struct NCDModule modules[] = {
  412. {
  413. .type = "dynamic_provide",
  414. .func_new = provide_func_new,
  415. .func_die = provide_func_die
  416. }, {
  417. .type = "dynamic_depend",
  418. .func_new = depend_func_new,
  419. .func_die = depend_func_die,
  420. .func_clean = depend_func_clean,
  421. .func_getvar = depend_func_getvar,
  422. .func_getobj = depend_func_getobj,
  423. .can_resolve_when_down = 1
  424. }, {
  425. .type = NULL
  426. }
  427. };
  428. const struct NCDModuleGroup ncdmodule_dynamic_depend = {
  429. .func_globalinit = func_globalinit,
  430. .modules = modules
  431. };