kexec.c 21 KB

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