kexec.c 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366
  1. /** @file kexec.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <unistd.h>
  8. #include <faux/list.h>
  9. #include <klish/khelper.h>
  10. #include <klish/kcontext.h>
  11. #include <klish/kexec.h>
  12. struct kexec_s {
  13. faux_list_t *contexts;
  14. bool_t dry_run;
  15. int stdin;
  16. int stdout;
  17. int stderr;
  18. };
  19. // Dry-run
  20. KGET_BOOL(exec, dry_run);
  21. KSET_BOOL(exec, dry_run);
  22. // STDIN
  23. KGET(exec, int, stdin);
  24. KSET(exec, int, stdin);
  25. // STDOUT
  26. KGET(exec, int, stdout);
  27. KSET(exec, int, stdout);
  28. // STDERR
  29. KGET(exec, int, stderr);
  30. KSET(exec, int, stderr);
  31. // CONTEXT list
  32. KADD_NESTED(exec, kcontext_t *, contexts);
  33. KNESTED_LEN(exec, contexts);
  34. KNESTED_IS_EMPTY(exec, contexts);
  35. KNESTED_ITER(exec, contexts);
  36. KNESTED_EACH(exec, kcontext_t *, contexts);
  37. kexec_t *kexec_new()
  38. {
  39. kexec_t *exec = NULL;
  40. exec = faux_zmalloc(sizeof(*exec));
  41. assert(exec);
  42. if (!exec)
  43. return NULL;
  44. exec->dry_run = BOOL_FALSE;
  45. // List of execute contexts
  46. exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  47. NULL, NULL, (void (*)(void *))kcontext_free);
  48. assert(exec->contexts);
  49. // I/O
  50. exec->stdin = -1;
  51. exec->stdout = -1;
  52. exec->stderr = -1;
  53. return exec;
  54. }
  55. void kexec_free(kexec_t *exec)
  56. {
  57. if (!exec)
  58. return;
  59. faux_list_free(exec->contexts);
  60. free(exec);
  61. }
  62. size_t kexec_len(const kexec_t *exec)
  63. {
  64. assert(exec);
  65. if (!exec)
  66. return 0;
  67. return faux_list_len(exec->contexts);
  68. }
  69. size_t kexec_is_empty(const kexec_t *exec)
  70. {
  71. assert(exec);
  72. if (!exec)
  73. return 0;
  74. return faux_list_is_empty(exec->contexts);
  75. }
  76. // kexec is done when all the kexec's contexts are done
  77. bool_t kexec_done(const kexec_t *exec)
  78. {
  79. faux_list_node_t *iter = NULL;
  80. kcontext_t *context = NULL;
  81. assert(exec);
  82. if (!exec)
  83. return BOOL_FALSE;
  84. iter = kexec_contexts_iter(exec);
  85. while ((context = kexec_contexts_each(&iter))) {
  86. if (!kcontext_done(context))
  87. return BOOL_FALSE;
  88. }
  89. return BOOL_TRUE;
  90. }
  91. // Retcode of kexec is a retcode of its first context execution because
  92. // next contexts just a filters. Retcode valid if kexec is done. Else current
  93. // retcode is non-valid and will not be returned at all.
  94. bool_t kexec_retcode(const kexec_t *exec, int *status)
  95. {
  96. assert(exec);
  97. if (!exec)
  98. return BOOL_FALSE;
  99. if (kexec_is_empty(exec))
  100. return BOOL_FALSE;
  101. if (!kexec_done(exec)) // Unfinished execution
  102. return BOOL_FALSE;
  103. if (status)
  104. *status = kcontext_retcode(
  105. (kcontext_t *)faux_list_data(faux_list_head(exec->contexts)));
  106. return BOOL_TRUE;
  107. }
  108. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  109. {
  110. assert(exec);
  111. assert(context);
  112. if (!exec)
  113. return BOOL_FALSE;
  114. if (!context)
  115. return BOOL_FALSE;
  116. if (!faux_list_add(exec->contexts, context))
  117. return BOOL_FALSE;
  118. return BOOL_TRUE;
  119. }
  120. static bool_t kexec_prepare(kexec_t *exec)
  121. {
  122. int pipefd[2] = {};
  123. faux_list_node_t *iter = NULL;
  124. int global_stderr = -1;
  125. assert(exec);
  126. if (!exec)
  127. return BOOL_FALSE;
  128. // Nothing to prepare for empty list
  129. if (kexec_contexts_is_empty(exec))
  130. return BOOL_FALSE;
  131. // Create "global" stdin, stdout, stderr for the whole job execution.
  132. // Now function creates only the simple pipes but somedays it will be
  133. // able to create pseudo-terminal for interactive sessions.
  134. // STDIN
  135. if (pipe(pipefd) < 0)
  136. return BOOL_FALSE;
  137. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  138. pipefd[0]); // Read end
  139. kexec_set_stdin(exec, pipefd[1]); // Write end
  140. // STDOUT
  141. if (pipe(pipefd) < 0)
  142. return BOOL_FALSE;
  143. kexec_set_stdout(exec, pipefd[0]); // Read end
  144. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  145. pipefd[1]); // Write end
  146. // STDERR
  147. if (pipe(pipefd) < 0)
  148. return BOOL_FALSE;
  149. kexec_set_stderr(exec, pipefd[0]); // Read end
  150. // STDERR write end will be set to all list members as stderr
  151. global_stderr = pipefd[1]; // Write end
  152. // Iterate all context_t elements to fill all stdin, stdout, stderr
  153. for (iter = faux_list_head(exec->contexts); iter;
  154. iter = faux_list_next_node(iter)) {
  155. faux_list_node_t *next = faux_list_next_node(iter);
  156. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  157. // Set the same STDERR to all contexts
  158. kcontext_set_stderr(context, global_stderr);
  159. // Create pipes beetween processes
  160. if (next) {
  161. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  162. if (pipe(pipefd) < 0)
  163. return BOOL_FALSE;
  164. kcontext_set_stdout(context, pipefd[1]); // Write end
  165. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  166. }
  167. }
  168. return BOOL_TRUE;
  169. }
  170. static int exec_action(kcontext_t *context, const kaction_t *action, pid_t *pid)
  171. {
  172. context = context;
  173. action = action;
  174. ksym_fn fn;
  175. int exitcode = 0;
  176. if (pid)
  177. *pid = -1;
  178. // printf("DDD: exec_action [%s]\n", kaction_script(action));
  179. fn = ksym_function(kaction_sym(action));
  180. exitcode = fn(context);
  181. return exitcode;
  182. }
  183. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  184. pid_t pid, int wstatus)
  185. {
  186. faux_list_node_t *iter = NULL;
  187. int exitstatus = WEXITSTATUS(wstatus);
  188. int *pexitstatus = &exitstatus;
  189. pid_t new_pid = -1; // PID of newly forked ACTION process
  190. assert(context);
  191. if (!context)
  192. return BOOL_FALSE;
  193. // There is two reasons to don't start any real actions.
  194. // - The ACTION sequence is already done;
  195. // - Passed PID (PID of completed process) is not owned by this context.
  196. // Returns false that indicates this PID is not mine.
  197. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  198. return BOOL_FALSE;
  199. // Here we know that given PID is our PID
  200. iter = kcontext_action_iter(context); // Get saved current ACTION
  201. do {
  202. faux_list_t *actions = NULL;
  203. const kaction_t *action = NULL;
  204. // Compute new value for retcode.
  205. // Here iter is a pointer to previous action but not new.
  206. // If iter == NULL then it will be a first ACTION from the sequence.
  207. if (iter && pexitstatus) {
  208. const kaction_t *terminated_action = NULL;
  209. terminated_action = faux_list_data(iter);
  210. assert(terminated_action);
  211. if (kaction_update_retcode(terminated_action))
  212. kcontext_set_retcode(context, *pexitstatus);
  213. }
  214. // Get next ACTION from sequence
  215. if (!iter) { // Is it the first ACTION within list
  216. actions = kentry_actions(kpargv_command(kcontext_pargv(context)));
  217. assert(actions);
  218. iter = faux_list_head(actions);
  219. } else {
  220. iter = faux_list_next_node(iter);
  221. }
  222. kcontext_set_action_iter(context, iter);
  223. // Was it end of ACTION sequence?
  224. if (!iter) {
  225. kcontext_set_done(context, BOOL_TRUE);
  226. break;
  227. }
  228. // Not all ACTIONs has an exit status. Some can have condition to
  229. // skip real execution. So they has no exit status.
  230. pexitstatus = NULL;
  231. // Get new ACTION to execute
  232. action = (const kaction_t *)faux_list_data(iter);
  233. assert(action);
  234. // Check for previous retcode to find out if next command must
  235. // be executed.
  236. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  237. continue; // Skip execution
  238. // Here we know that process will be executed. Dry-run mode is a
  239. // pseudo-execution too i.e. ACTION has exit status.
  240. pexitstatus = &exitstatus;
  241. // Check for dry-run flag and 'permanent' feature of ACTION.
  242. if (kexec_dry_run(exec) && !kaction_permanent(action)) {
  243. exitstatus = 0; // Exit status while dry-run is always 0
  244. continue;
  245. }
  246. exitstatus = exec_action(context, action, &new_pid);
  247. } while (-1 == new_pid); // PID is not -1 when new process was forked
  248. // Save PID of newly created process
  249. if (new_pid != -1) // It means that process was fork()ed
  250. kcontext_set_pid(context, new_pid);
  251. return BOOL_TRUE;
  252. }
  253. bool_t kexec_continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  254. {
  255. faux_list_node_t *iter = NULL;
  256. kcontext_t *context = NULL;
  257. assert(exec);
  258. if (!exec)
  259. return BOOL_FALSE;
  260. iter = kexec_contexts_iter(exec);
  261. while ((context = kexec_contexts_each(&iter))) {
  262. bool_t found = BOOL_FALSE;
  263. found = exec_action_sequence(exec, context, pid, wstatus);
  264. if (found && (pid != -1))
  265. break;
  266. }
  267. return BOOL_TRUE;
  268. }
  269. bool_t kexec_exec(kexec_t *exec)
  270. {
  271. assert(exec);
  272. if (!exec)
  273. return BOOL_FALSE;
  274. // Firsly prepare kexec object for execution. The file streams must
  275. // be created for stdin, stdout, stderr of processes.
  276. if (!kexec_prepare(exec))
  277. return BOOL_FALSE;
  278. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  279. // ACTION's process.
  280. kexec_continue_command_execution(exec, -1, 0);
  281. return BOOL_TRUE;
  282. }