kexec.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885
  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. ksym_t *sym = NULL;
  381. sym = kaction_sym(action);
  382. fn = ksym_function(kaction_sym(action));
  383. // Execute silent sync function and continue
  384. // Only last in pipeline stage can be silent because last stage
  385. // has bufout
  386. if (ksym_silent(sym) && kcontext_is_last_pipeline_stage(context)) {
  387. exitcode = fn(context);
  388. if (retcode)
  389. *retcode = exitcode;
  390. return BOOL_TRUE;
  391. }
  392. // Create pipes beetween sym function and grabber
  393. if (pipe(pipe_stdout) < 0)
  394. return BOOL_FALSE;
  395. if (pipe(pipe_stderr) < 0) {
  396. close(pipe_stdout[0]);
  397. close(pipe_stdout[1]);
  398. return BOOL_FALSE;
  399. }
  400. // Prepare streams before fork
  401. fflush(stdout);
  402. fflush(stderr);
  403. // Fork the grabber
  404. child_pid = fork();
  405. if (child_pid == -1) {
  406. close(pipe_stdout[0]);
  407. close(pipe_stdout[1]);
  408. close(pipe_stderr[0]);
  409. close(pipe_stderr[1]);
  410. return BOOL_FALSE;
  411. }
  412. // Parent
  413. if (child_pid != 0) {
  414. int saved_stdout = -1;
  415. int saved_stderr = -1;
  416. // Save pid of grabber
  417. if (pid)
  418. *pid = child_pid;
  419. // Temporarily replace orig output streams by pipe
  420. // stdout
  421. saved_stdout = dup(STDOUT_FILENO);
  422. dup2(pipe_stdout[1], STDOUT_FILENO);
  423. close(pipe_stdout[0]);
  424. close(pipe_stdout[1]);
  425. // stderr
  426. saved_stderr = dup(STDERR_FILENO);
  427. dup2(pipe_stderr[1], STDERR_FILENO);
  428. close(pipe_stderr[0]);
  429. close(pipe_stderr[1]);
  430. // Execute sym function right here
  431. exitcode = fn(context);
  432. if (retcode)
  433. *retcode = exitcode;
  434. // Restore orig output streams
  435. // stdout
  436. fflush(stdout);
  437. close(STDOUT_FILENO);
  438. dup2(saved_stdout, STDOUT_FILENO);
  439. close(saved_stdout);
  440. // stderr
  441. fflush(stderr);
  442. close(STDERR_FILENO);
  443. dup2(saved_stderr, STDERR_FILENO);
  444. close(saved_stderr);
  445. return BOOL_TRUE;
  446. // Child (Output grabber)
  447. } else {
  448. int fds[][2] = {
  449. {pipe_stdout[0], kcontext_stdout(context)},
  450. {pipe_stderr[0], kcontext_stderr(context)},
  451. {-1, -1},
  452. };
  453. grabber(fds); // Grabber will not return
  454. }
  455. exec = exec; // Happy compiler
  456. return BOOL_TRUE;
  457. }
  458. // === ASYNC symbol execution
  459. // The process will be forked and sym will be executed there.
  460. // The parent will save forked process's pid and immediately return
  461. // control to event loop which will get forked process stdout and
  462. // wait for process termination.
  463. static bool_t exec_action_async(const kexec_t *exec, kcontext_t *context,
  464. const kaction_t *action, pid_t *pid)
  465. {
  466. ksym_fn fn = NULL;
  467. int exitcode = 0;
  468. pid_t child_pid = -1;
  469. int i = 0;
  470. int fdmax = 0;
  471. sigset_t sigs;
  472. fn = ksym_function(kaction_sym(action));
  473. // Oh, it's amazing world of stdio!
  474. // Flush buffers before fork() because buffer content will be inherited
  475. // by child. Moreover dup2() can replace old stdout file descriptor by
  476. // the new one but buffer linked with stdout stream will remain the same.
  477. // It must be empty.
  478. fflush(stdout);
  479. fflush(stderr);
  480. child_pid = fork();
  481. if (child_pid == -1)
  482. return BOOL_FALSE;
  483. // Parent
  484. // Save the child pid and return control. Later event loop will wait
  485. // for saved pid.
  486. if (child_pid != 0) {
  487. if (pid)
  488. *pid = child_pid;
  489. return BOOL_TRUE;
  490. }
  491. // Child
  492. // Unblock signals
  493. sigemptyset(&sigs);
  494. sigprocmask(SIG_SETMASK, &sigs, NULL);
  495. // Reopen streams if the pseudoterminal is used.
  496. // It's necessary to set session terminal
  497. if (exec->pts_fname != NULL) {
  498. int fd = -1;
  499. setsid();
  500. fd = open(exec->pts_fname, O_RDWR, 0);
  501. if (fd < 0)
  502. _exit(-1);
  503. if (isatty(kcontext_stdin(context)))
  504. kcontext_set_stdin(context, fd);
  505. if (isatty(kcontext_stdout(context)))
  506. kcontext_set_stdout(context, fd);
  507. if (isatty(kcontext_stderr(context)))
  508. kcontext_set_stderr(context, fd);
  509. }
  510. dup2(kcontext_stdin(context), STDIN_FILENO);
  511. dup2(kcontext_stdout(context), STDOUT_FILENO);
  512. dup2(kcontext_stderr(context), STDERR_FILENO);
  513. // Close all inherited fds except stdin, stdout, stderr
  514. fdmax = (int)sysconf(_SC_OPEN_MAX);
  515. for (i = (STDERR_FILENO + 1); i < fdmax; i++)
  516. close(i);
  517. exitcode = fn(context);
  518. // We will use _exit() later so stdio streams will remain unflushed.
  519. // Some output data can be lost. Flush necessary streams here.
  520. fflush(stdout);
  521. fflush(stderr);
  522. // Use _exit() but not exit() to don't flush all the stdio streams. It
  523. // can be dangerous because parent can have a lot of streams inhereted
  524. // by child process.
  525. _exit(exitcode);
  526. return BOOL_TRUE;
  527. }
  528. static bool_t exec_action(const kexec_t *exec, kcontext_t *context,
  529. const kaction_t *action, pid_t *pid, int *retcode)
  530. {
  531. bool_t rc = BOOL_FALSE;
  532. assert(context);
  533. if (!context)
  534. return BOOL_FALSE;
  535. assert(action);
  536. if (!action)
  537. return BOOL_FALSE;
  538. if (kaction_is_sync(action))
  539. rc = exec_action_sync(exec, context, action, pid, retcode);
  540. else
  541. rc = exec_action_async(exec, context, action, pid);
  542. return rc;
  543. }
  544. static bool_t exec_action_sequence(const kexec_t *exec, kcontext_t *context,
  545. pid_t pid, int wstatus)
  546. {
  547. faux_list_node_t *iter = NULL;
  548. int exitstatus = WEXITSTATUS(wstatus);
  549. pid_t new_pid = -1; // PID of newly forked ACTION process
  550. assert(context);
  551. if (!context)
  552. return BOOL_FALSE;
  553. // There is two reasons to don't start any real actions.
  554. // - The ACTION sequence is already done;
  555. // - Passed PID (PID of completed process) is not owned by this context.
  556. // Returns false that indicates this PID is not mine.
  557. if (kcontext_done(context) || (kcontext_pid(context) != pid))
  558. return BOOL_FALSE;
  559. // Here we know that given PID is our PID
  560. iter = kcontext_action_iter(context); // Get saved current ACTION
  561. // ASYNC: Compute new value for retcode.
  562. // Here iter is a pointer to previous action but not new.
  563. // It's for async actions only. Sync actions will change global
  564. // retcode after the exec_action() invocation.
  565. if (iter) {
  566. const kaction_t *terminated_action = faux_list_data(iter);
  567. assert(terminated_action);
  568. if (!kaction_is_sync(terminated_action) &&
  569. kaction_update_retcode(terminated_action))
  570. kcontext_set_retcode(context, exitstatus);
  571. }
  572. // Loop is needed because some ACTIONs will be skipped due to specified
  573. // execution conditions. So try next actions.
  574. do {
  575. const kaction_t *action = NULL;
  576. bool_t is_sync = BOOL_FALSE;
  577. // Get next ACTION from sequence
  578. if (!iter) { // Is it the first ACTION within list
  579. faux_list_t *actions =
  580. kentry_actions(kpargv_command(kcontext_pargv(context)));
  581. assert(actions);
  582. iter = faux_list_head(actions);
  583. } else {
  584. iter = faux_list_next_node(iter);
  585. }
  586. kcontext_set_action_iter(context, iter);
  587. // Is it end of ACTION sequence?
  588. if (!iter) {
  589. kcontext_set_done(context, BOOL_TRUE);
  590. // Close the stdout of finished ACTION sequence to inform
  591. // process next in pipe about EOF. Else filter will not
  592. // stop at all.
  593. close(kcontext_stdout(context));
  594. kcontext_set_stdout(context, -1);
  595. return BOOL_TRUE;
  596. }
  597. // Get new ACTION to execute
  598. action = (const kaction_t *)faux_list_data(iter);
  599. assert(action);
  600. // Check for previous retcode to find out if next command must
  601. // be executed or skipped.
  602. if (!kaction_meet_exec_conditions(action, kcontext_retcode(context)))
  603. continue; // Skip action, try next one
  604. // Check for dry-run flag and 'permanent' feature of ACTION.
  605. if (kexec_dry_run(exec) && !kaction_is_permanent(action)) {
  606. is_sync = BOOL_TRUE; // Simulate sync action
  607. exitstatus = 0; // Exit status while dry-run is always 0
  608. } else { // Normal execution
  609. is_sync = kaction_is_sync(action);
  610. exec_action(exec, context, action, &new_pid, &exitstatus);
  611. }
  612. // SYNC: Compute new value for retcode.
  613. // Sync actions return retcode immediatelly. Their forked
  614. // processes are for output handling only.
  615. if (is_sync && kaction_update_retcode(action))
  616. kcontext_set_retcode(context, exitstatus);
  617. } while (-1 == new_pid); // PID is not -1 when new process was forked
  618. // Save PID of newly created process
  619. kcontext_set_pid(context, new_pid);
  620. return BOOL_TRUE;
  621. }
  622. bool_t kexec_continue_command_execution(kexec_t *exec, pid_t pid, int wstatus)
  623. {
  624. faux_list_node_t *iter = NULL;
  625. kcontext_t *context = NULL;
  626. assert(exec);
  627. if (!exec)
  628. return BOOL_FALSE;
  629. iter = kexec_contexts_iter(exec);
  630. while ((context = kexec_contexts_each(&iter))) {
  631. bool_t found = BOOL_FALSE;
  632. found = exec_action_sequence(exec, context, pid, wstatus);
  633. if (found && (pid != -1))
  634. break;
  635. }
  636. return BOOL_TRUE;
  637. }
  638. bool_t kexec_exec(kexec_t *exec)
  639. {
  640. kcontext_t *context = NULL;
  641. const kpargv_t *pargv = NULL;
  642. const kentry_t *entry = NULL;
  643. bool_t restore = BOOL_FALSE;
  644. assert(exec);
  645. if (!exec)
  646. return BOOL_FALSE;
  647. // Firsly prepare kexec object for execution. The file streams must
  648. // be created for stdin, stdout, stderr of processes.
  649. if (!kexec_prepare(exec))
  650. return BOOL_FALSE;
  651. // Pre-change VIEW if command has "restore" flag. Only first command in
  652. // line (if many commands are piped) matters. Filters can't change the
  653. // VIEW.
  654. context = (kcontext_t *)faux_list_data(faux_list_head(exec->contexts));
  655. pargv = kcontext_pargv(context);
  656. entry = kpargv_command(pargv);
  657. if (entry)
  658. restore = kentry_restore(entry);
  659. if (restore) {
  660. size_t level = kpargv_level(pargv);
  661. kpath_t *path = ksession_path(kcontext_session(context));
  662. while(kpath_len(path) > (level + 1))
  663. kpath_pop(path);
  664. }
  665. // Here no ACTIONs are executing, so pass -1 as pid of terminated
  666. // ACTION's process.
  667. kexec_continue_command_execution(exec, -1, 0);
  668. return BOOL_TRUE;
  669. }
  670. // If some kexec's kentry has tty as "out" then consider kexec as interactive
  671. bool_t kexec_interactive(const kexec_t *exec)
  672. {
  673. faux_list_node_t *iter = NULL;
  674. kcontext_t *context = NULL;
  675. assert(exec);
  676. if (!exec)
  677. return BOOL_FALSE;
  678. iter = kexec_contexts_iter(exec);
  679. while ((context = kexec_contexts_each(&iter))) {
  680. const kentry_t *entry = kcontext_command(context);
  681. if (!entry)
  682. return BOOL_FALSE;
  683. if (kentry_out(entry) == KACTION_IO_TTY)
  684. return BOOL_TRUE;
  685. }
  686. return BOOL_FALSE;
  687. }
  688. // If some kexec's kentry has tty as "in" then consider kexec as need_stdin.
  689. // The first kentry with "in=true" also does kexec need_stdin
  690. bool_t kexec_need_stdin(const kexec_t *exec)
  691. {
  692. faux_list_node_t *iter = NULL;
  693. size_t num = 0;
  694. kcontext_t *context = NULL;
  695. assert(exec);
  696. if (!exec)
  697. return BOOL_FALSE;
  698. iter = kexec_contexts_iter(exec);
  699. while ((context = kexec_contexts_each(&iter))) {
  700. const kentry_t *entry = kcontext_command(context);
  701. if (!entry)
  702. return BOOL_FALSE;
  703. // Check first command within pipeline
  704. if (num == 0) {
  705. if (kentry_in(entry) == KACTION_IO_TRUE)
  706. return BOOL_TRUE;
  707. }
  708. num++;
  709. if (kentry_in(entry) == KACTION_IO_TTY)
  710. return BOOL_TRUE;
  711. }
  712. return BOOL_FALSE;
  713. }
  714. const kaction_t *kexec_current_action(const kexec_t *exec)
  715. {
  716. kcontext_t *context = NULL;
  717. assert(exec);
  718. if (!exec)
  719. return NULL;
  720. context = (kcontext_t *)faux_list_data(faux_list_head(exec->contexts));
  721. if (!context)
  722. return NULL;
  723. return kcontext_action(context);
  724. }