ondemand.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405
  1. /**
  2. * @file ondemand.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * On-demand process manager.
  32. *
  33. * Synopsis:
  34. * ondemand(string template_name, list args)
  35. *
  36. * Description:
  37. * Manages an on-demand template process using a process template named
  38. * template_name.
  39. * On deinitialization, if the process is running, reqests its termination
  40. * and waits for it to terminate.
  41. *
  42. * Synopsis:
  43. * ondemand::demand()
  44. *
  45. * Description:
  46. * Demands the availability of an on-demand template process.
  47. * This statement is in UP state if and only if the template process of the
  48. * corresponding ondemand object is completely up.
  49. *
  50. * Variables:
  51. * Exposes variables and objects from the template process corresponding to
  52. * the ondemand object.
  53. */
  54. #include <stdlib.h>
  55. #include <string.h>
  56. #include <misc/offset.h>
  57. #include <misc/debug.h>
  58. #include <structure/LinkedList1.h>
  59. #include <ncd/NCDModule.h>
  60. #include <generated/blog_channel_ncd_ondemand.h>
  61. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  62. struct ondemand {
  63. NCDModuleInst *i;
  64. char *template_name;
  65. NCDValue *args;
  66. LinkedList1 demands_list;
  67. int dying;
  68. int have_process;
  69. NCDModuleProcess process;
  70. int process_terminating;
  71. int process_up;
  72. };
  73. struct demand {
  74. NCDModuleInst *i;
  75. struct ondemand *od;
  76. LinkedList1Node demands_list_node;
  77. };
  78. static int ondemand_start_process (struct ondemand *o);
  79. static void ondemand_terminate_process (struct ondemand *o);
  80. static void ondemand_process_handler (struct ondemand *o, int event);
  81. static void ondemand_free (struct ondemand *o);
  82. static void demand_free (struct demand *o);
  83. static int ondemand_start_process (struct ondemand *o)
  84. {
  85. ASSERT(!o->dying)
  86. ASSERT(!o->have_process)
  87. // copy arguments
  88. NCDValue args;
  89. if (!NCDValue_InitCopy(&args, o->args)) {
  90. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  91. goto fail0;
  92. }
  93. // start process
  94. if (!NCDModuleProcess_Init(&o->process, o->i, o->template_name, args, o, (NCDModuleProcess_handler_event)ondemand_process_handler)) {
  95. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  96. NCDValue_Free(&args);
  97. goto fail0;
  98. }
  99. // set have process
  100. o->have_process = 1;
  101. // set process not terminating
  102. o->process_terminating = 0;
  103. // set process not up
  104. o->process_up = 0;
  105. return 1;
  106. fail0:
  107. return 0;
  108. }
  109. static void ondemand_terminate_process (struct ondemand *o)
  110. {
  111. ASSERT(o->have_process)
  112. ASSERT(!o->process_terminating)
  113. // request termination
  114. NCDModuleProcess_Terminate(&o->process);
  115. // set process terminating
  116. o->process_terminating = 1;
  117. if (o->process_up) {
  118. // set process down
  119. o->process_up = 0;
  120. // signal demands down
  121. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  122. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  123. ASSERT(demand->od == o)
  124. NCDModuleInst_Backend_Down(demand->i);
  125. }
  126. }
  127. }
  128. static void ondemand_process_handler (struct ondemand *o, int event)
  129. {
  130. ASSERT(o->have_process)
  131. switch (event) {
  132. case NCDMODULEPROCESS_EVENT_UP: {
  133. ASSERT(!o->process_terminating)
  134. ASSERT(!o->process_up)
  135. // set process up
  136. o->process_up = 1;
  137. // signal demands up
  138. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  139. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  140. ASSERT(demand->od == o)
  141. NCDModuleInst_Backend_Up(demand->i);
  142. }
  143. } break;
  144. case NCDMODULEPROCESS_EVENT_DOWN: {
  145. ASSERT(!o->process_terminating)
  146. ASSERT(o->process_up)
  147. // continue process
  148. NCDModuleProcess_Continue(&o->process);
  149. // set process down
  150. o->process_up = 0;
  151. // signal demands down
  152. for (LinkedList1Node *n = LinkedList1_GetFirst(&o->demands_list); n; n = LinkedList1Node_Next(n)) {
  153. struct demand *demand = UPPER_OBJECT(n, struct demand, demands_list_node);
  154. ASSERT(demand->od == o)
  155. NCDModuleInst_Backend_Down(demand->i);
  156. }
  157. } break;
  158. case NCDMODULEPROCESS_EVENT_TERMINATED: {
  159. ASSERT(o->process_terminating)
  160. ASSERT(!o->process_up)
  161. // free process
  162. NCDModuleProcess_Free(&o->process);
  163. // set have no process
  164. o->have_process = 0;
  165. // if dying, die finally
  166. if (o->dying) {
  167. ondemand_free(o);
  168. return;
  169. }
  170. // if demands arrivied, restart process
  171. if (!LinkedList1_IsEmpty(&o->demands_list)) {
  172. if (!ondemand_start_process(o)) {
  173. // error demands
  174. while (!LinkedList1_IsEmpty(&o->demands_list)) {
  175. struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
  176. ASSERT(demand->od == o)
  177. NCDModuleInst_Backend_SetError(demand->i);
  178. demand_free(demand);
  179. }
  180. }
  181. }
  182. } break;
  183. }
  184. }
  185. static void ondemand_func_new (NCDModuleInst *i)
  186. {
  187. // allocate instance
  188. struct ondemand *o = malloc(sizeof(*o));
  189. if (!o) {
  190. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  191. goto fail0;
  192. }
  193. NCDModuleInst_Backend_SetUser(i, o);
  194. // init arguments
  195. o->i = i;
  196. // read arguments
  197. NCDValue *arg_template_name;
  198. NCDValue *arg_args;
  199. if (!NCDValue_ListRead(i->args, 2, &arg_template_name, &arg_args)) {
  200. ModuleLog(i, BLOG_ERROR, "wrong arity");
  201. goto fail1;
  202. }
  203. if (!NCDValue_IsStringNoNulls(arg_template_name) || NCDValue_Type(arg_args) != NCDVALUE_LIST) {
  204. ModuleLog(i, BLOG_ERROR, "wrong type");
  205. goto fail1;
  206. }
  207. o->template_name = NCDValue_StringValue(arg_template_name);
  208. o->args = arg_args;
  209. // init demands list
  210. LinkedList1_Init(&o->demands_list);
  211. // set not dying
  212. o->dying = 0;
  213. // set have no process
  214. o->have_process = 0;
  215. // signal up
  216. NCDModuleInst_Backend_Up(i);
  217. return;
  218. fail1:
  219. free(o);
  220. fail0:
  221. NCDModuleInst_Backend_SetError(i);
  222. NCDModuleInst_Backend_Dead(i);
  223. }
  224. static void ondemand_free (struct ondemand *o)
  225. {
  226. ASSERT(!o->have_process)
  227. NCDModuleInst *i = o->i;
  228. // die demands
  229. while (!LinkedList1_IsEmpty(&o->demands_list)) {
  230. struct demand *demand = UPPER_OBJECT(LinkedList1_GetFirst(&o->demands_list), struct demand, demands_list_node);
  231. ASSERT(demand->od == o)
  232. demand_free(demand);
  233. }
  234. // free instance
  235. free(o);
  236. NCDModuleInst_Backend_Dead(i);
  237. }
  238. static void ondemand_func_die (void *vo)
  239. {
  240. struct ondemand *o = vo;
  241. ASSERT(!o->dying)
  242. // if not have process, die right away
  243. if (!o->have_process) {
  244. ondemand_free(o);
  245. return;
  246. }
  247. // set dying
  248. o->dying = 1;
  249. // request process termination if not already
  250. if (!o->process_terminating) {
  251. ondemand_terminate_process(o);
  252. }
  253. }
  254. static void demand_func_new (NCDModuleInst *i)
  255. {
  256. // allocate instance
  257. struct demand *o = malloc(sizeof(*o));
  258. if (!o) {
  259. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  260. goto fail0;
  261. }
  262. NCDModuleInst_Backend_SetUser(i, o);
  263. // init arguments
  264. o->i = i;
  265. // read arguments
  266. if (!NCDValue_ListRead(i->args, 0)) {
  267. ModuleLog(i, BLOG_ERROR, "wrong arity");
  268. goto fail1;
  269. }
  270. // set ondemand
  271. o->od = ((NCDModuleInst *)i->method_user)->inst_user;
  272. // add to ondemand's demands list
  273. LinkedList1_Append(&o->od->demands_list, &o->demands_list_node);
  274. // start process if needed
  275. if (!o->od->have_process) {
  276. ASSERT(!o->od->dying)
  277. if (!ondemand_start_process(o->od)) {
  278. goto fail2;
  279. }
  280. }
  281. // if process is up, signal up
  282. if (o->od->process_up) {
  283. NCDModuleInst_Backend_Up(i);
  284. }
  285. return;
  286. fail2:
  287. LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
  288. fail1:
  289. free(o);
  290. fail0:
  291. NCDModuleInst_Backend_SetError(i);
  292. NCDModuleInst_Backend_Dead(i);
  293. }
  294. static void demand_free (struct demand *o)
  295. {
  296. NCDModuleInst *i = o->i;
  297. // remove from ondemand's demands list
  298. LinkedList1_Remove(&o->od->demands_list, &o->demands_list_node);
  299. // request process termination if no longer needed
  300. if (o->od->have_process && !o->od->process_terminating && LinkedList1_IsEmpty(&o->od->demands_list)) {
  301. ondemand_terminate_process(o->od);
  302. }
  303. // free instance
  304. free(o);
  305. NCDModuleInst_Backend_Dead(i);
  306. }
  307. static void demand_func_die (void *vo)
  308. {
  309. struct demand *o = vo;
  310. demand_free(o);
  311. }
  312. static int demand_func_getobj (void *vo, const char *objname, NCDObject *out_object)
  313. {
  314. struct demand *o = vo;
  315. ASSERT(o->od->have_process)
  316. ASSERT(o->od->process_up)
  317. return NCDModuleProcess_GetObj(&o->od->process, objname, out_object);
  318. }
  319. static const struct NCDModule modules[] = {
  320. {
  321. .type = "ondemand",
  322. .func_new = ondemand_func_new,
  323. .func_die = ondemand_func_die
  324. }, {
  325. .type = "ondemand::demand",
  326. .func_new = demand_func_new,
  327. .func_die = demand_func_die,
  328. .func_getobj = demand_func_getobj
  329. }, {
  330. .type = NULL
  331. }
  332. };
  333. const struct NCDModuleGroup ncdmodule_ondemand = {
  334. .modules = modules
  335. };