process_manager.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  1. /**
  2. * @file process_manager.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. * Module which allows starting and stopping processes from templates dynamically.
  32. *
  33. * Synopsis: process_manager()
  34. * Description: manages processes. On deinitialization, initiates termination of all
  35. * contained processes and waits for them to terminate.
  36. *
  37. * Synopsis: process_manager::start(string name, string template_name, list args)
  38. * Description: creates a new process from the template named template_name, with arguments args,
  39. * identified by name within the process manager. If a process with this name already exists
  40. * and is not being terminated, does nothing. If it is being terminated, it will be restarted
  41. * using the given parameters after it terminates.
  42. * The process can access objects as seen from the process_manager() statement via _caller.
  43. *
  44. * Synopsis: process_manager::stop(string name)
  45. * Description: initiates termination of the process identified by name within the process manager.
  46. * If there is no such process, or the process is already being terminated, does nothing.
  47. */
  48. #include <stdlib.h>
  49. #include <string.h>
  50. #include <misc/offset.h>
  51. #include <misc/debug.h>
  52. #include <structure/LinkedList1.h>
  53. #include <ncd/NCDModule.h>
  54. #include <ncd/value_utils.h>
  55. #include <generated/blog_channel_ncd_process_manager.h>
  56. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  57. #define RETRY_TIME 10000
  58. #define PROCESS_STATE_RUNNING 1
  59. #define PROCESS_STATE_STOPPING 2
  60. #define PROCESS_STATE_RESTARTING 3
  61. #define PROCESS_STATE_RETRYING 4
  62. struct instance {
  63. NCDModuleInst *i;
  64. LinkedList1 processes_list;
  65. int dying;
  66. };
  67. struct process {
  68. struct instance *manager;
  69. char *name;
  70. BTimer retry_timer;
  71. LinkedList1Node processes_list_node;
  72. int have_params;
  73. NCD_string_id_t params_template_name;
  74. NCDValMem params_mem;
  75. NCDValRef params_args;
  76. int have_module_process;
  77. NCDValMem process_mem;
  78. NCDValRef process_args;
  79. NCDModuleProcess module_process;
  80. int state;
  81. };
  82. static struct process * find_process (struct instance *o, const char *name);
  83. static int process_new (struct instance *o, const char *name, NCDValRef template_name, NCDValRef args);
  84. static void process_free (struct process *p);
  85. static void process_retry_timer_handler (struct process *p);
  86. static void process_module_process_handler_event (NCDModuleProcess *process, int event);
  87. static int process_module_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object);
  88. static int process_module_process_caller_obj_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object);
  89. static void process_stop (struct process *p);
  90. static int process_restart (struct process *p, NCDValRef template_name, NCDValRef args);
  91. static void process_try (struct process *p);
  92. static int process_set_params (struct process *p, NCDValRef template_name, NCDValMem mem, NCDValSafeRef args);
  93. static void instance_free (struct instance *o);
  94. enum {STRING_CALLER};
  95. static struct NCD_string_request strings[] = {
  96. {"_caller"}, {NULL}
  97. };
  98. struct process * find_process (struct instance *o, const char *name)
  99. {
  100. LinkedList1Node *n = LinkedList1_GetFirst(&o->processes_list);
  101. while (n) {
  102. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  103. if (!strcmp(p->name, name)) {
  104. return p;
  105. }
  106. n = LinkedList1Node_Next(n);
  107. }
  108. return NULL;
  109. }
  110. int process_new (struct instance *o, const char *name, NCDValRef template_name, NCDValRef args)
  111. {
  112. ASSERT(!o->dying)
  113. ASSERT(!find_process(o, name))
  114. ASSERT(NCDVal_IsString(template_name))
  115. ASSERT(NCDVal_IsList(args))
  116. // allocate structure
  117. struct process *p = malloc(sizeof(*p));
  118. if (!p) {
  119. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  120. goto fail0;
  121. }
  122. // set manager
  123. p->manager = o;
  124. // copy name
  125. if (!(p->name = strdup(name))) {
  126. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  127. goto fail1;
  128. }
  129. // init retry timer
  130. BTimer_Init(&p->retry_timer, RETRY_TIME, (BTimer_handler)process_retry_timer_handler, p);
  131. // insert to processes list
  132. LinkedList1_Append(&o->processes_list, &p->processes_list_node);
  133. // have no params
  134. p->have_params = 0;
  135. // have no module process
  136. p->have_module_process = 0;
  137. // copy arguments
  138. NCDValMem mem;
  139. NCDValMem_Init(&mem);
  140. NCDValRef args2 = NCDVal_NewCopy(&mem, args);
  141. if (NCDVal_IsInvalid(args2)) {
  142. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  143. goto fail2;
  144. }
  145. // set params
  146. if (!process_set_params(p, template_name, mem, NCDVal_ToSafe(args2))) {
  147. goto fail2;
  148. }
  149. // try starting it
  150. process_try(p);
  151. return 1;
  152. fail2:
  153. NCDValMem_Free(&mem);
  154. LinkedList1_Remove(&o->processes_list, &p->processes_list_node);
  155. free(p->name);
  156. fail1:
  157. free(p);
  158. fail0:
  159. return 0;
  160. }
  161. void process_free (struct process *p)
  162. {
  163. ASSERT(!p->have_module_process)
  164. struct instance *o = p->manager;
  165. // free params
  166. if (p->have_params) {
  167. NCDValMem_Free(&p->params_mem);
  168. }
  169. // remove from processes list
  170. LinkedList1_Remove(&o->processes_list, &p->processes_list_node);
  171. // free timer
  172. BReactor_RemoveTimer(o->i->params->iparams->reactor, &p->retry_timer);
  173. // free name
  174. free(p->name);
  175. // free structure
  176. free(p);
  177. }
  178. void process_retry_timer_handler (struct process *p)
  179. {
  180. struct instance *o = p->manager;
  181. B_USE(o)
  182. ASSERT(p->state == PROCESS_STATE_RETRYING)
  183. ASSERT(!o->dying)
  184. ASSERT(p->have_params)
  185. ASSERT(!p->have_module_process)
  186. // retry
  187. process_try(p);
  188. }
  189. void process_module_process_handler_event (NCDModuleProcess *process, int event)
  190. {
  191. struct process *p = UPPER_OBJECT(process, struct process, module_process);
  192. struct instance *o = p->manager;
  193. ASSERT(p->have_module_process)
  194. if (event == NCDMODULEPROCESS_EVENT_DOWN) {
  195. // allow process to continue
  196. NCDModuleProcess_Continue(&p->module_process);
  197. }
  198. if (event != NCDMODULEPROCESS_EVENT_TERMINATED) {
  199. return;
  200. }
  201. // free module process
  202. NCDModuleProcess_Free(&p->module_process);
  203. // free arguments mem
  204. NCDValMem_Free(&p->process_mem);
  205. // set no module process
  206. p->have_module_process = 0;
  207. switch (p->state) {
  208. case PROCESS_STATE_STOPPING: {
  209. // free process
  210. process_free(p);
  211. // if manager is dying and there are no more processes, let it die
  212. if (o->dying && LinkedList1_IsEmpty(&o->processes_list)) {
  213. instance_free(o);
  214. }
  215. return;
  216. } break;
  217. case PROCESS_STATE_RESTARTING: {
  218. ASSERT(!o->dying)
  219. ASSERT(p->have_params)
  220. // restart
  221. process_try(p);
  222. } break;
  223. default: ASSERT(0);
  224. }
  225. }
  226. int process_module_process_func_getspecialobj (NCDModuleProcess *process, NCD_string_id_t name, NCDObject *out_object)
  227. {
  228. struct process *p = UPPER_OBJECT(process, struct process, module_process);
  229. ASSERT(p->have_module_process)
  230. if (name == strings[STRING_CALLER].id) {
  231. *out_object = NCDObject_Build(-1, p, NULL, (NCDObject_func_getobj)process_module_process_caller_obj_func_getobj);
  232. return 1;
  233. }
  234. return 0;
  235. }
  236. int process_module_process_caller_obj_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object)
  237. {
  238. struct instance *o = p->manager;
  239. ASSERT(p->have_module_process)
  240. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  241. }
  242. void process_stop (struct process *p)
  243. {
  244. switch (p->state) {
  245. case PROCESS_STATE_RETRYING: {
  246. ASSERT(!p->have_module_process)
  247. // free process
  248. process_free(p);
  249. return;
  250. } break;
  251. case PROCESS_STATE_RUNNING: {
  252. ASSERT(p->have_module_process)
  253. // request process to terminate
  254. NCDModuleProcess_Terminate(&p->module_process);
  255. // set state
  256. p->state = PROCESS_STATE_STOPPING;
  257. } break;
  258. case PROCESS_STATE_RESTARTING: {
  259. ASSERT(p->have_params)
  260. // free params
  261. NCDValMem_Free(&p->params_mem);
  262. p->have_params = 0;
  263. // set state
  264. p->state = PROCESS_STATE_STOPPING;
  265. } break;
  266. case PROCESS_STATE_STOPPING: {
  267. // nothing to do
  268. } break;
  269. default: ASSERT(0);
  270. }
  271. }
  272. int process_restart (struct process *p, NCDValRef template_name, NCDValRef args)
  273. {
  274. struct instance *o = p->manager;
  275. ASSERT(!o->dying)
  276. ASSERT(p->state == PROCESS_STATE_STOPPING)
  277. ASSERT(!p->have_params)
  278. ASSERT(NCDVal_IsString(template_name))
  279. ASSERT(NCDVal_IsList(args))
  280. // copy arguments
  281. NCDValMem mem;
  282. NCDValMem_Init(&mem);
  283. NCDValRef args2 = NCDVal_NewCopy(&mem, args);
  284. if (NCDVal_IsInvalid(args2)) {
  285. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  286. goto fail1;
  287. }
  288. // set params
  289. if (!process_set_params(p, template_name, mem, NCDVal_ToSafe(args2))) {
  290. goto fail1;
  291. }
  292. // set state
  293. p->state = PROCESS_STATE_RESTARTING;
  294. return 1;
  295. fail1:
  296. NCDValMem_Free(&mem);
  297. return 0;
  298. }
  299. void process_try (struct process *p)
  300. {
  301. struct instance *o = p->manager;
  302. ASSERT(!o->dying)
  303. ASSERT(p->have_params)
  304. ASSERT(!p->have_module_process)
  305. ModuleLog(o->i, BLOG_INFO, "trying process %s", p->name);
  306. // move params
  307. p->process_mem = p->params_mem;
  308. p->process_args = NCDVal_Moved(&p->process_mem, p->params_args);
  309. // init module process
  310. if (!NCDModuleProcess_InitId(&p->module_process, o->i, p->params_template_name, p->process_args, process_module_process_handler_event)) {
  311. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  312. // set timer
  313. BReactor_SetTimer(o->i->params->iparams->reactor, &p->retry_timer);
  314. // set state
  315. p->state = PROCESS_STATE_RETRYING;
  316. return;
  317. }
  318. // set special objects function
  319. NCDModuleProcess_SetSpecialFuncs(&p->module_process, process_module_process_func_getspecialobj);
  320. // free params
  321. p->have_params = 0;
  322. // set have module process
  323. p->have_module_process = 1;
  324. // set state
  325. p->state = PROCESS_STATE_RUNNING;
  326. }
  327. int process_set_params (struct process *p, NCDValRef template_name, NCDValMem mem, NCDValSafeRef args)
  328. {
  329. ASSERT(!p->have_params)
  330. ASSERT(NCDVal_IsString(template_name))
  331. ASSERT(NCDVal_IsList(NCDVal_FromSafe(&mem, args)))
  332. // get string ID for template name
  333. p->params_template_name = ncd_get_string_id(template_name, p->manager->i->params->iparams->string_index);
  334. if (p->params_template_name < 0) {
  335. ModuleLog(p->manager->i, BLOG_ERROR, "ncd_get_string_id failed");
  336. return 0;
  337. }
  338. // eat arguments
  339. p->params_mem = mem;
  340. p->params_args = NCDVal_FromSafe(&p->params_mem, args);
  341. // set have params
  342. p->have_params = 1;
  343. return 1;
  344. }
  345. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  346. {
  347. struct instance *o = vo;
  348. o->i = i;
  349. // check arguments
  350. if (!NCDVal_ListRead(params->args, 0)) {
  351. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  352. goto fail0;
  353. }
  354. // init processes list
  355. LinkedList1_Init(&o->processes_list);
  356. // set not dying
  357. o->dying = 0;
  358. // signal up
  359. NCDModuleInst_Backend_Up(o->i);
  360. return;
  361. fail0:
  362. NCDModuleInst_Backend_SetError(i);
  363. NCDModuleInst_Backend_Dead(i);
  364. }
  365. void instance_free (struct instance *o)
  366. {
  367. ASSERT(LinkedList1_IsEmpty(&o->processes_list))
  368. NCDModuleInst_Backend_Dead(o->i);
  369. }
  370. static void func_die (void *vo)
  371. {
  372. struct instance *o = vo;
  373. ASSERT(!o->dying)
  374. // request all processes to die
  375. LinkedList1Node *n = LinkedList1_GetFirst(&o->processes_list);
  376. while (n) {
  377. LinkedList1Node *next = LinkedList1Node_Next(n);
  378. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  379. process_stop(p);
  380. n = next;
  381. }
  382. // if there are no processes, die immediately
  383. if (LinkedList1_IsEmpty(&o->processes_list)) {
  384. instance_free(o);
  385. return;
  386. }
  387. // set dying
  388. o->dying = 1;
  389. }
  390. static void start_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  391. {
  392. // check arguments
  393. NCDValRef name_arg;
  394. NCDValRef template_name_arg;
  395. NCDValRef args_arg;
  396. if (!NCDVal_ListRead(params->args, 3, &name_arg, &template_name_arg, &args_arg)) {
  397. ModuleLog(i, BLOG_ERROR, "wrong arity");
  398. goto fail0;
  399. }
  400. if (!NCDVal_IsStringNoNulls(name_arg) || !NCDVal_IsString(template_name_arg) ||
  401. !NCDVal_IsList(args_arg)) {
  402. ModuleLog(i, BLOG_ERROR, "wrong type");
  403. goto fail0;
  404. }
  405. const char *name = NCDVal_StringValue(name_arg);
  406. // signal up.
  407. // Do it before creating the process so that the process starts initializing before our own process continues.
  408. NCDModuleInst_Backend_Up(i);
  409. // get method object
  410. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  411. if (mo->dying) {
  412. ModuleLog(i, BLOG_INFO, "manager is dying, not creating process %s", name);
  413. } else {
  414. struct process *p = find_process(mo, name);
  415. if (p && p->state != PROCESS_STATE_STOPPING) {
  416. ModuleLog(i, BLOG_INFO, "process %s already started", name);
  417. } else {
  418. if (p) {
  419. if (!process_restart(p, template_name_arg, args_arg)) {
  420. ModuleLog(i, BLOG_ERROR, "failed to restart process %s", name);
  421. goto fail0;
  422. }
  423. } else {
  424. if (!process_new(mo, name, template_name_arg, args_arg)) {
  425. ModuleLog(i, BLOG_ERROR, "failed to create process %s", name);
  426. goto fail0;
  427. }
  428. }
  429. }
  430. }
  431. return;
  432. fail0:
  433. NCDModuleInst_Backend_SetError(i);
  434. NCDModuleInst_Backend_Dead(i);
  435. }
  436. static void stop_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  437. {
  438. // check arguments
  439. NCDValRef name_arg;
  440. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  441. ModuleLog(i, BLOG_ERROR, "wrong arity");
  442. goto fail0;
  443. }
  444. if (!NCDVal_IsStringNoNulls(name_arg)) {
  445. ModuleLog(i, BLOG_ERROR, "wrong type");
  446. goto fail0;
  447. }
  448. const char *name = NCDVal_StringValue(name_arg);
  449. // signal up.
  450. // Do it before stopping the process so that the process starts terminating before our own process continues.
  451. NCDModuleInst_Backend_Up(i);
  452. // get method object
  453. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  454. if (mo->dying) {
  455. ModuleLog(i, BLOG_INFO, "manager is dying, not stopping process %s", name);
  456. } else {
  457. struct process *p = find_process(mo, name);
  458. if (!(p && p->state != PROCESS_STATE_STOPPING)) {
  459. ModuleLog(i, BLOG_INFO, "process %s already stopped", name);
  460. } else {
  461. process_stop(p);
  462. }
  463. }
  464. return;
  465. fail0:
  466. NCDModuleInst_Backend_SetError(i);
  467. NCDModuleInst_Backend_Dead(i);
  468. }
  469. static struct NCDModule modules[] = {
  470. {
  471. .type = "process_manager",
  472. .func_new2 = func_new,
  473. .func_die = func_die,
  474. .alloc_size = sizeof(struct instance)
  475. }, {
  476. .type = "process_manager::start",
  477. .func_new2 = start_func_new
  478. }, {
  479. .type = "process_manager::stop",
  480. .func_new2 = stop_func_new
  481. }, {
  482. .type = NULL
  483. }
  484. };
  485. const struct NCDModuleGroup ncdmodule_process_manager = {
  486. .modules = modules,
  487. .strings = strings
  488. };