process_manager.c 17 KB

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