ksession_parse.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750
  1. /** @file ksession_parse.c
  2. */
  3. #include <assert.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #include <signal.h>
  8. #include <sys/types.h>
  9. #include <sys/wait.h>
  10. #include <unistd.h>
  11. #include <faux/eloop.h>
  12. #include <faux/buf.h>
  13. #include <faux/list.h>
  14. #include <faux/argv.h>
  15. #include <faux/error.h>
  16. #include <klish/khelper.h>
  17. #include <klish/kview.h>
  18. #include <klish/kscheme.h>
  19. #include <klish/kpath.h>
  20. #include <klish/kpargv.h>
  21. #include <klish/kexec.h>
  22. #include <klish/ksession.h>
  23. #include <klish/ksession_parse.h>
  24. static bool_t ksession_validate_arg(ksession_t *session, kpargv_t *pargv)
  25. {
  26. const char *out = NULL;
  27. int retcode = -1;
  28. const kentry_t *ptype_entry = NULL;
  29. kparg_t *candidate = NULL;
  30. assert(session);
  31. if (!session)
  32. return BOOL_FALSE;
  33. assert(pargv);
  34. if (!pargv)
  35. return BOOL_FALSE;
  36. candidate = kpargv_candidate_parg(pargv);
  37. if (!candidate)
  38. return BOOL_FALSE;
  39. ptype_entry = kentry_nested_by_purpose(kparg_entry(candidate),
  40. KENTRY_PURPOSE_PTYPE);
  41. if (!ptype_entry)
  42. return BOOL_FALSE;
  43. if (!ksession_exec_locally(session, ptype_entry, pargv,
  44. &retcode, &out)) {
  45. return BOOL_FALSE;
  46. }
  47. if (retcode != 0)
  48. return BOOL_FALSE;
  49. if (!faux_str_is_empty(out))
  50. kparg_set_value(candidate, out);
  51. return BOOL_TRUE;
  52. }
  53. static kpargv_status_e ksession_parse_arg(ksession_t *session,
  54. const kentry_t *current_entry, faux_argv_node_t **argv_iter,
  55. kpargv_t *pargv, bool_t entry_is_command)
  56. {
  57. const kentry_t *entry = current_entry;
  58. kentry_mode_e mode = KENTRY_MODE_NONE;
  59. kpargv_status_e retcode = KPARSE_INPROGRESS; // For ENTRY itself
  60. kpargv_status_e rc = KPARSE_NOTFOUND; // For nested ENTRYs
  61. faux_argv_node_t *saved_argv_iter = NULL;
  62. kpargv_purpose_e purpose = KPURPOSE_NONE;
  63. //fprintf(stderr, "PARSE: name=%s, ref=%s, arg=%s\n",
  64. //kentry_name(entry), kentry_ref_str(entry), faux_argv_current(*argv_iter));
  65. assert(current_entry);
  66. if (!current_entry)
  67. return KPARSE_ERROR;
  68. assert(argv_iter);
  69. if (!argv_iter)
  70. return KPARSE_ERROR;
  71. assert(pargv);
  72. if (!pargv)
  73. return KPARSE_ERROR;
  74. purpose = kpargv_purpose(pargv); // Purpose of parsing
  75. // If we know the entry is a command then don't validate it. This
  76. // behaviour is usefull for special purpose entries like PTYPEs, CONDs,
  77. // etc. These entries are the starting point for parsing their args.
  78. // We don't need to parse command itself. Command is predefined.
  79. if (entry_is_command) {
  80. kparg_t *parg = NULL;
  81. // Command is an ENTRY with ACTIONs
  82. if (kentry_actions_len(entry) <= 0)
  83. return KPARSE_ILLEGAL;
  84. parg = kparg_new(entry, NULL);
  85. kpargv_add_pargs(pargv, parg);
  86. kpargv_set_command(pargv, entry);
  87. retcode = KPARSE_INPROGRESS;
  88. // Is entry candidate to resolve current arg?
  89. // Container can't be a candidate.
  90. } else if (!kentry_container(entry)) {
  91. const char *current_arg = NULL;
  92. kparg_t *parg = NULL;
  93. // When purpose is COMPLETION or HELP then fill completion list.
  94. // Additionally if it's last continuable argument then lie to
  95. // engine: make all last arguments NOTFOUND. It's necessary to walk
  96. // through all variants to gether all completions.
  97. if ((KPURPOSE_COMPLETION == purpose) ||
  98. (KPURPOSE_HELP == purpose)) {
  99. if (!*argv_iter) {
  100. // That's time to add entry to completions list.
  101. if (!kpargv_continuable(pargv))
  102. kpargv_add_completions(pargv, entry);
  103. return KPARSE_INCOMPLETED;
  104. } else {
  105. // Add entry to completions if it's last incompleted arg.
  106. if (faux_argv_is_last(*argv_iter) &&
  107. kpargv_continuable(pargv)) {
  108. kpargv_add_completions(pargv, entry);
  109. return KPARSE_NOTFOUND;
  110. }
  111. }
  112. }
  113. // If all arguments are resolved already then return INCOMPLETED
  114. if (!*argv_iter)
  115. return KPARSE_INCOMPLETED;
  116. // Validate argument
  117. current_arg = faux_argv_current(*argv_iter);
  118. parg = kparg_new(entry, current_arg);
  119. kpargv_set_candidate_parg(pargv, parg);
  120. if (ksession_validate_arg(session, pargv)) {
  121. kpargv_accept_candidate_parg(pargv);
  122. // Command is an ENTRY with ACTIONs or NAVigation
  123. if (kentry_actions_len(entry) > 0)
  124. kpargv_set_command(pargv, entry);
  125. faux_argv_each(argv_iter); // Next argument
  126. retcode = KPARSE_INPROGRESS;
  127. } else {
  128. // It's not a container and is not validated so
  129. // no chance to find anything here.
  130. kpargv_decline_candidate_parg(pargv);
  131. kparg_free(parg);
  132. return KPARSE_NOTFOUND;
  133. }
  134. }
  135. // ENTRY has no nested ENTRYs so return
  136. if (kentry_entrys_is_empty(entry))
  137. return retcode;
  138. // Walk through the nested entries:
  139. saved_argv_iter = *argv_iter;
  140. // EMPTY mode
  141. mode = kentry_mode(entry);
  142. if (KENTRY_MODE_EMPTY == mode)
  143. return retcode;
  144. // Following code (SWITCH or SEQUENCE cases) sometimes don's set rc.
  145. // It happens when entry has nested entries but purposes of all entries
  146. // are not COMMON so they will be ignored. So return code of function
  147. // will be the code of ENTRY itself processing.
  148. rc = retcode;
  149. // SWITCH mode
  150. // Entries within SWITCH can't has 'min'/'max' else than 1.
  151. // So these attributes will be ignored. Note SWITCH itself can have
  152. // 'min'/'max'.
  153. if (KENTRY_MODE_SWITCH == mode) {
  154. kentry_entrys_node_t *iter = kentry_entrys_iter(entry);
  155. const kentry_t *nested = NULL;
  156. while ((nested = kentry_entrys_each(&iter))) {
  157. //printf("SWITCH arg: %s, entry %s\n", *argv_iter ? faux_argv_current(*argv_iter) : "<empty>", kentry_name(nested));
  158. // Ignore entries with non-COMMON purpose.
  159. if (kentry_purpose(nested) != KENTRY_PURPOSE_COMMON)
  160. continue;
  161. rc = ksession_parse_arg(session, nested, argv_iter,
  162. pargv, BOOL_FALSE);
  163. //printf("%s\n", kpargv_status_decode(rc));
  164. // If some arguments was consumed then we will not check
  165. // next SWITCH's entries in any case.
  166. if (saved_argv_iter != *argv_iter)
  167. break;
  168. // Try next entries if current status is NOTFOUND.
  169. // The INCOMPLETED status is for completion list. In this
  170. // case all next statuses will be INCOMPLETED too.
  171. if ((rc != KPARSE_NOTFOUND) && (rc != KPARSE_INCOMPLETED))
  172. break;
  173. }
  174. // SEQUENCE mode
  175. } else if (KENTRY_MODE_SEQUENCE == mode) {
  176. kentry_entrys_node_t *iter = kentry_entrys_iter(entry);
  177. kentry_entrys_node_t *saved_iter = iter;
  178. const kentry_t *nested = NULL;
  179. while ((nested = kentry_entrys_each(&iter))) {
  180. kpargv_status_e nrc = KPARSE_NOTFOUND;
  181. size_t num = 0;
  182. size_t min = kentry_min(nested);
  183. //fprintf(stderr, "SEQ arg: %s, entry %s\n", *argv_iter ? faux_argv_current(*argv_iter) : "<empty>", kentry_name(nested));
  184. // Ignore entries with non-COMMON purpose.
  185. if (kentry_purpose(nested) != KENTRY_PURPOSE_COMMON)
  186. continue;
  187. // Filter out double parsing for optional entries.
  188. if (kpargv_entry_exists(pargv, nested))
  189. continue;
  190. // Try to match argument and current entry
  191. // (from 'min' to 'max' times)
  192. for (num = 0; num < kentry_max(nested); num++) {
  193. nrc = ksession_parse_arg(session, nested,
  194. argv_iter, pargv, BOOL_FALSE);
  195. //fprintf(stderr, "%s: %s\n", kentry_name(nested), kpargv_status_decode(nrc));
  196. if (nrc != KPARSE_INPROGRESS)
  197. break;
  198. }
  199. // All errors will break the loop
  200. if ((KPARSE_ERROR == nrc) ||
  201. (KPARSE_ILLEGAL == nrc) ||
  202. (KPARSE_NONE == nrc)) {
  203. rc = nrc;
  204. break;
  205. }
  206. // Not found necessary number of mandatory instances
  207. if (num < min) {
  208. if (KPARSE_INPROGRESS == nrc)
  209. rc = KPARSE_NOTFOUND;
  210. else
  211. rc = nrc; // NOTFOUND or INCOMPLETED
  212. break;
  213. }
  214. // It's not an error if optional parameter is absend
  215. rc = KPARSE_INPROGRESS;
  216. // Mandatory or ordered parameter
  217. if ((min > 0) || kentry_order(nested))
  218. saved_iter = iter;
  219. // If optional entry is found then go back to nearest
  220. // non-optional (or ordered) entry to try to find
  221. // another optional entries.
  222. if ((0 == min) && (num > 0))
  223. iter = saved_iter;
  224. }
  225. }
  226. // If nested result is NOTFOUND but argument was consumed
  227. // within nested entries or by entry itself then whole sequence
  228. // is ILLEGAL.
  229. if ((KPARSE_NOTFOUND == rc) &&
  230. ((saved_argv_iter != *argv_iter) || !kentry_container(entry)))
  231. rc = KPARSE_ILLEGAL;
  232. return rc;
  233. }
  234. kpargv_t *ksession_parse_line(ksession_t *session, const faux_argv_t *argv,
  235. kpargv_purpose_e purpose)
  236. {
  237. faux_argv_node_t *argv_iter = NULL;
  238. kpargv_t *pargv = NULL;
  239. kpargv_status_e pstatus = KPARSE_NONE;
  240. kpath_levels_node_t *levels_iterr = NULL;
  241. klevel_t *level = NULL;
  242. size_t level_found = 0; // Level where command was found
  243. kpath_t *path = NULL;
  244. assert(session);
  245. if (!session)
  246. return NULL;
  247. assert(argv);
  248. if (!argv)
  249. return NULL;
  250. argv_iter = faux_argv_iter(argv);
  251. // Initialize kpargv_t
  252. pargv = kpargv_new();
  253. assert(pargv);
  254. kpargv_set_continuable(pargv, faux_argv_is_continuable(argv));
  255. kpargv_set_purpose(pargv, purpose);
  256. // Iterate levels of path from higher to lower. Note the reversed
  257. // iterator will be used.
  258. path = ksession_path(session);
  259. levels_iterr = kpath_iterr(path);
  260. level_found = kpath_len(path) - 1; // Levels begin with '0'
  261. while ((level = kpath_eachr(&levels_iterr))) {
  262. const kentry_t *current_entry = klevel_entry(level);
  263. // Ignore entries with non-COMMON purpose. These entries are for
  264. // special processing and will be ignored here.
  265. if (kentry_purpose(current_entry) != KENTRY_PURPOSE_COMMON)
  266. continue;
  267. // Parsing
  268. pstatus = ksession_parse_arg(session, current_entry, &argv_iter,
  269. pargv, BOOL_FALSE);
  270. if (pstatus != KPARSE_NOTFOUND)
  271. break;
  272. // NOTFOUND but some args were parsed.
  273. // When it's completion for first argument (that can be continued)
  274. // len == 0 and engine will search for completions on higher
  275. // levels of path.
  276. if (kpargv_pargs_len(pargv) > 0)
  277. break;
  278. level_found--;
  279. }
  280. // Save last argument
  281. if (argv_iter)
  282. kpargv_set_last_arg(pargv, faux_argv_current(argv_iter));
  283. // It's a higher level of parsing, so some statuses can have different
  284. // meanings
  285. if (KPARSE_NONE == pstatus)
  286. pstatus = KPARSE_ERROR; // Strange case
  287. else if (KPARSE_INPROGRESS == pstatus) {
  288. if (NULL == argv_iter) // All args are parsed
  289. pstatus = KPARSE_OK;
  290. else
  291. pstatus = KPARSE_ILLEGAL; // Additional not parsable args
  292. } else if (KPARSE_NOTFOUND == pstatus)
  293. pstatus = KPARSE_ILLEGAL; // Unknown command
  294. // If no ACTIONs were found i.e. command was not found
  295. if ((KPARSE_OK == pstatus) && !kpargv_command(pargv))
  296. pstatus = KPARSE_NOACTION;
  297. kpargv_set_status(pargv, pstatus);
  298. kpargv_set_level(pargv, level_found);
  299. return pargv;
  300. }
  301. // Delimeter of commands is '|' (pipe)
  302. faux_list_t *ksession_split_pipes(const char *raw_line, faux_error_t *error)
  303. {
  304. faux_list_t *list = NULL;
  305. faux_argv_t *argv = NULL;
  306. faux_argv_node_t *argv_iter = NULL;
  307. faux_argv_t *cur_argv = NULL; // Current argv
  308. const char *delimeter = "|";
  309. const char *arg = NULL;
  310. assert(raw_line);
  311. if (!raw_line)
  312. return NULL;
  313. // Split raw line to arguments
  314. argv = faux_argv_new();
  315. assert(argv);
  316. if (!argv)
  317. return NULL;
  318. if (faux_argv_parse(argv, raw_line) < 0) {
  319. faux_argv_free(argv);
  320. return NULL;
  321. }
  322. list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_NONUNIQUE,
  323. NULL, NULL, (void (*)(void *))faux_argv_free);
  324. assert(list);
  325. if (!list) {
  326. faux_argv_free(argv);
  327. return NULL;
  328. }
  329. argv_iter = faux_argv_iter(argv);
  330. cur_argv = faux_argv_new();
  331. assert(cur_argv);
  332. while ((arg = faux_argv_each(&argv_iter))) {
  333. if (strcmp(arg, delimeter) == 0) {
  334. // End of current line (from "|" to "|")
  335. // '|' in a first position is an error
  336. if (faux_argv_len(cur_argv) == 0) {
  337. faux_argv_free(argv);
  338. faux_list_free(list);
  339. faux_error_sprintf(error, "The pipe '|' can't "
  340. "be at the first position");
  341. return NULL;
  342. }
  343. // Add argv to argv's list
  344. faux_list_add(list, cur_argv);
  345. cur_argv = faux_argv_new();
  346. assert(cur_argv);
  347. } else {
  348. faux_argv_add(cur_argv, arg);
  349. }
  350. }
  351. // Continuable flag is usefull for last argv
  352. faux_argv_set_continuable(cur_argv, faux_argv_is_continuable(argv));
  353. // Empty cur_argv is not an error. It's usefull for completion and help.
  354. // But empty cur_argv and continuable is abnormal.
  355. if ((faux_argv_len(cur_argv) == 0) &&
  356. faux_argv_is_continuable(cur_argv)) {
  357. faux_argv_free(argv);
  358. faux_list_free(list);
  359. faux_error_sprintf(error, "The pipe '|' can't "
  360. "be the last argument");
  361. return NULL;
  362. }
  363. faux_list_add(list, cur_argv);
  364. faux_argv_free(argv);
  365. return list;
  366. }
  367. // All components except last one must be legal for execution but last
  368. // component must be parsed for completion.
  369. // Completion is a "back-end" operation so it doesn't need detailed error
  370. // reporting.
  371. kpargv_t *ksession_parse_for_completion(ksession_t *session,
  372. const char *raw_line)
  373. {
  374. faux_list_t *split = NULL;
  375. faux_list_node_t *iter = NULL;
  376. kpargv_t *pargv = NULL;
  377. assert(session);
  378. if (!session)
  379. return NULL;
  380. assert(raw_line);
  381. if (!raw_line)
  382. return NULL;
  383. // Split raw line (with '|') to components
  384. split = ksession_split_pipes(raw_line, NULL);
  385. if (!split || (faux_list_len(split) < 1)) {
  386. faux_list_free(split);
  387. return NULL;
  388. }
  389. iter = faux_list_head(split);
  390. while (iter) {
  391. faux_argv_t *argv = (faux_argv_t *)faux_list_data(iter);
  392. if (iter == faux_list_tail(split)) { // Last item
  393. pargv = ksession_parse_line(session, argv,
  394. KPURPOSE_COMPLETION);
  395. if (!pargv) {
  396. faux_list_free(split);
  397. return NULL;
  398. }
  399. } else { // Non-last item
  400. pargv = ksession_parse_line(session, argv,
  401. KPURPOSE_EXEC);
  402. // All non-last components must be ready for execution
  403. if (!pargv || kpargv_status(pargv) != KPARSE_OK) {
  404. kpargv_free(pargv);
  405. faux_list_free(split);
  406. return NULL;
  407. }
  408. }
  409. iter = faux_list_next_node(iter);
  410. }
  411. faux_list_free(split);
  412. return pargv;
  413. }
  414. kexec_t *ksession_parse_for_exec(ksession_t *session, const char *raw_line,
  415. faux_error_t *error)
  416. {
  417. faux_list_t *split = NULL;
  418. faux_list_node_t *iter = NULL;
  419. kpargv_t *pargv = NULL;
  420. kexec_t *exec = NULL;
  421. assert(session);
  422. if (!session)
  423. return NULL;
  424. assert(raw_line);
  425. if (!raw_line)
  426. return NULL;
  427. // Split raw line (with '|') to components
  428. split = ksession_split_pipes(raw_line, error);
  429. if (!split || (faux_list_len(split) < 1)) {
  430. faux_list_free(split);
  431. return NULL;
  432. }
  433. // Create exec list
  434. exec = kexec_new();
  435. assert(exec);
  436. if (!exec) {
  437. faux_list_free(split);
  438. return NULL;
  439. }
  440. iter = faux_list_head(split);
  441. while (iter) {
  442. faux_argv_t *argv = (faux_argv_t *)faux_list_data(iter);
  443. kcontext_t *context = NULL;
  444. pargv = ksession_parse_line(session, argv, KPURPOSE_EXEC);
  445. // All components must be ready for execution
  446. if (!pargv) {
  447. faux_list_free(split);
  448. return NULL;
  449. }
  450. if (kpargv_status(pargv) != KPARSE_OK) {
  451. faux_error_sprintf(error, "%s",
  452. kpargv_status_str(pargv));
  453. kpargv_free(pargv);
  454. faux_list_free(split);
  455. return NULL;
  456. }
  457. // Only the first component can have 'restore=true' attribute
  458. if ((iter != faux_list_head(split)) &&
  459. kentry_restore(kpargv_command(pargv))) {
  460. faux_error_sprintf(error, "The command \"%s\" "
  461. "can't be destination of pipe",
  462. kentry_name(kpargv_command(pargv)));
  463. kpargv_free(pargv);
  464. faux_list_free(split);
  465. return NULL;
  466. }
  467. // Fill the kexec_t
  468. context = kcontext_new(KCONTEXT_ACTION);
  469. assert(context);
  470. kcontext_set_pargv(context, pargv);
  471. // Context for ACTION execution contains session
  472. kcontext_set_session(context, session);
  473. kexec_add_contexts(exec, context);
  474. // Next component
  475. iter = faux_list_next_node(iter);
  476. }
  477. faux_list_free(split);
  478. return exec;
  479. }
  480. kexec_t *ksession_parse_for_local_exec(ksession_t *session,
  481. const kentry_t *entry, const kpargv_t *parent_pargv)
  482. {
  483. faux_argv_node_t *argv_iter = NULL;
  484. kpargv_t *pargv = NULL;
  485. kexec_t *exec = NULL;
  486. faux_argv_t *argv = NULL;
  487. kcontext_t *context = NULL;
  488. kpargv_status_e pstatus = KPARSE_NONE;
  489. const char *line = NULL; // TODO: Must be 'line' field of ENTRY
  490. assert(entry);
  491. if (!entry)
  492. return NULL;
  493. exec = kexec_new();
  494. assert(exec);
  495. argv = faux_argv_new();
  496. assert(argv);
  497. faux_argv_parse(argv, line);
  498. argv_iter = faux_argv_iter(argv);
  499. pargv = kpargv_new();
  500. assert(pargv);
  501. kpargv_set_continuable(pargv, faux_argv_is_continuable(argv));
  502. kpargv_set_purpose(pargv, KPURPOSE_EXEC);
  503. pstatus = ksession_parse_arg(session, entry, &argv_iter, pargv,
  504. BOOL_TRUE);
  505. // Parsing problems
  506. if ((pstatus != KPARSE_INPROGRESS) || (argv_iter != NULL)) {
  507. kexec_free(exec);
  508. faux_argv_free(argv);
  509. kpargv_free(pargv);
  510. return NULL;
  511. }
  512. context = kcontext_new(KCONTEXT_SERVICE_ACTION);
  513. assert(context);
  514. kcontext_set_pargv(context, pargv);
  515. kcontext_set_parent_pargv(context, parent_pargv);
  516. // Service ACTIONs like PTYPE, CONDitions etc. doesn't need session
  517. // data within context. Else it will be able to change path.
  518. kexec_add_contexts(exec, context);
  519. faux_argv_free(argv);
  520. return exec;
  521. }
  522. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  523. void *associated_data, void *user_data)
  524. {
  525. ksession_t *session = (ksession_t *)user_data;
  526. if (!session)
  527. return BOOL_FALSE;
  528. ksession_set_done(session, BOOL_TRUE); // Stop the whole session
  529. // Happy compiler
  530. eloop = eloop;
  531. type = type;
  532. associated_data = associated_data;
  533. return BOOL_FALSE; // Stop Event Loop
  534. }
  535. static bool_t action_terminated_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  536. void *associated_data, void *user_data)
  537. {
  538. int wstatus = 0;
  539. pid_t child_pid = -1;
  540. kexec_t *exec = (kexec_t *)user_data;
  541. if (!exec)
  542. return BOOL_FALSE;
  543. // Wait for any child process. Doesn't block.
  544. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0)
  545. kexec_continue_command_execution(exec, child_pid, wstatus);
  546. // Check if kexec is done now
  547. if (kexec_done(exec))
  548. return BOOL_FALSE; // To break a loop
  549. // Happy compiler
  550. eloop = eloop;
  551. type = type;
  552. associated_data = associated_data;
  553. return BOOL_TRUE;
  554. }
  555. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  556. void *associated_data, void *user_data)
  557. {
  558. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  559. kexec_t *exec = (kexec_t *)user_data;
  560. ssize_t r = -1;
  561. faux_buf_t *faux_buf = NULL;
  562. void *linear_buf = NULL;
  563. if (!exec)
  564. return BOOL_FALSE;
  565. faux_buf = kexec_bufout(exec);
  566. assert(faux_buf);
  567. do {
  568. ssize_t really_readed = 0;
  569. ssize_t linear_len =
  570. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  571. // Non-blocked read. The fd became non-blocked while
  572. // kexec_prepare().
  573. r = read(info->fd, linear_buf, linear_len);
  574. if (r > 0)
  575. really_readed = r;
  576. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  577. } while (r > 0);
  578. // Happy compiler
  579. eloop = eloop;
  580. type = type;
  581. return BOOL_TRUE;
  582. }
  583. bool_t ksession_exec_locally(ksession_t *session, const kentry_t *entry,
  584. kpargv_t *parent_pargv, int *retcode, const char **out)
  585. {
  586. kexec_t *exec = NULL;
  587. faux_eloop_t *eloop = NULL;
  588. faux_buf_t *buf = NULL;
  589. char *cstr = NULL;
  590. ssize_t len = 0;
  591. assert(entry);
  592. if (!entry)
  593. return BOOL_FALSE;
  594. // Parsing
  595. exec = ksession_parse_for_local_exec(session, entry, parent_pargv);
  596. if (!exec)
  597. return BOOL_FALSE;
  598. // Session status can be changed while parsing because it can execute
  599. // nested ksession_exec_locally() to check for PTYPEs, CONDitions etc.
  600. // So check for 'done' flag to propagate it.
  601. if (ksession_done(session)) {
  602. kexec_free(exec);
  603. return BOOL_FALSE; // Because action is not completed
  604. }
  605. // Execute kexec and then wait for completion using local Eloop
  606. if (!kexec_exec(exec)) {
  607. kexec_free(exec);
  608. return BOOL_FALSE; // Something went wrong
  609. }
  610. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  611. // we don't need event loop and can return here.
  612. if (kexec_retcode(exec, retcode)) {
  613. kexec_free(exec);
  614. return BOOL_TRUE;
  615. }
  616. // Local service loop
  617. eloop = faux_eloop_new(NULL);
  618. faux_eloop_add_signal(eloop, SIGINT, stop_loop_ev, session);
  619. faux_eloop_add_signal(eloop, SIGTERM, stop_loop_ev, session);
  620. faux_eloop_add_signal(eloop, SIGQUIT, stop_loop_ev, session);
  621. faux_eloop_add_signal(eloop, SIGCHLD, action_terminated_ev, exec);
  622. faux_eloop_add_fd(eloop, kexec_stdout(exec), POLLIN,
  623. action_stdout_ev, exec);
  624. faux_eloop_loop(eloop);
  625. faux_eloop_free(eloop);
  626. kexec_retcode(exec, retcode);
  627. if (!out) {
  628. kexec_free(exec);
  629. return BOOL_TRUE;
  630. }
  631. buf = kexec_bufout(exec);
  632. if ((len = faux_buf_len(buf)) <= 0) {
  633. kexec_free(exec);
  634. return BOOL_TRUE;
  635. }
  636. cstr = faux_malloc(len + 1);
  637. faux_buf_read(buf, cstr, len);
  638. cstr[len] = '\0';
  639. *out = cstr;
  640. kexec_free(exec);
  641. return BOOL_TRUE;
  642. }