kexec.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  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. int stdin;
  15. int stdout;
  16. int stderr;
  17. };
  18. // STDIN
  19. KGET(exec, int, stdin);
  20. KSET(exec, int, stdin);
  21. // STDOUT
  22. KGET(exec, int, stdout);
  23. KSET(exec, int, stdout);
  24. // STDERR
  25. KGET(exec, int, stderr);
  26. KSET(exec, int, stderr);
  27. // CONTEXT list
  28. KADD_NESTED(exec, kcontext_t *, contexts);
  29. KNESTED_LEN(exec, contexts);
  30. KNESTED_IS_EMPTY(exec, contexts);
  31. KNESTED_ITER(exec, contexts);
  32. KNESTED_EACH(exec, kcontext_t *, contexts);
  33. kexec_t *kexec_new()
  34. {
  35. kexec_t *exec = NULL;
  36. exec = faux_zmalloc(sizeof(*exec));
  37. assert(exec);
  38. if (!exec)
  39. return NULL;
  40. // List of execute contexts
  41. exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  42. NULL, NULL, (void (*)(void *))kcontext_free);
  43. assert(exec->contexts);
  44. // I/O
  45. exec->stdin = -1;
  46. exec->stdout = -1;
  47. exec->stderr = -1;
  48. return exec;
  49. }
  50. void kexec_free(kexec_t *exec)
  51. {
  52. if (!exec)
  53. return;
  54. faux_list_free(exec->contexts);
  55. free(exec);
  56. }
  57. size_t kexec_len(const kexec_t *exec)
  58. {
  59. assert(exec);
  60. if (!exec)
  61. return 0;
  62. return faux_list_len(exec->contexts);
  63. }
  64. size_t kexec_is_empty(const kexec_t *exec)
  65. {
  66. assert(exec);
  67. if (!exec)
  68. return 0;
  69. return faux_list_is_empty(exec->contexts);
  70. }
  71. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  72. {
  73. assert(exec);
  74. assert(context);
  75. if (!exec)
  76. return BOOL_FALSE;
  77. if (!context)
  78. return BOOL_FALSE;
  79. if (!faux_list_add(exec->contexts, context))
  80. return BOOL_FALSE;
  81. return BOOL_TRUE;
  82. }
  83. static bool_t kexec_prepare(kexec_t *exec)
  84. {
  85. int pipefd[2] = {};
  86. faux_list_node_t *iter = NULL;
  87. int global_stderr = -1;
  88. assert(exec);
  89. if (!exec)
  90. return BOOL_FALSE;
  91. // Nothing to prepare for empty list
  92. if (kexec_contexts_is_empty(exec))
  93. return BOOL_FALSE;
  94. // Create "global" stdin, stdout, stderr for the whole job execution.
  95. // Now function creates only the simple pipes but somedays it will be
  96. // able to create pseudo-terminal for interactive sessions.
  97. // STDIN
  98. if (pipe(pipefd) < 0)
  99. return BOOL_FALSE;
  100. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  101. pipefd[0]); // Read end
  102. kexec_set_stdin(exec, pipefd[1]); // Write end
  103. // STDOUT
  104. if (pipe(pipefd) < 0)
  105. return BOOL_FALSE;
  106. kexec_set_stdout(exec, pipefd[0]); // Read end
  107. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  108. pipefd[1]); // Write end
  109. // STDERR
  110. if (pipe(pipefd) < 0)
  111. return BOOL_FALSE;
  112. kexec_set_stderr(exec, pipefd[0]); // Read end
  113. // STDERR write end will be set to all list members as stderr
  114. global_stderr = pipefd[1]; // Write end
  115. // Iterate all context_t elements to fill all stdin, stdout, stderr
  116. for (iter = faux_list_head(exec->contexts); iter;
  117. iter = faux_list_next_node(iter)) {
  118. faux_list_node_t *next = faux_list_next_node(iter);
  119. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  120. // Set the same STDERR to all contexts
  121. kcontext_set_stderr(context, global_stderr);
  122. // Create pipes beetween processes
  123. if (next) {
  124. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  125. if (pipe(pipefd) < 0)
  126. return BOOL_FALSE;
  127. kcontext_set_stdout(context, pipefd[1]); // Write end
  128. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  129. }
  130. }
  131. return BOOL_TRUE;
  132. }
  133. static bool_t exec_action(kcontext_t *context, const kaction_t *action)
  134. {
  135. context = context;
  136. action = action;
  137. return BOOL_TRUE;
  138. }
  139. static bool_t exec_action_sequence(kcontext_t *context, pid_t pid, int wstatus)
  140. {
  141. faux_list_node_t *iter = NULL;
  142. int exitstatus = WEXITSTATUS(wstatus);
  143. assert(context);
  144. if (!context)
  145. return BOOL_FALSE;
  146. // There is two reasons to don't start any real actions.
  147. // - The ACTION sequence is already done;
  148. // - Passed PID (PID of completed process) is not owned by this context.
  149. // Returns true because it's not an error.
  150. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  151. return BOOL_TRUE;
  152. iter = kcontext_action_iter(context); // Get saved current ACTION
  153. do {
  154. faux_list_t *actions = NULL;
  155. const kaction_t *action = NULL;
  156. // Here we know that given PID is our PID and some ACTIONs are
  157. // left to be executed.
  158. // If some process returns then compute current retcode.
  159. if (iter) {
  160. const kaction_t *terminated_action = NULL;
  161. terminated_action = faux_list_data(iter);
  162. assert(terminated_action);
  163. if (kaction_update_retcode(terminated_action))
  164. kcontext_set_retcode(context, exitstatus);
  165. }
  166. if (!iter) { // Is it the first ACTION within list
  167. actions = kentry_actions(kpargv_command(kcontext_pargv(context)));
  168. assert(actions);
  169. iter = faux_list_head(actions);
  170. } else {
  171. iter = faux_list_next_node(iter);
  172. }
  173. kcontext_set_action_iter(context, iter);
  174. if (!iter) { // It was last ACTION
  175. kcontext_set_done(context, BOOL_TRUE);
  176. }
  177. action = (const kaction_t *)faux_list_data(iter);
  178. assert(action);
  179. exec_action(context, action);
  180. printf("CONTEXT\n");
  181. } while (iter);
  182. wstatus = wstatus;
  183. return BOOL_TRUE;
  184. }
  185. static bool_t continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  186. {
  187. faux_list_node_t *iter = NULL;
  188. kcontext_t *context = NULL;
  189. assert(exec);
  190. if (!exec)
  191. return BOOL_FALSE;
  192. iter = kexec_contexts_iter(exec);
  193. while ((context = kexec_contexts_each(&iter))) {
  194. exec_action_sequence(context, pid, wstatus);
  195. }
  196. return BOOL_TRUE;
  197. }
  198. bool_t kexec_exec(kexec_t *exec)
  199. {
  200. assert(exec);
  201. if (!exec)
  202. return BOOL_FALSE;
  203. // Firsly prepare kexec object for execution. The file streams must
  204. // be created for stdin, stdout, stderr of processes.
  205. if (!kexec_prepare(exec))
  206. return BOOL_FALSE;
  207. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  208. // ACTION's process.
  209. continue_command_execution(exec, -1, 0);
  210. return BOOL_TRUE;
  211. }