ksession_parse.c 24 KB

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