kexec.c 20 KB

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