process_manager.c 17 KB

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