daemon.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /**
  2. * @file daemon.c
  3. * @author Ambroz Bizjak <ambrop7@gmail.com>
  4. *
  5. * @section LICENSE
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. * 1. Redistributions of source code must retain the above copyright
  10. * notice, this list of conditions and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. * 3. Neither the name of the author nor the
  15. * names of its contributors may be used to endorse or promote products
  16. * derived from this software without specific prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  19. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  20. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  21. * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
  22. * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  23. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  24. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  25. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  26. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  27. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  28. *
  29. * @section DESCRIPTION
  30. *
  31. * Runs a program in the background, restarting it if it crashes.
  32. * On deinitialization, sends SIGTERM to the daemon and waits for it to terminate
  33. * (unless it's crashed at the time).
  34. *
  35. * Synopsis:
  36. * daemon(list(string) cmd)
  37. *
  38. * Arguments:
  39. * cmd - Command for the daemon. The first element is the full path
  40. * to the executable, other elements are command line arguments (excluding
  41. * the zeroth argument).
  42. */
  43. #include <stdlib.h>
  44. #include <stdio.h>
  45. #include <string.h>
  46. #include <misc/cmdline.h>
  47. #include <system/BProcess.h>
  48. #include <ncd/NCDModule.h>
  49. #include <generated/blog_channel_ncd_daemon.h>
  50. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  51. #define RETRY_TIME 10000
  52. #define STATE_RETRYING 1
  53. #define STATE_RUNNING 2
  54. #define STATE_RUNNING_DIE 3
  55. struct instance {
  56. NCDModuleInst *i;
  57. NCDValRef cmd_arg;
  58. BTimer timer;
  59. BProcess process;
  60. int state;
  61. };
  62. static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl);
  63. static void start_process (struct instance *o);
  64. static void timer_handler (struct instance *o);
  65. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  66. static void instance_free (struct instance *o);
  67. static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl)
  68. {
  69. ASSERT(!NCDVal_IsInvalid(cmd_arg))
  70. if (!NCDVal_IsList(cmd_arg)) {
  71. ModuleLog(i, BLOG_ERROR, "wrong type");
  72. goto fail0;
  73. }
  74. size_t count = NCDVal_ListCount(cmd_arg);
  75. // read exec
  76. if (count == 0) {
  77. ModuleLog(i, BLOG_ERROR, "missing executable name");
  78. goto fail0;
  79. }
  80. NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
  81. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  82. ModuleLog(i, BLOG_ERROR, "wrong type");
  83. goto fail0;
  84. }
  85. if (!(*exec = strdup(NCDVal_StringValue(exec_arg)))) {
  86. ModuleLog(i, BLOG_ERROR, "strdup failed");
  87. goto fail0;
  88. }
  89. // start cmdline
  90. if (!CmdLine_Init(cl)) {
  91. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  92. goto fail1;
  93. }
  94. // add header
  95. if (!CmdLine_Append(cl, *exec)) {
  96. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  97. goto fail2;
  98. }
  99. // add additional arguments
  100. for (size_t j = 1; j < count; j++) {
  101. NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
  102. if (!NCDVal_IsStringNoNulls(arg)) {
  103. ModuleLog(i, BLOG_ERROR, "wrong type");
  104. goto fail2;
  105. }
  106. if (!CmdLine_Append(cl, NCDVal_StringValue(arg))) {
  107. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  108. goto fail2;
  109. }
  110. }
  111. // finish
  112. if (!CmdLine_Finish(cl)) {
  113. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  114. goto fail2;
  115. }
  116. return 1;
  117. fail2:
  118. CmdLine_Free(cl);
  119. fail1:
  120. free(*exec);
  121. fail0:
  122. return 0;
  123. }
  124. static void start_process (struct instance *o)
  125. {
  126. // build cmdline
  127. char *exec;
  128. CmdLine cl;
  129. if (!build_cmdline(o->i, o->cmd_arg, &exec, &cl)) {
  130. goto fail;
  131. }
  132. // start process
  133. int res = BProcess_Init(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL);
  134. CmdLine_Free(&cl);
  135. free(exec);
  136. if (!res) {
  137. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  138. goto fail;
  139. }
  140. // set state running
  141. o->state = STATE_RUNNING;
  142. return;
  143. fail:
  144. // start timer
  145. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  146. // set state retrying
  147. o->state = STATE_RETRYING;
  148. }
  149. static void timer_handler (struct instance *o)
  150. {
  151. ASSERT(o->state == STATE_RETRYING)
  152. ModuleLog(o->i, BLOG_INFO, "restarting after crash");
  153. start_process(o);
  154. }
  155. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  156. {
  157. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  158. // free process
  159. BProcess_Free(&o->process);
  160. // if we were requested to die, die now
  161. if (o->state == STATE_RUNNING_DIE) {
  162. instance_free(o);
  163. return;
  164. }
  165. BLog(BLOG_ERROR, "daemon crashed");
  166. // start timer
  167. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  168. // set state retrying
  169. o->state = STATE_RETRYING;
  170. }
  171. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  172. {
  173. struct instance *o = vo;
  174. o->i = i;
  175. // read arguments
  176. if (!NCDVal_ListRead(params->args, 1, &o->cmd_arg)) {
  177. ModuleLog(i, BLOG_ERROR, "wrong arity");
  178. goto fail0;
  179. }
  180. // init timer
  181. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  182. // signal up
  183. NCDModuleInst_Backend_Up(i);
  184. // try starting process
  185. start_process(o);
  186. return;
  187. fail0:
  188. NCDModuleInst_Backend_SetError(i);
  189. NCDModuleInst_Backend_Dead(i);
  190. }
  191. static void instance_free (struct instance *o)
  192. {
  193. // free timer
  194. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  195. NCDModuleInst_Backend_Dead(o->i);
  196. }
  197. static void func_die (void *vo)
  198. {
  199. struct instance *o = vo;
  200. ASSERT(o->state != STATE_RUNNING_DIE)
  201. // if not running, die immediately
  202. if (o->state == STATE_RETRYING) {
  203. instance_free(o);
  204. return;
  205. }
  206. // request termination
  207. BProcess_Terminate(&o->process);
  208. // set state running die
  209. o->state = STATE_RUNNING_DIE;
  210. }
  211. static struct NCDModule modules[] = {
  212. {
  213. .type = "daemon",
  214. .func_new2 = func_new,
  215. .func_die = func_die,
  216. .alloc_size = sizeof(struct instance)
  217. }, {
  218. .type = NULL
  219. }
  220. };
  221. const struct NCDModuleGroup ncdmodule_daemon = {
  222. .modules = modules
  223. };