ksession_parse.c 25 KB

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