process_manager.c 15 KB

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