kexec.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762
  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 pts = -1;
  228. int ptm = -1;
  229. char *pts_name = NULL;
  230. kcontext_t *context = (kcontext_t *)faux_list_data(
  231. faux_list_head(exec->contexts));
  232. ptm = open(PTMX_PATH, O_RDWR, O_NOCTTY);
  233. if (ptm < 0)
  234. return BOOL_FALSE;
  235. // Set O_NONBLOCK flag here. Because this flag is ignored while
  236. // open() ptmx. I don't know why. fcntl() is working fine.
  237. fflags = fcntl(ptm, F_GETFL);
  238. fcntl(ptm, F_SETFL, fflags | O_NONBLOCK);
  239. grantpt(ptm);
  240. unlockpt(ptm);
  241. pts_name = ptsname(ptm);
  242. // Open client side (pts) of pseudo terminal. It's necessary for
  243. // sync action execution. Additionally open descriptor makes
  244. // action (from child) to don't send SIGHUP on terminal handler.
  245. pts = open(pts_name, O_RDWR, O_NOCTTY);
  246. if (pts < 0)
  247. return BOOL_FALSE;
  248. kexec_set_stdin(exec, ptm);
  249. kexec_set_stdout(exec, ptm);
  250. kexec_set_stderr(exec, ptm);
  251. kcontext_set_stdin(context, pts);
  252. kcontext_set_stdout(context, pts);
  253. kcontext_set_stderr(context, pts);
  254. // In a case of pseudo-terminal the pts
  255. // must be reopened later in the child after setsid(). So just
  256. // save filename of pts.
  257. kcontext_set_pts_fname(context, pts_name);
  258. // Set pseudo terminal window size
  259. kexec_set_winsize(exec);
  260. return BOOL_TRUE;
  261. }
  262. // Create "global" stdin, stdout, stderr for the whole job execution.
  263. // STDIN
  264. if (pipe(pipefd) < 0)
  265. return BOOL_FALSE;
  266. kcontext_set_stdin(faux_list_data(faux_list_head(exec->contexts)),
  267. pipefd[0]); // Read end
  268. kexec_set_stdin(exec, pipefd[1]); // Write end
  269. // STDOUT
  270. if (pipe(pipefd) < 0)
  271. return BOOL_FALSE;
  272. // Read end of 'stdout' pipe must be non-blocked
  273. fflags = fcntl(pipefd[0], F_GETFL);
  274. fcntl(pipefd[0], F_SETFL, fflags | O_NONBLOCK);
  275. kexec_set_stdout(exec, pipefd[0]); // Read end
  276. kcontext_set_stdout(faux_list_data(faux_list_tail(exec->contexts)),
  277. pipefd[1]); // Write end
  278. // STDERR
  279. if (pipe(pipefd) < 0)
  280. return BOOL_FALSE;
  281. // Read end of 'stderr' pipe must be non-blocked
  282. fflags = fcntl(pipefd[0], F_GETFL);
  283. fcntl(pipefd[0], F_SETFL, fflags | O_NONBLOCK);
  284. kexec_set_stderr(exec, pipefd[0]); // Read end
  285. // STDERR write end will be set to all list members as stderr
  286. global_stderr = pipefd[1]; // Write end
  287. // Iterate all context_t elements to fill all stdin, stdout, stderr
  288. for (iter = faux_list_head(exec->contexts); iter;
  289. iter = faux_list_next_node(iter)) {
  290. faux_list_node_t *next = faux_list_next_node(iter);
  291. kcontext_t *context = (kcontext_t *)faux_list_data(iter);
  292. // The first context is a context of main command. The other
  293. // contexts are contexts of filters. So save current path from
  294. // first context.
  295. if (iter == faux_list_head(exec->contexts)) {
  296. ksession_t *session = kcontext_session(context);
  297. if (session && ksession_path(session))
  298. exec->saved_path = kpath_clone(
  299. ksession_path(session));
  300. }
  301. // Set the same STDERR to all contexts
  302. kcontext_set_stderr(context, global_stderr);
  303. // Create pipes beetween processes
  304. if (next) {
  305. kcontext_t *next_context = (kcontext_t *)faux_list_data(next);
  306. if (pipe(pipefd) < 0)
  307. return BOOL_FALSE;
  308. kcontext_set_stdout(context, pipefd[1]); // Write end
  309. kcontext_set_stdin(next_context, pipefd[0]); // Read end
  310. }
  311. }
  312. return BOOL_TRUE;
  313. }
  314. // === SYNC symbol execution
  315. // The function will be executed right here. It's necessary for
  316. // navigation implementation for example. To grab function output the
  317. // service process will be forked. It gets output and stores it to the
  318. // internal buffer. After sym function return grabber will write
  319. // buffered data back. So grabber will simulate async sym execution.
  320. static bool_t exec_action_sync(kcontext_t *context, const kaction_t *action,
  321. pid_t *pid, int *retcode)
  322. {
  323. ksym_fn fn = NULL;
  324. int exitcode = 0;
  325. pid_t child_pid = -1;
  326. int pipe_stdout[2] = {};
  327. int pipe_stderr[2] = {};
  328. // Create pipes beetween sym function and grabber
  329. if (pipe(pipe_stdout) < 0)
  330. return BOOL_FALSE;
  331. if (pipe(pipe_stderr) < 0) {
  332. close(pipe_stdout[0]);
  333. close(pipe_stdout[1]);
  334. return BOOL_FALSE;
  335. }
  336. fn = ksym_function(kaction_sym(action));
  337. // Prepare streams before fork
  338. fflush(stdout);
  339. fflush(stderr);
  340. // Fork the grabber
  341. child_pid = fork();
  342. if (child_pid == -1) {
  343. close(pipe_stdout[0]);
  344. close(pipe_stdout[1]);
  345. close(pipe_stderr[0]);
  346. close(pipe_stderr[1]);
  347. return BOOL_FALSE;
  348. }
  349. // Parent
  350. if (child_pid != 0) {
  351. int saved_stdout = -1;
  352. int saved_stderr = -1;
  353. // Save pid of grabber
  354. if (pid)
  355. *pid = child_pid;
  356. // Temporarily replace orig output streams by pipe
  357. // stdout
  358. saved_stdout = dup(STDOUT_FILENO);
  359. dup2(pipe_stdout[1], STDOUT_FILENO);
  360. close(pipe_stdout[0]);
  361. close(pipe_stdout[1]);
  362. // stderr
  363. saved_stderr = dup(STDERR_FILENO);
  364. dup2(pipe_stderr[1], STDERR_FILENO);
  365. close(pipe_stderr[0]);
  366. close(pipe_stderr[1]);
  367. // Execute sym function right here
  368. exitcode = fn(context);
  369. if (retcode)
  370. *retcode = exitcode;
  371. // Restore orig output streams
  372. // stdout
  373. fflush(stdout);
  374. dup2(saved_stdout, STDOUT_FILENO);
  375. close(saved_stdout);
  376. // stderr
  377. fflush(stderr);
  378. dup2(saved_stderr, STDERR_FILENO);
  379. close(saved_stderr);
  380. return BOOL_TRUE;
  381. }
  382. // Child (Output grabber)
  383. close(pipe_stdout[1]);
  384. close(pipe_stderr[1]);
  385. {
  386. int fds[][2] = {
  387. {pipe_stdout[0], kcontext_stdout(context)},
  388. {pipe_stderr[0], kcontext_stderr(context)},
  389. {-1, -1},
  390. };
  391. grabber(fds);
  392. }
  393. close(pipe_stdout[0]);
  394. close(pipe_stderr[0]);
  395. _exit(0);
  396. return BOOL_TRUE;
  397. }
  398. // === ASYNC symbol execution
  399. // The process will be forked and sym will be executed there.
  400. // The parent will save forked process's pid and immediately return
  401. // control to event loop which will get forked process stdout and
  402. // wait for process termination.
  403. static bool_t exec_action_async(kcontext_t *context, const kaction_t *action,
  404. pid_t *pid)
  405. {
  406. ksym_fn fn = NULL;
  407. int exitcode = 0;
  408. pid_t child_pid = -1;
  409. int i = 0;
  410. int fdmax = 0;
  411. const char *pts_fname = NULL;
  412. sigset_t sigs = {};
  413. fn = ksym_function(kaction_sym(action));
  414. // Oh, it's amazing world of stdio!
  415. // Flush buffers before fork() because buffer content will be inherited
  416. // by child. Moreover dup2() can replace old stdout file descriptor by
  417. // the new one but buffer linked with stdout stream will remain the same.
  418. // It must be empty.
  419. fflush(stdout);
  420. fflush(stderr);
  421. child_pid = fork();
  422. if (child_pid == -1)
  423. return BOOL_FALSE;
  424. // Parent
  425. // Save the child pid and return control. Later event loop will wait
  426. // for saved pid.
  427. if (child_pid != 0) {
  428. if (pid)
  429. *pid = child_pid;
  430. return BOOL_TRUE;
  431. }
  432. // Child
  433. // Unblock signals
  434. sigemptyset(&sigs);
  435. sigprocmask(SIG_SETMASK, &sigs, NULL);
  436. if ((pts_fname = kcontext_pts_fname(context)) != NULL) {
  437. int fd = -1;
  438. setsid();
  439. fd = open(pts_fname, O_RDWR, 0);
  440. if (fd < 0)
  441. _exit(-1);
  442. dup2(fd, STDIN_FILENO);
  443. dup2(fd, STDOUT_FILENO);
  444. dup2(fd, STDERR_FILENO);
  445. } else {
  446. dup2(kcontext_stdin(context), STDIN_FILENO);
  447. dup2(kcontext_stdout(context), STDOUT_FILENO);
  448. dup2(kcontext_stderr(context), STDERR_FILENO);
  449. }
  450. // Close all inherited fds except stdin, stdout, stderr
  451. fdmax = (int)sysconf(_SC_OPEN_MAX);
  452. for (i = (STDERR_FILENO + 1); i < fdmax; i++)
  453. close(i);
  454. exitcode = fn(context);
  455. // We will use _exit() later so stdio streams will remain unflushed.
  456. // Some output data can be lost. Flush necessary streams here.
  457. fflush(stdout);
  458. fflush(stderr);
  459. // Use _exit() but not exit() to don't flush all the stdio streams. It
  460. // can be dangerous because parent can have a lot of streams inhereted
  461. // by child process.
  462. _exit(exitcode);
  463. return BOOL_TRUE;
  464. }
  465. static bool_t exec_action(kcontext_t *context, const kaction_t *action,
  466. pid_t *pid, int *retcode)
  467. {
  468. assert(context);
  469. if (!context)
  470. return BOOL_FALSE;
  471. assert(action);
  472. if (!action)
  473. return BOOL_FALSE;
  474. if (kaction_is_sync(action))
  475. return exec_action_sync(context, action, pid, retcode);
  476. return exec_action_async(context, action, pid);
  477. }
  478. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  479. pid_t pid, int wstatus)
  480. {
  481. faux_list_node_t *iter = NULL;
  482. int exitstatus = WEXITSTATUS(wstatus);
  483. pid_t new_pid = -1; // PID of newly forked ACTION process
  484. assert(context);
  485. if (!context)
  486. return BOOL_FALSE;
  487. // There is two reasons to don't start any real actions.
  488. // - The ACTION sequence is already done;
  489. // - Passed PID (PID of completed process) is not owned by this context.
  490. // Returns false that indicates this PID is not mine.
  491. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  492. return BOOL_FALSE;
  493. // Here we know that given PID is our PID
  494. iter = kcontext_action_iter(context); // Get saved current ACTION
  495. // ASYNC: Compute new value for retcode.
  496. // Here iter is a pointer to previous action but not new.
  497. // It's for async actions only. Sync actions will change global
  498. // retcode after the exec_action() invocation.
  499. if (iter) {
  500. const kaction_t *terminated_action = faux_list_data(iter);
  501. assert(terminated_action);
  502. if (!kaction_is_sync(terminated_action) &&
  503. kaction_update_retcode(terminated_action))
  504. kcontext_set_retcode(context, exitstatus);
  505. }
  506. // Loop is needed because some ACTIONs will be skipped due to specified
  507. // execution conditions. So try next actions.
  508. do {
  509. const kaction_t *action = NULL;
  510. bool_t is_sync = BOOL_FALSE;
  511. // Get next ACTION from sequence
  512. if (!iter) { // Is it the first ACTION within list
  513. faux_list_t *actions =
  514. kentry_actions(kpargv_command(kcontext_pargv(context)));
  515. assert(actions);
  516. iter = faux_list_head(actions);
  517. } else {
  518. iter = faux_list_next_node(iter);
  519. }
  520. kcontext_set_action_iter(context, iter);
  521. // Is it end of ACTION sequence?
  522. if (!iter) {
  523. kcontext_set_done(context, BOOL_TRUE);
  524. return BOOL_TRUE;
  525. }
  526. // Get new ACTION to execute
  527. action = (const kaction_t *)faux_list_data(iter);
  528. assert(action);
  529. // Check for previous retcode to find out if next command must
  530. // be executed or skipped.
  531. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  532. continue; // Skip action, try next one
  533. // Check for dry-run flag and 'permanent' feature of ACTION.
  534. if (kexec_dry_run(exec) && !kaction_is_permanent(action)) {
  535. is_sync = BOOL_TRUE; // Simulate sync action
  536. exitstatus = 0; // Exit status while dry-run is always 0
  537. } else { // Normal execution
  538. is_sync = kaction_is_sync(action);
  539. exec_action(context, action, &new_pid, &exitstatus);
  540. }
  541. // SYNC: Compute new value for retcode.
  542. // Sync actions return retcode immediatelly. Their forked
  543. // processes are for output handling only.
  544. if (is_sync && kaction_update_retcode(action))
  545. kcontext_set_retcode(context, exitstatus);
  546. } while (-1 == new_pid); // PID is not -1 when new process was forked
  547. // Save PID of newly created process
  548. kcontext_set_pid(context, new_pid);
  549. return BOOL_TRUE;
  550. }
  551. bool_t kexec_continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  552. {
  553. faux_list_node_t *iter = NULL;
  554. kcontext_t *context = NULL;
  555. assert(exec);
  556. if (!exec)
  557. return BOOL_FALSE;
  558. iter = kexec_contexts_iter(exec);
  559. while ((context = kexec_contexts_each(&iter))) {
  560. bool_t found = BOOL_FALSE;
  561. found = exec_action_sequence(exec, context, pid, wstatus);
  562. if (found && (pid != -1))
  563. break;
  564. }
  565. return BOOL_TRUE;
  566. }
  567. bool_t kexec_exec(kexec_t *exec)
  568. {
  569. kcontext_t *context = NULL;
  570. const kpargv_t *pargv = NULL;
  571. const kentry_t *entry = NULL;
  572. bool_t restore = BOOL_FALSE;
  573. assert(exec);
  574. if (!exec)
  575. return BOOL_FALSE;
  576. // Firsly prepare kexec object for execution. The file streams must
  577. // be created for stdin, stdout, stderr of processes.
  578. if (!kexec_prepare(exec))
  579. return BOOL_FALSE;
  580. // Pre-change VIEW if command has "restore" flag. Only first command in
  581. // line (if many commands are piped) matters. Filters can't change the
  582. // VIEW.
  583. context = (kcontext_t *)faux_list_data(faux_list_head(exec->contexts));
  584. pargv = kcontext_pargv(context);
  585. entry = kpargv_command(pargv);
  586. if (entry)
  587. restore = kentry_restore(entry);
  588. if (restore) {
  589. size_t level = kpargv_level(pargv);
  590. kpath_t *path = ksession_path(kcontext_session(context));
  591. while(kpath_len(path) > (level + 1))
  592. kpath_pop(path);
  593. }
  594. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  595. // ACTION's process.
  596. kexec_continue_command_execution(exec, -1, 0);
  597. return BOOL_TRUE;
  598. }
  599. bool_t kexec_interactive(const kexec_t *exec)
  600. {
  601. faux_list_node_t *node = NULL;
  602. kcontext_t *context = NULL;
  603. const kentry_t *entry = NULL;
  604. assert(exec);
  605. if (!exec)
  606. return BOOL_FALSE;
  607. // Only the ACTION of first context can be interactive
  608. node = faux_list_head(exec->contexts);
  609. if (!node)
  610. return BOOL_FALSE;
  611. context = (kcontext_t *)faux_list_data(node);
  612. if (!context)
  613. return BOOL_FALSE;
  614. entry = kcontext_command(context);
  615. if (!entry)
  616. return BOOL_FALSE;
  617. return kentry_interactive(entry);
  618. }