runonce.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  1. /**
  2. * @file runonce.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. * Imperative program execution module. On initialization, starts the process.
  25. * Goes to UP state when the process terminates. When requested to die, waits for
  26. * the process to terminate if it's running, without sending any signals.
  27. *
  28. * Synopsis: runonce(list(string) cmd)
  29. * Arguments:
  30. * cmd - Command to run on startup. The first element is the full path
  31. * to the executable, other elements are command line arguments (excluding
  32. * the zeroth argument).
  33. * Variables:
  34. * string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
  35. */
  36. #include <stdlib.h>
  37. #include <stdio.h>
  38. #include <string.h>
  39. #include <misc/cmdline.h>
  40. #include <system/BProcess.h>
  41. #include <ncd/NCDModule.h>
  42. #include <generated/blog_channel_ncd_runonce.h>
  43. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  44. #define STATE_RUNNING 1
  45. #define STATE_RUNNING_DIE 2
  46. #define STATE_FINISHED 3
  47. struct instance {
  48. NCDModuleInst *i;
  49. int state;
  50. BProcess process;
  51. int exit_status;
  52. };
  53. static void instance_free (struct instance *o);
  54. static int build_cmdline (NCDModuleInst *i, NCDValue *cmd_arg, char **exec, CmdLine *cl)
  55. {
  56. if (NCDValue_Type(cmd_arg) != NCDVALUE_LIST) {
  57. ModuleLog(i, BLOG_ERROR, "wrong type");
  58. goto fail0;
  59. }
  60. // read exec
  61. NCDValue *exec_arg = NCDValue_ListFirst(cmd_arg);
  62. if (!exec_arg) {
  63. ModuleLog(i, BLOG_ERROR, "missing executable name");
  64. goto fail0;
  65. }
  66. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  67. ModuleLog(i, BLOG_ERROR, "wrong type");
  68. goto fail0;
  69. }
  70. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  71. ModuleLog(i, BLOG_ERROR, "strdup failed");
  72. goto fail0;
  73. }
  74. // start cmdline
  75. if (!CmdLine_Init(cl)) {
  76. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  77. goto fail1;
  78. }
  79. // add header
  80. if (!CmdLine_Append(cl, *exec)) {
  81. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  82. goto fail2;
  83. }
  84. // add additional arguments
  85. NCDValue *arg = exec_arg;
  86. while (arg = NCDValue_ListNext(cmd_arg, arg)) {
  87. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  88. ModuleLog(i, BLOG_ERROR, "wrong type");
  89. goto fail2;
  90. }
  91. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  92. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  93. goto fail2;
  94. }
  95. }
  96. // finish
  97. if (!CmdLine_Finish(cl)) {
  98. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  99. goto fail2;
  100. }
  101. return 1;
  102. fail2:
  103. CmdLine_Free(cl);
  104. fail1:
  105. free(*exec);
  106. fail0:
  107. return 0;
  108. }
  109. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  110. {
  111. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  112. // free process
  113. BProcess_Free(&o->process);
  114. // if we were requested to die, die now
  115. if (o->state == STATE_RUNNING_DIE) {
  116. instance_free(o);
  117. return;
  118. }
  119. // remember exit status
  120. o->exit_status = (normally ? normally_exit_status : -1);
  121. // set state
  122. o->state = STATE_FINISHED;
  123. // set up
  124. NCDModuleInst_Backend_Up(o->i);
  125. }
  126. static void func_new (NCDModuleInst *i)
  127. {
  128. // allocate instance
  129. struct instance *o = malloc(sizeof(*o));
  130. if (!o) {
  131. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  132. goto fail0;
  133. }
  134. NCDModuleInst_Backend_SetUser(i, o);
  135. // init arguments
  136. o->i = i;
  137. // read arguments
  138. NCDValue *cmd_arg;
  139. if (!NCDValue_ListRead(i->args, 1, &cmd_arg)) {
  140. ModuleLog(i, BLOG_ERROR, "wrong arity");
  141. goto fail1;
  142. }
  143. // build cmdline
  144. char *exec;
  145. CmdLine cl;
  146. if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
  147. goto fail1;
  148. }
  149. // start process
  150. if (!BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL)) {
  151. ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
  152. CmdLine_Free(&cl);
  153. free(exec);
  154. goto fail1;
  155. }
  156. CmdLine_Free(&cl);
  157. free(exec);
  158. // set state
  159. o->state = STATE_RUNNING;
  160. return;
  161. fail1:
  162. free(o);
  163. fail0:
  164. NCDModuleInst_Backend_SetError(i);
  165. NCDModuleInst_Backend_Dead(i);
  166. }
  167. static void instance_free (struct instance *o)
  168. {
  169. NCDModuleInst *i = o->i;
  170. // free instance
  171. free(o);
  172. NCDModuleInst_Backend_Dead(i);
  173. }
  174. static void func_die (void *vo)
  175. {
  176. struct instance *o = vo;
  177. ASSERT(o->state != STATE_RUNNING_DIE)
  178. if (o->state == STATE_FINISHED) {
  179. instance_free(o);
  180. return;
  181. }
  182. o->state = STATE_RUNNING_DIE;
  183. }
  184. static int func_getvar (void *vo, const char *name, NCDValue *out)
  185. {
  186. struct instance *o = vo;
  187. ASSERT(o->state == STATE_FINISHED)
  188. if (!strcmp(name, "exit_status")) {
  189. char str[30];
  190. snprintf(str, sizeof(str), "%d", o->exit_status);
  191. if (!NCDValue_InitString(out, str)) {
  192. ModuleLog(o->i, BLOG_ERROR, "NCDValue_InitString failed");
  193. return 0;
  194. }
  195. return 1;
  196. }
  197. return 0;
  198. }
  199. static const struct NCDModule modules[] = {
  200. {
  201. .type = "runonce",
  202. .func_new = func_new,
  203. .func_die = func_die,
  204. .func_getvar = func_getvar
  205. }, {
  206. .type = NULL
  207. }
  208. };
  209. const struct NCDModuleGroup ncdmodule_runonce = {
  210. .modules = modules
  211. };