kexec.c 18 KB

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