process_manager.c 17 KB

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