kexec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754
  1. /** @file kexec.c
  2. */
  3. #define _XOPEN_SOURCE
  4. #define _XOPEN_SOURCE_EXTENDED
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <assert.h>
  8. #include <string.h>
  9. #include <sys/types.h>
  10. #include <unistd.h>
  11. #include <fcntl.h>
  12. #include <syslog.h>
  13. #include <sys/ioctl.h>
  14. #include <termios.h>
  15. #include <signal.h>
  16. #include <faux/list.h>
  17. #include <faux/buf.h>
  18. #include <faux/eloop.h>
  19. #include <klish/khelper.h>
  20. #include <klish/kcontext.h>
  21. #include <klish/kpath.h>
  22. #include <klish/kexec.h>
  23. #define PTMX_PATH "/dev/ptmx"
  24. // Declaration of grabber. Implementation is in the grabber.c
  25. void grabber(int fds[][2]);
  26. struct kexec_s {
  27. faux_list_t *contexts;
  28. bool_t dry_run;
  29. int stdin;
  30. int stdout;
  31. int stderr;
  32. faux_buf_t *bufin;
  33. faux_buf_t *bufout;
  34. faux_buf_t *buferr;
  35. kpath_t *saved_path;
  36. };
  37. // Dry-run
  38. KGET_BOOL(exec, dry_run);
  39. KSET_BOOL(exec, dry_run);
  40. // STDIN
  41. KGET(exec, int, stdin);
  42. KSET(exec, int, stdin);
  43. // STDOUT
  44. KGET(exec, int, stdout);
  45. KSET(exec, int, stdout);
  46. // STDERR
  47. KGET(exec, int, stderr);
  48. KSET(exec, int, stderr);
  49. // BufIN
  50. KGET(exec, faux_buf_t *, bufin);
  51. KSET(exec, faux_buf_t *, bufin);
  52. // BufOUT
  53. KGET(exec, faux_buf_t *, bufout);
  54. KSET(exec, faux_buf_t *, bufout);
  55. // BufERR
  56. KGET(exec, faux_buf_t *, buferr);
  57. KSET(exec, faux_buf_t *, buferr);
  58. // Saved path
  59. KGET(exec, kpath_t *, saved_path);
  60. // CONTEXT list
  61. KADD_NESTED(exec, kcontext_t *, contexts);
  62. KNESTED_LEN(exec, contexts);
  63. KNESTED_IS_EMPTY(exec, contexts);
  64. KNESTED_ITER(exec, contexts);
  65. KNESTED_EACH(exec, kcontext_t *, contexts);
  66. kexec_t *kexec_new()
  67. {
  68. kexec_t *exec = NULL;
  69. exec = faux_zmalloc(sizeof(*exec));
  70. assert(exec);
  71. if (!exec)
  72. return NULL;
  73. exec->dry_run = BOOL_FALSE;
  74. exec->saved_path = NULL;
  75. // List of execute contexts
  76. exec->contexts = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  77. NULL, NULL, (void (*)(void *))kcontext_free);
  78. assert(exec->contexts);
  79. // I/O
  80. exec->stdin = -1;
  81. exec->stdout = -1;
  82. exec->stderr = -1;
  83. exec->bufin = faux_buf_new(0);
  84. exec->bufout = faux_buf_new(0);
  85. exec->buferr = faux_buf_new(0);
  86. return exec;
  87. }
  88. void kexec_free(kexec_t *exec)
  89. {
  90. if (!exec)
  91. return;
  92. faux_list_free(exec->contexts);
  93. if (exec->stdin != -1)
  94. close(exec->stdin);
  95. if (exec->stdout != -1)
  96. close(exec->stdout);
  97. if (exec->stderr != -1)
  98. close(exec->stderr);
  99. faux_buf_free(exec->bufin);
  100. faux_buf_free(exec->bufout);
  101. faux_buf_free(exec->buferr);
  102. kpath_free(exec->saved_path);
  103. free(exec);
  104. }
  105. size_t kexec_len(const kexec_t *exec)
  106. {
  107. assert(exec);
  108. if (!exec)
  109. return 0;
  110. return faux_list_len(exec->contexts);
  111. }
  112. size_t kexec_is_empty(const kexec_t *exec)
  113. {
  114. assert(exec);
  115. if (!exec)
  116. return 0;
  117. return faux_list_is_empty(exec->contexts);
  118. }
  119. // kexec is done when all the kexec's contexts are done
  120. bool_t kexec_done(const kexec_t *exec)
  121. {
  122. faux_list_node_t *iter = NULL;
  123. kcontext_t *context = NULL;
  124. assert(exec);
  125. if (!exec)
  126. return BOOL_FALSE;
  127. iter = kexec_contexts_iter(exec);
  128. while ((context = kexec_contexts_each(&iter))) {
  129. if (!kcontext_done(context))
  130. return BOOL_FALSE;
  131. }
  132. return BOOL_TRUE;
  133. }
  134. // Retcode of kexec is a retcode of its first context execution because
  135. // next contexts just a filters. Retcode valid if kexec is done. Else current
  136. // retcode is non-valid and will not be returned at all.
  137. bool_t kexec_retcode(const kexec_t *exec, int *status)
  138. {
  139. assert(exec);
  140. if (!exec)
  141. return BOOL_FALSE;
  142. if (kexec_is_empty(exec))
  143. return BOOL_FALSE;
  144. if (!kexec_done(exec)) // Unfinished execution
  145. return BOOL_FALSE;
  146. if (status)
  147. *status = kcontext_retcode(
  148. (kcontext_t *)faux_list_data(faux_list_head(exec->contexts)));
  149. return BOOL_TRUE;
  150. }
  151. bool_t kexec_path_is_changed(const kexec_t *exec)
  152. {
  153. kpath_t *path = NULL;
  154. kcontext_t *context = NULL;
  155. assert(exec);
  156. if (!exec)
  157. return BOOL_FALSE;
  158. context = (kcontext_t *)faux_list_data(faux_list_head(exec->contexts));
  159. path = ksession_path(kcontext_session(context));
  160. if (kpath_is_equal(exec->saved_path, path))
  161. return BOOL_FALSE;
  162. return BOOL_TRUE;
  163. }
  164. bool_t kexec_add(kexec_t *exec, kcontext_t *context)
  165. {
  166. assert(exec);
  167. assert(context);
  168. if (!exec)
  169. return BOOL_FALSE;
  170. if (!context)
  171. return BOOL_FALSE;
  172. if (!faux_list_add(exec->contexts, context))
  173. return BOOL_FALSE;
  174. return BOOL_TRUE;
  175. }
  176. bool_t kexec_set_winsize(kexec_t *exec)
  177. {
  178. int fd = -1;
  179. size_t width = 0;
  180. size_t height = 0;
  181. struct winsize ws = {};
  182. int res = -1;
  183. kcontext_t *context = NULL;
  184. ksession_t *session = NULL;
  185. if (!exec)
  186. return BOOL_FALSE;
  187. if (!kexec_interactive(exec))
  188. return BOOL_FALSE;
  189. fd = kexec_stdin(exec);
  190. if (fd < 0)
  191. return BOOL_FALSE;
  192. if (!isatty(fd))
  193. return BOOL_FALSE;
  194. context = (kcontext_t *)faux_list_data(faux_list_head(exec->contexts));
  195. if (!context)
  196. return BOOL_FALSE;
  197. session = kcontext_session(context);
  198. if (!session)
  199. return BOOL_FALSE;
  200. // Set pseudo terminal window size
  201. width = ksession_term_width(session);
  202. height = ksession_term_height(session);
  203. if ((width == 0) || (height == 0))
  204. return BOOL_FALSE;
  205. ws.ws_col = (unsigned short)width;
  206. ws.ws_row = (unsigned short)height;
  207. res = ioctl(fd, TIOCSWINSZ, &ws);
  208. if (res < 0)
  209. return BOOL_FALSE;
  210. return BOOL_TRUE;
  211. }
  212. static bool_t kexec_prepare(kexec_t *exec)
  213. {
  214. int pipefd[2] = {};
  215. faux_list_node_t *iter = NULL;
  216. int global_stderr = -1;
  217. int fflags = 0;
  218. assert(exec);
  219. if (!exec)
  220. return BOOL_FALSE;
  221. // Nothing to prepare for empty list
  222. if (kexec_contexts_is_empty(exec))
  223. return BOOL_FALSE;
  224. // If command is interactive then prepare pseudoterminal. Note
  225. // interactive commands can't have filters
  226. if (kexec_interactive(exec)) {
  227. int ptm = -1;
  228. char *pts_name = NULL;
  229. kcontext_t *context = (kcontext_t *)faux_list_data(
  230. faux_list_head(exec->contexts));
  231. ptm = open(PTMX_PATH, O_RDWR, O_NOCTTY);
  232. if (ptm < 0)
  233. return BOOL_FALSE;
  234. // Set O_NONBLOCK flag here. Because this flag is ignored while
  235. // open() ptmx. I don't know why. fcntl() is working fine.
  236. fflags = fcntl(ptm, F_GETFL);
  237. fcntl(ptm, F_SETFL, fflags | O_NONBLOCK);
  238. grantpt(ptm);
  239. unlockpt(ptm);
  240. pts_name = ptsname(ptm);
  241. kexec_set_stdin(exec, ptm);
  242. kexec_set_stdout(exec, ptm);
  243. kexec_set_stderr(exec, ptm);
  244. // Don't set pts fd here. In a case of pseudo-terminal the pts
  245. // must be opened later in the child after setsid(). So just
  246. // save filename of pts.
  247. kcontext_set_pts_fn(context, pts_name);
  248. // Set pseudo terminal window size
  249. kexec_set_winsize(exec);
  250. return BOOL_TRUE;
  251. }
  252. // Create "global" stdin, stdout, stderr for the whole job execution.
  253. // Now function creates only the simple pipes but somedays it will be
  254. // able to create pseudo-terminal for interactive sessions.
  255. // STDIN
  256. if (pipe(pipefd) < 0)
  257. return BOOL_FALSE;
  258. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  259. pipefd[0]); // Read end
  260. kexec_set_stdin(exec, pipefd[1]); // Write end
  261. // STDOUT
  262. if (pipe(pipefd) < 0)
  263. return BOOL_FALSE;
  264. // Read end of 'stdout' pipe must be non-blocked
  265. fflags = fcntl(pipefd[0], F_GETFL);
  266. fcntl(pipefd[0], F_SETFL, fflags | O_NONBLOCK);
  267. kexec_set_stdout(exec, pipefd[0]); // Read end
  268. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  269. pipefd[1]); // Write end
  270. // STDERR
  271. if (pipe(pipefd) < 0)
  272. return BOOL_FALSE;
  273. // Read end of 'stderr' pipe must be non-blocked
  274. fflags = fcntl(pipefd[0], F_GETFL);
  275. fcntl(pipefd[0], F_SETFL, fflags | O_NONBLOCK);
  276. kexec_set_stderr(exec, pipefd[0]); // Read end
  277. // STDERR write end will be set to all list members as stderr
  278. global_stderr = pipefd[1]; // Write end
  279. // Iterate all context_t elements to fill all stdin, stdout, stderr
  280. for (iter = faux_list_head(exec->contexts); iter;
  281. iter = faux_list_next_node(iter)) {
  282. faux_list_node_t *next = faux_list_next_node(iter);
  283. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  284. // The first context is a context of main command. The other
  285. // contexts are contexts of filters. So save current path from
  286. // first context.
  287. if (iter == faux_list_head(exec->contexts)) {
  288. ksession_t *session = kcontext_session(context);
  289. if (session && ksession_path(session))
  290. exec->saved_path = kpath_clone(
  291. ksession_path(session));
  292. }
  293. // Set the same STDERR to all contexts
  294. kcontext_set_stderr(context, global_stderr);
  295. // Create pipes beetween processes
  296. if (next) {
  297. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  298. if (pipe(pipefd) < 0)
  299. return BOOL_FALSE;
  300. kcontext_set_stdout(context, pipefd[1]); // Write end
  301. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  302. }
  303. }
  304. return BOOL_TRUE;
  305. }
  306. // === SYNC symbol execution
  307. // The function will be executed right here. It's necessary for
  308. // navigation implementation for example. To grab function output the
  309. // service process will be forked. It gets output and stores it to the
  310. // internal buffer. After sym function return grabber will write
  311. // buffered data back. So grabber will simulate async sym execution.
  312. static bool_t exec_action_sync(kcontext_t *context, const kaction_t *action,
  313. pid_t *pid, int *retcode)
  314. {
  315. ksym_fn fn = NULL;
  316. int exitcode = 0;
  317. pid_t child_pid = -1;
  318. int pipe_stdout[2] = {};
  319. int pipe_stderr[2] = {};
  320. // Create pipes beetween sym function and grabber
  321. if (pipe(pipe_stdout) < 0)
  322. return BOOL_FALSE;
  323. if (pipe(pipe_stderr) < 0) {
  324. close(pipe_stdout[0]);
  325. close(pipe_stdout[1]);
  326. return BOOL_FALSE;
  327. }
  328. fn = ksym_function(kaction_sym(action));
  329. // Prepare streams before fork
  330. fflush(stdout);
  331. fflush(stderr);
  332. // Fork the grabber
  333. child_pid = fork();
  334. if (child_pid == -1) {
  335. close(pipe_stdout[0]);
  336. close(pipe_stdout[1]);
  337. close(pipe_stderr[0]);
  338. close(pipe_stderr[1]);
  339. return BOOL_FALSE;
  340. }
  341. // Parent
  342. if (child_pid != 0) {
  343. int saved_stdout = -1;
  344. int saved_stderr = -1;
  345. // Save pid of grabber
  346. if (pid)
  347. *pid = child_pid;
  348. // Temporarily replace orig output streams by pipe
  349. // stdout
  350. saved_stdout = dup(STDOUT_FILENO);
  351. dup2(pipe_stdout[1], STDOUT_FILENO);
  352. close(pipe_stdout[0]);
  353. close(pipe_stdout[1]);
  354. // stderr
  355. saved_stderr = dup(STDERR_FILENO);
  356. dup2(pipe_stderr[1], STDERR_FILENO);
  357. close(pipe_stderr[0]);
  358. close(pipe_stderr[1]);
  359. // Execute sym function right here
  360. exitcode = fn(context);
  361. if (retcode)
  362. *retcode = exitcode;
  363. // Restore orig output streams
  364. // stdout
  365. fflush(stdout);
  366. dup2(saved_stdout, STDOUT_FILENO);
  367. close(saved_stdout);
  368. // stderr
  369. fflush(stderr);
  370. dup2(saved_stderr, STDERR_FILENO);
  371. close(saved_stderr);
  372. return BOOL_TRUE;
  373. }
  374. // Child (Output grabber)
  375. close(pipe_stdout[1]);
  376. close(pipe_stderr[1]);
  377. {
  378. int fds[][2] = {
  379. {pipe_stdout[0], kcontext_stdout(context)},
  380. {pipe_stderr[0], kcontext_stderr(context)},
  381. {-1, -1},
  382. };
  383. grabber(fds);
  384. }
  385. close(pipe_stdout[0]);
  386. close(pipe_stderr[0]);
  387. _exit(0);
  388. return BOOL_TRUE;
  389. }
  390. // === ASYNC symbol execution
  391. // The process will be forked and sym will be executed there.
  392. // The parent will save forked process's pid and immediately return
  393. // control to event loop which will get forked process stdout and
  394. // wait for process termination.
  395. static bool_t exec_action_async(kcontext_t *context, const kaction_t *action,
  396. pid_t *pid)
  397. {
  398. ksym_fn fn = NULL;
  399. int exitcode = 0;
  400. pid_t child_pid = -1;
  401. int i = 0;
  402. int fdmax = 0;
  403. const char *pts_fn = NULL;
  404. sigset_t sigs = {};
  405. fn = ksym_function(kaction_sym(action));
  406. // Oh, it's amazing world of stdio!
  407. // Flush buffers before fork() because buffer content will be inherited
  408. // by child. Moreover dup2() can replace old stdout file descriptor by
  409. // the new one but buffer linked with stdout stream will remain the same.
  410. // It must be empty.
  411. fflush(stdout);
  412. fflush(stderr);
  413. child_pid = fork();
  414. if (child_pid == -1)
  415. return BOOL_FALSE;
  416. // Parent
  417. // Save the child pid and return control. Later event loop will wait
  418. // for saved pid.
  419. if (child_pid != 0) {
  420. if (pid)
  421. *pid = child_pid;
  422. return BOOL_TRUE;
  423. }
  424. // Child
  425. // Unblock signals
  426. sigemptyset(&sigs);
  427. sigprocmask(SIG_SETMASK, &sigs, NULL);
  428. if ((pts_fn = kcontext_pts_fn(context)) != NULL) {
  429. int fd = -1;
  430. setsid();
  431. fd = open(pts_fn, O_RDWR, 0);
  432. if (fd < 0)
  433. _exit(-1);
  434. dup2(fd, STDIN_FILENO);
  435. dup2(fd, STDOUT_FILENO);
  436. dup2(fd, STDERR_FILENO);
  437. } else {
  438. dup2(kcontext_stdin(context), STDIN_FILENO);
  439. dup2(kcontext_stdout(context), STDOUT_FILENO);
  440. dup2(kcontext_stderr(context), STDERR_FILENO);
  441. }
  442. // Close all inherited fds except stdin, stdout, stderr
  443. fdmax = (int)sysconf(_SC_OPEN_MAX);
  444. for (i = (STDERR_FILENO + 1); i < fdmax; i++)
  445. close(i);
  446. exitcode = fn(context);
  447. // We will use _exit() later so stdio streams will remain unflushed.
  448. // Some output data can be lost. Flush necessary streams here.
  449. fflush(stdout);
  450. fflush(stderr);
  451. // Use _exit() but not exit() to don't flush all the stdio streams. It
  452. // can be dangerous because parent can have a lot of streams inhereted
  453. // by child process.
  454. _exit(exitcode);
  455. return BOOL_TRUE;
  456. }
  457. static bool_t exec_action(kcontext_t *context, const kaction_t *action,
  458. pid_t *pid, int *retcode)
  459. {
  460. assert(context);
  461. if (!context)
  462. return BOOL_FALSE;
  463. assert(action);
  464. if (!action)
  465. return BOOL_FALSE;
  466. if (kaction_is_sync(action))
  467. return exec_action_sync(context, action, pid, retcode);
  468. return exec_action_async(context, action, pid);
  469. }
  470. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  471. pid_t pid, int wstatus)
  472. {
  473. faux_list_node_t *iter = NULL;
  474. int exitstatus = WEXITSTATUS(wstatus);
  475. pid_t new_pid = -1; // PID of newly forked ACTION process
  476. assert(context);
  477. if (!context)
  478. return BOOL_FALSE;
  479. // There is two reasons to don't start any real actions.
  480. // - The ACTION sequence is already done;
  481. // - Passed PID (PID of completed process) is not owned by this context.
  482. // Returns false that indicates this PID is not mine.
  483. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  484. return BOOL_FALSE;
  485. // Here we know that given PID is our PID
  486. iter = kcontext_action_iter(context); // Get saved current ACTION
  487. // ASYNC: Compute new value for retcode.
  488. // Here iter is a pointer to previous action but not new.
  489. // It's for async actions only. Sync actions will change global
  490. // retcode after the exec_action() invocation.
  491. if (iter) {
  492. const kaction_t *terminated_action = faux_list_data(iter);
  493. assert(terminated_action);
  494. if (!kaction_is_sync(terminated_action) &&
  495. kaction_update_retcode(terminated_action))
  496. kcontext_set_retcode(context, exitstatus);
  497. }
  498. // Loop is needed because some ACTIONs will be skipped due to specified
  499. // execution conditions. So try next actions.
  500. do {
  501. const kaction_t *action = NULL;
  502. bool_t is_sync = BOOL_FALSE;
  503. // Get next ACTION from sequence
  504. if (!iter) { // Is it the first ACTION within list
  505. faux_list_t *actions =
  506. kentry_actions(kpargv_command(kcontext_pargv(context)));
  507. assert(actions);
  508. iter = faux_list_head(actions);
  509. } else {
  510. iter = faux_list_next_node(iter);
  511. }
  512. kcontext_set_action_iter(context, iter);
  513. // Is it end of ACTION sequence?
  514. if (!iter) {
  515. kcontext_set_done(context, BOOL_TRUE);
  516. return BOOL_TRUE;
  517. }
  518. // Get new ACTION to execute
  519. action = (const kaction_t *)faux_list_data(iter);
  520. assert(action);
  521. // Check for previous retcode to find out if next command must
  522. // be executed or skipped.
  523. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  524. continue; // Skip action, try next one
  525. // Check for dry-run flag and 'permanent' feature of ACTION.
  526. if (kexec_dry_run(exec) && !kaction_is_permanent(action)) {
  527. is_sync = BOOL_TRUE; // Simulate sync action
  528. exitstatus = 0; // Exit status while dry-run is always 0
  529. } else { // Normal execution
  530. is_sync = kaction_is_sync(action);
  531. exec_action(context, action, &new_pid, &exitstatus);
  532. }
  533. // SYNC: Compute new value for retcode.
  534. // Sync actions return retcode immediatelly. Their forked
  535. // processes are for output handling only.
  536. if (is_sync && kaction_update_retcode(action))
  537. kcontext_set_retcode(context, exitstatus);
  538. } while (-1 == new_pid); // PID is not -1 when new process was forked
  539. // Save PID of newly created process
  540. kcontext_set_pid(context, new_pid);
  541. return BOOL_TRUE;
  542. }
  543. bool_t kexec_continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  544. {
  545. faux_list_node_t *iter = NULL;
  546. kcontext_t *context = NULL;
  547. assert(exec);
  548. if (!exec)
  549. return BOOL_FALSE;
  550. iter = kexec_contexts_iter(exec);
  551. while ((context = kexec_contexts_each(&iter))) {
  552. bool_t found = BOOL_FALSE;
  553. found = exec_action_sequence(exec, context, pid, wstatus);
  554. if (found && (pid != -1))
  555. break;
  556. }
  557. return BOOL_TRUE;
  558. }
  559. bool_t kexec_exec(kexec_t *exec)
  560. {
  561. kcontext_t *context = NULL;
  562. const kpargv_t *pargv = NULL;
  563. const kentry_t *entry = NULL;
  564. bool_t restore = BOOL_FALSE;
  565. assert(exec);
  566. if (!exec)
  567. return BOOL_FALSE;
  568. // Firsly prepare kexec object for execution. The file streams must
  569. // be created for stdin, stdout, stderr of processes.
  570. if (!kexec_prepare(exec))
  571. return BOOL_FALSE;
  572. // Pre-change VIEW if command has "restore" flag. Only first command in
  573. // line (if many commands are piped) matters. Filters can't change the
  574. // VIEW.
  575. context = (kcontext_t *)faux_list_data(faux_list_head(exec->contexts));
  576. pargv = kcontext_pargv(context);
  577. entry = kpargv_command(pargv);
  578. if (entry)
  579. restore = kentry_restore(entry);
  580. if (restore) {
  581. size_t level = kpargv_level(pargv);
  582. kpath_t *path = ksession_path(kcontext_session(context));
  583. while(kpath_len(path) > (level + 1))
  584. kpath_pop(path);
  585. }
  586. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  587. // ACTION's process.
  588. kexec_continue_command_execution(exec, -1, 0);
  589. return BOOL_TRUE;
  590. }
  591. bool_t kexec_interactive(const kexec_t *exec)
  592. {
  593. faux_list_node_t *node = NULL;
  594. kcontext_t *context = NULL;
  595. const kentry_t *entry = NULL;
  596. assert(exec);
  597. if (!exec)
  598. return BOOL_FALSE;
  599. // Only the ACTION of first context can be interactive
  600. node = faux_list_head(exec->contexts);
  601. if (!node)
  602. return BOOL_FALSE;
  603. context = (kcontext_t *)faux_list_data(node);
  604. if (!context)
  605. return BOOL_FALSE;
  606. entry = kcontext_command(context);
  607. if (!entry)
  608. return BOOL_FALSE;
  609. return kentry_interactive(entry);
  610. }