multidepend.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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 <misc/offset.h>
  41. #include <structure/LinkedList2.h>
  42. #include <ncd/NCDModule.h>
  43. #include <generated/blog_channel_ncd_multidepend.h>
  44. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  45. struct provide {
  46. NCDModuleInst *i;
  47. char *name;
  48. LinkedList2Node provides_node;
  49. LinkedList2 depends;
  50. int dying;
  51. };
  52. struct depend {
  53. NCDModuleInst *i;
  54. NCDValue *names;
  55. LinkedList2Node depends_node;
  56. struct provide *provide;
  57. LinkedList2Node provide_node;
  58. int provide_collapsing;
  59. };
  60. LinkedList2 provides;
  61. LinkedList2 depends;
  62. static struct provide * find_provide (const char *name)
  63. {
  64. LinkedList2Iterator it;
  65. LinkedList2Iterator_InitForward(&it, &provides);
  66. LinkedList2Node *n;
  67. while (n = LinkedList2Iterator_Next(&it)) {
  68. struct provide *p = UPPER_OBJECT(n, struct provide, provides_node);
  69. if (!strcmp(p->name, name)) {
  70. LinkedList2Iterator_Free(&it);
  71. return p;
  72. }
  73. }
  74. return NULL;
  75. }
  76. static struct provide * depend_find_best_provide (struct depend *o)
  77. {
  78. NCDValue *e = NCDValue_ListFirst(o->names);
  79. while (e) {
  80. struct provide *p = find_provide(NCDValue_StringValue(e));
  81. if (p && !p->dying) {
  82. return p;
  83. }
  84. e = NCDValue_ListNext(o->names, e);
  85. }
  86. return NULL;
  87. }
  88. static void depend_update (struct depend *o)
  89. {
  90. // if we're collapsing, do nothing
  91. if (o->provide && o->provide_collapsing) {
  92. return;
  93. }
  94. // find best provide
  95. struct provide *bp = depend_find_best_provide(o);
  96. // has anything changed?
  97. if (bp == o->provide) {
  98. return;
  99. }
  100. // if we have an existing provide, start collpsing
  101. if (o->provide) {
  102. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_DOWN);
  103. o->provide_collapsing = 1;
  104. return;
  105. }
  106. if (bp) {
  107. // insert to provide's list
  108. LinkedList2_Append(&bp->depends, &o->provide_node);
  109. // set not collapsing
  110. o->provide_collapsing = 0;
  111. // set provide
  112. o->provide = bp;
  113. // signal up
  114. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  115. }
  116. }
  117. static int func_globalinit (struct NCDModuleInitParams params)
  118. {
  119. // init provides list
  120. LinkedList2_Init(&provides);
  121. // init depends list
  122. LinkedList2_Init(&depends);
  123. return 1;
  124. }
  125. static void * provide_func_new (NCDModuleInst *i)
  126. {
  127. // allocate instance
  128. struct provide *o = malloc(sizeof(*o));
  129. if (!o) {
  130. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  131. goto fail0;
  132. }
  133. // init arguments
  134. o->i = i;
  135. // read arguments
  136. NCDValue *name_arg;
  137. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  138. ModuleLog(i, BLOG_ERROR, "wrong arity");
  139. goto fail1;
  140. }
  141. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  142. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  143. goto fail1;
  144. }
  145. o->name = NCDValue_StringValue(name_arg);
  146. // check for existing provide with this name
  147. if (find_provide(o->name)) {
  148. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  149. goto fail1;
  150. }
  151. // insert to provides list
  152. LinkedList2_Append(&provides, &o->provides_node);
  153. // init depends list
  154. LinkedList2_Init(&o->depends);
  155. // set not dying
  156. o->dying = 0;
  157. // update depends
  158. LinkedList2Iterator it;
  159. LinkedList2Iterator_InitForward(&it, &depends);
  160. LinkedList2Node *n;
  161. while (n = LinkedList2Iterator_Next(&it)) {
  162. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  163. depend_update(d);
  164. }
  165. // signal up
  166. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  167. return o;
  168. fail1:
  169. free(o);
  170. fail0:
  171. return NULL;
  172. }
  173. static void provide_func_free (void *vo)
  174. {
  175. struct provide *o = vo;
  176. ASSERT(LinkedList2_IsEmpty(&o->depends))
  177. // remove from provides list
  178. LinkedList2_Remove(&provides, &o->provides_node);
  179. // free instance
  180. free(o);
  181. }
  182. static void provide_func_die (void *vo)
  183. {
  184. struct provide *o = vo;
  185. ASSERT(!o->dying)
  186. // if we have no depends, die immediately
  187. if (LinkedList2_IsEmpty(&o->depends)) {
  188. NCDModuleInst_Backend_Died(o->i, 0);
  189. return;
  190. }
  191. // set dying
  192. o->dying = 1;
  193. // start collapsing our depends
  194. LinkedList2Iterator it;
  195. LinkedList2Iterator_InitForward(&it, &o->depends);
  196. LinkedList2Node *n;
  197. while (n = LinkedList2Iterator_Next(&it)) {
  198. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  199. ASSERT(d->provide == o)
  200. depend_update(d);
  201. }
  202. }
  203. static void * depend_func_new (NCDModuleInst *i)
  204. {
  205. // allocate instance
  206. struct depend *o = malloc(sizeof(*o));
  207. if (!o) {
  208. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  209. goto fail0;
  210. }
  211. // init arguments
  212. o->i = i;
  213. // read arguments
  214. NCDValue *names_arg;
  215. if (!NCDValue_ListRead(o->i->args, 1, &names_arg)) {
  216. ModuleLog(i, BLOG_ERROR, "wrong arity");
  217. goto fail1;
  218. }
  219. if (NCDValue_Type(names_arg) != NCDVALUE_LIST) {
  220. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  221. goto fail1;
  222. }
  223. o->names = names_arg;
  224. // check names list
  225. NCDValue *e = NCDValue_ListFirst(o->names);
  226. while (e) {
  227. if (NCDValue_Type(e) != NCDVALUE_STRING) {
  228. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  229. goto fail1;
  230. }
  231. e = NCDValue_ListNext(o->names, e);
  232. }
  233. // insert to depends list
  234. LinkedList2_Append(&depends, &o->depends_node);
  235. // set no provide
  236. o->provide = NULL;
  237. // update
  238. depend_update(o);
  239. return o;
  240. fail1:
  241. free(o);
  242. fail0:
  243. return NULL;
  244. }
  245. static void depend_func_free (void *vo)
  246. {
  247. struct depend *o = vo;
  248. if (o->provide) {
  249. // remove from provide's list
  250. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  251. // if provide is dying and is empty, let it die
  252. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  253. NCDModuleInst_Backend_Died(o->provide->i, 0);
  254. }
  255. }
  256. // remove from depends list
  257. LinkedList2_Remove(&depends, &o->depends_node);
  258. // free instance
  259. free(o);
  260. }
  261. static void depend_func_clean (void *vo)
  262. {
  263. struct depend *o = vo;
  264. if (!(o->provide && o->provide_collapsing)) {
  265. return;
  266. }
  267. // remove from provide's list
  268. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  269. // if provide is dying and is empty, let it die
  270. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  271. NCDModuleInst_Backend_Died(o->provide->i, 0);
  272. }
  273. // set no provide
  274. o->provide = NULL;
  275. // update
  276. depend_update(o);
  277. }
  278. static int depend_func_getvar (void *vo, const char *name_orig, NCDValue *out)
  279. {
  280. struct depend *o = vo;
  281. ASSERT(o->provide)
  282. ASSERT(!o->provide_collapsing)
  283. int ret = 0;
  284. char *name = strdup(name_orig);
  285. if (!name) {
  286. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  287. goto fail0;
  288. }
  289. const char *modname;
  290. const char *varname;
  291. char *dot = strstr(name, ".");
  292. if (!dot) {
  293. modname = name;
  294. varname = "";
  295. } else {
  296. *dot = '\0';
  297. modname = name;
  298. varname = dot + 1;
  299. }
  300. ret = NCDModuleInst_Backend_GetVar(o->provide->i, modname, varname, out);
  301. free(name);
  302. fail0:
  303. return ret;
  304. }
  305. static const struct NCDModule modules[] = {
  306. {
  307. .type = "multiprovide",
  308. .func_new = provide_func_new,
  309. .func_free = provide_func_free,
  310. .func_die = provide_func_die
  311. }, {
  312. .type = "multidepend",
  313. .func_new = depend_func_new,
  314. .func_free = depend_func_free,
  315. .func_clean = depend_func_clean,
  316. .func_getvar = depend_func_getvar
  317. }, {
  318. .type = NULL
  319. }
  320. };
  321. const struct NCDModuleGroup ncdmodule_multidepend = {
  322. .func_globalinit = func_globalinit,
  323. .modules = modules
  324. };