runonce.c 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324
  1. /**
  2. * @file runonce.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. * Imperative program execution module. On initialization, starts the process.
  32. * Goes to UP state when the process terminates. When requested to die, waits for
  33. * the process to terminate if it's running, optionally sending SIGTERM.
  34. *
  35. * Synopsis: runonce(list(string) cmd, [list opts])
  36. * Arguments:
  37. * cmd - Command to run on startup. The first element is the full path
  38. * to the executable, other elements are command line arguments (excluding
  39. * the zeroth argument).
  40. * opts - List of options:
  41. * "term_on_deinit" - If we get a deinit request while the process is running,
  42. * send it SIGTERM.
  43. * "keep_stdout" - Start the program with the same stdout as the NCD process.
  44. * "keep_stderr" - Start the program with the same stderr as the NCD process.
  45. * "do_setsid" - Call setsid() in the child before exec. This is needed to
  46. * start the 'agetty' program.
  47. * Variables:
  48. * string exit_status - if the program exited normally, the non-negative exit code, otherwise -1
  49. */
  50. #include <stdlib.h>
  51. #include <stdio.h>
  52. #include <string.h>
  53. #include <misc/cmdline.h>
  54. #include <misc/strdup.h>
  55. #include <system/BProcess.h>
  56. #include <ncd/NCDModule.h>
  57. #include <ncd/extra/value_utils.h>
  58. #include <generated/blog_channel_ncd_runonce.h>
  59. #define ModuleLog(i, ...) NCDModuleInst_Backend_Log((i), BLOG_CURRENT_CHANNEL, __VA_ARGS__)
  60. #define STATE_RUNNING 1
  61. #define STATE_RUNNING_DIE 2
  62. #define STATE_FINISHED 3
  63. struct instance {
  64. NCDModuleInst *i;
  65. int term_on_deinit;
  66. int state;
  67. BProcess process;
  68. int exit_status;
  69. };
  70. static void instance_free (struct instance *o);
  71. static int build_cmdline (NCDModuleInst *i, NCDValRef cmd_arg, char **exec, CmdLine *cl)
  72. {
  73. ASSERT(!NCDVal_IsInvalid(cmd_arg))
  74. if (!NCDVal_IsList(cmd_arg)) {
  75. ModuleLog(i, BLOG_ERROR, "wrong type");
  76. goto fail0;
  77. }
  78. size_t count = NCDVal_ListCount(cmd_arg);
  79. // read exec
  80. if (count == 0) {
  81. ModuleLog(i, BLOG_ERROR, "missing executable name");
  82. goto fail0;
  83. }
  84. NCDValRef exec_arg = NCDVal_ListGet(cmd_arg, 0);
  85. if (!NCDVal_IsStringNoNulls(exec_arg)) {
  86. ModuleLog(i, BLOG_ERROR, "wrong type");
  87. goto fail0;
  88. }
  89. if (!(*exec = ncd_strdup(exec_arg))) {
  90. ModuleLog(i, BLOG_ERROR, "strdup failed");
  91. goto fail0;
  92. }
  93. // start cmdline
  94. if (!CmdLine_Init(cl)) {
  95. ModuleLog(i, BLOG_ERROR, "CmdLine_Init failed");
  96. goto fail1;
  97. }
  98. // add header
  99. if (!CmdLine_Append(cl, *exec)) {
  100. ModuleLog(i, BLOG_ERROR, "CmdLine_Append failed");
  101. goto fail2;
  102. }
  103. // add additional arguments
  104. for (size_t j = 1; j < count; j++) {
  105. NCDValRef arg = NCDVal_ListGet(cmd_arg, j);
  106. if (!NCDVal_IsStringNoNulls(arg)) {
  107. ModuleLog(i, BLOG_ERROR, "wrong type");
  108. goto fail2;
  109. }
  110. if (!CmdLine_AppendNoNull(cl, NCDVal_StringData(arg), NCDVal_StringLength(arg))) {
  111. ModuleLog(i, BLOG_ERROR, "CmdLine_AppendNoNull failed");
  112. goto fail2;
  113. }
  114. }
  115. // finish
  116. if (!CmdLine_Finish(cl)) {
  117. ModuleLog(i, BLOG_ERROR, "CmdLine_Finish failed");
  118. goto fail2;
  119. }
  120. return 1;
  121. fail2:
  122. CmdLine_Free(cl);
  123. fail1:
  124. free(*exec);
  125. fail0:
  126. return 0;
  127. }
  128. static void process_handler (struct instance *o, int normally, uint8_t normally_exit_status)
  129. {
  130. ASSERT(o->state == STATE_RUNNING || o->state == STATE_RUNNING_DIE)
  131. // free process
  132. BProcess_Free(&o->process);
  133. // if we were requested to die, die now
  134. if (o->state == STATE_RUNNING_DIE) {
  135. instance_free(o);
  136. return;
  137. }
  138. // remember exit status
  139. o->exit_status = (normally ? normally_exit_status : -1);
  140. // set state
  141. o->state = STATE_FINISHED;
  142. // set up
  143. NCDModuleInst_Backend_Up(o->i);
  144. }
  145. static void func_new (void *vo, NCDModuleInst *i, const struct NCDModuleInst_new_params *params)
  146. {
  147. struct instance *o = vo;
  148. o->i = i;
  149. // init arguments
  150. o->term_on_deinit = 0;
  151. // read arguments
  152. NCDValRef cmd_arg;
  153. NCDValRef opts_arg = NCDVal_NewInvalid();
  154. if (!NCDVal_ListRead(params->args, 1, &cmd_arg) && !NCDVal_ListRead(params->args, 2, &cmd_arg, &opts_arg)) {
  155. ModuleLog(i, BLOG_ERROR, "wrong arity");
  156. goto fail0;
  157. }
  158. if (!NCDVal_IsInvalid(opts_arg) && !NCDVal_IsList(opts_arg)) {
  159. ModuleLog(i, BLOG_ERROR, "wrong type");
  160. goto fail0;
  161. }
  162. int keep_stdout = 0;
  163. int keep_stderr = 0;
  164. int do_setsid = 0;
  165. // read options
  166. size_t count = NCDVal_IsInvalid(opts_arg) ? 0 : NCDVal_ListCount(opts_arg);
  167. for (size_t j = 0; j < count; j++) {
  168. NCDValRef opt = NCDVal_ListGet(opts_arg, j);
  169. // read name
  170. if (!NCDVal_IsString(opt)) {
  171. ModuleLog(o->i, BLOG_ERROR, "wrong option name type");
  172. goto fail0;
  173. }
  174. if (NCDVal_StringEquals(opt, "term_on_deinit")) {
  175. o->term_on_deinit = 1;
  176. }
  177. else if (NCDVal_StringEquals(opt, "keep_stdout")) {
  178. keep_stdout = 1;
  179. }
  180. else if (NCDVal_StringEquals(opt, "keep_stderr")) {
  181. keep_stderr = 1;
  182. }
  183. else if (NCDVal_StringEquals(opt, "do_setsid")) {
  184. do_setsid = 1;
  185. }
  186. else {
  187. ModuleLog(o->i, BLOG_ERROR, "unknown option name");
  188. goto fail0;
  189. }
  190. }
  191. // build cmdline
  192. char *exec;
  193. CmdLine cl;
  194. if (!build_cmdline(o->i, cmd_arg, &exec, &cl)) {
  195. goto fail0;
  196. }
  197. // build fd mapping
  198. int fds[3];
  199. int fds_map[2];
  200. int nfds = 0;
  201. if (keep_stdout) {
  202. fds[nfds] = 1;
  203. fds_map[nfds++] = 1;
  204. }
  205. if (keep_stderr) {
  206. fds[nfds] = 2;
  207. fds_map[nfds++] = 2;
  208. }
  209. fds[nfds] = -1;
  210. // build params
  211. struct BProcess_params p_params;
  212. p_params.username = NULL;
  213. p_params.fds = fds;
  214. p_params.fds_map = fds_map;
  215. p_params.do_setsid = do_setsid;
  216. // start process
  217. if (!BProcess_Init2(&o->process, o->i->params->iparams->manager, (BProcess_handler)process_handler, o, exec, CmdLine_Get(&cl), p_params)) {
  218. ModuleLog(i, BLOG_ERROR, "BProcess_Init failed");
  219. CmdLine_Free(&cl);
  220. free(exec);
  221. goto fail0;
  222. }
  223. CmdLine_Free(&cl);
  224. free(exec);
  225. // set state
  226. o->state = STATE_RUNNING;
  227. return;
  228. fail0:
  229. NCDModuleInst_Backend_DeadError(i);
  230. }
  231. static void instance_free (struct instance *o)
  232. {
  233. NCDModuleInst_Backend_Dead(o->i);
  234. }
  235. static void func_die (void *vo)
  236. {
  237. struct instance *o = vo;
  238. ASSERT(o->state != STATE_RUNNING_DIE)
  239. if (o->state == STATE_FINISHED) {
  240. instance_free(o);
  241. return;
  242. }
  243. // send SIGTERM if requested
  244. if (o->term_on_deinit) {
  245. BProcess_Terminate(&o->process);
  246. }
  247. o->state = STATE_RUNNING_DIE;
  248. }
  249. static int func_getvar (void *vo, const char *name, NCDValMem *mem, NCDValRef *out)
  250. {
  251. struct instance *o = vo;
  252. ASSERT(o->state == STATE_FINISHED)
  253. if (!strcmp(name, "exit_status")) {
  254. char str[30];
  255. snprintf(str, sizeof(str), "%d", o->exit_status);
  256. *out = NCDVal_NewString(mem, str);
  257. return 1;
  258. }
  259. return 0;
  260. }
  261. static struct NCDModule modules[] = {
  262. {
  263. .type = "runonce",
  264. .func_new2 = func_new,
  265. .func_die = func_die,
  266. .func_getvar = func_getvar,
  267. .alloc_size = sizeof(struct instance)
  268. }, {
  269. .type = NULL
  270. }
  271. };
  272. const struct NCDModuleGroup ncdmodule_runonce = {
  273. .modules = modules
  274. };