kexec.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. /** @file kexec.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <sys/types.h>
  8. #include <unistd.h>
  9. #include <faux/list.h>
  10. #include <klish/khelper.h>
  11. #include <klish/kcontext.h>
  12. #include <klish/kexec.h>
  13. struct kexec_s {
  14. faux_list_t *contexts;
  15. bool_t dry_run;
  16. int stdin;
  17. int stdout;
  18. int stderr;
  19. };
  20. // Dry-run
  21. KGET_BOOL(exec, dry_run);
  22. KSET_BOOL(exec, dry_run);
  23. // STDIN
  24. KGET(exec, int, stdin);
  25. KSET(exec, int, stdin);
  26. // STDOUT
  27. KGET(exec, int, stdout);
  28. KSET(exec, int, stdout);
  29. // STDERR
  30. KGET(exec, int, stderr);
  31. KSET(exec, int, stderr);
  32. // CONTEXT list
  33. KADD_NESTED(exec, kcontext_t *, contexts);
  34. KNESTED_LEN(exec, contexts);
  35. KNESTED_IS_EMPTY(exec, contexts);
  36. KNESTED_ITER(exec, contexts);
  37. KNESTED_EACH(exec, kcontext_t *, contexts);
  38. kexec_t *kexec_new()
  39. {
  40. kexec_t *exec = NULL;
  41. exec = faux_zmalloc(sizeof(*exec));
  42. assert(exec);
  43. if (!exec)
  44. return NULL;
  45. exec->dry_run = BOOL_FALSE;
  46. // List of execute contexts
  47. exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  48. NULL, NULL, (void (*)(void *))kcontext_free);
  49. assert(exec->contexts);
  50. // I/O
  51. exec->stdin = -1;
  52. exec->stdout = -1;
  53. exec->stderr = -1;
  54. return exec;
  55. }
  56. void kexec_free(kexec_t *exec)
  57. {
  58. if (!exec)
  59. return;
  60. faux_list_free(exec->contexts);
  61. free(exec);
  62. }
  63. size_t kexec_len(const kexec_t *exec)
  64. {
  65. assert(exec);
  66. if (!exec)
  67. return 0;
  68. return faux_list_len(exec->contexts);
  69. }
  70. size_t kexec_is_empty(const kexec_t *exec)
  71. {
  72. assert(exec);
  73. if (!exec)
  74. return 0;
  75. return faux_list_is_empty(exec->contexts);
  76. }
  77. // kexec is done when all the kexec's contexts are done
  78. bool_t kexec_done(const kexec_t *exec)
  79. {
  80. faux_list_node_t *iter = NULL;
  81. kcontext_t *context = NULL;
  82. assert(exec);
  83. if (!exec)
  84. return BOOL_FALSE;
  85. iter = kexec_contexts_iter(exec);
  86. while ((context = kexec_contexts_each(&iter))) {
  87. if (!kcontext_done(context))
  88. return BOOL_FALSE;
  89. }
  90. return BOOL_TRUE;
  91. }
  92. // Retcode of kexec is a retcode of its first context execution because
  93. // next contexts just a filters. Retcode valid if kexec is done. Else current
  94. // retcode is non-valid and will not be returned at all.
  95. bool_t kexec_retcode(const kexec_t *exec, int *status)
  96. {
  97. assert(exec);
  98. if (!exec)
  99. return BOOL_FALSE;
  100. if (kexec_is_empty(exec))
  101. return BOOL_FALSE;
  102. if (!kexec_done(exec)) // Unfinished execution
  103. return BOOL_FALSE;
  104. if (status)
  105. *status = kcontext_retcode(
  106. (kcontext_t *)faux_list_data(faux_list_head(exec->contexts)));
  107. return BOOL_TRUE;
  108. }
  109. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  110. {
  111. assert(exec);
  112. assert(context);
  113. if (!exec)
  114. return BOOL_FALSE;
  115. if (!context)
  116. return BOOL_FALSE;
  117. if (!faux_list_add(exec->contexts, context))
  118. return BOOL_FALSE;
  119. return BOOL_TRUE;
  120. }
  121. static bool_t kexec_prepare(kexec_t *exec)
  122. {
  123. int pipefd[2] = {};
  124. faux_list_node_t *iter = NULL;
  125. int global_stderr = -1;
  126. assert(exec);
  127. if (!exec)
  128. return BOOL_FALSE;
  129. // Nothing to prepare for empty list
  130. if (kexec_contexts_is_empty(exec))
  131. return BOOL_FALSE;
  132. // Create "global" stdin, stdout, stderr for the whole job execution.
  133. // Now function creates only the simple pipes but somedays it will be
  134. // able to create pseudo-terminal for interactive sessions.
  135. // STDIN
  136. if (pipe(pipefd) < 0)
  137. return BOOL_FALSE;
  138. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  139. pipefd[0]); // Read end
  140. kexec_set_stdin(exec, pipefd[1]); // Write end
  141. // STDOUT
  142. if (pipe(pipefd) < 0)
  143. return BOOL_FALSE;
  144. kexec_set_stdout(exec, pipefd[0]); // Read end
  145. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  146. pipefd[1]); // Write end
  147. // STDERR
  148. if (pipe(pipefd) < 0)
  149. return BOOL_FALSE;
  150. kexec_set_stderr(exec, pipefd[0]); // Read end
  151. // STDERR write end will be set to all list members as stderr
  152. global_stderr = pipefd[1]; // Write end
  153. // Iterate all context_t elements to fill all stdin, stdout, stderr
  154. for (iter = faux_list_head(exec->contexts); iter;
  155. iter = faux_list_next_node(iter)) {
  156. faux_list_node_t *next = faux_list_next_node(iter);
  157. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  158. // Set the same STDERR to all contexts
  159. kcontext_set_stderr(context, global_stderr);
  160. // Create pipes beetween processes
  161. if (next) {
  162. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  163. if (pipe(pipefd) < 0)
  164. return BOOL_FALSE;
  165. kcontext_set_stdout(context, pipefd[1]); // Write end
  166. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  167. }
  168. }
  169. return BOOL_TRUE;
  170. }
  171. static bool_t exec_action(kcontext_t *context, const kaction_t *action,
  172. pid_t *pid, int *retcode)
  173. {
  174. ksym_fn fn = NULL;
  175. int exitcode = 0;
  176. pid_t child_pid = -1;
  177. assert(context);
  178. if (!context)
  179. return BOOL_FALSE;
  180. assert(action);
  181. if (!action)
  182. return BOOL_FALSE;
  183. fn = ksym_function(kaction_sym(action));
  184. // Sync symbol execution
  185. if (kaction_is_sync(action)) {
  186. if (pid)
  187. *pid = -1;
  188. exitcode = fn(context);
  189. if (retcode)
  190. *retcode = exitcode;
  191. return BOOL_TRUE;
  192. }
  193. // Unsync symbol execution i.e. using forked process
  194. child_pid = fork();
  195. if (child_pid == -1)
  196. return BOOL_FALSE;
  197. // Parent
  198. // Save the child pid and return control. Later event loop will wait
  199. // for saved pid.
  200. if (child_pid != 0) {
  201. if (pid)
  202. *pid = child_pid;
  203. return BOOL_TRUE;
  204. }
  205. // Child
  206. _exit(fn(context));
  207. return BOOL_TRUE;
  208. }
  209. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  210. pid_t pid, int wstatus)
  211. {
  212. faux_list_node_t *iter = NULL;
  213. int exitstatus = WEXITSTATUS(wstatus);
  214. int *pexitstatus = &exitstatus;
  215. pid_t new_pid = -1; // PID of newly forked ACTION process
  216. assert(context);
  217. if (!context)
  218. return BOOL_FALSE;
  219. // There is two reasons to don't start any real actions.
  220. // - The ACTION sequence is already done;
  221. // - Passed PID (PID of completed process) is not owned by this context.
  222. // Returns false that indicates this PID is not mine.
  223. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  224. return BOOL_FALSE;
  225. // Here we know that given PID is our PID
  226. iter = kcontext_action_iter(context); // Get saved current ACTION
  227. do {
  228. faux_list_t *actions = NULL;
  229. const kaction_t *action = NULL;
  230. // Compute new value for retcode.
  231. // Here iter is a pointer to previous action but not new.
  232. // If iter == NULL then it will be a first ACTION from the sequence.
  233. if (iter && pexitstatus) {
  234. const kaction_t *terminated_action = NULL;
  235. terminated_action = faux_list_data(iter);
  236. assert(terminated_action);
  237. if (kaction_update_retcode(terminated_action))
  238. kcontext_set_retcode(context, *pexitstatus);
  239. }
  240. // Get next ACTION from sequence
  241. if (!iter) { // Is it the first ACTION within list
  242. actions = kentry_actions(kpargv_command(kcontext_pargv(context)));
  243. assert(actions);
  244. iter = faux_list_head(actions);
  245. } else {
  246. iter = faux_list_next_node(iter);
  247. }
  248. kcontext_set_action_iter(context, iter);
  249. // Was it end of ACTION sequence?
  250. if (!iter) {
  251. kcontext_set_done(context, BOOL_TRUE);
  252. break;
  253. }
  254. // Not all ACTIONs has an exit status. Some can have condition to
  255. // skip real execution. So they has no exit status.
  256. pexitstatus = NULL;
  257. // Get new ACTION to execute
  258. action = (const kaction_t *)faux_list_data(iter);
  259. assert(action);
  260. // Check for previous retcode to find out if next command must
  261. // be executed.
  262. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  263. continue; // Skip execution
  264. // Here we know that process will be executed. Dry-run mode is a
  265. // pseudo-execution too i.e. ACTION has exit status.
  266. pexitstatus = &exitstatus;
  267. // Check for dry-run flag and 'permanent' feature of ACTION.
  268. if (kexec_dry_run(exec) && !kaction_permanent(action)) {
  269. exitstatus = 0; // Exit status while dry-run is always 0
  270. continue;
  271. }
  272. exec_action(context, action, &new_pid, &exitstatus);
  273. } while (-1 == new_pid); // PID is not -1 when new process was forked
  274. // Save PID of newly created process
  275. if (new_pid != -1) // It means that process was fork()ed
  276. kcontext_set_pid(context, new_pid);
  277. return BOOL_TRUE;
  278. }
  279. bool_t kexec_continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  280. {
  281. faux_list_node_t *iter = NULL;
  282. kcontext_t *context = NULL;
  283. assert(exec);
  284. if (!exec)
  285. return BOOL_FALSE;
  286. iter = kexec_contexts_iter(exec);
  287. while ((context = kexec_contexts_each(&iter))) {
  288. bool_t found = BOOL_FALSE;
  289. found = exec_action_sequence(exec, context, pid, wstatus);
  290. if (found && (pid != -1))
  291. break;
  292. }
  293. return BOOL_TRUE;
  294. }
  295. bool_t kexec_exec(kexec_t *exec)
  296. {
  297. assert(exec);
  298. if (!exec)
  299. return BOOL_FALSE;
  300. // Firsly prepare kexec object for execution. The file streams must
  301. // be created for stdin, stdout, stderr of processes.
  302. if (!kexec_prepare(exec))
  303. return BOOL_FALSE;
  304. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  305. // ACTION's process.
  306. kexec_continue_command_execution(exec, -1, 0);
  307. return BOOL_TRUE;
  308. }