process_manager.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580
  1. /**
  2. * @file process_manager.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * This file is part of BadVPN.
  8. *
  9. * BadVPN is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License version 2
  11. * as published by the Free Software Foundation.
  12. *
  13. * BadVPN is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License along
  19. * with this program; if not, write to the Free Software Foundation, Inc.,
  20. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  21. *
  22. * @section DESCRIPTION
  23. *
  24. * Module which allows starting and stopping processes from templates dynamically.
  25. *
  26. * Synopsis: process_manager()
  27. * Description: manages processes. On deinitialization, initiates termination of all
  28. * contained processes and waits for them to terminate.
  29. *
  30. * Synopsis: process_manager::start(string name, string template_name, list(string) args)
  31. * Description: creates a new process from the template named template_name, with arguments args,
  32. * identified by name within the process manager. If a process with this name already exists
  33. * and is not being terminated, does nothing. If it is being terminated, it will be restarted
  34. * using the given parameters after it terminates.
  35. *
  36. * Synopsis: process_manager::stop(string name)
  37. * Description: initiates termination of the process identified by name within the process manager.
  38. * If there is no such process, or the process is already being terminated, does nothing.
  39. */
  40. #include <stdlib.h>
  41. #include <string.h>
  42. #include <misc/offset.h>
  43. #include <structure/LinkedList2.h>
  44. #include <ncd/NCDModule.h>
  45. #include <generated/blog_channel_ncd_process_manager.h>
  46. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  47. #define RETRY_TIME 10000
  48. #define PROCESS_STATE_RUNNING 1
  49. #define PROCESS_STATE_STOPPING 2
  50. #define PROCESS_STATE_RESTARTING 3
  51. #define PROCESS_STATE_RETRYING 4
  52. struct instance {
  53. NCDModuleInst *i;
  54. LinkedList2 processes_list;
  55. int dying;
  56. };
  57. struct process {
  58. struct instance *manager;
  59. char *name;
  60. BTimer retry_timer;
  61. LinkedList2Node processes_list_node;
  62. int have_params;
  63. char *params_template_name;
  64. NCDValue params_args;
  65. int have_module_process;
  66. NCDModuleProcess module_process;
  67. int state;
  68. };
  69. struct startstop_instance {
  70. NCDModuleInst *i;
  71. };
  72. static struct process * find_process (struct instance *o, const char *name);
  73. static int process_new (struct instance *o, const char *name, const char *template_name, NCDValue *args);
  74. static void process_free (struct process *p);
  75. static void process_retry_timer_handler (struct process *p);
  76. static void process_module_process_handler_dead (struct process *p);
  77. static void process_stop (struct process *p);
  78. static int process_restart (struct process *p, const char *template_name, NCDValue *args);
  79. static void process_try (struct process *p);
  80. static int process_set_params (struct process *p, const char *template_name, NCDValue args);
  81. static void instance_free (struct instance *o);
  82. struct process * find_process (struct instance *o, const char *name)
  83. {
  84. LinkedList2Node *n = LinkedList2_GetFirst(&o->processes_list);
  85. while (n) {
  86. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  87. if (!strcmp(p->name, name)) {
  88. return p;
  89. }
  90. n = LinkedList2Node_Next(n);
  91. }
  92. return NULL;
  93. }
  94. int process_new (struct instance *o, const char *name, const char *template_name, NCDValue *args)
  95. {
  96. ASSERT(!o->dying)
  97. ASSERT(!find_process(o, name))
  98. ASSERT(NCDValue_Type(args) == NCDVALUE_LIST)
  99. // allocate structure
  100. struct process *p = malloc(sizeof(*p));
  101. if (!p) {
  102. ModuleLog(o->i, BLOG_ERROR, "malloc failed");
  103. goto fail0;
  104. }
  105. // set manager
  106. p->manager = o;
  107. // copy name
  108. if (!(p->name = strdup(name))) {
  109. ModuleLog(o->i, BLOG_ERROR, "strdup failed");
  110. goto fail1;
  111. }
  112. // init retry timer
  113. BTimer_Init(&p->retry_timer, RETRY_TIME, (BTimer_handler)process_retry_timer_handler, p);
  114. // insert to processes list
  115. LinkedList2_Append(&o->processes_list, &p->processes_list_node);
  116. // have no params
  117. p->have_params = 0;
  118. // have no module process
  119. p->have_module_process = 0;
  120. // copy arguments
  121. NCDValue args2;
  122. if (!NCDValue_InitCopy(&args2, args)) {
  123. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  124. goto fail2;
  125. }
  126. // set params
  127. if (!process_set_params(p, template_name, args2)) {
  128. NCDValue_Free(&args2);
  129. goto fail2;
  130. }
  131. // try starting it
  132. process_try(p);
  133. return 1;
  134. fail2:
  135. LinkedList2_Remove(&o->processes_list, &p->processes_list_node);
  136. free(p->name);
  137. fail1:
  138. free(p);
  139. fail0:
  140. return 0;
  141. }
  142. void process_free (struct process *p)
  143. {
  144. ASSERT(!p->have_module_process)
  145. struct instance *o = p->manager;
  146. // free params
  147. if (p->have_params) {
  148. NCDValue_Free(&p->params_args);
  149. free(p->params_template_name);
  150. }
  151. // remove from processes list
  152. LinkedList2_Remove(&o->processes_list, &p->processes_list_node);
  153. // free timer
  154. BReactor_RemoveTimer(o->i->reactor, &p->retry_timer);
  155. // free name
  156. free(p->name);
  157. // free structure
  158. free(p);
  159. }
  160. void process_retry_timer_handler (struct process *p)
  161. {
  162. struct instance *o = p->manager;
  163. ASSERT(p->state == PROCESS_STATE_RETRYING)
  164. ASSERT(!o->dying)
  165. ASSERT(p->have_params)
  166. ASSERT(!p->have_module_process)
  167. // retry
  168. process_try(p);
  169. }
  170. void process_module_process_handler_dead (struct process *p)
  171. {
  172. struct instance *o = p->manager;
  173. ASSERT(p->have_module_process)
  174. // free module process
  175. NCDModuleProcess_Free(&p->module_process);
  176. // set no module process
  177. p->have_module_process = 0;
  178. switch (p->state) {
  179. case PROCESS_STATE_STOPPING: {
  180. // free process
  181. process_free(p);
  182. // if manager is dying and there are no more processes, let it die
  183. if (o->dying && LinkedList2_IsEmpty(&o->processes_list)) {
  184. instance_free(o);
  185. }
  186. return;
  187. } break;
  188. case PROCESS_STATE_RESTARTING: {
  189. ASSERT(!o->dying)
  190. ASSERT(p->have_params)
  191. // restart
  192. process_try(p);
  193. } break;
  194. default: ASSERT(0);
  195. }
  196. }
  197. void process_stop (struct process *p)
  198. {
  199. switch (p->state) {
  200. case PROCESS_STATE_RETRYING: {
  201. ASSERT(!p->have_module_process)
  202. // free process
  203. process_free(p);
  204. return;
  205. } break;
  206. case PROCESS_STATE_RUNNING: {
  207. ASSERT(p->have_module_process)
  208. // request process to die
  209. NCDModuleProcess_Die(&p->module_process);
  210. // set state
  211. p->state = PROCESS_STATE_STOPPING;
  212. } break;
  213. case PROCESS_STATE_RESTARTING: {
  214. ASSERT(p->have_params)
  215. // free params
  216. NCDValue_Free(&p->params_args);
  217. free(p->params_template_name);
  218. p->have_params = 0;
  219. // set state
  220. p->state = PROCESS_STATE_STOPPING;
  221. } break;
  222. case PROCESS_STATE_STOPPING: {
  223. // nothing to do
  224. } break;
  225. default: ASSERT(0);
  226. }
  227. }
  228. int process_restart (struct process *p, const char *template_name, NCDValue *args)
  229. {
  230. struct instance *o = p->manager;
  231. ASSERT(!o->dying)
  232. ASSERT(p->state == PROCESS_STATE_STOPPING)
  233. ASSERT(!p->have_params)
  234. // copy arguments
  235. NCDValue args2;
  236. if (!NCDValue_InitCopy(&args2, args)) {
  237. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  238. return 0;
  239. }
  240. // set params
  241. if (!process_set_params(p, template_name, args2)) {
  242. NCDValue_Free(&args2);
  243. return 0;
  244. }
  245. // set state
  246. p->state = PROCESS_STATE_RESTARTING;
  247. return 1;
  248. }
  249. void process_try (struct process *p)
  250. {
  251. struct instance *o = p->manager;
  252. ASSERT(!o->dying)
  253. ASSERT(p->have_params)
  254. ASSERT(!p->have_module_process)
  255. ModuleLog(o->i, BLOG_INFO, "trying process %s", p->name);
  256. // init module process
  257. if (!NCDModuleProcess_Init(&p->module_process, o->i, p->params_template_name, p->params_args, p, (NCDModuleProcess_handler_dead)process_module_process_handler_dead)) {
  258. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  259. // set timer
  260. BReactor_SetTimer(o->i->reactor, &p->retry_timer);
  261. // set state
  262. p->state = PROCESS_STATE_RETRYING;
  263. return;
  264. }
  265. // free params
  266. free(p->params_template_name);
  267. p->have_params = 0;
  268. // set have module process
  269. p->have_module_process = 1;
  270. // set state
  271. p->state = PROCESS_STATE_RUNNING;
  272. }
  273. int process_set_params (struct process *p, const char *template_name, NCDValue args)
  274. {
  275. ASSERT(!p->have_params)
  276. // copy template name
  277. if (!(p->params_template_name = strdup(template_name))) {
  278. ModuleLog(p->manager->i, BLOG_ERROR, "strdup failed");
  279. return 0;
  280. }
  281. // eat arguments
  282. p->params_args = args;
  283. // set have params
  284. p->have_params = 1;
  285. return 1;
  286. }
  287. static void func_new (NCDModuleInst *i)
  288. {
  289. // allocate instance
  290. struct instance *o = malloc(sizeof(*o));
  291. if (!o) {
  292. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  293. goto fail0;
  294. }
  295. NCDModuleInst_Backend_SetUser(i, o);
  296. // init arguments
  297. o->i = i;
  298. // check arguments
  299. if (!NCDValue_ListRead(o->i->args, 0)) {
  300. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  301. goto fail1;
  302. }
  303. // init processes list
  304. LinkedList2_Init(&o->processes_list);
  305. // set not dying
  306. o->dying = 0;
  307. // signal up
  308. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  309. return;
  310. fail1:
  311. free(o);
  312. fail0:
  313. NCDModuleInst_Backend_SetError(i);
  314. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  315. }
  316. void instance_free (struct instance *o)
  317. {
  318. ASSERT(LinkedList2_IsEmpty(&o->processes_list))
  319. NCDModuleInst *i = o->i;
  320. // free instance
  321. free(o);
  322. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  323. }
  324. static void func_die (void *vo)
  325. {
  326. struct instance *o = vo;
  327. ASSERT(!o->dying)
  328. // request all processes to die
  329. LinkedList2Iterator it;
  330. LinkedList2Iterator_InitForward(&it, &o->processes_list);
  331. LinkedList2Node *n;
  332. while (n = LinkedList2Iterator_Next(&it)) {
  333. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  334. process_stop(p);
  335. }
  336. // if there are no processes, die immediately
  337. if (LinkedList2_IsEmpty(&o->processes_list)) {
  338. instance_free(o);
  339. return;
  340. }
  341. // set dying
  342. o->dying = 1;
  343. }
  344. static void start_func_new (NCDModuleInst *i)
  345. {
  346. // allocate instance
  347. struct startstop_instance *o = malloc(sizeof(*o));
  348. if (!o) {
  349. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  350. goto fail0;
  351. }
  352. NCDModuleInst_Backend_SetUser(i, o);
  353. // init arguments
  354. o->i = i;
  355. // check arguments
  356. NCDValue *name_arg;
  357. NCDValue *template_name_arg;
  358. NCDValue *args_arg;
  359. if (!NCDValue_ListRead(o->i->args, 3, &name_arg, &template_name_arg, &args_arg)) {
  360. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  361. goto fail1;
  362. }
  363. if (NCDValue_Type(name_arg) != NCDVALUE_STRING || NCDValue_Type(template_name_arg) != NCDVALUE_STRING ||
  364. NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  365. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  366. goto fail1;
  367. }
  368. char *name = NCDValue_StringValue(name_arg);
  369. char *template_name = NCDValue_StringValue(template_name_arg);
  370. // signal up.
  371. // Do it before creating the process so that the process starts initializing before our own process continues.
  372. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  373. // get method object
  374. struct instance *mo = i->method_object->inst_user;
  375. if (mo->dying) {
  376. ModuleLog(o->i, BLOG_INFO, "manager is dying, not creating process %s", name);
  377. } else {
  378. struct process *p = find_process(mo, name);
  379. if (p && p->state != PROCESS_STATE_STOPPING) {
  380. ModuleLog(o->i, BLOG_INFO, "process %s already started", name);
  381. } else {
  382. if (p) {
  383. if (!process_restart(p, template_name, args_arg)) {
  384. ModuleLog(o->i, BLOG_ERROR, "failed to restart process %s", name);
  385. goto fail1;
  386. }
  387. } else {
  388. if (!process_new(mo, name, template_name, args_arg)) {
  389. ModuleLog(o->i, BLOG_ERROR, "failed to create process %s", name);
  390. goto fail1;
  391. }
  392. }
  393. }
  394. }
  395. return;
  396. fail1:
  397. free(o);
  398. fail0:
  399. NCDModuleInst_Backend_SetError(i);
  400. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  401. }
  402. static void stop_func_new (NCDModuleInst *i)
  403. {
  404. // allocate instance
  405. struct startstop_instance *o = malloc(sizeof(*o));
  406. if (!o) {
  407. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  408. goto fail0;
  409. }
  410. NCDModuleInst_Backend_SetUser(i, o);
  411. // init arguments
  412. o->i = i;
  413. // check arguments
  414. NCDValue *name_arg;
  415. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  416. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  417. goto fail1;
  418. }
  419. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  420. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  421. goto fail1;
  422. }
  423. char *name = NCDValue_StringValue(name_arg);
  424. // signal up.
  425. // Do it before stopping the process so that the process starts terminating before our own process continues.
  426. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  427. // get method object
  428. struct instance *mo = i->method_object->inst_user;
  429. if (mo->dying) {
  430. ModuleLog(o->i, BLOG_INFO, "manager is dying, not stopping process %s", name);
  431. } else {
  432. struct process *p = find_process(mo, name);
  433. if (!(p && p->state != PROCESS_STATE_STOPPING)) {
  434. ModuleLog(o->i, BLOG_INFO, "process %s already stopped", name);
  435. } else {
  436. process_stop(p);
  437. }
  438. }
  439. return;
  440. fail1:
  441. free(o);
  442. fail0:
  443. NCDModuleInst_Backend_SetError(i);
  444. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  445. }
  446. static void startstop_func_die (void *vo)
  447. {
  448. struct startstop_instance *o = vo;
  449. NCDModuleInst *i = o->i;
  450. // free instance
  451. free(o);
  452. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  453. }
  454. static const struct NCDModule modules[] = {
  455. {
  456. .type = "process_manager",
  457. .func_new = func_new,
  458. .func_die = func_die
  459. }, {
  460. .type = "process_manager::start",
  461. .func_new = start_func_new,
  462. .func_die = startstop_func_die
  463. }, {
  464. .type = "process_manager::stop",
  465. .func_new = stop_func_new,
  466. .func_die = startstop_func_die
  467. }, {
  468. .type = NULL
  469. }
  470. };
  471. const struct NCDModuleGroup ncdmodule_process_manager = {
  472. .modules = modules
  473. };