kexec.c 21 KB

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