depend.c 8.3 KB

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