ksession_parse.c 22 KB

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