kexec.c 20 KB

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