depend.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /**
  2. * @file 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. * Dependencies module.
  25. *
  26. * Synopsis: provide(string name)
  27. * Description: Provides a resource. On initialization, transitions any depend()-s
  28. * waiting for this resource to UP state. On deinitialization, transitions
  29. * depend()-s using this resource to DOWN state, and waits for all of them to
  30. * receive the clean signal (i.e. wait for all of the statements following them in
  31. * their processes to terminate). Initialization fails if a provide() already
  32. * exists for this resource (including if it is being deinitialized).
  33. *
  34. * Synopsis: provide_event(string name)
  35. * Description: Like provide(), but if another provide() already exists for this
  36. * resource, initialization does not fail, and the request is queued to the active
  37. * provide() for this resource. When an active provide() disappears that has
  38. * queued provide()-s, one of them is promoted to be the active provide() for this
  39. * resource, and the remaining queue is transferred to it.
  40. * (mentions of provide() in this text also apply to provide_event())
  41. *
  42. * Synopsis: depend(string name)
  43. * Description: Depends on a resource. Is in UP state when a provide()
  44. * for this resource is available, and in DOWN state when it is not (either
  45. * it does not exist or is being terminated).
  46. * Variables: Provides variables available from the corresponding provide,
  47. * ("modname.varname" or "modname").
  48. */
  49. #include <stdlib.h>
  50. #include <string.h>
  51. #include <misc/offset.h>
  52. #include <misc/debug.h>
  53. #include <structure/LinkedList2.h>
  54. #include <structure/LinkedList3.h>
  55. #include <ncd/NCDModule.h>
  56. #include <generated/blog_channel_ncd_depend.h>
  57. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  58. struct provide {
  59. NCDModuleInst *i;
  60. char *name;
  61. int is_queued;
  62. union {
  63. struct {
  64. LinkedList3Node queued_node; // node in list which begins with provide.queued_provides_firstnode
  65. };
  66. struct {
  67. LinkedList2Node provides_node; // node in provides
  68. LinkedList2 depends;
  69. LinkedList3Node queued_provides_firstnode;
  70. int dying;
  71. };
  72. };
  73. };
  74. struct depend {
  75. NCDModuleInst *i;
  76. char *name;
  77. struct provide *p;
  78. LinkedList2Node node;
  79. };
  80. static LinkedList2 provides;
  81. static LinkedList2 free_depends;
  82. static struct provide * find_provide (const char *name)
  83. {
  84. LinkedList2Iterator it;
  85. LinkedList2Iterator_InitForward(&it, &provides);
  86. LinkedList2Node *n;
  87. while (n = LinkedList2Iterator_Next(&it)) {
  88. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  89. ASSERT(!p->is_queued)
  90. if (!strcmp(p->name, name)) {
  91. LinkedList2Iterator_Free(&it);
  92. return p;
  93. }
  94. }
  95. return NULL;
  96. }
  97. static void provide_promote (struct provide *o)
  98. {
  99. ASSERT(!find_provide(o->name))
  100. // set not queued
  101. o->is_queued = 0;
  102. // insert to provides list
  103. LinkedList2_Append(&provides, &o->provides_node);
  104. // init depends list
  105. LinkedList2_Init(&o->depends);
  106. // set not dying
  107. o->dying = 0;
  108. // attach free depends with this name
  109. LinkedList2Iterator it;
  110. LinkedList2Iterator_InitForward(&it, &free_depends);
  111. LinkedList2Node *n;
  112. while (n = LinkedList2Iterator_Next(&it)) {
  113. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  114. ASSERT(!d->p)
  115. if (strcmp(d->name, o->name)) {
  116. continue;
  117. }
  118. // remove from free depends list
  119. LinkedList2_Remove(&free_depends, &d->node);
  120. // insert to provide's list
  121. LinkedList2_Append(&o->depends, &d->node);
  122. // set provide
  123. d->p = o;
  124. // signal up
  125. NCDModuleInst_Backend_Event(d->i, NCDMODULE_EVENT_UP);
  126. }
  127. }
  128. static int func_globalinit (struct NCDModuleInitParams params)
  129. {
  130. // init provides list
  131. LinkedList2_Init(&provides);
  132. // init free depends list
  133. LinkedList2_Init(&free_depends);
  134. return 1;
  135. }
  136. static void provide_func_new_templ (NCDModuleInst *i, int event)
  137. {
  138. // allocate instance
  139. struct provide *o = malloc(sizeof(*o));
  140. if (!o) {
  141. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  142. goto fail0;
  143. }
  144. NCDModuleInst_Backend_SetUser(i, o);
  145. // init arguments
  146. o->i = i;
  147. // read arguments
  148. NCDValue *name_arg;
  149. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  150. ModuleLog(i, BLOG_ERROR, "wrong arity");
  151. goto fail1;
  152. }
  153. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  154. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  155. goto fail1;
  156. }
  157. o->name = NCDValue_StringValue(name_arg);
  158. // signal up.
  159. // This comes above provide_promote(), so that effects on related depend statements are
  160. // computed before this process advances, avoiding problems like failed variable resolutions.
  161. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  162. // check for existing provide with this name
  163. struct provide *ep = find_provide(o->name);
  164. if (ep) {
  165. ASSERT(!ep->is_queued)
  166. if (!event) {
  167. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  168. goto fail1;
  169. }
  170. // set queued
  171. o->is_queued = 1;
  172. // insert to existing provide's queued provides list
  173. LinkedList3Node_InitAfter(&o->queued_node, &ep->queued_provides_firstnode);
  174. } else {
  175. // init first node for queued provides list
  176. LinkedList3Node_InitLonely(&o->queued_provides_firstnode);
  177. // promote provide
  178. provide_promote(o);
  179. }
  180. return;
  181. fail1:
  182. free(o);
  183. fail0:
  184. NCDModuleInst_Backend_SetError(i);
  185. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  186. }
  187. static void provide_func_new (NCDModuleInst *i)
  188. {
  189. provide_func_new_templ(i, 0);
  190. }
  191. static void provide_event_func_new (NCDModuleInst *i)
  192. {
  193. provide_func_new_templ(i, 1);
  194. }
  195. static void provide_free (struct provide *o)
  196. {
  197. ASSERT(o->is_queued || LinkedList2_IsEmpty(&o->depends))
  198. NCDModuleInst *i = o->i;
  199. if (o->is_queued) {
  200. // remove from existing provide's queued provides list
  201. LinkedList3Node_Free(&o->queued_node);
  202. } else {
  203. // remove from provides list
  204. LinkedList2_Remove(&provides, &o->provides_node);
  205. // if we have provides queued, promote the first one
  206. if (LinkedList3Node_Next(&o->queued_provides_firstnode)) {
  207. // get first queued provide
  208. struct provide *qp = UPPER_OBJECT(LinkedList3Node_Next(&o->queued_provides_firstnode), struct provide, queued_node);
  209. ASSERT(qp->is_queued)
  210. // make it the head of the queued provides list
  211. LinkedList3Node_Free(&qp->queued_node);
  212. LinkedList3Node_InitAfter(&qp->queued_provides_firstnode, &o->queued_provides_firstnode);
  213. LinkedList3Node_Free(&o->queued_provides_firstnode);
  214. // promote provide
  215. provide_promote(qp);
  216. }
  217. }
  218. // free instance
  219. free(o);
  220. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  221. }
  222. static void provide_func_die (void *vo)
  223. {
  224. struct provide *o = vo;
  225. ASSERT(o->is_queued || !o->dying)
  226. // if we are queued or have no depends, die immediately
  227. if (o->is_queued || LinkedList2_IsEmpty(&o->depends)) {
  228. provide_free(o);
  229. return;
  230. }
  231. // set dying
  232. o->dying = 1;
  233. // signal our depends down
  234. LinkedList2Iterator it;
  235. LinkedList2Iterator_InitForward(&it, &o->depends);
  236. LinkedList2Node *n;
  237. while (n = LinkedList2Iterator_Next(&it)) {
  238. struct depend *d = UPPER_OBJECT(n, struct depend, node);
  239. ASSERT(d->p == o)
  240. // signal down
  241. NCDModuleInst_Backend_Event(d->i, NCDMODULE_EVENT_DOWN);
  242. }
  243. }
  244. static void depend_func_new (NCDModuleInst *i)
  245. {
  246. // allocate instance
  247. struct depend *o = malloc(sizeof(*o));
  248. if (!o) {
  249. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  250. goto fail0;
  251. }
  252. NCDModuleInst_Backend_SetUser(i, o);
  253. // init arguments
  254. o->i = i;
  255. // read arguments
  256. NCDValue *name_arg;
  257. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  258. ModuleLog(i, BLOG_ERROR, "wrong arity");
  259. goto fail1;
  260. }
  261. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  262. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  263. goto fail1;
  264. }
  265. o->name = NCDValue_StringValue(name_arg);
  266. // find a provide with our name
  267. struct provide *p = find_provide(o->name);
  268. ASSERT(!p || !p->is_queued)
  269. if (p && !p->dying) {
  270. // insert to provide's list
  271. LinkedList2_Append(&p->depends, &o->node);
  272. // set provide
  273. o->p = p;
  274. // signal up
  275. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  276. } else {
  277. // insert to free depends list
  278. LinkedList2_Append(&free_depends, &o->node);
  279. // set no provide
  280. o->p = NULL;
  281. }
  282. return;
  283. fail1:
  284. free(o);
  285. fail0:
  286. NCDModuleInst_Backend_SetError(i);
  287. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  288. }
  289. static void depend_free (struct depend *o)
  290. {
  291. NCDModuleInst *i = o->i;
  292. ASSERT(!o->p || !o->p->is_queued)
  293. if (o->p) {
  294. // remove from provide's list
  295. LinkedList2_Remove(&o->p->depends, &o->node);
  296. // if provide is dying and is empty, let it die
  297. if (o->p->dying && LinkedList2_IsEmpty(&o->p->depends)) {
  298. provide_free(o->p);
  299. }
  300. } else {
  301. // remove free depends list
  302. LinkedList2_Remove(&free_depends, &o->node);
  303. }
  304. // free instance
  305. free(o);
  306. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  307. }
  308. static void depend_func_die (void *vo)
  309. {
  310. struct depend *o = vo;
  311. depend_free(o);
  312. }
  313. static void depend_func_clean (void *vo)
  314. {
  315. struct depend *o = vo;
  316. ASSERT(!o->p || !o->p->is_queued)
  317. if (!(o->p && o->p->dying)) {
  318. return;
  319. }
  320. struct provide *p = o->p;
  321. // remove from provide's list
  322. LinkedList2_Remove(&p->depends, &o->node);
  323. // insert to free depends list
  324. LinkedList2_Append(&free_depends, &o->node);
  325. // set no provide
  326. o->p = NULL;
  327. // if provide is empty, let it die
  328. if (LinkedList2_IsEmpty(&p->depends)) {
  329. provide_free(p);
  330. }
  331. }
  332. static int depend_func_getvar (void *vo, const char *varname, NCDValue *out)
  333. {
  334. struct depend *o = vo;
  335. ASSERT(o->p)
  336. ASSERT(!o->p->is_queued)
  337. ASSERT(!o->p->dying)
  338. return NCDModuleInst_Backend_GetVar(o->p->i, varname, out);
  339. }
  340. static NCDModuleInst * depend_func_getobj (void *vo, const char *objname)
  341. {
  342. struct depend *o = vo;
  343. ASSERT(o->p)
  344. ASSERT(!o->p->is_queued)
  345. ASSERT(!o->p->dying)
  346. return NCDModuleInst_Backend_GetObj(o->p->i, objname);
  347. }
  348. static const struct NCDModule modules[] = {
  349. {
  350. .type = "provide",
  351. .func_new = provide_func_new,
  352. .func_die = provide_func_die
  353. }, {
  354. .type = "provide_event",
  355. .func_new = provide_event_func_new,
  356. .func_die = provide_func_die
  357. }, {
  358. .type = "depend",
  359. .func_new = depend_func_new,
  360. .func_die = depend_func_die,
  361. .func_clean = depend_func_clean,
  362. .func_getvar = depend_func_getvar,
  363. .func_getobj = depend_func_getobj
  364. }, {
  365. .type = NULL
  366. }
  367. };
  368. const struct NCDModuleGroup ncdmodule_depend = {
  369. .func_globalinit = func_globalinit,
  370. .modules = modules
  371. };