process_manager.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596
  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. *
  43. * Synopsis: process_manager::stop(string name)
  44. * Description: initiates termination of the process identified by name within the process manager.
  45. * If there is no such process, or the process is already being terminated, does nothing.
  46. */
  47. #include <stdlib.h>
  48. #include <string.h>
  49. #include <misc/offset.h>
  50. #include <structure/LinkedList2.h>
  51. #include <ncd/NCDModule.h>
  52. #include <generated/blog_channel_ncd_process_manager.h>
  53. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  54. #define RETRY_TIME 10000
  55. #define PROCESS_STATE_RUNNING 1
  56. #define PROCESS_STATE_STOPPING 2
  57. #define PROCESS_STATE_RESTARTING 3
  58. #define PROCESS_STATE_RETRYING 4
  59. struct instance {
  60. NCDModuleInst *i;
  61. LinkedList2 processes_list;
  62. int dying;
  63. };
  64. struct process {
  65. struct instance *manager;
  66. char *name;
  67. BTimer retry_timer;
  68. LinkedList2Node processes_list_node;
  69. int have_params;
  70. char *params_template_name;
  71. NCDValue params_args;
  72. int have_module_process;
  73. NCDModuleProcess module_process;
  74. int state;
  75. };
  76. struct startstop_instance {
  77. NCDModuleInst *i;
  78. };
  79. static struct process * find_process (struct instance *o, const char *name);
  80. static int process_new (struct instance *o, const char *name, const char *template_name, NCDValue *args);
  81. static void process_free (struct process *p);
  82. static void process_retry_timer_handler (struct process *p);
  83. static void process_module_process_handler_dead (struct process *p);
  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->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. void process_stop (struct process *p)
  212. {
  213. switch (p->state) {
  214. case PROCESS_STATE_RETRYING: {
  215. ASSERT(!p->have_module_process)
  216. // free process
  217. process_free(p);
  218. return;
  219. } break;
  220. case PROCESS_STATE_RUNNING: {
  221. ASSERT(p->have_module_process)
  222. // request process to terminate
  223. NCDModuleProcess_Terminate(&p->module_process);
  224. // set state
  225. p->state = PROCESS_STATE_STOPPING;
  226. } break;
  227. case PROCESS_STATE_RESTARTING: {
  228. ASSERT(p->have_params)
  229. // free params
  230. NCDValue_Free(&p->params_args);
  231. free(p->params_template_name);
  232. p->have_params = 0;
  233. // set state
  234. p->state = PROCESS_STATE_STOPPING;
  235. } break;
  236. case PROCESS_STATE_STOPPING: {
  237. // nothing to do
  238. } break;
  239. default: ASSERT(0);
  240. }
  241. }
  242. int process_restart (struct process *p, const char *template_name, NCDValue *args)
  243. {
  244. struct instance *o = p->manager;
  245. ASSERT(!o->dying)
  246. ASSERT(p->state == PROCESS_STATE_STOPPING)
  247. ASSERT(!p->have_params)
  248. // copy arguments
  249. NCDValue args2;
  250. if (!NCDValue_InitCopy(&args2, args)) {
  251. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  252. return 0;
  253. }
  254. // set params
  255. if (!process_set_params(p, template_name, args2)) {
  256. NCDValue_Free(&args2);
  257. return 0;
  258. }
  259. // set state
  260. p->state = PROCESS_STATE_RESTARTING;
  261. return 1;
  262. }
  263. void process_try (struct process *p)
  264. {
  265. struct instance *o = p->manager;
  266. ASSERT(!o->dying)
  267. ASSERT(p->have_params)
  268. ASSERT(!p->have_module_process)
  269. ModuleLog(o->i, BLOG_INFO, "trying process %s", p->name);
  270. // init module process
  271. if (!NCDModuleProcess_Init(&p->module_process, o->i, p->params_template_name, p->params_args, p, (NCDModuleProcess_handler_event)process_module_process_handler_event)) {
  272. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  273. // set timer
  274. BReactor_SetTimer(o->i->reactor, &p->retry_timer);
  275. // set state
  276. p->state = PROCESS_STATE_RETRYING;
  277. return;
  278. }
  279. // free params
  280. free(p->params_template_name);
  281. p->have_params = 0;
  282. // set have module process
  283. p->have_module_process = 1;
  284. // set state
  285. p->state = PROCESS_STATE_RUNNING;
  286. }
  287. int process_set_params (struct process *p, const char *template_name, NCDValue args)
  288. {
  289. ASSERT(!p->have_params)
  290. // copy template name
  291. if (!(p->params_template_name = strdup(template_name))) {
  292. ModuleLog(p->manager->i, BLOG_ERROR, "strdup failed");
  293. return 0;
  294. }
  295. // eat arguments
  296. p->params_args = args;
  297. // set have params
  298. p->have_params = 1;
  299. return 1;
  300. }
  301. static void func_new (NCDModuleInst *i)
  302. {
  303. // allocate instance
  304. struct instance *o = malloc(sizeof(*o));
  305. if (!o) {
  306. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  307. goto fail0;
  308. }
  309. NCDModuleInst_Backend_SetUser(i, o);
  310. // init arguments
  311. o->i = i;
  312. // check arguments
  313. if (!NCDValue_ListRead(o->i->args, 0)) {
  314. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  315. goto fail1;
  316. }
  317. // init processes list
  318. LinkedList2_Init(&o->processes_list);
  319. // set not dying
  320. o->dying = 0;
  321. // signal up
  322. NCDModuleInst_Backend_Up(o->i);
  323. return;
  324. fail1:
  325. free(o);
  326. fail0:
  327. NCDModuleInst_Backend_SetError(i);
  328. NCDModuleInst_Backend_Dead(i);
  329. }
  330. void instance_free (struct instance *o)
  331. {
  332. ASSERT(LinkedList2_IsEmpty(&o->processes_list))
  333. NCDModuleInst *i = o->i;
  334. // free instance
  335. free(o);
  336. NCDModuleInst_Backend_Dead(i);
  337. }
  338. static void func_die (void *vo)
  339. {
  340. struct instance *o = vo;
  341. ASSERT(!o->dying)
  342. // request all processes to die
  343. LinkedList2Iterator it;
  344. LinkedList2Iterator_InitForward(&it, &o->processes_list);
  345. LinkedList2Node *n;
  346. while (n = LinkedList2Iterator_Next(&it)) {
  347. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  348. process_stop(p);
  349. }
  350. // if there are no processes, die immediately
  351. if (LinkedList2_IsEmpty(&o->processes_list)) {
  352. instance_free(o);
  353. return;
  354. }
  355. // set dying
  356. o->dying = 1;
  357. }
  358. static void start_func_new (NCDModuleInst *i)
  359. {
  360. // allocate instance
  361. struct startstop_instance *o = malloc(sizeof(*o));
  362. if (!o) {
  363. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  364. goto fail0;
  365. }
  366. NCDModuleInst_Backend_SetUser(i, o);
  367. // init arguments
  368. o->i = i;
  369. // check arguments
  370. NCDValue *name_arg;
  371. NCDValue *template_name_arg;
  372. NCDValue *args_arg;
  373. if (!NCDValue_ListRead(o->i->args, 3, &name_arg, &template_name_arg, &args_arg)) {
  374. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  375. goto fail1;
  376. }
  377. if (NCDValue_Type(name_arg) != NCDVALUE_STRING || NCDValue_Type(template_name_arg) != NCDVALUE_STRING ||
  378. NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  379. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  380. goto fail1;
  381. }
  382. char *name = NCDValue_StringValue(name_arg);
  383. char *template_name = NCDValue_StringValue(template_name_arg);
  384. // signal up.
  385. // Do it before creating the process so that the process starts initializing before our own process continues.
  386. NCDModuleInst_Backend_Up(o->i);
  387. // get method object
  388. struct instance *mo = i->method_object->inst_user;
  389. if (mo->dying) {
  390. ModuleLog(o->i, BLOG_INFO, "manager is dying, not creating process %s", name);
  391. } else {
  392. struct process *p = find_process(mo, name);
  393. if (p && p->state != PROCESS_STATE_STOPPING) {
  394. ModuleLog(o->i, BLOG_INFO, "process %s already started", name);
  395. } else {
  396. if (p) {
  397. if (!process_restart(p, template_name, args_arg)) {
  398. ModuleLog(o->i, BLOG_ERROR, "failed to restart process %s", name);
  399. goto fail1;
  400. }
  401. } else {
  402. if (!process_new(mo, name, template_name, args_arg)) {
  403. ModuleLog(o->i, BLOG_ERROR, "failed to create process %s", name);
  404. goto fail1;
  405. }
  406. }
  407. }
  408. }
  409. return;
  410. fail1:
  411. free(o);
  412. fail0:
  413. NCDModuleInst_Backend_SetError(i);
  414. NCDModuleInst_Backend_Dead(i);
  415. }
  416. static void stop_func_new (NCDModuleInst *i)
  417. {
  418. // allocate instance
  419. struct startstop_instance *o = malloc(sizeof(*o));
  420. if (!o) {
  421. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  422. goto fail0;
  423. }
  424. NCDModuleInst_Backend_SetUser(i, o);
  425. // init arguments
  426. o->i = i;
  427. // check arguments
  428. NCDValue *name_arg;
  429. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  430. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  431. goto fail1;
  432. }
  433. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  434. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  435. goto fail1;
  436. }
  437. char *name = NCDValue_StringValue(name_arg);
  438. // signal up.
  439. // Do it before stopping the process so that the process starts terminating before our own process continues.
  440. NCDModuleInst_Backend_Up(o->i);
  441. // get method object
  442. struct instance *mo = i->method_object->inst_user;
  443. if (mo->dying) {
  444. ModuleLog(o->i, BLOG_INFO, "manager is dying, not stopping process %s", name);
  445. } else {
  446. struct process *p = find_process(mo, name);
  447. if (!(p && p->state != PROCESS_STATE_STOPPING)) {
  448. ModuleLog(o->i, BLOG_INFO, "process %s already stopped", name);
  449. } else {
  450. process_stop(p);
  451. }
  452. }
  453. return;
  454. fail1:
  455. free(o);
  456. fail0:
  457. NCDModuleInst_Backend_SetError(i);
  458. NCDModuleInst_Backend_Dead(i);
  459. }
  460. static void startstop_func_die (void *vo)
  461. {
  462. struct startstop_instance *o = vo;
  463. NCDModuleInst *i = o->i;
  464. // free instance
  465. free(o);
  466. NCDModuleInst_Backend_Dead(i);
  467. }
  468. static const struct NCDModule modules[] = {
  469. {
  470. .type = "process_manager",
  471. .func_new = func_new,
  472. .func_die = func_die
  473. }, {
  474. .type = "process_manager::start",
  475. .func_new = start_func_new,
  476. .func_die = startstop_func_die
  477. }, {
  478. .type = "process_manager::stop",
  479. .func_new = stop_func_new,
  480. .func_die = startstop_func_die
  481. }, {
  482. .type = NULL
  483. }
  484. };
  485. const struct NCDModuleGroup ncdmodule_process_manager = {
  486. .modules = modules
  487. };