kexec.c 9.7 KB

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