kexec.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255
  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. /*
  84. static bool_t exec_action(kcontext_t context,
  85. {
  86. }
  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 bool_t exec_action_sequence(kcontext_t *context, pid_t pid)
  139. {
  140. faux_list_node_t *iter = NULL;
  141. faux_list_t *actions = NULL;
  142. assert(context);
  143. if (!context)
  144. return BOOL_FALSE;
  145. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  146. return BOOL_TRUE;
  147. // iter = kexec_contexts_iter(exec);
  148. // while ((context = kexec_contexts_each(&iter))) {
  149. // exec_action_sequence(context, pid);
  150. // }
  151. printf("CONTEXT\n");
  152. actions = actions;
  153. iter = iter;
  154. pid = pid;
  155. return BOOL_TRUE;
  156. }
  157. static bool_t exec_command(kexec_t *exec, pid_t pid)
  158. {
  159. faux_list_node_t *iter = NULL;
  160. kcontext_t *context = NULL;
  161. assert(exec);
  162. if (!exec)
  163. return BOOL_FALSE;
  164. iter = kexec_contexts_iter(exec);
  165. while ((context = kexec_contexts_each(&iter))) {
  166. exec_action_sequence(context, pid);
  167. }
  168. return BOOL_TRUE;
  169. }
  170. bool_t kexec_exec(kexec_t *exec)
  171. {
  172. faux_list_node_t *iter = NULL;
  173. kcontext_t *context = NULL;
  174. assert(exec);
  175. if (!exec)
  176. return BOOL_FALSE;
  177. // Firsly prepare kexec object for execution. The file streams must
  178. // be created for stdin, stdout, stderr of processes.
  179. if (!kexec_prepare(exec))
  180. return BOOL_FALSE;
  181. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  182. // ACTION's process.
  183. exec_command(exec, -1);
  184. iter = faux_list_tail(exec->contexts);
  185. while ((context = faux_list_data(iter))) {
  186. iter = faux_list_prev_node(iter);
  187. }
  188. return BOOL_TRUE;
  189. }