kexec.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533
  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. // === SYNC symbol execution
  199. // The function will be executed right here. It's necessary for
  200. // navigation implementation for example. To grab function output the
  201. // service process will be forked. It gets output and stores it to the
  202. // internal buffer. After sym function return grabber will write
  203. // buffered data back. So grabber will simulate async sym execution.
  204. static bool_t exec_action_sync(kcontext_t *context, const kaction_t *action,
  205. pid_t *pid, int *retcode)
  206. {
  207. ksym_fn fn = NULL;
  208. int exitcode = 0;
  209. pid_t child_pid = -1;
  210. int pipefd[2] = {};
  211. // int fflags = 0;
  212. // Create pipe beetween sym function and grabber
  213. if (pipe(pipefd) < 0)
  214. return BOOL_FALSE;
  215. fn = ksym_function(kaction_sym(action));
  216. fflush(stdout);
  217. fflush(stderr);
  218. // Fork the grabber
  219. child_pid = fork();
  220. if (child_pid == -1) {
  221. close(pipefd[0]);
  222. close(pipefd[1]);
  223. return BOOL_FALSE;
  224. }
  225. // Parent
  226. if (child_pid != 0) {
  227. int saved_stdout = -1;
  228. // Save pid of grabber
  229. if (pid)
  230. *pid = child_pid;
  231. saved_stdout = dup(STDOUT_FILENO);
  232. dup2(pipefd[1], STDOUT_FILENO);
  233. close(pipefd[0]);
  234. close(pipefd[1]);
  235. exitcode = fn(context);
  236. if (retcode)
  237. *retcode = exitcode;
  238. fflush(stdout);
  239. fflush(stderr);
  240. dup2(saved_stdout, STDOUT_FILENO);
  241. close(saved_stdout);
  242. return BOOL_TRUE;
  243. }
  244. // Child (Output grabber)
  245. dup2(pipefd[0], STDIN_FILENO);
  246. close(pipefd[0]);
  247. close(pipefd[1]);
  248. dup2(kcontext_stdout(context), STDOUT_FILENO);
  249. dup2(kcontext_stderr(context), STDERR_FILENO);
  250. char buf[100];
  251. int r = -1;
  252. write(STDOUT_FILENO, "grabber:", 8);
  253. r = read(STDIN_FILENO, buf, 100);
  254. write(STDOUT_FILENO, buf, r);
  255. fflush(stdout);
  256. fflush(stderr);
  257. _exit(0);
  258. return BOOL_TRUE;
  259. }
  260. // === ASYNC symbol execution
  261. // The process will be forked and sym will be executed there.
  262. // The parent will save forked process's pid and immediately return
  263. // control to event loop which will get forked process stdout and
  264. // wait for process termination.
  265. static bool_t exec_action_async(kcontext_t *context, const kaction_t *action,
  266. pid_t *pid)
  267. {
  268. ksym_fn fn = NULL;
  269. int exitcode = 0;
  270. pid_t child_pid = -1;
  271. fn = ksym_function(kaction_sym(action));
  272. // Oh, it's amazing world of stdio!
  273. // Flush buffers before fork() because buffer content will be inherited
  274. // by child. Moreover dup2() can replace old stdout file descriptor by
  275. // the new one but buffer linked with stdout stream will remain the same.
  276. // It must be empty.
  277. fflush(stdout);
  278. fflush(stderr);
  279. child_pid = fork();
  280. if (child_pid == -1)
  281. return BOOL_FALSE;
  282. // Parent
  283. // Save the child pid and return control. Later event loop will wait
  284. // for saved pid.
  285. if (child_pid != 0) {
  286. if (pid)
  287. *pid = child_pid;
  288. return BOOL_TRUE;
  289. }
  290. // Child
  291. dup2(kcontext_stdin(context), STDIN_FILENO);
  292. dup2(kcontext_stdout(context), STDOUT_FILENO);
  293. dup2(kcontext_stderr(context), STDERR_FILENO);
  294. exitcode = fn(context);
  295. // We will use _exit() later so stdio streams will remain unflushed.
  296. // Some output data can be lost. Flush necessary streams here.
  297. fflush(stdout);
  298. fflush(stderr);
  299. // Use _exit() but not exit() to don't flush all the stdio streams. It
  300. // can be dangerous because parent can have a lot of streams inhereted
  301. // by child process.
  302. _exit(exitcode);
  303. return BOOL_TRUE;
  304. }
  305. static bool_t exec_action(kcontext_t *context, const kaction_t *action,
  306. pid_t *pid, int *retcode)
  307. {
  308. assert(context);
  309. if (!context)
  310. return BOOL_FALSE;
  311. assert(action);
  312. if (!action)
  313. return BOOL_FALSE;
  314. if (kaction_is_sync(action))
  315. return exec_action_sync(context, action, pid, retcode);
  316. return exec_action_async(context, action, pid);
  317. }
  318. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  319. pid_t pid, int wstatus)
  320. {
  321. faux_list_node_t *iter = NULL;
  322. int exitstatus = WEXITSTATUS(wstatus);
  323. pid_t new_pid = -1; // PID of newly forked ACTION process
  324. assert(context);
  325. if (!context)
  326. return BOOL_FALSE;
  327. // There is two reasons to don't start any real actions.
  328. // - The ACTION sequence is already done;
  329. // - Passed PID (PID of completed process) is not owned by this context.
  330. // Returns false that indicates this PID is not mine.
  331. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  332. return BOOL_FALSE;
  333. // Here we know that given PID is our PID
  334. iter = kcontext_action_iter(context); // Get saved current ACTION
  335. // ASYNC: Compute new value for retcode.
  336. // Here iter is a pointer to previous action but not new.
  337. // It's for async actions only. Sync actions will change global
  338. // retcode after the exec_action() invocation.
  339. if (iter) {
  340. const kaction_t *terminated_action = faux_list_data(iter);
  341. assert(terminated_action);
  342. if (!kaction_is_sync(terminated_action) &&
  343. kaction_update_retcode(terminated_action))
  344. kcontext_set_retcode(context, exitstatus);
  345. }
  346. // Loop is needed because some ACTIONs will be skipped due to specified
  347. // execution conditions. So try next actions.
  348. do {
  349. const kaction_t *action = NULL;
  350. bool_t is_sync = BOOL_FALSE;
  351. // Get next ACTION from sequence
  352. if (!iter) { // Is it the first ACTION within list
  353. faux_list_t *actions =
  354. kentry_actions(kpargv_command(kcontext_pargv(context)));
  355. assert(actions);
  356. iter = faux_list_head(actions);
  357. } else {
  358. iter = faux_list_next_node(iter);
  359. }
  360. kcontext_set_action_iter(context, iter);
  361. // Is it end of ACTION sequence?
  362. if (!iter) {
  363. kcontext_set_done(context, BOOL_TRUE);
  364. return BOOL_TRUE;
  365. }
  366. // Get new ACTION to execute
  367. action = (const kaction_t *)faux_list_data(iter);
  368. assert(action);
  369. // Check for previous retcode to find out if next command must
  370. // be executed or skipped.
  371. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  372. continue; // Skip action, try next one
  373. // Check for dry-run flag and 'permanent' feature of ACTION.
  374. if (kexec_dry_run(exec) && !kaction_permanent(action)) {
  375. is_sync = BOOL_TRUE; // Simulate sync action
  376. exitstatus = 0; // Exit status while dry-run is always 0
  377. } else { // Normal execution
  378. is_sync = kaction_is_sync(action);
  379. exec_action(context, action, &new_pid, &exitstatus);
  380. }
  381. // SYNC: Compute new value for retcode.
  382. // Sync actions return retcode immediatelly. Their forked
  383. // processes are for output handling only.
  384. if (is_sync && kaction_update_retcode(action))
  385. kcontext_set_retcode(context, exitstatus);
  386. } while (-1 == new_pid); // PID is not -1 when new process was forked
  387. // Save PID of newly created process
  388. kcontext_set_pid(context, new_pid);
  389. return BOOL_TRUE;
  390. }
  391. bool_t kexec_continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  392. {
  393. faux_list_node_t *iter = NULL;
  394. kcontext_t *context = NULL;
  395. assert(exec);
  396. if (!exec)
  397. return BOOL_FALSE;
  398. iter = kexec_contexts_iter(exec);
  399. while ((context = kexec_contexts_each(&iter))) {
  400. bool_t found = BOOL_FALSE;
  401. found = exec_action_sequence(exec, context, pid, wstatus);
  402. if (found && (pid != -1))
  403. break;
  404. }
  405. return BOOL_TRUE;
  406. }
  407. bool_t kexec_exec(kexec_t *exec)
  408. {
  409. assert(exec);
  410. if (!exec)
  411. return BOOL_FALSE;
  412. // Firsly prepare kexec object for execution. The file streams must
  413. // be created for stdin, stdout, stderr of processes.
  414. if (!kexec_prepare(exec))
  415. return BOOL_FALSE;
  416. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  417. // ACTION's process.
  418. kexec_continue_command_execution(exec, -1, 0);
  419. return BOOL_TRUE;
  420. }