kexec.c 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  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. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  77. {
  78. assert(exec);
  79. assert(context);
  80. if (!exec)
  81. return BOOL_FALSE;
  82. if (!context)
  83. return BOOL_FALSE;
  84. if (!faux_list_add(exec->contexts, context))
  85. return BOOL_FALSE;
  86. return BOOL_TRUE;
  87. }
  88. static bool_t kexec_prepare(kexec_t *exec)
  89. {
  90. int pipefd[2] = {};
  91. faux_list_node_t *iter = NULL;
  92. int global_stderr = -1;
  93. assert(exec);
  94. if (!exec)
  95. return BOOL_FALSE;
  96. // Nothing to prepare for empty list
  97. if (kexec_contexts_is_empty(exec))
  98. return BOOL_FALSE;
  99. // Create "global" stdin, stdout, stderr for the whole job execution.
  100. // Now function creates only the simple pipes but somedays it will be
  101. // able to create pseudo-terminal for interactive sessions.
  102. // STDIN
  103. if (pipe(pipefd) < 0)
  104. return BOOL_FALSE;
  105. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  106. pipefd[0]); // Read end
  107. kexec_set_stdin(exec, pipefd[1]); // Write end
  108. // STDOUT
  109. if (pipe(pipefd) < 0)
  110. return BOOL_FALSE;
  111. kexec_set_stdout(exec, pipefd[0]); // Read end
  112. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  113. pipefd[1]); // Write end
  114. // STDERR
  115. if (pipe(pipefd) < 0)
  116. return BOOL_FALSE;
  117. kexec_set_stderr(exec, pipefd[0]); // Read end
  118. // STDERR write end will be set to all list members as stderr
  119. global_stderr = pipefd[1]; // Write end
  120. // Iterate all context_t elements to fill all stdin, stdout, stderr
  121. for (iter = faux_list_head(exec->contexts); iter;
  122. iter = faux_list_next_node(iter)) {
  123. faux_list_node_t *next = faux_list_next_node(iter);
  124. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  125. // Set the same STDERR to all contexts
  126. kcontext_set_stderr(context, global_stderr);
  127. // Create pipes beetween processes
  128. if (next) {
  129. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  130. if (pipe(pipefd) < 0)
  131. return BOOL_FALSE;
  132. kcontext_set_stdout(context, pipefd[1]); // Write end
  133. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  134. }
  135. }
  136. return BOOL_TRUE;
  137. }
  138. static int exec_action(kcontext_t *context, const kaction_t *action, pid_t *pid)
  139. {
  140. context = context;
  141. action = action;
  142. ksym_fn fn;
  143. int exitcode = 0;
  144. if (pid)
  145. *pid = -1;
  146. // printf("DDD: exec_action [%s]\n", kaction_script(action));
  147. fn = ksym_function(kaction_sym(action));
  148. exitcode = fn(context);
  149. return exitcode;
  150. }
  151. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  152. pid_t pid, int wstatus)
  153. {
  154. faux_list_node_t *iter = NULL;
  155. int exitstatus = WEXITSTATUS(wstatus);
  156. int *pexitstatus = &exitstatus;
  157. pid_t new_pid = -1; // PID of newly forked ACTION process
  158. assert(context);
  159. if (!context)
  160. return BOOL_FALSE;
  161. // There is two reasons to don't start any real actions.
  162. // - The ACTION sequence is already done;
  163. // - Passed PID (PID of completed process) is not owned by this context.
  164. // Returns false that indicates this PID is not mine.
  165. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  166. return BOOL_FALSE;
  167. // Here we know that given PID is our PID
  168. iter = kcontext_action_iter(context); // Get saved current ACTION
  169. do {
  170. faux_list_t *actions = NULL;
  171. const kaction_t *action = NULL;
  172. // Compute new value for retcode.
  173. // Here iter is a pointer to previous action but not new.
  174. // If iter == NULL then it will be a first ACTION from the sequence.
  175. if (iter && pexitstatus) {
  176. const kaction_t *terminated_action = NULL;
  177. terminated_action = faux_list_data(iter);
  178. assert(terminated_action);
  179. if (kaction_update_retcode(terminated_action))
  180. kcontext_set_retcode(context, *pexitstatus);
  181. }
  182. // Get next ACTION from sequence
  183. if (!iter) { // Is it the first ACTION within list
  184. actions = kentry_actions(kpargv_command(kcontext_pargv(context)));
  185. assert(actions);
  186. iter = faux_list_head(actions);
  187. } else {
  188. iter = faux_list_next_node(iter);
  189. }
  190. kcontext_set_action_iter(context, iter);
  191. // Was it end of ACTION sequence?
  192. if (!iter) {
  193. kcontext_set_done(context, BOOL_TRUE);
  194. break;
  195. }
  196. // Not all ACTIONs has an exit status. Some can have condition to
  197. // skip real execution. So they has no exit status.
  198. pexitstatus = NULL;
  199. // Get new ACTION to execute
  200. action = (const kaction_t *)faux_list_data(iter);
  201. assert(action);
  202. // Check for previous retcode to find out if next command must
  203. // be executed.
  204. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  205. continue; // Skip execution
  206. // Here we know that process will be executed. Dry-run mode is a
  207. // pseudo-execution too i.e. ACTION has exit status.
  208. pexitstatus = &exitstatus;
  209. // Check for dry-run flag and 'permanent' feature of ACTION.
  210. if (kexec_dry_run(exec) && !kaction_permanent(action)) {
  211. exitstatus = 0; // Exit status while dry-run is always 0
  212. continue;
  213. }
  214. exitstatus = exec_action(context, action, &new_pid);
  215. } while (-1 == new_pid); // PID is not -1 when new process was forked
  216. // Save PID of newly created process
  217. if (new_pid != -1) // It means that process was fork()ed
  218. kcontext_set_pid(context, new_pid);
  219. return BOOL_TRUE;
  220. }
  221. static bool_t continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  222. {
  223. faux_list_node_t *iter = NULL;
  224. kcontext_t *context = NULL;
  225. assert(exec);
  226. if (!exec)
  227. return BOOL_FALSE;
  228. iter = kexec_contexts_iter(exec);
  229. while ((context = kexec_contexts_each(&iter))) {
  230. bool_t found = BOOL_FALSE;
  231. found = exec_action_sequence(exec, context, pid, wstatus);
  232. if (found && (pid != -1))
  233. break;
  234. }
  235. return BOOL_TRUE;
  236. }
  237. bool_t kexec_exec(kexec_t *exec)
  238. {
  239. assert(exec);
  240. if (!exec)
  241. return BOOL_FALSE;
  242. // Firsly prepare kexec object for execution. The file streams must
  243. // be created for stdin, stdout, stderr of processes.
  244. if (!kexec_prepare(exec))
  245. return BOOL_FALSE;
  246. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  247. // ACTION's process.
  248. continue_command_execution(exec, -1, 0);
  249. return BOOL_TRUE;
  250. }