ondemand.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400
  1. /**
  2. * @file ondemand.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. * On-demand process manager.
  25. *
  26. * Synopsis:
  27. * ondemand(string template_name, list args)
  28. *
  29. * Description:
  30. * Manages an on-demand template process using a process template named
  31. * template_name.
  32. * On deinitialization, if the process is running, reqests its termination
  33. * and waits for it to terminate.
  34. *
  35. * Synopsis:
  36. * ondemand::demand()
  37. *
  38. * Description:
  39. * Demands the availability of an on-demand template process.
  40. * This statement is in UP state if and only if the template process of the
  41. * corresponding ondemand object is completely up.
  42. *
  43. * Variables:
  44. * Exposes variables and objects from the template process corresponding to
  45. * the ondemand object.
  46. */
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <misc/offset.h>
  50. #include <misc/debug.h>
  51. #include <structure/LinkedList1.h>
  52. #include <ncd/NCDModule.h>
  53. #include <generated/blog_channel_ncd_ondemand.h>
  54. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  55. struct ondemand {
  56. NCDModuleInst *i;
  57. char *template_name;
  58. NCDValue *args;
  59. LinkedList1 demands_list;
  60. int dying;
  61. int have_process;
  62. NCDModuleProcess process;
  63. int process_terminating;
  64. int process_up;
  65. };
  66. struct demand {
  67. NCDModuleInst *i;
  68. struct ondemand *od;
  69. LinkedList1Node demands_list_node;
  70. };
  71. static int ondemand_start_process (struct ondemand *o);
  72. static void ondemand_terminate_process (struct ondemand *o);
  73. static void ondemand_process_handler (struct ondemand *o, int event);
  74. static void ondemand_free (struct ondemand *o);
  75. static void demand_free (struct demand *o);
  76. static int ondemand_start_process (struct ondemand *o)
  77. {
  78. ASSERT(!o->dying)
  79. ASSERT(!o->have_process)
  80. // copy arguments
  81. NCDValue args;
  82. if (!NCDValue_InitCopy(&args, o->args)) {
  83. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  84. goto fail0;
  85. }
  86. // start process
  87. if (!NCDModuleProcess_Init(&o->process, o->i, o->template_name, args, o, (NCDModuleProcess_handler_event)ondemand_process_handler)) {
  88. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  89. NCDValue_Free(&args);
  90. goto fail0;
  91. }
  92. // set have process
  93. o->have_process = 1;
  94. // set process not terminating
  95. o->process_terminating = 0;
  96. // set process not up
  97. o->process_up = 0;
  98. return 1;
  99. fail0:
  100. return 0;
  101. }
  102. static void ondemand_terminate_process (struct ondemand *o)
  103. {
  104. ASSERT(o->have_process)
  105. ASSERT(!o->process_terminating)
  106. // request termination
  107. NCDModuleProcess_Terminate(&o->process);
  108. // set process terminating
  109. o->process_terminating = 1;
  110. if (o->process_up) {
  111. // set process down
  112. o->process_up = 0;
  113. // signal demands down
  114. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  115. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  116. ASSERT(demand->od == o)
  117. NCDModuleInst_Backend_Down(demand->i);
  118. }
  119. }
  120. }
  121. static void ondemand_process_handler (struct ondemand *o, int event)
  122. {
  123. ASSERT(o->have_process)
  124. switch (event) {
  125. case NCDMODULEPROCESS_EVENT_UP: {
  126. ASSERT(!o->process_terminating)
  127. ASSERT(!o->process_up)
  128. // set process up
  129. o->process_up = 1;
  130. // signal demands up
  131. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  132. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  133. ASSERT(demand->od == o)
  134. NCDModuleInst_Backend_Up(demand->i);
  135. }
  136. } break;
  137. case NCDMODULEPROCESS_EVENT_DOWN: {
  138. ASSERT(!o->process_terminating)
  139. ASSERT(o->process_up)
  140. // continue process
  141. NCDModuleProcess_Continue(&o->process);
  142. // set process down
  143. o->process_up = 0;
  144. // signal demands down
  145. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  146. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  147. ASSERT(demand->od == o)
  148. NCDModuleInst_Backend_Down(demand->i);
  149. }
  150. } break;
  151. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  152. ASSERT(o->process_terminating)
  153. ASSERT(!o->process_up)
  154. // free process
  155. NCDModuleProcess_Free(&o->process);
  156. // set have no process
  157. o->have_process = 0;
  158. // if dying, die finally
  159. if (o->dying) {
  160. ondemand_free(o);
  161. return;
  162. }
  163. // if demands arrivied, restart process
  164. if (!LinkedList1_IsEmpty(&o->demands_list)) {
  165. ondemand_start_process(o);
  166. }
  167. } break;
  168. }
  169. }
  170. static void ondemand_func_new (NCDModuleInst *i)
  171. {
  172. // allocate instance
  173. struct ondemand *o = malloc(sizeof(*o));
  174. if (!o) {
  175. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  176. goto fail0;
  177. }
  178. NCDModuleInst_Backend_SetUser(i, o);
  179. // init arguments
  180. o->i = i;
  181. // read arguments
  182. NCDValue *arg_template_name;
  183. NCDValue *arg_args;
  184. if (!NCDValue_ListRead(i->args, 2, &arg_template_name, &arg_args)) {
  185. ModuleLog(i, BLOG_ERROR, "wrong arity");
  186. goto fail1;
  187. }
  188. if (NCDValue_Type(arg_template_name) != NCDVALUE_STRING || NCDValue_Type(arg_args) != NCDVALUE_LIST) {
  189. ModuleLog(i, BLOG_ERROR, "wrong type");
  190. goto fail1;
  191. }
  192. o->template_name = NCDValue_StringValue(arg_template_name);
  193. o->args = arg_args;
  194. // init demands list
  195. LinkedList1_Init(&o->demands_list);
  196. // set not dying
  197. o->dying = 0;
  198. // set have no process
  199. o->have_process = 0;
  200. // signal up
  201. NCDModuleInst_Backend_Up(i);
  202. return;
  203. fail1:
  204. free(o);
  205. fail0:
  206. NCDModuleInst_Backend_SetError(i);
  207. NCDModuleInst_Backend_Dead(i);
  208. }
  209. static void ondemand_free (struct ondemand *o)
  210. {
  211. ASSERT(!o->have_process)
  212. NCDModuleInst *i = o->i;
  213. // die demands
  214. while (!LinkedList1_IsEmpty(&o->demands_list)) {
  215. struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
  216. ASSERT(demand->od == o)
  217. demand_free(demand);
  218. }
  219. // free instance
  220. free(o);
  221. NCDModuleInst_Backend_Dead(i);
  222. }
  223. static void ondemand_func_die (void *vo)
  224. {
  225. struct ondemand *o = vo;
  226. ASSERT(!o->dying)
  227. // if not have process, die right away
  228. if (!o->have_process) {
  229. ondemand_free(o);
  230. return;
  231. }
  232. // set dying
  233. o->dying = 1;
  234. // request process termination if not already
  235. if (!o->process_terminating) {
  236. ondemand_terminate_process(o);
  237. }
  238. }
  239. static void demand_func_new (NCDModuleInst *i)
  240. {
  241. // allocate instance
  242. struct demand *o = malloc(sizeof(*o));
  243. if (!o) {
  244. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  245. goto fail0;
  246. }
  247. NCDModuleInst_Backend_SetUser(i, o);
  248. // init arguments
  249. o->i = i;
  250. // read arguments
  251. if (!NCDValue_ListRead(i->args, 0)) {
  252. ModuleLog(i, BLOG_ERROR, "wrong arity");
  253. goto fail1;
  254. }
  255. // set ondemand
  256. o->od = i->method_object->inst_user;
  257. // add to ondemand's demands list
  258. LinkedList1_Append(&o->od->demands_list, &o->demands_list_node);
  259. // start process if needed
  260. if (!o->od->have_process) {
  261. ASSERT(!o->od->dying)
  262. if (!ondemand_start_process(o->od)) {
  263. goto fail2;
  264. }
  265. }
  266. // if process is up, signal up
  267. if (o->od->process_up) {
  268. NCDModuleInst_Backend_Up(i);
  269. }
  270. return;
  271. fail2:
  272. LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
  273. fail1:
  274. free(o);
  275. fail0:
  276. NCDModuleInst_Backend_SetError(i);
  277. NCDModuleInst_Backend_Dead(i);
  278. }
  279. static void demand_free (struct demand *o)
  280. {
  281. NCDModuleInst *i = o->i;
  282. // remove from ondemand's demands list
  283. LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
  284. // request process termination if no longer needed
  285. if (o->od->have_process && !o->od->process_terminating && LinkedList1_IsEmpty(&o->od->demands_list)) {
  286. ondemand_terminate_process(o->od);
  287. }
  288. // free instance
  289. free(o);
  290. NCDModuleInst_Backend_Dead(i);
  291. }
  292. static void demand_func_die (void *vo)
  293. {
  294. struct demand *o = vo;
  295. demand_free(o);
  296. }
  297. static int demand_func_getvar (void *vo, const char *varname, NCDValue *out)
  298. {
  299. struct demand *o = vo;
  300. ASSERT(o->od->have_process)
  301. ASSERT(o->od->process_up)
  302. return NCDModuleProcess_GetVar(&o->od->process, varname, out);
  303. }
  304. static NCDModuleInst * demand_func_getobj (void *vo, const char *objname)
  305. {
  306. struct demand *o = vo;
  307. ASSERT(o->od->have_process)
  308. ASSERT(o->od->process_up)
  309. return NCDModuleProcess_GetObj(&o->od->process, objname);
  310. }
  311. static const struct NCDModule modules[] = {
  312. {
  313. .type = "ondemand",
  314. .func_new = ondemand_func_new,
  315. .func_die = ondemand_func_die
  316. }, {
  317. .type = "ondemand::demand",
  318. .func_new = demand_func_new,
  319. .func_die = demand_func_die,
  320. .func_getvar = demand_func_getvar,
  321. .func_getobj = demand_func_getobj
  322. }, {
  323. .type = NULL
  324. }
  325. };
  326. const struct NCDModuleGroup ncdmodule_ondemand = {
  327. .modules = modules
  328. };