process_manager.c 17 KB

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