process_manager.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  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 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_event (struct process *p, int event)
  171. {
  172. struct instance *o = p->manager;
  173. ASSERT(p->have_module_process)
  174. if (event == NCDMODULEPROCESS_EVENT_DOWN) {
  175. // allow process to continue
  176. NCDModuleProcess_Continue(&p->module_process);
  177. }
  178. if (event != NCDMODULEPROCESS_EVENT_TERMINATED) {
  179. return;
  180. }
  181. // free module process
  182. NCDModuleProcess_Free(&p->module_process);
  183. // set no module process
  184. p->have_module_process = 0;
  185. switch (p->state) {
  186. case PROCESS_STATE_STOPPING: {
  187. // free process
  188. process_free(p);
  189. // if manager is dying and there are no more processes, let it die
  190. if (o->dying && LinkedList2_IsEmpty(&o->processes_list)) {
  191. instance_free(o);
  192. }
  193. return;
  194. } break;
  195. case PROCESS_STATE_RESTARTING: {
  196. ASSERT(!o->dying)
  197. ASSERT(p->have_params)
  198. // restart
  199. process_try(p);
  200. } break;
  201. default: ASSERT(0);
  202. }
  203. }
  204. void process_stop (struct process *p)
  205. {
  206. switch (p->state) {
  207. case PROCESS_STATE_RETRYING: {
  208. ASSERT(!p->have_module_process)
  209. // free process
  210. process_free(p);
  211. return;
  212. } break;
  213. case PROCESS_STATE_RUNNING: {
  214. ASSERT(p->have_module_process)
  215. // request process to terminate
  216. NCDModuleProcess_Terminate(&p->module_process);
  217. // set state
  218. p->state = PROCESS_STATE_STOPPING;
  219. } break;
  220. case PROCESS_STATE_RESTARTING: {
  221. ASSERT(p->have_params)
  222. // free params
  223. NCDValue_Free(&p->params_args);
  224. free(p->params_template_name);
  225. p->have_params = 0;
  226. // set state
  227. p->state = PROCESS_STATE_STOPPING;
  228. } break;
  229. case PROCESS_STATE_STOPPING: {
  230. // nothing to do
  231. } break;
  232. default: ASSERT(0);
  233. }
  234. }
  235. int process_restart (struct process *p, const char *template_name, NCDValue *args)
  236. {
  237. struct instance *o = p->manager;
  238. ASSERT(!o->dying)
  239. ASSERT(p->state == PROCESS_STATE_STOPPING)
  240. ASSERT(!p->have_params)
  241. // copy arguments
  242. NCDValue args2;
  243. if (!NCDValue_InitCopy(&args2, args)) {
  244. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  245. return 0;
  246. }
  247. // set params
  248. if (!process_set_params(p, template_name, args2)) {
  249. NCDValue_Free(&args2);
  250. return 0;
  251. }
  252. // set state
  253. p->state = PROCESS_STATE_RESTARTING;
  254. return 1;
  255. }
  256. void process_try (struct process *p)
  257. {
  258. struct instance *o = p->manager;
  259. ASSERT(!o->dying)
  260. ASSERT(p->have_params)
  261. ASSERT(!p->have_module_process)
  262. ModuleLog(o->i, BLOG_INFO, "trying process %s", p->name);
  263. // init module process
  264. if (!NCDModuleProcess_Init(&p->module_process, o->i, p->params_template_name, p->params_args, p, (NCDModuleProcess_handler_event)process_module_process_handler_event)) {
  265. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  266. // set timer
  267. BReactor_SetTimer(o->i->reactor, &p->retry_timer);
  268. // set state
  269. p->state = PROCESS_STATE_RETRYING;
  270. return;
  271. }
  272. // free params
  273. free(p->params_template_name);
  274. p->have_params = 0;
  275. // set have module process
  276. p->have_module_process = 1;
  277. // set state
  278. p->state = PROCESS_STATE_RUNNING;
  279. }
  280. int process_set_params (struct process *p, const char *template_name, NCDValue args)
  281. {
  282. ASSERT(!p->have_params)
  283. // copy template name
  284. if (!(p->params_template_name = strdup(template_name))) {
  285. ModuleLog(p->manager->i, BLOG_ERROR, "strdup failed");
  286. return 0;
  287. }
  288. // eat arguments
  289. p->params_args = args;
  290. // set have params
  291. p->have_params = 1;
  292. return 1;
  293. }
  294. static void func_new (NCDModuleInst *i)
  295. {
  296. // allocate instance
  297. struct instance *o = malloc(sizeof(*o));
  298. if (!o) {
  299. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  300. goto fail0;
  301. }
  302. NCDModuleInst_Backend_SetUser(i, o);
  303. // init arguments
  304. o->i = i;
  305. // check arguments
  306. if (!NCDValue_ListRead(o->i->args, 0)) {
  307. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  308. goto fail1;
  309. }
  310. // init processes list
  311. LinkedList2_Init(&o->processes_list);
  312. // set not dying
  313. o->dying = 0;
  314. // signal up
  315. NCDModuleInst_Backend_Up(o->i);
  316. return;
  317. fail1:
  318. free(o);
  319. fail0:
  320. NCDModuleInst_Backend_SetError(i);
  321. NCDModuleInst_Backend_Dead(i);
  322. }
  323. void instance_free (struct instance *o)
  324. {
  325. ASSERT(LinkedList2_IsEmpty(&o->processes_list))
  326. NCDModuleInst *i = o->i;
  327. // free instance
  328. free(o);
  329. NCDModuleInst_Backend_Dead(i);
  330. }
  331. static void func_die (void *vo)
  332. {
  333. struct instance *o = vo;
  334. ASSERT(!o->dying)
  335. // request all processes to die
  336. LinkedList2Iterator it;
  337. LinkedList2Iterator_InitForward(&it, &o->processes_list);
  338. LinkedList2Node *n;
  339. while (n = LinkedList2Iterator_Next(&it)) {
  340. struct process *p = UPPER_OBJECT(n, struct process, processes_list_node);
  341. process_stop(p);
  342. }
  343. // if there are no processes, die immediately
  344. if (LinkedList2_IsEmpty(&o->processes_list)) {
  345. instance_free(o);
  346. return;
  347. }
  348. // set dying
  349. o->dying = 1;
  350. }
  351. static void start_func_new (NCDModuleInst *i)
  352. {
  353. // allocate instance
  354. struct startstop_instance *o = malloc(sizeof(*o));
  355. if (!o) {
  356. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  357. goto fail0;
  358. }
  359. NCDModuleInst_Backend_SetUser(i, o);
  360. // init arguments
  361. o->i = i;
  362. // check arguments
  363. NCDValue *name_arg;
  364. NCDValue *template_name_arg;
  365. NCDValue *args_arg;
  366. if (!NCDValue_ListRead(o->i->args, 3, &name_arg, &template_name_arg, &args_arg)) {
  367. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  368. goto fail1;
  369. }
  370. if (NCDValue_Type(name_arg) != NCDVALUE_STRING || NCDValue_Type(template_name_arg) != NCDVALUE_STRING ||
  371. NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  372. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  373. goto fail1;
  374. }
  375. char *name = NCDValue_StringValue(name_arg);
  376. char *template_name = NCDValue_StringValue(template_name_arg);
  377. // signal up.
  378. // Do it before creating the process so that the process starts initializing before our own process continues.
  379. NCDModuleInst_Backend_Up(o->i);
  380. // get method object
  381. struct instance *mo = i->method_object->inst_user;
  382. if (mo->dying) {
  383. ModuleLog(o->i, BLOG_INFO, "manager is dying, not creating process %s", name);
  384. } else {
  385. struct process *p = find_process(mo, name);
  386. if (p && p->state != PROCESS_STATE_STOPPING) {
  387. ModuleLog(o->i, BLOG_INFO, "process %s already started", name);
  388. } else {
  389. if (p) {
  390. if (!process_restart(p, template_name, args_arg)) {
  391. ModuleLog(o->i, BLOG_ERROR, "failed to restart process %s", name);
  392. goto fail1;
  393. }
  394. } else {
  395. if (!process_new(mo, name, template_name, args_arg)) {
  396. ModuleLog(o->i, BLOG_ERROR, "failed to create process %s", name);
  397. goto fail1;
  398. }
  399. }
  400. }
  401. }
  402. return;
  403. fail1:
  404. free(o);
  405. fail0:
  406. NCDModuleInst_Backend_SetError(i);
  407. NCDModuleInst_Backend_Dead(i);
  408. }
  409. static void stop_func_new (NCDModuleInst *i)
  410. {
  411. // allocate instance
  412. struct startstop_instance *o = malloc(sizeof(*o));
  413. if (!o) {
  414. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  415. goto fail0;
  416. }
  417. NCDModuleInst_Backend_SetUser(i, o);
  418. // init arguments
  419. o->i = i;
  420. // check arguments
  421. NCDValue *name_arg;
  422. if (!NCDValue_ListRead(o->i->args, 1, &name_arg)) {
  423. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  424. goto fail1;
  425. }
  426. if (NCDValue_Type(name_arg) != NCDVALUE_STRING) {
  427. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  428. goto fail1;
  429. }
  430. char *name = NCDValue_StringValue(name_arg);
  431. // signal up.
  432. // Do it before stopping the process so that the process starts terminating before our own process continues.
  433. NCDModuleInst_Backend_Up(o->i);
  434. // get method object
  435. struct instance *mo = i->method_object->inst_user;
  436. if (mo->dying) {
  437. ModuleLog(o->i, BLOG_INFO, "manager is dying, not stopping process %s", name);
  438. } else {
  439. struct process *p = find_process(mo, name);
  440. if (!(p && p->state != PROCESS_STATE_STOPPING)) {
  441. ModuleLog(o->i, BLOG_INFO, "process %s already stopped", name);
  442. } else {
  443. process_stop(p);
  444. }
  445. }
  446. return;
  447. fail1:
  448. free(o);
  449. fail0:
  450. NCDModuleInst_Backend_SetError(i);
  451. NCDModuleInst_Backend_Dead(i);
  452. }
  453. static void startstop_func_die (void *vo)
  454. {
  455. struct startstop_instance *o = vo;
  456. NCDModuleInst *i = o->i;
  457. // free instance
  458. free(o);
  459. NCDModuleInst_Backend_Dead(i);
  460. }
  461. static const struct NCDModule modules[] = {
  462. {
  463. .type = "process_manager",
  464. .func_new = func_new,
  465. .func_die = func_die
  466. }, {
  467. .type = "process_manager::start",
  468. .func_new = start_func_new,
  469. .func_die = startstop_func_die
  470. }, {
  471. .type = "process_manager::stop",
  472. .func_new = stop_func_new,
  473. .func_die = startstop_func_die
  474. }, {
  475. .type = NULL
  476. }
  477. };
  478. const struct NCDModuleGroup ncdmodule_process_manager = {
  479. .modules = modules
  480. };