process_manager.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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 (struct process *p, int event);
  87. static int process_module_process_func_getspecialobj (struct process *p, 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 (struct process *p, int event)
  190. {
  191. struct instance *o = p->manager;
  192. ASSERT(p->have_module_process)
  193. if (event == NCDMODULEPROCESS_EVENT_DOWN) {
  194. // allow process to continue
  195. NCDModuleProcess_Continue(&p->module_process);
  196. }
  197. if (event != NCDMODULEPROCESS_EVENT_TERMINATED) {
  198. return;
  199. }
  200. // free module process
  201. NCDModuleProcess_Free(&p->module_process);
  202. // free arguments mem
  203. NCDValMem_Free(&p->process_mem);
  204. // set no module process
  205. p->have_module_process = 0;
  206. switch (p->state) {
  207. case PROCESS_STATE_STOPPING: {
  208. // free process
  209. process_free(p);
  210. // if manager is dying and there are no more processes, let it die
  211. if (o->dying && LinkedList1_IsEmpty(&o->processes_list)) {
  212. instance_free(o);
  213. }
  214. return;
  215. } break;
  216. case PROCESS_STATE_RESTARTING: {
  217. ASSERT(!o->dying)
  218. ASSERT(p->have_params)
  219. // restart
  220. process_try(p);
  221. } break;
  222. default: ASSERT(0);
  223. }
  224. }
  225. int process_module_process_func_getspecialobj (struct process *p, NCD_string_id_t name, NCDObject *out_object)
  226. {
  227. ASSERT(p->have_module_process)
  228. if (name == strings[STRING_CALLER].id) {
  229. *out_object = NCDObject_Build(-1, p, NULL, (NCDObject_func_getobj)process_module_process_caller_obj_func_getobj);
  230. return 1;
  231. }
  232. return 0;
  233. }
  234. int process_module_process_caller_obj_func_getobj (struct process *p, NCD_string_id_t name, NCDObject *out_object)
  235. {
  236. struct instance *o = p->manager;
  237. ASSERT(p->have_module_process)
  238. return NCDModuleInst_Backend_GetObj(o->i, name, out_object);
  239. }
  240. void process_stop (struct process *p)
  241. {
  242. switch (p->state) {
  243. case PROCESS_STATE_RETRYING: {
  244. ASSERT(!p->have_module_process)
  245. // free process
  246. process_free(p);
  247. return;
  248. } break;
  249. case PROCESS_STATE_RUNNING: {
  250. ASSERT(p->have_module_process)
  251. // request process to terminate
  252. NCDModuleProcess_Terminate(&p->module_process);
  253. // set state
  254. p->state = PROCESS_STATE_STOPPING;
  255. } break;
  256. case PROCESS_STATE_RESTARTING: {
  257. ASSERT(p->have_params)
  258. // free params
  259. NCDValMem_Free(&p->params_mem);
  260. p->have_params = 0;
  261. // set state
  262. p->state = PROCESS_STATE_STOPPING;
  263. } break;
  264. case PROCESS_STATE_STOPPING: {
  265. // nothing to do
  266. } break;
  267. default: ASSERT(0);
  268. }
  269. }
  270. int process_restart (struct process *p, NCDValRef template_name, NCDValRef args)
  271. {
  272. struct instance *o = p->manager;
  273. ASSERT(!o->dying)
  274. ASSERT(p->state == PROCESS_STATE_STOPPING)
  275. ASSERT(!p->have_params)
  276. ASSERT(NCDVal_IsString(template_name))
  277. ASSERT(NCDVal_IsList(args))
  278. // copy arguments
  279. NCDValMem mem;
  280. NCDValMem_Init(&mem);
  281. NCDValRef args2 = NCDVal_NewCopy(&mem, args);
  282. if (NCDVal_IsInvalid(args2)) {
  283. ModuleLog(o->i, BLOG_ERROR, "NCDVal_NewCopy failed");
  284. goto fail1;
  285. }
  286. // set params
  287. if (!process_set_params(p, template_name, mem, NCDVal_ToSafe(args2))) {
  288. goto fail1;
  289. }
  290. // set state
  291. p->state = PROCESS_STATE_RESTARTING;
  292. return 1;
  293. fail1:
  294. NCDValMem_Free(&mem);
  295. return 0;
  296. }
  297. void process_try (struct process *p)
  298. {
  299. struct instance *o = p->manager;
  300. ASSERT(!o->dying)
  301. ASSERT(p->have_params)
  302. ASSERT(!p->have_module_process)
  303. ModuleLog(o->i, BLOG_INFO, "trying process %s", p->name);
  304. // move params
  305. p->process_mem = p->params_mem;
  306. p->process_args = NCDVal_Moved(&p->process_mem, p->params_args);
  307. // init module process
  308. if (!NCDModuleProcess_InitId(&p->module_process, o->i, p->params_template_name, p->process_args, p, (NCDModuleProcess_handler_event)process_module_process_handler_event)) {
  309. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  310. // set timer
  311. BReactor_SetTimer(o->i->params->iparams->reactor, &p->retry_timer);
  312. // set state
  313. p->state = PROCESS_STATE_RETRYING;
  314. return;
  315. }
  316. // set special objects function
  317. NCDModuleProcess_SetSpecialFuncs(&p->module_process, (NCDModuleProcess_func_getspecialobj)process_module_process_func_getspecialobj);
  318. // free params
  319. p->have_params = 0;
  320. // set have module process
  321. p->have_module_process = 1;
  322. // set state
  323. p->state = PROCESS_STATE_RUNNING;
  324. }
  325. int process_set_params (struct process *p, NCDValRef template_name, NCDValMem mem, NCDValSafeRef args)
  326. {
  327. ASSERT(!p->have_params)
  328. ASSERT(NCDVal_IsString(template_name))
  329. ASSERT(NCDVal_IsList(NCDVal_FromSafe(&mem, args)))
  330. // get string ID for template name
  331. p->params_template_name = ncd_get_string_id(template_name, p->manager->i->params->iparams->string_index);
  332. if (p->params_template_name < 0) {
  333. ModuleLog(p->manager->i, BLOG_ERROR, "ncd_get_string_id failed");
  334. return 0;
  335. }
  336. // eat arguments
  337. p->params_mem = mem;
  338. p->params_args = NCDVal_FromSafe(&p->params_mem, args);
  339. // set have params
  340. p->have_params = 1;
  341. return 1;
  342. }
  343. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  344. {
  345. struct instance *o = vo;
  346. o->i = i;
  347. // check arguments
  348. if (!NCDVal_ListRead(params->args, 0)) {
  349. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  350. goto fail0;
  351. }
  352. // init processes list
  353. LinkedList1_Init(&o->processes_list);
  354. // set not dying
  355. o->dying = 0;
  356. // signal up
  357. NCDModuleInst_Backend_Up(o->i);
  358. return;
  359. fail0:
  360. NCDModuleInst_Backend_SetError(i);
  361. NCDModuleInst_Backend_Dead(i);
  362. }
  363. void instance_free (struct instance *o)
  364. {
  365. ASSERT(LinkedList1_IsEmpty(&o->processes_list))
  366. NCDModuleInst_Backend_Dead(o->i);
  367. }
  368. static void func_die (void *vo)
  369. {
  370. struct instance *o = vo;
  371. ASSERT(!o->dying)
  372. // request all processes to die
  373. LinkedList1Node *n = LinkedList1_GetFirst(&o->processes_list);
  374. while (n) {
  375. LinkedList1Node *next = LinkedList1Node_Next(n);
  376. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  377. process_stop(p);
  378. n = next;
  379. }
  380. // if there are no processes, die immediately
  381. if (LinkedList1_IsEmpty(&o->processes_list)) {
  382. instance_free(o);
  383. return;
  384. }
  385. // set dying
  386. o->dying = 1;
  387. }
  388. static void start_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  389. {
  390. // check arguments
  391. NCDValRef name_arg;
  392. NCDValRef template_name_arg;
  393. NCDValRef args_arg;
  394. if (!NCDVal_ListRead(params->args, 3, &name_arg, &template_name_arg, &args_arg)) {
  395. ModuleLog(i, BLOG_ERROR, "wrong arity");
  396. goto fail0;
  397. }
  398. if (!NCDVal_IsStringNoNulls(name_arg) || !NCDVal_IsString(template_name_arg) ||
  399. !NCDVal_IsList(args_arg)) {
  400. ModuleLog(i, BLOG_ERROR, "wrong type");
  401. goto fail0;
  402. }
  403. const char *name = NCDVal_StringValue(name_arg);
  404. // signal up.
  405. // Do it before creating the process so that the process starts initializing before our own process continues.
  406. NCDModuleInst_Backend_Up(i);
  407. // get method object
  408. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  409. if (mo->dying) {
  410. ModuleLog(i, BLOG_INFO, "manager is dying, not creating process %s", name);
  411. } else {
  412. struct process *p = find_process(mo, name);
  413. if (p && p->state != PROCESS_STATE_STOPPING) {
  414. ModuleLog(i, BLOG_INFO, "process %s already started", name);
  415. } else {
  416. if (p) {
  417. if (!process_restart(p, template_name_arg, args_arg)) {
  418. ModuleLog(i, BLOG_ERROR, "failed to restart process %s", name);
  419. goto fail0;
  420. }
  421. } else {
  422. if (!process_new(mo, name, template_name_arg, args_arg)) {
  423. ModuleLog(i, BLOG_ERROR, "failed to create process %s", name);
  424. goto fail0;
  425. }
  426. }
  427. }
  428. }
  429. return;
  430. fail0:
  431. NCDModuleInst_Backend_SetError(i);
  432. NCDModuleInst_Backend_Dead(i);
  433. }
  434. static void stop_func_new (void *unused, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  435. {
  436. // check arguments
  437. NCDValRef name_arg;
  438. if (!NCDVal_ListRead(params->args, 1, &name_arg)) {
  439. ModuleLog(i, BLOG_ERROR, "wrong arity");
  440. goto fail0;
  441. }
  442. if (!NCDVal_IsStringNoNulls(name_arg)) {
  443. ModuleLog(i, BLOG_ERROR, "wrong type");
  444. goto fail0;
  445. }
  446. const char *name = NCDVal_StringValue(name_arg);
  447. // signal up.
  448. // Do it before stopping the process so that the process starts terminating before our own process continues.
  449. NCDModuleInst_Backend_Up(i);
  450. // get method object
  451. struct instance *mo = NCDModuleInst_Backend_GetUser((NCDModuleInst *)params->method_user);
  452. if (mo->dying) {
  453. ModuleLog(i, BLOG_INFO, "manager is dying, not stopping process %s", name);
  454. } else {
  455. struct process *p = find_process(mo, name);
  456. if (!(p && p->state != PROCESS_STATE_STOPPING)) {
  457. ModuleLog(i, BLOG_INFO, "process %s already stopped", name);
  458. } else {
  459. process_stop(p);
  460. }
  461. }
  462. return;
  463. fail0:
  464. NCDModuleInst_Backend_SetError(i);
  465. NCDModuleInst_Backend_Dead(i);
  466. }
  467. static struct NCDModule modules[] = {
  468. {
  469. .type = "process_manager",
  470. .func_new2 = func_new,
  471. .func_die = func_die,
  472. .alloc_size = sizeof(struct instance)
  473. }, {
  474. .type = "process_manager::start",
  475. .func_new2 = start_func_new
  476. }, {
  477. .type = "process_manager::stop",
  478. .func_new2 = stop_func_new
  479. }, {
  480. .type = NULL
  481. }
  482. };
  483. const struct NCDModuleGroup ncdmodule_process_manager = {
  484. .modules = modules,
  485. .strings = strings
  486. };