daemon.c 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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 <misc/strdup.h>
  48. #include <system/BProcess.h>
  49. #include <ncd/NCDModule.h>
  50. #include <ncd/extra/value_utils.h>
  51. #include <generated/blog_channel_ncd_daemon.h>
  52. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  53. #define RETRY_TIME 10000
  54. #define STATE_RETRYING 1
  55. #define STATE_RUNNING 2
  56. #define STATE_RUNNING_DIE 3
  57. struct instance {
  58. NCDModuleInst *i;
  59. NCDValRef cmd_arg;
  60. BTimer timer;
  61. BProcess process;
  62. int state;
  63. };
  64. static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl);
  65. static void start_process (struct instance *o);
  66. static void timer_handler (struct instance *o);
  67. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status);
  68. static void instance_free (struct instance *o);
  69. static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl)
  70. {
  71. ASSERT(!NCDVal_IsInvalid(cmd_arg))
  72. if (!NCDVal_IsList(cmd_arg)) {
  73. ModuleLog(i, BLOG_ERROR, "wrong type");
  74. goto fail0;
  75. }
  76. size_t count = NCDVal_ListCount(cmd_arg);
  77. // read exec
  78. if (count == 0) {
  79. ModuleLog(i, BLOG_ERROR, "missing executable name");
  80. goto fail0;
  81. }
  82. NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
  83. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  84. ModuleLog(i, BLOG_ERROR, "wrong type");
  85. goto fail0;
  86. }
  87. if (!(*exec = ncd_strdup(exec_arg))) {
  88. ModuleLog(i, BLOG_ERROR, "ncd_strdup failed");
  89. goto fail0;
  90. }
  91. // start cmdline
  92. if (!CmdLine_Init(cl)) {
  93. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  94. goto fail1;
  95. }
  96. // add header
  97. if (!CmdLine_Append(cl, *exec)) {
  98. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  99. goto fail2;
  100. }
  101. // add additional arguments
  102. for (size_t j = 1; j < count; j++) {
  103. NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
  104. if (!NCDVal_IsStringNoNulls(arg)) {
  105. ModuleLog(i, BLOG_ERROR, "wrong type");
  106. goto fail2;
  107. }
  108. b_cstring cstr = NCDVal_StringCstring(arg);
  109. if (!CmdLine_AppendCstring(cl, cstr, 0, cstr.length)) {
  110. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendCstring failed");
  111. goto fail2;
  112. }
  113. }
  114. // finish
  115. if (!CmdLine_Finish(cl)) {
  116. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  117. goto fail2;
  118. }
  119. return 1;
  120. fail2:
  121. CmdLine_Free(cl);
  122. fail1:
  123. free(*exec);
  124. fail0:
  125. return 0;
  126. }
  127. static void start_process (struct instance *o)
  128. {
  129. // build cmdline
  130. char *exec;
  131. CmdLine cl;
  132. if (!build_cmdline(o->i, o->cmd_arg, &exec, &cl)) {
  133. goto fail;
  134. }
  135. // start process
  136. int res = BProcess_Init(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL);
  137. CmdLine_Free(&cl);
  138. free(exec);
  139. if (!res) {
  140. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  141. goto fail;
  142. }
  143. // set state running
  144. o->state = STATE_RUNNING;
  145. return;
  146. fail:
  147. // start timer
  148. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  149. // set state retrying
  150. o->state = STATE_RETRYING;
  151. }
  152. static void timer_handler (struct instance *o)
  153. {
  154. ASSERT(o->state == STATE_RETRYING)
  155. ModuleLog(o->i, BLOG_INFO, "restarting after crash");
  156. start_process(o);
  157. }
  158. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  159. {
  160. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  161. // free process
  162. BProcess_Free(&o->process);
  163. // if we were requested to die, die now
  164. if (o->state == STATE_RUNNING_DIE) {
  165. instance_free(o);
  166. return;
  167. }
  168. BLog(BLOG_ERROR, "daemon crashed");
  169. // start timer
  170. BReactor_SetTimer(o->i->params->iparams->reactor, &o->timer);
  171. // set state retrying
  172. o->state = STATE_RETRYING;
  173. }
  174. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  175. {
  176. struct instance *o = vo;
  177. o->i = i;
  178. // read arguments
  179. if (!NCDVal_ListRead(params->args, 1, &o->cmd_arg)) {
  180. ModuleLog(i, BLOG_ERROR, "wrong arity");
  181. goto fail0;
  182. }
  183. // init timer
  184. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  185. // signal up
  186. NCDModuleInst_Backend_Up(i);
  187. // try starting process
  188. start_process(o);
  189. return;
  190. fail0:
  191. NCDModuleInst_Backend_DeadError(i);
  192. }
  193. static void instance_free (struct instance *o)
  194. {
  195. // free timer
  196. BReactor_RemoveTimer(o->i->params->iparams->reactor, &o->timer);
  197. NCDModuleInst_Backend_Dead(o->i);
  198. }
  199. static void func_die (void *vo)
  200. {
  201. struct instance *o = vo;
  202. ASSERT(o->state != STATE_RUNNING_DIE)
  203. // if not running, die immediately
  204. if (o->state == STATE_RETRYING) {
  205. instance_free(o);
  206. return;
  207. }
  208. // request termination
  209. BProcess_Terminate(&o->process);
  210. // set state running die
  211. o->state = STATE_RUNNING_DIE;
  212. }
  213. static struct NCDModule modules[] = {
  214. {
  215. .type = "daemon",
  216. .func_new2 = func_new,
  217. .func_die = func_die,
  218. .alloc_size = sizeof(struct instance),
  219. .flags = NCDMODULE_FLAG_ACCEPT_NON_CONTINUOUS_STRINGS
  220. }, {
  221. .type = NULL
  222. }
  223. };
  224. const struct NCDModuleGroup ncdmodule_daemon = {
  225. .modules = modules
  226. };