ktpd_session.c 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. #define _GNU_SOURCE
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #include <assert.h>
  6. #include <unistd.h>
  7. #include <errno.h>
  8. #include <sys/types.h>
  9. #include <sys/stat.h>
  10. #include <fcntl.h>
  11. #include <sys/socket.h>
  12. #include <sys/un.h>
  13. #include <syslog.h>
  14. #include <poll.h>
  15. #include <sys/wait.h>
  16. #include <faux/str.h>
  17. #include <faux/conv.h>
  18. #include <faux/async.h>
  19. #include <faux/msg.h>
  20. #include <faux/eloop.h>
  21. #include <faux/sysdb.h>
  22. #include <klish/ksession.h>
  23. #include <klish/ksession_parse.h>
  24. #include <klish/ktp.h>
  25. #include <klish/ktp_session.h>
  26. typedef enum {
  27. KTPD_SESSION_STATE_DISCONNECTED = 'd',
  28. KTPD_SESSION_STATE_UNAUTHORIZED = 'a',
  29. KTPD_SESSION_STATE_IDLE = 'i',
  30. KTPD_SESSION_STATE_WAIT_FOR_PROCESS = 'p',
  31. } ktpd_session_state_e;
  32. struct ktpd_session_s {
  33. ksession_t *session;
  34. ktpd_session_state_e state;
  35. faux_async_t *async; // Object for data exchange with client (KTP)
  36. faux_hdr_t *hdr; // Engine will receive header and then msg
  37. faux_eloop_t *eloop; // External link, dont's free()
  38. kexec_t *exec;
  39. bool_t exit;
  40. };
  41. // Static declarations
  42. static bool_t ktpd_session_read_cb(faux_async_t *async,
  43. faux_buf_t *buf, size_t len, void *user_data);
  44. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  45. void *associated_data, void *user_data);
  46. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  47. void *associated_data, void *user_data);
  48. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  49. int *retcode, faux_error_t *error,
  50. bool_t dry_run, bool_t *view_was_changed);
  51. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  52. void *associated_data, void *user_data);
  53. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  54. void *associated_data, void *user_data);
  55. static bool_t get_stream(ktpd_session_t *ktpd, int fd, bool_t is_stderr);
  56. ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
  57. const char *start_entry, faux_eloop_t *eloop)
  58. {
  59. ktpd_session_t *ktpd = NULL;
  60. if (sock < 0)
  61. return NULL;
  62. if (!eloop)
  63. return NULL;
  64. ktpd = faux_zmalloc(sizeof(*ktpd));
  65. assert(ktpd);
  66. if (!ktpd)
  67. return NULL;
  68. // Init
  69. ktpd->state = KTPD_SESSION_STATE_UNAUTHORIZED;
  70. ktpd->eloop = eloop;
  71. ktpd->session = ksession_new(scheme, start_entry);
  72. if (!ktpd->session) {
  73. faux_free(ktpd);
  74. return NULL;
  75. }
  76. ktpd->exec = NULL;
  77. // Exit flag. It differs from ksession done flag because KTPD session
  78. // can't exit immediately. It must finish current command processing
  79. // before really stop the event loop. Note: User defined plugin
  80. // function must use ksession done flag. This exit flag is internal
  81. // feature of KTPD session.
  82. ktpd->exit = BOOL_FALSE;
  83. // Async object
  84. ktpd->async = faux_async_new(sock);
  85. assert(ktpd->async);
  86. // Receive message header first
  87. faux_async_set_read_limits(ktpd->async,
  88. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  89. faux_async_set_read_cb(ktpd->async, ktpd_session_read_cb, ktpd);
  90. ktpd->hdr = NULL;
  91. faux_async_set_stall_cb(ktpd->async, ktp_stall_cb, ktpd->eloop);
  92. // Eloop callbacks
  93. faux_eloop_add_fd(ktpd->eloop, ktpd_session_fd(ktpd), POLLIN,
  94. client_ev, ktpd);
  95. faux_eloop_add_signal(ktpd->eloop, SIGCHLD, wait_for_actions_ev, ktpd);
  96. return ktpd;
  97. }
  98. void ktpd_session_free(ktpd_session_t *ktpd)
  99. {
  100. kcontext_t *context = NULL;
  101. kscheme_t *scheme = NULL;
  102. if (!ktpd)
  103. return;
  104. // fini session for plugins
  105. if (ktpd->state != KTPD_SESSION_STATE_UNAUTHORIZED) {
  106. scheme = ksession_scheme(ktpd->session);
  107. context = kcontext_new(KCONTEXT_TYPE_PLUGIN_FINI);
  108. kcontext_set_session(context, ktpd->session);
  109. kcontext_set_scheme(context, scheme);
  110. kscheme_fini_session_plugins(scheme, context, NULL);
  111. kcontext_free(context);
  112. }
  113. kexec_free(ktpd->exec);
  114. ksession_free(ktpd->session);
  115. faux_free(ktpd->hdr);
  116. close(ktpd_session_fd(ktpd));
  117. faux_async_free(ktpd->async);
  118. faux_free(ktpd);
  119. }
  120. static char *generate_prompt(ktpd_session_t *ktpd)
  121. {
  122. kpath_levels_node_t *iter = NULL;
  123. klevel_t *level = NULL;
  124. char *prompt = NULL;
  125. iter = kpath_iterr(ksession_path(ktpd->session));
  126. while ((level = kpath_eachr(&iter))) {
  127. const kentry_t *view = klevel_entry(level);
  128. kentry_t *prompt_entry = kentry_nested_by_purpose(view,
  129. KENTRY_PURPOSE_PROMPT);
  130. if (!prompt_entry)
  131. continue;
  132. if (kentry_actions_len(prompt_entry) > 0) {
  133. int rc = -1;
  134. bool_t res = BOOL_FALSE;
  135. res = ksession_exec_locally(ktpd->session,
  136. prompt_entry, NULL, &rc, &prompt);
  137. if (!res || (rc < 0) || !prompt) {
  138. if (prompt)
  139. faux_str_free(prompt);
  140. prompt = NULL;
  141. }
  142. }
  143. if (!prompt) {
  144. if (kentry_value(prompt_entry))
  145. prompt = faux_str_dup(kentry_value(prompt_entry));
  146. }
  147. if (prompt)
  148. break;
  149. }
  150. return prompt;
  151. }
  152. // Format: <key>'\0'<cmd>
  153. static bool_t add_hotkey(faux_msg_t *msg, khotkey_t *hotkey)
  154. {
  155. const char *key = NULL;
  156. const char *cmd = NULL;
  157. char *whole_str = NULL;
  158. size_t key_s = 0;
  159. size_t cmd_s = 0;
  160. key = khotkey_key(hotkey);
  161. key_s = strlen(key);
  162. cmd = khotkey_cmd(hotkey);
  163. cmd_s = strlen(cmd);
  164. whole_str = faux_zmalloc(key_s + 1 + cmd_s);
  165. memcpy(whole_str, key, key_s);
  166. memcpy(whole_str + key_s + 1, cmd, cmd_s);
  167. faux_msg_add_param(msg, KTP_PARAM_HOTKEY, whole_str, key_s + 1 + cmd_s);
  168. faux_free(whole_str);
  169. return BOOL_TRUE;
  170. }
  171. static bool_t add_hotkeys_to_msg(ktpd_session_t *ktpd, faux_msg_t *msg)
  172. {
  173. faux_list_t *list = NULL;
  174. kpath_t *path = NULL;
  175. kentry_hotkeys_node_t *l_iter = NULL;
  176. khotkey_t *hotkey = NULL;
  177. assert(ktpd);
  178. assert(msg);
  179. path = ksession_path(ktpd->session);
  180. assert(path);
  181. if (kpath_len(path) == 1) {
  182. // We don't need additional list because there is only one
  183. // VIEW in the path so hotkey's list is only one too. Get it.
  184. list = kentry_hotkeys(klevel_entry(
  185. (klevel_t *)faux_list_data(kpath_iter(path))));
  186. } else {
  187. faux_list_node_t *iterr = NULL;
  188. klevel_t *level = NULL;
  189. // Create temp hotkeys list to add hotkeys from all VIEWs in
  190. // the path and exclude duplications. Don't free elements
  191. // because they are just a references.
  192. list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  193. kentry_hotkey_compare, NULL, NULL);
  194. // Begin with the end. Because hotkeys from nested VIEWs has
  195. // higher priority.
  196. iterr = kpath_iterr(path);
  197. while ((level = kpath_eachr(&iterr))) {
  198. const kentry_t *entry = klevel_entry(level);
  199. kentry_hotkeys_node_t *hk_iter = kentry_hotkeys_iter(entry);
  200. while ((hotkey = kentry_hotkeys_each(&hk_iter)))
  201. faux_list_add(list, hotkey);
  202. }
  203. }
  204. // Add found hotkeys to msg
  205. l_iter = faux_list_head(list);
  206. while ((hotkey = (khotkey_t *)faux_list_each(&l_iter)))
  207. add_hotkey(msg, hotkey);
  208. if (kpath_len(path) != 1)
  209. faux_list_free(list);
  210. return BOOL_TRUE;
  211. }
  212. // Now it's not really an auth function. Just a hand-shake with client and
  213. // passing prompt to client.
  214. static bool_t ktpd_session_process_auth(ktpd_session_t *ktpd, faux_msg_t *msg)
  215. {
  216. ktp_cmd_e cmd = KTP_AUTH_ACK;
  217. uint32_t status = KTP_STATUS_NONE;
  218. faux_msg_t *ack = NULL;
  219. char *prompt = NULL;
  220. uint8_t retcode8bit = 0;
  221. struct ucred ucred = {};
  222. socklen_t len = sizeof(ucred);
  223. int sock = -1;
  224. char *user = NULL;
  225. kcontext_t *context = NULL;
  226. kscheme_t *scheme = NULL;
  227. uint32_t client_status = KTP_STATUS_NONE;
  228. assert(ktpd);
  229. assert(msg);
  230. // Get UNIX socket peer information
  231. sock = faux_async_fd(ktpd->async);
  232. if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
  233. const char *err = "Can't get peer credentials";
  234. syslog(LOG_ERR, "%s for connection %d", err, sock);
  235. ack = ktp_msg_preform(cmd, KTP_STATUS_ERROR | KTP_STATUS_EXIT);
  236. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  237. faux_msg_send_async(ack, ktpd->async);
  238. faux_msg_free(ack);
  239. ktpd->exit = BOOL_TRUE;
  240. return BOOL_FALSE;
  241. }
  242. ksession_set_pid(ktpd->session, ucred.pid);
  243. ksession_set_uid(ktpd->session, ucred.uid);
  244. user = faux_sysdb_name_by_uid(ucred.uid);
  245. ksession_set_user(ktpd->session, user);
  246. faux_str_free(user);
  247. // Get tty information from auth message status
  248. client_status = faux_msg_get_status(msg);
  249. ksession_set_isatty_stdin(ktpd->session,
  250. KTP_STATUS_IS_TTY_STDIN(client_status));
  251. ksession_set_isatty_stdout(ktpd->session,
  252. KTP_STATUS_IS_TTY_STDOUT(client_status));
  253. ksession_set_isatty_stderr(ktpd->session,
  254. KTP_STATUS_IS_TTY_STDERR(client_status));
  255. // init session for plugins
  256. scheme = ksession_scheme(ktpd->session);
  257. context = kcontext_new(KCONTEXT_TYPE_PLUGIN_INIT);
  258. kcontext_set_session(context, ktpd->session);
  259. kcontext_set_scheme(context, scheme);
  260. kscheme_init_session_plugins(scheme, context, NULL);
  261. kcontext_free(context);
  262. // Prepare ACK message
  263. ack = ktp_msg_preform(cmd, status);
  264. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  265. // Generate prompt
  266. prompt = generate_prompt(ktpd);
  267. if (prompt) {
  268. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  269. faux_str_free(prompt);
  270. }
  271. add_hotkeys_to_msg(ktpd, ack);
  272. faux_msg_send_async(ack, ktpd->async);
  273. faux_msg_free(ack);
  274. ktpd->state = KTPD_SESSION_STATE_IDLE;
  275. return BOOL_TRUE;
  276. }
  277. static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
  278. {
  279. char *line = NULL;
  280. int retcode = -1;
  281. ktp_cmd_e cmd = KTP_CMD_ACK;
  282. faux_error_t *error = NULL;
  283. bool_t rc = BOOL_FALSE;
  284. bool_t dry_run = BOOL_FALSE;
  285. uint32_t status = KTP_STATUS_NONE;
  286. bool_t ret = BOOL_TRUE;
  287. char *prompt = NULL;
  288. bool_t view_was_changed = BOOL_FALSE;
  289. assert(ktpd);
  290. assert(msg);
  291. // Get line from message
  292. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  293. ktp_send_error(ktpd->async, cmd, "The line is not specified");
  294. return BOOL_FALSE;
  295. }
  296. // Get dry-run flag from message
  297. if (KTP_STATUS_IS_DRY_RUN(faux_msg_get_status(msg)))
  298. dry_run = BOOL_TRUE;
  299. error = faux_error_new();
  300. ktpd->exec = NULL;
  301. rc = ktpd_session_exec(ktpd, line, &retcode, error,
  302. dry_run, &view_was_changed);
  303. faux_str_free(line);
  304. // Command is scheduled. Eloop will wait for ACTION completion.
  305. // So inform client about it and about command features like
  306. // interactive/non-interactive.
  307. if (ktpd->exec) {
  308. faux_msg_t *ack = NULL;
  309. ktp_status_e status = KTP_STATUS_INCOMPLETED;
  310. if (kexec_interactive(ktpd->exec))
  311. status |= KTP_STATUS_INTERACTIVE;
  312. ack = ktp_msg_preform(cmd, status);
  313. faux_msg_send_async(ack, ktpd->async);
  314. faux_msg_free(ack);
  315. faux_error_free(error);
  316. return BOOL_TRUE; // Continue and wait for ACTION
  317. }
  318. // Here we don't need to wait for the action. We have retcode already.
  319. if (ksession_done(ktpd->session)) {
  320. ktpd->exit = BOOL_TRUE;
  321. status |= KTP_STATUS_EXIT;
  322. }
  323. // Prepare ACK message
  324. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  325. if (rc) {
  326. uint8_t retcode8bit = 0;
  327. retcode8bit = (uint8_t)(retcode & 0xff);
  328. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  329. } else {
  330. faux_msg_set_status(ack, KTP_STATUS_ERROR);
  331. char *err = faux_error_cstr(error);
  332. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  333. faux_str_free(err);
  334. ret = BOOL_FALSE;
  335. }
  336. // Generate prompt
  337. prompt = generate_prompt(ktpd);
  338. if (prompt) {
  339. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  340. faux_str_free(prompt);
  341. }
  342. // Add hotkeys
  343. if (view_was_changed)
  344. add_hotkeys_to_msg(ktpd, ack);
  345. faux_msg_send_async(ack, ktpd->async);
  346. faux_msg_free(ack);
  347. faux_error_free(error);
  348. return ret;
  349. }
  350. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  351. int *retcode, faux_error_t *error,
  352. bool_t dry_run, bool_t *view_was_changed_p)
  353. {
  354. kexec_t *exec = NULL;
  355. assert(ktpd);
  356. if (!ktpd)
  357. return BOOL_FALSE;
  358. // Parsing
  359. exec = ksession_parse_for_exec(ktpd->session, line, error);
  360. if (!exec)
  361. return BOOL_FALSE;
  362. // Set dry-run flag
  363. kexec_set_dry_run(exec, dry_run);
  364. // Session status can be changed while parsing
  365. // NOTE: kexec_t is atomic now
  366. // if (ksession_done(ktpd->session)) {
  367. // kexec_free(exec);
  368. // return BOOL_FALSE; // Because action is not completed
  369. // }
  370. // Execute kexec and then wait for completion using global Eloop
  371. if (!kexec_exec(exec)) {
  372. kexec_free(exec);
  373. return BOOL_FALSE; // Something went wrong
  374. }
  375. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  376. // we don't need event loop and can return here.
  377. if (kexec_retcode(exec, retcode)) {
  378. if (view_was_changed_p)
  379. *view_was_changed_p = !kpath_is_equal(
  380. ksession_path(ktpd->session),
  381. kexec_saved_path(exec));
  382. kexec_free(exec);
  383. return BOOL_TRUE;
  384. }
  385. // Save kexec pointer to use later
  386. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  387. ktpd->exec = exec;
  388. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  389. action_stdout_ev, ktpd);
  390. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  391. action_stderr_ev, ktpd);
  392. return BOOL_TRUE;
  393. }
  394. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  395. void *associated_data, void *user_data)
  396. {
  397. int wstatus = 0;
  398. pid_t child_pid = -1;
  399. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  400. int retcode = -1;
  401. uint8_t retcode8bit = 0;
  402. faux_msg_t *ack = NULL;
  403. ktp_cmd_e cmd = KTP_CMD_ACK;
  404. uint32_t status = KTP_STATUS_NONE;
  405. char *prompt = NULL;
  406. bool_t view_was_changed = BOOL_FALSE;
  407. if (!ktpd)
  408. return BOOL_FALSE;
  409. // Wait for any child process. Doesn't block.
  410. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  411. if (ktpd->exec)
  412. kexec_continue_command_execution(ktpd->exec, child_pid,
  413. wstatus);
  414. }
  415. if (!ktpd->exec)
  416. return BOOL_TRUE;
  417. // Check if kexec is done now
  418. if (!kexec_retcode(ktpd->exec, &retcode))
  419. return BOOL_TRUE; // Continue
  420. // Sometimes SIGCHILD signal can appear before all data were really read
  421. // from process stdout buffer. So read the least data before closing
  422. // file descriptors and send it to client.
  423. get_stream(ktpd, kexec_stdout(ktpd->exec), BOOL_FALSE);
  424. get_stream(ktpd, kexec_stderr(ktpd->exec), BOOL_TRUE);
  425. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  426. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  427. view_was_changed = !kpath_is_equal(
  428. ksession_path(ktpd->session), kexec_saved_path(ktpd->exec));
  429. kexec_free(ktpd->exec);
  430. ktpd->exec = NULL;
  431. ktpd->state = KTPD_SESSION_STATE_IDLE;
  432. // All kexec_t actions are done so can break the loop if needed.
  433. if (ksession_done(ktpd->session)) {
  434. ktpd->exit = BOOL_TRUE;
  435. status |= KTP_STATUS_EXIT; // Notify client about exiting
  436. }
  437. // Send ACK message
  438. ack = ktp_msg_preform(cmd, status);
  439. retcode8bit = (uint8_t)(retcode & 0xff);
  440. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  441. // Generate prompt
  442. prompt = generate_prompt(ktpd);
  443. if (prompt) {
  444. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  445. faux_str_free(prompt);
  446. }
  447. // Add hotkeys
  448. if (view_was_changed)
  449. add_hotkeys_to_msg(ktpd, ack);
  450. faux_msg_send_async(ack, ktpd->async);
  451. faux_msg_free(ack);
  452. type = type; // Happy compiler
  453. associated_data = associated_data; // Happy compiler
  454. if (ktpd->exit)
  455. return BOOL_FALSE;
  456. return BOOL_TRUE;
  457. }
  458. static int compl_compare(const void *first, const void *second)
  459. {
  460. const char *f = (const char *)first;
  461. const char *s = (const char *)second;
  462. return strcmp(f, s);
  463. }
  464. static int compl_kcompare(const void *key, const void *list_item)
  465. {
  466. const char *f = (const char *)key;
  467. const char *s = (const char *)list_item;
  468. return strcmp(f, s);
  469. }
  470. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  471. {
  472. char *line = NULL;
  473. faux_msg_t *ack = NULL;
  474. kpargv_t *pargv = NULL;
  475. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  476. uint32_t status = KTP_STATUS_NONE;
  477. const char *prefix = NULL;
  478. size_t prefix_len = 0;
  479. assert(ktpd);
  480. assert(msg);
  481. // Get line from message
  482. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  483. ktp_send_error(ktpd->async, cmd, NULL);
  484. return BOOL_FALSE;
  485. }
  486. // Parsing
  487. pargv = ksession_parse_for_completion(ktpd->session, line);
  488. faux_str_free(line);
  489. if (!pargv) {
  490. ktp_send_error(ktpd->async, cmd, NULL);
  491. return BOOL_FALSE;
  492. }
  493. kpargv_debug(pargv);
  494. if (ksession_done(ktpd->session)) {
  495. ktpd->exit = BOOL_TRUE;
  496. status |= KTP_STATUS_EXIT; // Notify client about exiting
  497. }
  498. // Prepare ACK message
  499. ack = ktp_msg_preform(cmd, status);
  500. // Last unfinished word. Common prefix for all completions
  501. prefix = kpargv_last_arg(pargv);
  502. if (!faux_str_is_empty(prefix)) {
  503. prefix_len = strlen(prefix);
  504. faux_msg_add_param(ack, KTP_PARAM_PREFIX, prefix, prefix_len);
  505. }
  506. // Fill msg with possible completions
  507. if (!kpargv_completions_is_empty(pargv)) {
  508. const kentry_t *candidate = NULL;
  509. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  510. faux_list_node_t *compl_iter = NULL;
  511. faux_list_t *completions = NULL;
  512. char *compl_str = NULL;
  513. completions = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  514. compl_compare, compl_kcompare,
  515. (void (*)(void *))faux_str_free);
  516. while ((candidate = kpargv_completions_each(&citer))) {
  517. const kentry_t *completion = NULL;
  518. kparg_t *parg = NULL;
  519. int rc = -1;
  520. char *out = NULL;
  521. bool_t res = BOOL_FALSE;
  522. char *l = NULL; // One line of completion
  523. const char *str = NULL;
  524. // Get completion entry from candidate entry
  525. completion = kentry_nested_by_purpose(candidate,
  526. KENTRY_PURPOSE_COMPLETION);
  527. // If candidate entry doesn't contain completion then try
  528. // to get completion from entry's PTYPE
  529. if (!completion) {
  530. const kentry_t *ptype = NULL;
  531. ptype = kentry_nested_by_purpose(candidate,
  532. KENTRY_PURPOSE_PTYPE);
  533. if (!ptype)
  534. continue;
  535. completion = kentry_nested_by_purpose(ptype,
  536. KENTRY_PURPOSE_COMPLETION);
  537. }
  538. if (!completion)
  539. continue;
  540. parg = kparg_new(candidate, prefix);
  541. kpargv_set_candidate_parg(pargv, parg);
  542. res = ksession_exec_locally(ktpd->session, completion,
  543. pargv, &rc, &out);
  544. kparg_free(parg);
  545. if (!res || (rc < 0) || !out) {
  546. if (out)
  547. faux_str_free(out);
  548. continue;
  549. }
  550. // Get all completions one by one
  551. str = out;
  552. while ((l = faux_str_getline(str, &str))) {
  553. // Compare prefix
  554. if ((prefix_len > 0) &&
  555. (faux_str_cmpn(prefix, l, prefix_len) != 0)) {
  556. faux_str_free(l);
  557. continue;
  558. }
  559. compl_str = l + prefix_len;
  560. faux_list_add(completions, faux_str_dup(compl_str));
  561. faux_str_free(l);
  562. }
  563. faux_str_free(out);
  564. }
  565. // Put completion list to message
  566. compl_iter = faux_list_head(completions);
  567. while ((compl_str = faux_list_each(&compl_iter))) {
  568. faux_msg_add_param(ack, KTP_PARAM_LINE,
  569. compl_str, strlen(compl_str));
  570. }
  571. faux_list_free(completions);
  572. }
  573. faux_msg_send_async(ack, ktpd->async);
  574. faux_msg_free(ack);
  575. kpargv_free(pargv);
  576. return BOOL_TRUE;
  577. }
  578. // The most priority source of help is candidate's help ACTION output. Next
  579. // source is candidate's PTYPE help ACTION output.
  580. // Function generates two lines for one resulting help line. The first
  581. // component is a 'prefix' and the second component is 'text'.
  582. // The 'prefix' can be something like 'ip', 'filter' i.e.
  583. // subcommand or '3..89', '<STRING>' i.e. description of type. The 'text'
  584. // field is description of current parameter. For example 'Interface IP
  585. // address'. So the full help can be:
  586. // AAA.BBB.CCC.DDD Interface IP address
  587. // [ first field ] [ second field ]
  588. //
  589. // If not candidate parameter nor PTYPE contains the help functions the engine
  590. // tries to construct help itself.
  591. //
  592. // It uses the following sources for 'prefix':
  593. // * 'help' field of PTYPE
  594. // * 'value' field of PTYPE
  595. // * 'name' field of PTYPE
  596. // * 'value' field of parameter
  597. // * 'name' field of parameter
  598. //
  599. // Engine uses the following sources for 'text':
  600. // * 'help' field of parameter
  601. // * 'value' field of parameter
  602. // * 'name' field of parameter
  603. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  604. {
  605. char *line = NULL;
  606. faux_msg_t *ack = NULL;
  607. kpargv_t *pargv = NULL;
  608. ktp_cmd_e cmd = KTP_HELP_ACK;
  609. uint32_t status = KTP_STATUS_NONE;
  610. const char *prefix = NULL;
  611. assert(ktpd);
  612. assert(msg);
  613. // Get line from message
  614. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  615. ktp_send_error(ktpd->async, cmd, NULL);
  616. return BOOL_FALSE;
  617. }
  618. // Parsing
  619. pargv = ksession_parse_for_completion(ktpd->session, line);
  620. faux_str_free(line);
  621. if (!pargv) {
  622. ktp_send_error(ktpd->async, cmd, NULL);
  623. return BOOL_FALSE;
  624. }
  625. if (ksession_done(ktpd->session)) {
  626. ktpd->exit = BOOL_TRUE;
  627. status |= KTP_STATUS_EXIT; // Notify client about exiting
  628. }
  629. // Prepare ACK message
  630. ack = ktp_msg_preform(cmd, status);
  631. // Last unfinished word. Common prefix for all entries
  632. prefix = kpargv_last_arg(pargv);
  633. // Fill msg with possible help messages
  634. if (!kpargv_completions_is_empty(pargv)) {
  635. const kentry_t *candidate = NULL;
  636. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  637. faux_list_node_t *help_iter = NULL;
  638. faux_list_t *help_list = NULL;
  639. help_t *help_struct = NULL;
  640. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  641. help_compare, NULL, help_free);
  642. while ((candidate = kpargv_completions_each(&citer))) {
  643. const kentry_t *help = NULL;
  644. const kentry_t *ptype = NULL;
  645. // Get PTYPE of parameter
  646. ptype = kentry_nested_by_purpose(candidate,
  647. KENTRY_PURPOSE_PTYPE);
  648. // Try to get help fn from parameter itself
  649. help = kentry_nested_by_purpose(candidate,
  650. KENTRY_PURPOSE_HELP);
  651. if (!help && ptype)
  652. help = kentry_nested_by_purpose(ptype,
  653. KENTRY_PURPOSE_HELP);
  654. // Generate help with found ACTION
  655. if (help) {
  656. char *out = NULL;
  657. kparg_t *parg = NULL;
  658. int rc = -1;
  659. parg = kparg_new(candidate, prefix);
  660. kpargv_set_candidate_parg(pargv, parg);
  661. ksession_exec_locally(ktpd->session,
  662. help, pargv, &rc, &out);
  663. kparg_free(parg);
  664. if (out) {
  665. const char *str = out;
  666. char *prefix_str = NULL;
  667. char *line_str = NULL;
  668. do {
  669. prefix_str = faux_str_getline(str, &str);
  670. if (!prefix_str)
  671. break;
  672. line_str = faux_str_getline(str, &str);
  673. if (!line_str) {
  674. faux_str_free(prefix_str);
  675. break;
  676. }
  677. help_struct = help_new(prefix_str, line_str);
  678. if (!faux_list_add(help_list, help_struct))
  679. help_free(help_struct);
  680. } while (line_str);
  681. faux_str_free(out);
  682. }
  683. // Generate help with available information
  684. } else {
  685. const char *prefix_str = NULL;
  686. const char *line_str = NULL;
  687. // Prefix_str
  688. if (ptype) {
  689. prefix_str = kentry_help(ptype);
  690. if (!prefix_str)
  691. prefix_str = kentry_value(ptype);
  692. if (!prefix_str)
  693. prefix_str = kentry_name(ptype);
  694. } else {
  695. prefix_str = kentry_value(candidate);
  696. if (!prefix_str)
  697. prefix_str = kentry_name(candidate);
  698. }
  699. assert(prefix_str);
  700. // Line_str
  701. line_str = kentry_help(candidate);
  702. if (!line_str)
  703. line_str = kentry_value(candidate);
  704. if (!line_str)
  705. line_str = kentry_name(candidate);
  706. assert(line_str);
  707. help_struct = help_new(
  708. faux_str_dup(prefix_str),
  709. faux_str_dup(line_str));
  710. if (!faux_list_add(help_list, help_struct))
  711. help_free(help_struct);
  712. }
  713. }
  714. // Put help list to message
  715. help_iter = faux_list_head(help_list);
  716. while ((help_struct = (help_t *)faux_list_each(&help_iter))) {
  717. faux_msg_add_param(ack, KTP_PARAM_PREFIX,
  718. help_struct->prefix, strlen(help_struct->prefix));
  719. faux_msg_add_param(ack, KTP_PARAM_LINE,
  720. help_struct->line, strlen(help_struct->line));
  721. }
  722. faux_list_free(help_list);
  723. }
  724. faux_msg_send_async(ack, ktpd->async);
  725. faux_msg_free(ack);
  726. kpargv_free(pargv);
  727. return BOOL_TRUE;
  728. }
  729. static ssize_t stdin_out(int fd, faux_buf_t *buf)
  730. {
  731. ssize_t total_written = 0;
  732. assert(buf);
  733. if (!buf)
  734. return -1;
  735. assert(fd >= 0);
  736. while (faux_buf_len(buf) > 0) {
  737. ssize_t data_to_write = 0;
  738. ssize_t bytes_written = 0;
  739. void *data = NULL;
  740. data_to_write = faux_buf_dread_lock_easy(buf, &data);
  741. if (data_to_write <= 0)
  742. return -1;
  743. bytes_written = write(fd, data, data_to_write);
  744. if (bytes_written > 0) {
  745. total_written += bytes_written;
  746. faux_buf_dread_unlock_easy(buf, bytes_written);
  747. } else {
  748. faux_buf_dread_unlock_easy(buf, 0);
  749. }
  750. if (bytes_written < 0) {
  751. if ( // Something went wrong
  752. (errno != EINTR) &&
  753. (errno != EAGAIN) &&
  754. (errno != EWOULDBLOCK)
  755. )
  756. return -1;
  757. // Not whole data block was written
  758. } else if (bytes_written != data_to_write) {
  759. break;
  760. }
  761. }
  762. return total_written;
  763. }
  764. static bool_t push_stdin(faux_eloop_t *eloop, faux_eloop_type_e type,
  765. void *associated_data, void *user_data)
  766. {
  767. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  768. faux_buf_t *bufin = NULL;
  769. int fd = -1;
  770. if (!ktpd)
  771. return BOOL_TRUE;
  772. if (!ktpd->exec)
  773. return BOOL_TRUE;
  774. fd = kexec_stdin(ktpd->exec);
  775. if (fd < 0) // Something strange
  776. return BOOL_FALSE;
  777. bufin = kexec_bufin(ktpd->exec);
  778. assert(bufin);
  779. stdin_out(fd, bufin); // Non-blocking write
  780. if (faux_buf_len(bufin) != 0) // Try later
  781. return BOOL_TRUE;
  782. // All data is written
  783. faux_eloop_exclude_fd_event(ktpd->eloop, fd, POLLOUT);
  784. // Happy compiler
  785. eloop = eloop;
  786. type = type;
  787. associated_data = associated_data;
  788. return BOOL_TRUE;
  789. }
  790. static bool_t ktpd_session_process_stdin(ktpd_session_t *ktpd, faux_msg_t *msg)
  791. {
  792. char *line = NULL;
  793. unsigned int len = 0;
  794. faux_buf_t *bufin = NULL;
  795. int fd = -1;
  796. assert(ktpd);
  797. assert(msg);
  798. if (!ktpd->exec)
  799. return BOOL_FALSE;
  800. if (!kexec_interactive(ktpd->exec))
  801. return BOOL_FALSE;
  802. fd = kexec_stdin(ktpd->exec);
  803. if (fd < 0)
  804. return BOOL_FALSE;
  805. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_LINE, (void **)&line, &len))
  806. return BOOL_TRUE; // It's strange but not a bug
  807. if (len == 0)
  808. return BOOL_TRUE;
  809. bufin = kexec_bufin(ktpd->exec);
  810. assert(bufin);
  811. faux_buf_write(bufin, line, len);
  812. stdin_out(fd, bufin); // Non-blocking write
  813. if (faux_buf_len(bufin) == 0)
  814. return BOOL_TRUE;
  815. // Non-blocking write can't write all data so plan to write later
  816. faux_eloop_include_fd_event(ktpd->eloop, fd, POLLOUT);
  817. return BOOL_TRUE;
  818. }
  819. static bool_t ktpd_session_process_winch(ktpd_session_t *ktpd, faux_msg_t *msg)
  820. {
  821. char *line = NULL;
  822. char *p = NULL;
  823. unsigned short width = 0;
  824. unsigned short height = 0;
  825. assert(ktpd);
  826. assert(msg);
  827. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_WINCH)))
  828. return BOOL_TRUE;
  829. p = strchr(line, ' ');
  830. if (!p || (p == line)) {
  831. faux_str_free(line);
  832. return BOOL_FALSE;
  833. }
  834. if (!faux_conv_atous(line, &width, 0)) {
  835. faux_str_free(line);
  836. return BOOL_FALSE;
  837. }
  838. if (!faux_conv_atous(p + 1, &height, 0)) {
  839. faux_str_free(line);
  840. return BOOL_FALSE;
  841. }
  842. ksession_set_term_width(ktpd->session, width);
  843. ksession_set_term_height(ktpd->session, height);
  844. faux_str_free(line);
  845. if (!ktpd->exec)
  846. return BOOL_TRUE;
  847. // Set pseudo terminal window size
  848. kexec_set_winsize(ktpd->exec);
  849. return BOOL_TRUE;
  850. }
  851. static bool_t ktpd_session_process_notification(ktpd_session_t *ktpd, faux_msg_t *msg)
  852. {
  853. assert(ktpd);
  854. assert(msg);
  855. ktpd_session_process_winch(ktpd, msg);
  856. return BOOL_TRUE;
  857. }
  858. static bool_t ktpd_session_process_stdout_close(ktpd_session_t *ktpd,
  859. faux_msg_t *msg)
  860. {
  861. int fd = -1;
  862. assert(ktpd);
  863. assert(msg);
  864. if (!ktpd->exec)
  865. return BOOL_FALSE;
  866. fd = kexec_stdout(ktpd->exec);
  867. if (fd < 0)
  868. return BOOL_FALSE;
  869. close(fd);
  870. // Remove already generated data from out buffer. This data is not
  871. // needed now
  872. faux_buf_empty(kexec_bufout(ktpd->exec));
  873. return BOOL_TRUE;
  874. }
  875. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  876. {
  877. uint16_t cmd = 0;
  878. const char *err = NULL;
  879. ktp_cmd_e ecmd = KTP_NOTIFICATION; // Answer command if error
  880. assert(ktpd);
  881. if (!ktpd)
  882. return BOOL_FALSE;
  883. assert(msg);
  884. if (!msg)
  885. return BOOL_FALSE;
  886. cmd = faux_msg_get_cmd(msg);
  887. switch (cmd) {
  888. case KTP_AUTH:
  889. if ((ktpd->state != KTPD_SESSION_STATE_UNAUTHORIZED) &&
  890. (ktpd->state != KTPD_SESSION_STATE_IDLE)) {
  891. ecmd = KTP_AUTH_ACK;
  892. err = "Server illegal state for authorization";
  893. break;
  894. }
  895. ktpd_session_process_auth(ktpd, msg);
  896. break;
  897. case KTP_CMD:
  898. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  899. ecmd = KTP_CMD_ACK;
  900. err = "Server illegal state for command execution";
  901. break;
  902. }
  903. ktpd_session_process_cmd(ktpd, msg);
  904. break;
  905. case KTP_COMPLETION:
  906. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  907. ecmd = KTP_COMPLETION_ACK;
  908. err = "Server illegal state for completion";
  909. break;
  910. }
  911. ktpd_session_process_completion(ktpd, msg);
  912. break;
  913. case KTP_HELP:
  914. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  915. ecmd = KTP_HELP_ACK;
  916. err = "Server illegal state for help";
  917. break;
  918. }
  919. ktpd_session_process_help(ktpd, msg);
  920. break;
  921. case KTP_STDIN:
  922. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
  923. err = "Nobody is waiting for stdin";
  924. break;
  925. }
  926. ktpd_session_process_stdin(ktpd, msg);
  927. break;
  928. case KTP_NOTIFICATION:
  929. ktpd_session_process_notification(ktpd, msg);
  930. break;
  931. case KTP_STDOUT_CLOSE:
  932. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
  933. err = "No active command is running";
  934. break;
  935. }
  936. ktpd_session_process_stdout_close(ktpd, msg);
  937. break;
  938. default:
  939. syslog(LOG_WARNING, "Unsupported command: 0x%04u", cmd);
  940. err = "Unsupported command";
  941. break;
  942. }
  943. // On error
  944. if (err)
  945. ktp_send_error(ktpd->async, ecmd, err);
  946. return BOOL_TRUE;
  947. }
  948. /** @brief Low-level function to receive KTP message.
  949. *
  950. * Firstly function gets the header of message. Then it checks and parses
  951. * header and find out the length of whole message. Then it receives the rest
  952. * of message.
  953. */
  954. static bool_t ktpd_session_read_cb(faux_async_t *async,
  955. faux_buf_t *buf, size_t len, void *user_data)
  956. {
  957. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  958. faux_msg_t *completed_msg = NULL;
  959. char *data = NULL;
  960. assert(async);
  961. assert(buf);
  962. assert(ktpd);
  963. // Linearize buffer
  964. data = malloc(len);
  965. faux_buf_read(buf, data, len);
  966. // Receive header
  967. if (!ktpd->hdr) {
  968. size_t whole_len = 0;
  969. size_t msg_wo_hdr = 0;
  970. ktpd->hdr = (faux_hdr_t *)data;
  971. // Check for broken header
  972. if (!ktp_check_header(ktpd->hdr)) {
  973. faux_free(ktpd->hdr);
  974. ktpd->hdr = NULL;
  975. return BOOL_FALSE;
  976. }
  977. whole_len = faux_hdr_len(ktpd->hdr);
  978. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  979. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  980. // Plan to receive message body
  981. if (msg_wo_hdr > 0) {
  982. faux_async_set_read_limits(async,
  983. msg_wo_hdr, msg_wo_hdr);
  984. return BOOL_TRUE;
  985. }
  986. // Here message is completed (msg body has zero length)
  987. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  988. // Receive message body
  989. } else {
  990. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  991. faux_free(data);
  992. }
  993. // Plan to receive msg header
  994. faux_async_set_read_limits(ktpd->async,
  995. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  996. faux_free(ktpd->hdr);
  997. ktpd->hdr = NULL; // Ready to recv new header
  998. // Here message is completed
  999. ktpd_session_dispatch(ktpd, completed_msg);
  1000. faux_msg_free(completed_msg);
  1001. return BOOL_TRUE;
  1002. }
  1003. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  1004. {
  1005. assert(ktpd);
  1006. if (!ktpd)
  1007. return BOOL_FALSE;
  1008. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  1009. return BOOL_FALSE;
  1010. return BOOL_TRUE;
  1011. }
  1012. int ktpd_session_fd(const ktpd_session_t *ktpd)
  1013. {
  1014. assert(ktpd);
  1015. if (!ktpd)
  1016. return BOOL_FALSE;
  1017. return faux_async_fd(ktpd->async);
  1018. }
  1019. static bool_t get_stream(ktpd_session_t *ktpd, int fd, bool_t is_stderr)
  1020. {
  1021. ssize_t r = -1;
  1022. faux_buf_t *faux_buf = NULL;
  1023. char *buf = NULL;
  1024. ssize_t len = 0;
  1025. faux_msg_t *ack = NULL;
  1026. if (!ktpd)
  1027. return BOOL_TRUE;
  1028. if (!ktpd->exec)
  1029. return BOOL_TRUE;
  1030. if (is_stderr)
  1031. faux_buf = kexec_buferr(ktpd->exec);
  1032. else
  1033. faux_buf = kexec_bufout(ktpd->exec);
  1034. assert(faux_buf);
  1035. do {
  1036. void *linear_buf = NULL;
  1037. ssize_t really_readed = 0;
  1038. ssize_t linear_len =
  1039. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  1040. // Non-blocked read. The fd became non-blocked while
  1041. // kexec_prepare().
  1042. r = read(fd, linear_buf, linear_len);
  1043. if (r > 0)
  1044. really_readed = r;
  1045. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  1046. } while (r > 0);
  1047. len = faux_buf_len(faux_buf);
  1048. if (0 == len)
  1049. return BOOL_TRUE;
  1050. buf = malloc(len);
  1051. faux_buf_read(faux_buf, buf, len);
  1052. // Create KTP_STDOUT/KTP_STDERR message to send to client
  1053. ack = ktp_msg_preform(is_stderr ? KTP_STDERR : KTP_STDOUT, KTP_STATUS_NONE);
  1054. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  1055. faux_msg_send_async(ack, ktpd->async);
  1056. faux_msg_free(ack);
  1057. free(buf);
  1058. return BOOL_TRUE;
  1059. }
  1060. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1061. void *associated_data, void *user_data)
  1062. {
  1063. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1064. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1065. // Interactive command use these function as callback not only for
  1066. // getting stdout but for writing stdin too. Because pseudo-terminal
  1067. // uses the same fd for in and out.
  1068. if (info->revents & POLLOUT)
  1069. push_stdin(eloop, type, associated_data, user_data);
  1070. if (info->revents & POLLIN)
  1071. get_stream(ktpd, info->fd, BOOL_FALSE);
  1072. // Some errors or fd is closed so remove it from polling
  1073. // EOF || POLERR || POLLNVAL
  1074. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1075. faux_eloop_del_fd(eloop, info->fd);
  1076. return BOOL_TRUE;
  1077. }
  1078. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1079. void *associated_data, void *user_data)
  1080. {
  1081. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1082. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1083. if (info->revents & POLLIN)
  1084. get_stream(ktpd, info->fd, BOOL_TRUE);
  1085. // Some errors or fd is closed so remove it from polling
  1086. // EOF || POLERR || POLLNVAL
  1087. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1088. faux_eloop_del_fd(eloop, info->fd);
  1089. type = type; // Happy compiler
  1090. return BOOL_TRUE;
  1091. }
  1092. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1093. void *associated_data, void *user_data)
  1094. {
  1095. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1096. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1097. faux_async_t *async = ktpd->async;
  1098. assert(async);
  1099. // Write data
  1100. if (info->revents & POLLOUT) {
  1101. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  1102. if (faux_async_out(async) < 0) {
  1103. // Someting went wrong
  1104. faux_eloop_del_fd(eloop, info->fd);
  1105. syslog(LOG_ERR, "Can't send data to client");
  1106. return BOOL_FALSE; // Stop event loop
  1107. }
  1108. }
  1109. // Read data
  1110. if (info->revents & POLLIN) {
  1111. if (faux_async_in(async) < 0) {
  1112. // Someting went wrong
  1113. faux_eloop_del_fd(eloop, info->fd);
  1114. syslog(LOG_ERR, "Can't get data from client");
  1115. return BOOL_FALSE; // Stop event loop
  1116. }
  1117. }
  1118. // EOF
  1119. if (info->revents & POLLHUP) {
  1120. faux_eloop_del_fd(eloop, info->fd);
  1121. syslog(LOG_DEBUG, "Connection %d is closed by client", info->fd);
  1122. return BOOL_FALSE; // Stop event loop
  1123. }
  1124. // POLLERR
  1125. if (info->revents & POLLERR) {
  1126. faux_eloop_del_fd(eloop, info->fd);
  1127. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  1128. return BOOL_FALSE; // Stop event loop
  1129. }
  1130. // POLLNVAL
  1131. if (info->revents & POLLNVAL) {
  1132. faux_eloop_del_fd(eloop, info->fd);
  1133. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  1134. return BOOL_FALSE; // Stop event loop
  1135. }
  1136. type = type; // Happy compiler
  1137. // Session can be really finished here. Note KTPD session can't be
  1138. // stopped immediately so it's only two places within code to really
  1139. // break the loop. This one and within wait_for_action_ev().
  1140. if (ktpd->exit)
  1141. return BOOL_FALSE;
  1142. return BOOL_TRUE;
  1143. }
  1144. #if 0
  1145. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  1146. {
  1147. assert(ktpd);
  1148. if (!ktpd)
  1149. return;
  1150. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  1151. }
  1152. #endif