kexec.c 20 KB

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