kexec.c 19 KB

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