kexec.c 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. static bool_t exec_action_sequence(kcontext_t *context)
  88. {
  89. faux_list_node_t *iter = NULL;
  90. faux_list_t *actions = NULL;
  91. assert(context);
  92. if (!context)
  93. return BOOL_FALSE;
  94. actions =
  95. }
  96. */
  97. static bool_t kexec_prepare(kexec_t *exec)
  98. {
  99. int pipefd[2] = {};
  100. faux_list_node_t *iter = NULL;
  101. int global_stderr = -1;
  102. assert(exec);
  103. if (!exec)
  104. return BOOL_FALSE;
  105. // Nothing to prepare for empty list
  106. if (kexec_contexts_is_empty(exec))
  107. return BOOL_FALSE;
  108. // Create "global" stdin, stdout, stderr for the whole job execution.
  109. // Now function creates only the simple pipes but somedays it will be
  110. // able to create pseudo-terminal for interactive sessions.
  111. // STDIN
  112. if (pipe(pipefd) < 0)
  113. return BOOL_FALSE;
  114. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  115. pipefd[0]); // Read end
  116. kexec_set_stdin(exec, pipefd[1]); // Write end
  117. // STDOUT
  118. if (pipe(pipefd) < 0)
  119. return BOOL_FALSE;
  120. kexec_set_stdout(exec, pipefd[0]); // Read end
  121. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  122. pipefd[1]); // Write end
  123. // STDERR
  124. if (pipe(pipefd) < 0)
  125. return BOOL_FALSE;
  126. kexec_set_stderr(exec, pipefd[0]); // Read end
  127. // STDERR write end will be set to all list members as stderr
  128. global_stderr = pipefd[1]; // Write end
  129. // Iterate all context_t elements to fill all stdin, stdout, stderr
  130. for (iter = faux_list_head(exec->contexts); iter;
  131. iter = faux_list_next_node(iter)) {
  132. faux_list_node_t *next = faux_list_next_node(iter);
  133. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  134. // Set the same STDERR to all contexts
  135. kcontext_set_stderr(context, global_stderr);
  136. // Create pipes beetween processes
  137. if (next) {
  138. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  139. if (pipe(pipefd) < 0)
  140. return BOOL_FALSE;
  141. kcontext_set_stdout(context, pipefd[1]); // Write end
  142. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  143. }
  144. }
  145. return BOOL_TRUE;
  146. }
  147. bool_t kexec_exec(kexec_t *exec)
  148. {
  149. faux_list_node_t *iter = NULL;
  150. kcontext_t *context = NULL;
  151. assert(exec);
  152. if (!exec)
  153. return BOOL_FALSE;
  154. // Firsly prepare kexec object for execution. The file streams must
  155. // be created for stdin, stdout, stderr of processes.
  156. if (!kexec_prepare(exec))
  157. return BOOL_FALSE;
  158. iter = faux_list_tail(exec->contexts);
  159. while ((context = faux_list_data(iter))) {
  160. iter = faux_list_prev_node(iter);
  161. }
  162. return BOOL_TRUE;
  163. }