process_manager.c 17 KB

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