kexec.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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. if (pid)
  143. *pid = -1;
  144. printf("DDD: exec_action [%s]\n", kaction_script(action));
  145. return 0;
  146. }
  147. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  148. pid_t pid, int wstatus)
  149. {
  150. faux_list_node_t *iter = NULL;
  151. int exitstatus = WEXITSTATUS(wstatus);
  152. int *pexitstatus = &exitstatus;
  153. pid_t new_pid = -1; // PID of newly forked ACTION process
  154. assert(context);
  155. if (!context)
  156. return BOOL_FALSE;
  157. // There is two reasons to don't start any real actions.
  158. // - The ACTION sequence is already done;
  159. // - Passed PID (PID of completed process) is not owned by this context.
  160. // Returns false that indicates this PID is not mine.
  161. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  162. return BOOL_FALSE;
  163. // Here we know that given PID is our PID
  164. iter = kcontext_action_iter(context); // Get saved current ACTION
  165. do {
  166. faux_list_t *actions = NULL;
  167. const kaction_t *action = NULL;
  168. // Compute new value for retcode.
  169. // Here iter is a pointer to previous action but not new.
  170. // If iter == NULL then it will be a first ACTION from the sequence.
  171. if (iter && pexitstatus) {
  172. const kaction_t *terminated_action = NULL;
  173. terminated_action = faux_list_data(iter);
  174. assert(terminated_action);
  175. if (kaction_update_retcode(terminated_action))
  176. kcontext_set_retcode(context, *pexitstatus);
  177. }
  178. // Get next ACTION from sequence
  179. if (!iter) { // Is it the first ACTION within list
  180. actions = kentry_actions(kpargv_command(kcontext_pargv(context)));
  181. assert(actions);
  182. iter = faux_list_head(actions);
  183. } else {
  184. iter = faux_list_next_node(iter);
  185. }
  186. kcontext_set_action_iter(context, iter);
  187. // Was it end of ACTION sequence?
  188. if (!iter) {
  189. kcontext_set_done(context, BOOL_TRUE);
  190. break;
  191. }
  192. // Not all ACTIONs has an exit status. Some can have condition to
  193. // skip real execution. So they has no exit status.
  194. pexitstatus = NULL;
  195. // Get new ACTION to execute
  196. action = (const kaction_t *)faux_list_data(iter);
  197. assert(action);
  198. // Check for previous retcode to find out if next command must
  199. // be executed.
  200. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  201. continue; // Skip execution
  202. // Here we know that process will be executed. Dry-run mode is a
  203. // pseudo-execution too i.e. ACTION has exit status.
  204. pexitstatus = &exitstatus;
  205. // Check for dry-run flag and 'permanent' feature of ACTION.
  206. if (kexec_dry_run(exec) && !kaction_permanent(action)) {
  207. exitstatus = 0; // Exit status while dry-run is always 0
  208. continue;
  209. }
  210. exitstatus = exec_action(context, action, &new_pid);
  211. } while (-1 == new_pid); // PID is not -1 when new process was forked
  212. // Save PID of newly created process
  213. if (new_pid != -1) // It means that process was fork()ed
  214. kcontext_set_pid(context, new_pid);
  215. return BOOL_TRUE;
  216. }
  217. static bool_t continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  218. {
  219. faux_list_node_t *iter = NULL;
  220. kcontext_t *context = NULL;
  221. assert(exec);
  222. if (!exec)
  223. return BOOL_FALSE;
  224. iter = kexec_contexts_iter(exec);
  225. while ((context = kexec_contexts_each(&iter))) {
  226. bool_t found = BOOL_FALSE;
  227. found = exec_action_sequence(exec, context, pid, wstatus);
  228. if (found && (pid != -1))
  229. break;
  230. }
  231. return BOOL_TRUE;
  232. }
  233. bool_t kexec_exec(kexec_t *exec)
  234. {
  235. assert(exec);
  236. if (!exec)
  237. return BOOL_FALSE;
  238. // Firsly prepare kexec object for execution. The file streams must
  239. // be created for stdin, stdout, stderr of processes.
  240. if (!kexec_prepare(exec))
  241. return BOOL_FALSE;
  242. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  243. // ACTION's process.
  244. continue_command_execution(exec, -1, 0);
  245. return BOOL_TRUE;
  246. }