synchronous_process.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  1. /**
  2. * @file synchronous_process.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 starts a process from a process template on initialization, and
  25. * stops it on deinitialization.
  26. *
  27. * Synopsis: synchronous_process(string template_name, list(string) args)
  28. * Description: on initialization, creates a new process from the template named
  29. * template_name, with arguments args. On deinitialization, initiates termination
  30. * of the process and waits for it to terminate.
  31. */
  32. #include <stdlib.h>
  33. #include <ncd/NCDModule.h>
  34. #include <generated/blog_channel_ncd_synchronous_process.h>
  35. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  36. struct instance {
  37. NCDModuleInst *i;
  38. NCDModuleProcess process;
  39. };
  40. static void instance_free (struct instance *o);
  41. static void process_handler_dead (struct instance *o)
  42. {
  43. // die now
  44. instance_free(o);
  45. }
  46. static void func_new (NCDModuleInst *i)
  47. {
  48. // allocate instance
  49. struct instance *o = malloc(sizeof(*o));
  50. if (!o) {
  51. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  52. goto fail0;
  53. }
  54. NCDModuleInst_Backend_SetUser(i, o);
  55. // init arguments
  56. o->i = i;
  57. // check arguments
  58. NCDValue *template_name_arg;
  59. NCDValue *args_arg;
  60. if (!NCDValue_ListRead(o->i->args, 2, &template_name_arg, &args_arg)) {
  61. ModuleLog(o->i, BLOG_ERROR, "wrong arity");
  62. goto fail1;
  63. }
  64. if (NCDValue_Type(template_name_arg) != NCDVALUE_STRING || NCDValue_Type(args_arg) != NCDVALUE_LIST) {
  65. ModuleLog(o->i, BLOG_ERROR, "wrong type");
  66. goto fail1;
  67. }
  68. // signal up.
  69. // Do it before creating the process so that the process starts initializing before our own process continues.
  70. NCDModuleInst_Backend_Event(o->i, NCDMODULE_EVENT_UP);
  71. // copy arguments
  72. NCDValue args;
  73. if (!NCDValue_InitCopy(&args, args_arg)) {
  74. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitCopy failed");
  75. goto fail1;
  76. }
  77. // create process
  78. if (!NCDModuleProcess_Init(&o->process, o->i, NCDValue_StringValue(template_name_arg), args, o, (NCDModuleProcess_handler_dead)process_handler_dead)) {
  79. ModuleLog(o->i, BLOG_ERROR, "NCDModuleProcess_Init failed");
  80. NCDValue_Free(&args);
  81. goto fail1;
  82. }
  83. return;
  84. fail1:
  85. free(o);
  86. fail0:
  87. NCDModuleInst_Backend_SetError(i);
  88. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  89. }
  90. void instance_free (struct instance *o)
  91. {
  92. NCDModuleInst *i = o->i;
  93. // free process
  94. NCDModuleProcess_Free(&o->process);
  95. // free instance
  96. free(o);
  97. NCDModuleInst_Backend_Event(i, NCDMODULE_EVENT_DEAD);
  98. }
  99. static void func_die (void *vo)
  100. {
  101. struct instance *o = vo;
  102. // request process to die
  103. NCDModuleProcess_Die(&o->process);
  104. }
  105. static const struct NCDModule modules[] = {
  106. {
  107. .type = "synchronous_process",
  108. .func_new = func_new,
  109. .func_die = func_die
  110. }, {
  111. .type = NULL
  112. }
  113. };
  114. const struct NCDModuleGroup ncdmodule_synchronous_process = {
  115. .modules = modules
  116. };