daemon.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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. NCDValue *cmd_arg;
  58. BTimer timer;
  59. BProcess process;
  60. int state;
  61. };
  62. static int build_cmdline (NCDModuleInst *i, NCDValue *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, NCDValue *cmd_arg, char **exec, CmdLine *cl)
  68. {
  69. if (NCDValue_Type(cmd_arg) != NCDVALUE_LIST) {
  70. ModuleLog(i, BLOG_ERROR, "wrong type");
  71. goto fail0;
  72. }
  73. // read exec
  74. NCDValue *exec_arg = NCDValue_ListFirst(cmd_arg);
  75. if (!exec_arg) {
  76. ModuleLog(i, BLOG_ERROR, "missing executable name");
  77. goto fail0;
  78. }
  79. if (!NCDValue_IsStringNoNulls(exec_arg)) {
  80. ModuleLog(i, BLOG_ERROR, "wrong type");
  81. goto fail0;
  82. }
  83. if (!(*exec = strdup(NCDValue_StringValue(exec_arg)))) {
  84. ModuleLog(i, BLOG_ERROR, "strdup failed");
  85. goto fail0;
  86. }
  87. // start cmdline
  88. if (!CmdLine_Init(cl)) {
  89. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  90. goto fail1;
  91. }
  92. // add header
  93. if (!CmdLine_Append(cl, *exec)) {
  94. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  95. goto fail2;
  96. }
  97. // add additional arguments
  98. NCDValue *arg = exec_arg;
  99. while (arg = NCDValue_ListNext(cmd_arg, arg)) {
  100. if (!NCDValue_IsStringNoNulls(arg)) {
  101. ModuleLog(i, BLOG_ERROR, "wrong type");
  102. goto fail2;
  103. }
  104. if (!CmdLine_Append(cl, NCDValue_StringValue(arg))) {
  105. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  106. goto fail2;
  107. }
  108. }
  109. // finish
  110. if (!CmdLine_Finish(cl)) {
  111. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  112. goto fail2;
  113. }
  114. return 1;
  115. fail2:
  116. CmdLine_Free(cl);
  117. fail1:
  118. free(*exec);
  119. fail0:
  120. return 0;
  121. }
  122. static void start_process (struct instance *o)
  123. {
  124. // build cmdline
  125. char *exec;
  126. CmdLine cl;
  127. if (!build_cmdline(o->i, o->cmd_arg, &exec, &cl)) {
  128. goto fail;
  129. }
  130. // start process
  131. int res = BProcess_Init(&o->process, o->i->params->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), NULL);
  132. CmdLine_Free(&cl);
  133. free(exec);
  134. if (!res) {
  135. ModuleLog(o->i, BLOG_ERROR, "BProcess_Init failed");
  136. goto fail;
  137. }
  138. // set state running
  139. o->state = STATE_RUNNING;
  140. return;
  141. fail:
  142. // start timer
  143. BReactor_SetTimer(o->i->params->reactor, &o->timer);
  144. // set state retrying
  145. o->state = STATE_RETRYING;
  146. }
  147. static void timer_handler (struct instance *o)
  148. {
  149. ASSERT(o->state == STATE_RETRYING)
  150. ModuleLog(o->i, BLOG_INFO, "restarting after crash");
  151. start_process(o);
  152. }
  153. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  154. {
  155. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  156. // free process
  157. BProcess_Free(&o->process);
  158. // if we were requested to die, die now
  159. if (o->state == STATE_RUNNING_DIE) {
  160. instance_free(o);
  161. return;
  162. }
  163. BLog(BLOG_ERROR, "daemon crashed");
  164. // start timer
  165. BReactor_SetTimer(o->i->params->reactor, &o->timer);
  166. // set state retrying
  167. o->state = STATE_RETRYING;
  168. }
  169. static void func_new (NCDModuleInst *i)
  170. {
  171. // allocate instance
  172. struct instance *o = malloc(sizeof(*o));
  173. if (!o) {
  174. ModuleLog(i, BLOG_ERROR, "failed to allocate instance");
  175. goto fail0;
  176. }
  177. NCDModuleInst_Backend_SetUser(i, o);
  178. // init arguments
  179. o->i = i;
  180. // read arguments
  181. if (!NCDValue_ListRead(i->args, 1, &o->cmd_arg)) {
  182. ModuleLog(i, BLOG_ERROR, "wrong arity");
  183. goto fail1;
  184. }
  185. // init timer
  186. BTimer_Init(&o->timer, RETRY_TIME, (BTimer_handler)timer_handler, o);
  187. // signal up
  188. NCDModuleInst_Backend_Up(i);
  189. // try starting process
  190. start_process(o);
  191. return;
  192. fail1:
  193. free(o);
  194. fail0:
  195. NCDModuleInst_Backend_SetError(i);
  196. NCDModuleInst_Backend_Dead(i);
  197. }
  198. static void instance_free (struct instance *o)
  199. {
  200. NCDModuleInst *i = o->i;
  201. // free timer
  202. BReactor_RemoveTimer(o->i->params->reactor, &o->timer);
  203. // free instance
  204. free(o);
  205. NCDModuleInst_Backend_Dead(i);
  206. }
  207. static void func_die (void *vo)
  208. {
  209. struct instance *o = vo;
  210. ASSERT(o->state != STATE_RUNNING_DIE)
  211. // if not running, die immediately
  212. if (o->state == STATE_RETRYING) {
  213. instance_free(o);
  214. return;
  215. }
  216. // request termination
  217. BProcess_Terminate(&o->process);
  218. // set state running die
  219. o->state = STATE_RUNNING_DIE;
  220. }
  221. static const struct NCDModule modules[] = {
  222. {
  223. .type = "daemon",
  224. .func_new = func_new,
  225. .func_die = func_die
  226. }, {
  227. .type = NULL
  228. }
  229. };
  230. const struct NCDModuleGroup ncdmodule_daemon = {
  231. .modules = modules
  232. };