daemon.c 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274
  1. /**
  2. * @file daemon.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. * Runs a program in the background, restarting it if it crashes.
  25. * On deinitialization, sends SIGTERM to the daemon and waits for it to terminate
  26. * (unless it's crashed at the time).
  27. *
  28. * Synopsis:
  29. * daemon(list(string) cmd)
  30. *
  31. * Arguments:
  32. * cmd - Command for the daemon. The first element is the full path
  33. * to the executable, other elements are command line arguments (excluding
  34. * the zeroth argument).
  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_daemon.h>
  43. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  44. #define RETRY_TIME 10000
  45. #define STATE_RETRYING 1
  46. #define STATE_RUNNING 2
  47. #define STATE_RUNNING_DIE 3
  48. struct instance {
  49. NCDModuleInst *i;
  50. NCDValue *cmd_arg;
  51. BTimer timer;
  52. BProcess process;
  53. int state;
  54. };
  55. static int build_cmdline (NCDModuleInst *i, NCDValue *cmd_arg, char **exec, CmdLine *cl);
  56. static void start_process (struct instance *o);
  57. static void timer_handler (struct instance *o);
  58. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  59. static void instance_free (struct instance *o);
  60. static int build_cmdline (NCDModuleInst *i, NCDValue *cmd_arg, char **exec, CmdLine *cl)
  61. {
  62. if (NCDValue_Type(cmd_arg) != NCDVALUE_LIST) {
  63. ModuleLog(i, BLOG_ERROR, "wrong type");
  64. goto fail0;
  65. }
  66. // read exec
  67. NCDValue *exec_arg = NCDValue_ListFirst(cmd_arg);
  68. if (!exec_arg) {
  69. ModuleLog(i, BLOG_ERROR, "missing executable name");
  70. goto fail0;
  71. }
  72. if (NCDValue_Type(exec_arg) != NCDVALUE_STRING) {
  73. ModuleLog(i, BLOG_ERROR, "wrong type");
  74. goto fail0;
  75. }
  76. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  77. ModuleLog(i, BLOG_ERROR, "strdup failed");
  78. goto fail0;
  79. }
  80. // start cmdline
  81. if (!CmdLine_Init(cl)) {
  82. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  83. goto fail1;
  84. }
  85. // add header
  86. if (!CmdLine_Append(cl, *exec)) {
  87. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  88. goto fail2;
  89. }
  90. // add additional arguments
  91. NCDValue *arg = exec_arg;
  92. while (arg = NCDValue_ListNext(cmd_arg, arg)) {
  93. if (NCDValue_Type(arg) != NCDVALUE_STRING) {
  94. ModuleLog(i, BLOG_ERROR, "wrong type");
  95. goto fail2;
  96. }
  97. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  98. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  99. goto fail2;
  100. }
  101. }
  102. // finish
  103. if (!CmdLine_Finish(cl)) {
  104. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  105. goto fail2;
  106. }
  107. return 1;
  108. fail2:
  109. CmdLine_Free(cl);
  110. fail1:
  111. free(*exec);
  112. fail0:
  113. return 0;
  114. }
  115. static void start_process (struct instance *o)
  116. {
  117. // build cmdline
  118. char *exec;
  119. CmdLine cl;
  120. if (!build_cmdline(o->i, o->cmd_arg, &exec, &cl)) {
  121. goto fail;
  122. }
  123. // start process
  124. int res = BProcess_Init(&o->process, o->i->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL);
  125. CmdLine_Free(&cl);
  126. free(exec);
  127. if (!res) {
  128. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  129. goto fail;
  130. }
  131. // set state running
  132. o->state = STATE_RUNNING;
  133. return;
  134. fail:
  135. // start timer
  136. BReactor_SetTimer(o->i->reactor, &o->timer);
  137. // set state retrying
  138. o->state = STATE_RETRYING;
  139. }
  140. static void timer_handler (struct instance *o)
  141. {
  142. ASSERT(o->state == STATE_RETRYING)
  143. ModuleLog(o->i, BLOG_INFO, "restarting after crash");
  144. start_process(o);
  145. }
  146. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  147. {
  148. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  149. // free process
  150. BProcess_Free(&o->process);
  151. // if we were requested to die, die now
  152. if (o->state == STATE_RUNNING_DIE) {
  153. instance_free(o);
  154. return;
  155. }
  156. BLog(BLOG_ERROR, "daemon crashed");
  157. // start timer
  158. BReactor_SetTimer(o->i->reactor, &o->timer);
  159. // set state retrying
  160. o->state = STATE_RETRYING;
  161. }
  162. static void func_new (NCDModuleInst *i)
  163. {
  164. // allocate instance
  165. struct instance *o = malloc(sizeof(*o));
  166. if (!o) {
  167. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  168. goto fail0;
  169. }
  170. NCDModuleInst_Backend_SetUser(i, o);
  171. // init arguments
  172. o->i = i;
  173. // read arguments
  174. if (!NCDValue_ListRead(i->args, 1, &o->cmd_arg)) {
  175. ModuleLog(i, BLOG_ERROR, "wrong arity");
  176. goto fail1;
  177. }
  178. // init timer
  179. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  180. // signal up
  181. NCDModuleInst_Backend_Up(i);
  182. // try starting process
  183. start_process(o);
  184. return;
  185. fail1:
  186. free(o);
  187. fail0:
  188. NCDModuleInst_Backend_SetError(i);
  189. NCDModuleInst_Backend_Dead(i);
  190. }
  191. static void instance_free (struct instance *o)
  192. {
  193. NCDModuleInst *i = o->i;
  194. // free timer
  195. BReactor_RemoveTimer(o->i->reactor, &o->timer);
  196. // free instance
  197. free(o);
  198. NCDModuleInst_Backend_Dead(i);
  199. }
  200. static void func_die (void *vo)
  201. {
  202. struct instance *o = vo;
  203. ASSERT(o->state != STATE_RUNNING_DIE)
  204. // if not running, die immediately
  205. if (o->state == STATE_RETRYING) {
  206. instance_free(o);
  207. return;
  208. }
  209. // request termination
  210. BProcess_Terminate(&o->process);
  211. // set state running die
  212. o->state = STATE_RUNNING_DIE;
  213. }
  214. static const struct NCDModule modules[] = {
  215. {
  216. .type = "daemon",
  217. .func_new = func_new,
  218. .func_die = func_die
  219. }, {
  220. .type = NULL
  221. }
  222. };
  223. const struct NCDModuleGroup ncdmodule_daemon = {
  224. .modules = modules
  225. };