multidepend.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  1. /**
  2. * @file multidepend.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. * Multiple-option dependencies module.
  25. *
  26. * Synopsis: multiprovide(string name)
  27. * Arguments:
  28. * name - provider identifier
  29. *
  30. * Synopsis: multidepend(list(string) names)
  31. * Arguments:
  32. * names - list of provider identifiers. The dependency is satisfied by any
  33. * provide statement with a provider identifier contained in this list.
  34. * The order of provider identifiers in the list specifies priority
  35. * (higher priority first).
  36. * Variables: Provides variables available from the corresponding provide,
  37. * ("modname.varname" or "modname").
  38. */
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #include <misc/offset.h>
  42. #include <misc/debug.h>
  43. #include <structure/LinkedList2.h>
  44. #include <ncd/NCDModule.h>
  45. #include <generated/blog_channel_ncd_multidepend.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. struct provide {
  48. NCDModuleInst *i;
  49. char *name;
  50. LinkedList2Node provides_node;
  51. LinkedList2 depends;
  52. int dying;
  53. };
  54. struct depend {
  55. NCDModuleInst *i;
  56. NCDValue *names;
  57. LinkedList2Node depends_node;
  58. struct provide *provide;
  59. LinkedList2Node provide_node;
  60. int provide_collapsing;
  61. };
  62. static LinkedList2 provides;
  63. static LinkedList2 depends;
  64. static struct provide * find_provide (const char *name)
  65. {
  66. LinkedList2Iterator it;
  67. LinkedList2Iterator_InitForward(&it, &provides);
  68. LinkedList2Node *n;
  69. while (n = LinkedList2Iterator_Next(&it)) {
  70. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  71. if (!strcmp(p->name, name)) {
  72. LinkedList2Iterator_Free(&it);
  73. return p;
  74. }
  75. }
  76. return NULL;
  77. }
  78. static struct provide * depend_find_best_provide (struct depend *o)
  79. {
  80. NCDValue *e = NCDValue_ListFirst(o->names);
  81. while (e) {
  82. struct provide *p = find_provide(NCDValue_StringValue(e));
  83. if (p && !p->dying) {
  84. return p;
  85. }
  86. e = NCDValue_ListNext(o->names, e);
  87. }
  88. return NULL;
  89. }
  90. static void depend_update (struct depend *o)
  91. {
  92. // if we're collapsing, do nothing
  93. if (o->provide && o->provide_collapsing) {
  94. return;
  95. }
  96. // find best provide
  97. struct provide *bp = depend_find_best_provide(o);
  98. ASSERT(!bp || !bp->dying)
  99. // has anything changed?
  100. if (bp == o->provide) {
  101. return;
  102. }
  103. if (o->provide) {
  104. // set collapsing
  105. o->provide_collapsing = 1;
  106. // signal down
  107. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  108. } else {
  109. // insert to provide's list
  110. LinkedList2_Append(&bp->depends, &o->provide_node);
  111. // set not collapsing
  112. o->provide_collapsing = 0;
  113. // set provide
  114. o->provide = bp;
  115. // signal up
  116. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  117. }
  118. }
  119. static int func_globalinit (struct NCDModuleInitParams params)
  120. {
  121. // init provides list
  122. LinkedList2_Init(&provides);
  123. // init depends list
  124. LinkedList2_Init(&depends);
  125. return 1;
  126. }
  127. static void provide_func_new (NCDModuleInst *i)
  128. {
  129. // allocate instance
  130. struct provide *o = malloc(sizeof(*o));
  131. if (!o) {
  132. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  133. goto fail0;
  134. }
  135. NCDModuleInst_Backend_SetUser(i, o);
  136. // init arguments
  137. o->i = i;
  138. // read arguments
  139. NCDValue *name_arg;
  140. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  141. ModuleLog(i, BLOG_ERROR, "wrong arity");
  142. goto fail1;
  143. }
  144. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  145. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  146. goto fail1;
  147. }
  148. o->name = NCDValue_StringValue(name_arg);
  149. // check for existing provide with this name
  150. if (find_provide(o->name)) {
  151. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  152. goto fail1;
  153. }
  154. // insert to provides list
  155. LinkedList2_Append(&provides, &o->provides_node);
  156. // init depends list
  157. LinkedList2_Init(&o->depends);
  158. // set not dying
  159. o->dying = 0;
  160. // signal up.
  161. // This comes above the loop which follows, so that effects on related depend statements are
  162. // computed before this process advances, avoiding problems like failed variable resolutions.
  163. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  164. // update depends
  165. LinkedList2Iterator it;
  166. LinkedList2Iterator_InitForward(&it, &depends);
  167. LinkedList2Node *n;
  168. while (n = LinkedList2Iterator_Next(&it)) {
  169. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  170. depend_update(d);
  171. }
  172. return;
  173. fail1:
  174. free(o);
  175. fail0:
  176. NCDModuleInst_Backend_SetError(i);
  177. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  178. }
  179. static void provide_free (struct provide *o)
  180. {
  181. ASSERT(LinkedList2_IsEmpty(&o->depends))
  182. NCDModuleInst *i = o->i;
  183. // remove from provides list
  184. LinkedList2_Remove(&provides, &o->provides_node);
  185. // free instance
  186. free(o);
  187. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  188. }
  189. static void provide_func_die (void *vo)
  190. {
  191. struct provide *o = vo;
  192. ASSERT(!o->dying)
  193. // if we have no depends, die immediately
  194. if (LinkedList2_IsEmpty(&o->depends)) {
  195. provide_free(o);
  196. return;
  197. }
  198. // set dying
  199. o->dying = 1;
  200. // start collapsing our depends
  201. LinkedList2Iterator it;
  202. LinkedList2Iterator_InitForward(&it, &o->depends);
  203. LinkedList2Node *n;
  204. while (n = LinkedList2Iterator_Next(&it)) {
  205. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  206. ASSERT(d->provide == o)
  207. // update depend to make sure it is collapsing
  208. depend_update(d);
  209. }
  210. }
  211. static void depend_func_new (NCDModuleInst *i)
  212. {
  213. // allocate instance
  214. struct depend *o = malloc(sizeof(*o));
  215. if (!o) {
  216. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  217. goto fail0;
  218. }
  219. NCDModuleInst_Backend_SetUser(i, o);
  220. // init arguments
  221. o->i = i;
  222. // read arguments
  223. NCDValue *names_arg;
  224. if (!NCDValue_ListRead(o->i->args, 1, &names_arg)) {
  225. ModuleLog(i, BLOG_ERROR, "wrong arity");
  226. goto fail1;
  227. }
  228. if (NCDValue_Type(names_arg) != NCDVALUE_LIST) {
  229. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  230. goto fail1;
  231. }
  232. o->names = names_arg;
  233. // check names list
  234. NCDValue *e = NCDValue_ListFirst(o->names);
  235. while (e) {
  236. if (NCDValue_Type(e) != NCDVALUE_STRING) {
  237. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  238. goto fail1;
  239. }
  240. e = NCDValue_ListNext(o->names, e);
  241. }
  242. // insert to depends list
  243. LinkedList2_Append(&depends, &o->depends_node);
  244. // set no provide
  245. o->provide = NULL;
  246. // update
  247. depend_update(o);
  248. return;
  249. fail1:
  250. free(o);
  251. fail0:
  252. NCDModuleInst_Backend_SetError(i);
  253. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  254. }
  255. static void depend_free (struct depend *o)
  256. {
  257. NCDModuleInst *i = o->i;
  258. if (o->provide) {
  259. // remove from provide's list
  260. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  261. // if provide is dying and is empty, let it die
  262. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  263. provide_free(o->provide);
  264. }
  265. }
  266. // remove from depends list
  267. LinkedList2_Remove(&depends, &o->depends_node);
  268. // free instance
  269. free(o);
  270. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  271. }
  272. static void depend_func_die (void *vo)
  273. {
  274. struct depend *o = vo;
  275. depend_free(o);
  276. }
  277. static void depend_func_clean (void *vo)
  278. {
  279. struct depend *o = vo;
  280. if (!(o->provide && o->provide_collapsing)) {
  281. return;
  282. }
  283. // remove from provide's list
  284. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  285. // if provide is dying and is empty, let it die
  286. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  287. provide_free(o->provide);
  288. }
  289. // set no provide
  290. o->provide = NULL;
  291. // update
  292. depend_update(o);
  293. }
  294. static int depend_func_getvar (void *vo, const char *name_orig, NCDValue *out)
  295. {
  296. struct depend *o = vo;
  297. ASSERT(o->provide)
  298. ASSERT(!o->provide_collapsing)
  299. ASSERT(!o->provide->dying)
  300. int ret = 0;
  301. char *name = strdup(name_orig);
  302. if (!name) {
  303. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  304. goto fail0;
  305. }
  306. const char *modname;
  307. const char *varname;
  308. char *dot = strstr(name, ".");
  309. if (!dot) {
  310. modname = name;
  311. varname = "";
  312. } else {
  313. *dot = '\0';
  314. modname = name;
  315. varname = dot + 1;
  316. }
  317. ret = NCDModuleInst_Backend_GetVar(o->provide->i, modname, varname, out);
  318. free(name);
  319. fail0:
  320. return ret;
  321. }
  322. static NCDModuleInst * depend_func_getobj (void *vo, const char *objname)
  323. {
  324. struct depend *o = vo;
  325. ASSERT(o->provide)
  326. ASSERT(!o->provide_collapsing)
  327. ASSERT(!o->provide->dying)
  328. return NCDModuleInst_Backend_GetObj(o->provide->i, objname);
  329. }
  330. static const struct NCDModule modules[] = {
  331. {
  332. .type = "multiprovide",
  333. .func_new = provide_func_new,
  334. .func_die = provide_func_die
  335. }, {
  336. .type = "multidepend",
  337. .func_new = depend_func_new,
  338. .func_die = depend_func_die,
  339. .func_clean = depend_func_clean,
  340. .func_getvar = depend_func_getvar,
  341. .func_getobj = depend_func_getobj
  342. }, {
  343. .type = NULL
  344. }
  345. };
  346. const struct NCDModuleGroup ncdmodule_multidepend = {
  347. .func_globalinit = func_globalinit,
  348. .modules = modules
  349. };