multidepend.c 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  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. // insert to provide's list
  107. LinkedList2_Append(&bp->depends, &o->provide_node);
  108. // set not collapsing
  109. o->provide_collapsing = 0;
  110. // set provide
  111. o->provide = bp;
  112. // signal up
  113. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  114. }
  115. static int func_globalinit (struct NCDModuleInitParams params)
  116. {
  117. // init provides list
  118. LinkedList2_Init(&provides);
  119. // init depends list
  120. LinkedList2_Init(&depends);
  121. return 1;
  122. }
  123. static void * provide_func_new (NCDModuleInst *i)
  124. {
  125. // allocate instance
  126. struct provide *o = malloc(sizeof(*o));
  127. if (!o) {
  128. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  129. goto fail0;
  130. }
  131. // init arguments
  132. o->i = i;
  133. // read arguments
  134. NCDValue *name_arg;
  135. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  136. ModuleLog(i, BLOG_ERROR, "wrong arity");
  137. goto fail1;
  138. }
  139. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  140. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  141. goto fail1;
  142. }
  143. o->name = NCDValue_StringValue(name_arg);
  144. // check for existing provide with this name
  145. if (find_provide(o->name)) {
  146. ModuleLog(o->i, BLOG_ERROR, "a provide with this name already exists");
  147. goto fail1;
  148. }
  149. // insert to provides list
  150. LinkedList2_Append(&provides, &o->provides_node);
  151. // init depends list
  152. LinkedList2_Init(&o->depends);
  153. // set not dying
  154. o->dying = 0;
  155. // update depends
  156. LinkedList2Iterator it;
  157. LinkedList2Iterator_InitForward(&it, &depends);
  158. LinkedList2Node *n;
  159. while (n = LinkedList2Iterator_Next(&it)) {
  160. struct depend *d = UPPER_OBJECT(n, struct depend, depends_node);
  161. depend_update(d);
  162. }
  163. // signal up
  164. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  165. return o;
  166. fail1:
  167. free(o);
  168. fail0:
  169. return NULL;
  170. }
  171. static void provide_func_free (void *vo)
  172. {
  173. struct provide *o = vo;
  174. ASSERT(LinkedList2_IsEmpty(&o->depends))
  175. // remove from provides list
  176. LinkedList2_Remove(&provides, &o->provides_node);
  177. // free instance
  178. free(o);
  179. }
  180. static void provide_func_die (void *vo)
  181. {
  182. struct provide *o = vo;
  183. ASSERT(!o->dying)
  184. // if we have no depends, die immediately
  185. if (LinkedList2_IsEmpty(&o->depends)) {
  186. NCDModuleInst_Backend_Died(o->i, 0);
  187. return;
  188. }
  189. // set dying
  190. o->dying = 1;
  191. // start collapsing our depends
  192. LinkedList2Iterator it;
  193. LinkedList2Iterator_InitForward(&it, &o->depends);
  194. LinkedList2Node *n;
  195. while (n = LinkedList2Iterator_Next(&it)) {
  196. struct depend *d = UPPER_OBJECT(n, struct depend, provide_node);
  197. ASSERT(d->provide == o)
  198. depend_update(d);
  199. }
  200. }
  201. static void * depend_func_new (NCDModuleInst *i)
  202. {
  203. // allocate instance
  204. struct depend *o = malloc(sizeof(*o));
  205. if (!o) {
  206. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  207. goto fail0;
  208. }
  209. // init arguments
  210. o->i = i;
  211. // read arguments
  212. NCDValue *names_arg;
  213. if (!NCDValue_ListRead(o->i->args, 1, &names_arg)) {
  214. ModuleLog(i, BLOG_ERROR, "wrong arity");
  215. goto fail1;
  216. }
  217. if (NCDValue_Type(names_arg) != NCDVALUE_LIST) {
  218. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  219. goto fail1;
  220. }
  221. o->names = names_arg;
  222. // check names list
  223. NCDValue *e = NCDValue_ListFirst(o->names);
  224. while (e) {
  225. if (NCDValue_Type(e) != NCDVALUE_STRING) {
  226. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  227. goto fail1;
  228. }
  229. e = NCDValue_ListNext(o->names, e);
  230. }
  231. // insert to depends list
  232. LinkedList2_Append(&depends, &o->depends_node);
  233. // set no provide
  234. o->provide = NULL;
  235. // update
  236. depend_update(o);
  237. return o;
  238. fail1:
  239. free(o);
  240. fail0:
  241. return NULL;
  242. }
  243. static void depend_func_free (void *vo)
  244. {
  245. struct depend *o = vo;
  246. if (o->provide) {
  247. // remove from provide's list
  248. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  249. // if provide is dying and is empty, let it die
  250. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  251. NCDModuleInst_Backend_Died(o->provide->i, 0);
  252. }
  253. }
  254. // remove from depends list
  255. LinkedList2_Remove(&depends, &o->depends_node);
  256. // free instance
  257. free(o);
  258. }
  259. static void depend_func_clean (void *vo)
  260. {
  261. struct depend *o = vo;
  262. if (!(o->provide && o->provide_collapsing)) {
  263. return;
  264. }
  265. // remove from provide's list
  266. LinkedList2_Remove(&o->provide->depends, &o->provide_node);
  267. // if provide is dying and is empty, let it die
  268. if (o->provide->dying && LinkedList2_IsEmpty(&o->provide->depends)) {
  269. NCDModuleInst_Backend_Died(o->provide->i, 0);
  270. }
  271. // set no provide
  272. o->provide = NULL;
  273. // update
  274. depend_update(o);
  275. }
  276. static int depend_func_getvar (void *vo, const char *name_orig, NCDValue *out)
  277. {
  278. struct depend *o = vo;
  279. ASSERT(o->provide)
  280. ASSERT(!o->provide_collapsing)
  281. int ret = 0;
  282. char *name = strdup(name_orig);
  283. if (!name) {
  284. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  285. goto fail0;
  286. }
  287. const char *modname;
  288. const char *varname;
  289. char *dot = strstr(name, ".");
  290. if (!dot) {
  291. modname = name;
  292. varname = "";
  293. } else {
  294. *dot = '\0';
  295. modname = name;
  296. varname = dot + 1;
  297. }
  298. ret = NCDModuleInst_Backend_GetVar(o->provide->i, modname, varname, out);
  299. free(name);
  300. fail0:
  301. return ret;
  302. }
  303. static const struct NCDModule modules[] = {
  304. {
  305. .type = "multiprovide",
  306. .func_new = provide_func_new,
  307. .func_free = provide_func_free,
  308. .func_die = provide_func_die
  309. }, {
  310. .type = "multidepend",
  311. .func_new = depend_func_new,
  312. .func_free = depend_func_free,
  313. .func_clean = depend_func_clean,
  314. .func_getvar = depend_func_getvar
  315. }, {
  316. .type = NULL
  317. }
  318. };
  319. const struct NCDModuleGroup ncdmodule_multidepend = {
  320. .func_globalinit = func_globalinit,
  321. .modules = modules
  322. };