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. if (kexec_need_stdin(ktpd->exec))
  313. status |= KTP_STATUS_NEED_STDIN;
  314. ack = ktp_msg_preform(cmd, status);
  315. faux_msg_send_async(ack, ktpd->async);
  316. faux_msg_free(ack);
  317. faux_error_free(error);
  318. return BOOL_TRUE; // Continue and wait for ACTION
  319. }
  320. // Here we don't need to wait for the action. We have retcode already.
  321. if (ksession_done(ktpd->session)) {
  322. ktpd->exit = BOOL_TRUE;
  323. status |= KTP_STATUS_EXIT;
  324. }
  325. // Prepare ACK message
  326. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  327. if (rc) {
  328. uint8_t retcode8bit = 0;
  329. retcode8bit = (uint8_t)(retcode & 0xff);
  330. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  331. } else {
  332. faux_msg_set_status(ack, KTP_STATUS_ERROR);
  333. char *err = faux_error_cstr(error);
  334. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  335. faux_str_free(err);
  336. ret = BOOL_FALSE;
  337. }
  338. // Generate prompt
  339. prompt = generate_prompt(ktpd);
  340. if (prompt) {
  341. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  342. faux_str_free(prompt);
  343. }
  344. // Add hotkeys
  345. if (view_was_changed)
  346. add_hotkeys_to_msg(ktpd, ack);
  347. faux_msg_send_async(ack, ktpd->async);
  348. faux_msg_free(ack);
  349. faux_error_free(error);
  350. return ret;
  351. }
  352. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  353. int *retcode, faux_error_t *error,
  354. bool_t dry_run, bool_t *view_was_changed_p)
  355. {
  356. kexec_t *exec = NULL;
  357. assert(ktpd);
  358. if (!ktpd)
  359. return BOOL_FALSE;
  360. // Parsing
  361. exec = ksession_parse_for_exec(ktpd->session, line, error);
  362. if (!exec)
  363. return BOOL_FALSE;
  364. // Set dry-run flag
  365. kexec_set_dry_run(exec, dry_run);
  366. // Session status can be changed while parsing
  367. // NOTE: kexec_t is atomic now
  368. // if (ksession_done(ktpd->session)) {
  369. // kexec_free(exec);
  370. // return BOOL_FALSE; // Because action is not completed
  371. // }
  372. // Execute kexec and then wait for completion using global Eloop
  373. if (!kexec_exec(exec)) {
  374. kexec_free(exec);
  375. return BOOL_FALSE; // Something went wrong
  376. }
  377. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  378. // we don't need event loop and can return here.
  379. if (kexec_retcode(exec, retcode)) {
  380. if (view_was_changed_p)
  381. *view_was_changed_p = !kpath_is_equal(
  382. ksession_path(ktpd->session),
  383. kexec_saved_path(exec));
  384. kexec_free(exec);
  385. return BOOL_TRUE;
  386. }
  387. // Save kexec pointer to use later
  388. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  389. ktpd->exec = exec;
  390. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  391. action_stdout_ev, ktpd);
  392. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  393. action_stderr_ev, ktpd);
  394. return BOOL_TRUE;
  395. }
  396. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  397. void *associated_data, void *user_data)
  398. {
  399. int wstatus = 0;
  400. pid_t child_pid = -1;
  401. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  402. int retcode = -1;
  403. uint8_t retcode8bit = 0;
  404. faux_msg_t *ack = NULL;
  405. ktp_cmd_e cmd = KTP_CMD_ACK;
  406. uint32_t status = KTP_STATUS_NONE;
  407. char *prompt = NULL;
  408. bool_t view_was_changed = BOOL_FALSE;
  409. if (!ktpd)
  410. return BOOL_FALSE;
  411. // Wait for any child process. Doesn't block.
  412. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  413. if (ktpd->exec)
  414. kexec_continue_command_execution(ktpd->exec, child_pid,
  415. wstatus);
  416. }
  417. if (!ktpd->exec)
  418. return BOOL_TRUE;
  419. // Check if kexec is done now
  420. if (!kexec_retcode(ktpd->exec, &retcode))
  421. return BOOL_TRUE; // Continue
  422. // Sometimes SIGCHILD signal can appear before all data were really read
  423. // from process stdout buffer. So read the least data before closing
  424. // file descriptors and send it to client.
  425. get_stream(ktpd, kexec_stdout(ktpd->exec), BOOL_FALSE);
  426. get_stream(ktpd, kexec_stderr(ktpd->exec), BOOL_TRUE);
  427. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  428. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  429. view_was_changed = !kpath_is_equal(
  430. ksession_path(ktpd->session), kexec_saved_path(ktpd->exec));
  431. kexec_free(ktpd->exec);
  432. ktpd->exec = NULL;
  433. ktpd->state = KTPD_SESSION_STATE_IDLE;
  434. // All kexec_t actions are done so can break the loop if needed.
  435. if (ksession_done(ktpd->session)) {
  436. ktpd->exit = BOOL_TRUE;
  437. status |= KTP_STATUS_EXIT; // Notify client about exiting
  438. }
  439. // Send ACK message
  440. ack = ktp_msg_preform(cmd, status);
  441. retcode8bit = (uint8_t)(retcode & 0xff);
  442. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  443. // Generate prompt
  444. prompt = generate_prompt(ktpd);
  445. if (prompt) {
  446. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  447. faux_str_free(prompt);
  448. }
  449. // Add hotkeys
  450. if (view_was_changed)
  451. add_hotkeys_to_msg(ktpd, ack);
  452. faux_msg_send_async(ack, ktpd->async);
  453. faux_msg_free(ack);
  454. type = type; // Happy compiler
  455. associated_data = associated_data; // Happy compiler
  456. if (ktpd->exit)
  457. return BOOL_FALSE;
  458. return BOOL_TRUE;
  459. }
  460. static int compl_compare(const void *first, const void *second)
  461. {
  462. const char *f = (const char *)first;
  463. const char *s = (const char *)second;
  464. return strcmp(f, s);
  465. }
  466. static int compl_kcompare(const void *key, const void *list_item)
  467. {
  468. const char *f = (const char *)key;
  469. const char *s = (const char *)list_item;
  470. return strcmp(f, s);
  471. }
  472. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  473. {
  474. char *line = NULL;
  475. faux_msg_t *ack = NULL;
  476. kpargv_t *pargv = NULL;
  477. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  478. uint32_t status = KTP_STATUS_NONE;
  479. const char *prefix = NULL;
  480. size_t prefix_len = 0;
  481. assert(ktpd);
  482. assert(msg);
  483. // Get line from message
  484. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  485. ktp_send_error(ktpd->async, cmd, NULL);
  486. return BOOL_FALSE;
  487. }
  488. // Parsing
  489. pargv = ksession_parse_for_completion(ktpd->session, line);
  490. faux_str_free(line);
  491. if (!pargv) {
  492. ktp_send_error(ktpd->async, cmd, NULL);
  493. return BOOL_FALSE;
  494. }
  495. kpargv_debug(pargv);
  496. if (ksession_done(ktpd->session)) {
  497. ktpd->exit = BOOL_TRUE;
  498. status |= KTP_STATUS_EXIT; // Notify client about exiting
  499. }
  500. // Prepare ACK message
  501. ack = ktp_msg_preform(cmd, status);
  502. // Last unfinished word. Common prefix for all completions
  503. prefix = kpargv_last_arg(pargv);
  504. if (!faux_str_is_empty(prefix)) {
  505. prefix_len = strlen(prefix);
  506. faux_msg_add_param(ack, KTP_PARAM_PREFIX, prefix, prefix_len);
  507. }
  508. // Fill msg with possible completions
  509. if (!kpargv_completions_is_empty(pargv)) {
  510. const kentry_t *candidate = NULL;
  511. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  512. faux_list_node_t *compl_iter = NULL;
  513. faux_list_t *completions = NULL;
  514. char *compl_str = NULL;
  515. completions = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  516. compl_compare, compl_kcompare,
  517. (void (*)(void *))faux_str_free);
  518. while ((candidate = kpargv_completions_each(&citer))) {
  519. const kentry_t *completion = NULL;
  520. kparg_t *parg = NULL;
  521. int rc = -1;
  522. char *out = NULL;
  523. bool_t res = BOOL_FALSE;
  524. char *l = NULL; // One line of completion
  525. const char *str = NULL;
  526. // Get completion entry from candidate entry
  527. completion = kentry_nested_by_purpose(candidate,
  528. KENTRY_PURPOSE_COMPLETION);
  529. // If candidate entry doesn't contain completion then try
  530. // to get completion from entry's PTYPE
  531. if (!completion) {
  532. const kentry_t *ptype = NULL;
  533. ptype = kentry_nested_by_purpose(candidate,
  534. KENTRY_PURPOSE_PTYPE);
  535. if (!ptype)
  536. continue;
  537. completion = kentry_nested_by_purpose(ptype,
  538. KENTRY_PURPOSE_COMPLETION);
  539. }
  540. if (!completion)
  541. continue;
  542. parg = kparg_new(candidate, prefix);
  543. kpargv_set_candidate_parg(pargv, parg);
  544. res = ksession_exec_locally(ktpd->session, completion,
  545. pargv, &rc, &out);
  546. kparg_free(parg);
  547. if (!res || (rc < 0) || !out) {
  548. if (out)
  549. faux_str_free(out);
  550. continue;
  551. }
  552. // Get all completions one by one
  553. str = out;
  554. while ((l = faux_str_getline(str, &str))) {
  555. // Compare prefix
  556. if ((prefix_len > 0) &&
  557. (faux_str_cmpn(prefix, l, prefix_len) != 0)) {
  558. faux_str_free(l);
  559. continue;
  560. }
  561. compl_str = l + prefix_len;
  562. faux_list_add(completions, faux_str_dup(compl_str));
  563. faux_str_free(l);
  564. }
  565. faux_str_free(out);
  566. }
  567. // Put completion list to message
  568. compl_iter = faux_list_head(completions);
  569. while ((compl_str = faux_list_each(&compl_iter))) {
  570. faux_msg_add_param(ack, KTP_PARAM_LINE,
  571. compl_str, strlen(compl_str));
  572. }
  573. faux_list_free(completions);
  574. }
  575. faux_msg_send_async(ack, ktpd->async);
  576. faux_msg_free(ack);
  577. kpargv_free(pargv);
  578. return BOOL_TRUE;
  579. }
  580. // The most priority source of help is candidate's help ACTION output. Next
  581. // source is candidate's PTYPE help ACTION output.
  582. // Function generates two lines for one resulting help line. The first
  583. // component is a 'prefix' and the second component is 'text'.
  584. // The 'prefix' can be something like 'ip', 'filter' i.e.
  585. // subcommand or '3..89', '<STRING>' i.e. description of type. The 'text'
  586. // field is description of current parameter. For example 'Interface IP
  587. // address'. So the full help can be:
  588. // AAA.BBB.CCC.DDD Interface IP address
  589. // [ first field ] [ second field ]
  590. //
  591. // If not candidate parameter nor PTYPE contains the help functions the engine
  592. // tries to construct help itself.
  593. //
  594. // It uses the following sources for 'prefix':
  595. // * 'help' field of PTYPE
  596. // * 'value' field of PTYPE
  597. // * 'name' field of PTYPE
  598. // * 'value' field of parameter
  599. // * 'name' field of parameter
  600. //
  601. // Engine uses the following sources for 'text':
  602. // * 'help' field of parameter
  603. // * 'value' field of parameter
  604. // * 'name' field of parameter
  605. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  606. {
  607. char *line = NULL;
  608. faux_msg_t *ack = NULL;
  609. kpargv_t *pargv = NULL;
  610. ktp_cmd_e cmd = KTP_HELP_ACK;
  611. uint32_t status = KTP_STATUS_NONE;
  612. const char *prefix = NULL;
  613. assert(ktpd);
  614. assert(msg);
  615. // Get line from message
  616. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  617. ktp_send_error(ktpd->async, cmd, NULL);
  618. return BOOL_FALSE;
  619. }
  620. // Parsing
  621. pargv = ksession_parse_for_completion(ktpd->session, line);
  622. faux_str_free(line);
  623. if (!pargv) {
  624. ktp_send_error(ktpd->async, cmd, NULL);
  625. return BOOL_FALSE;
  626. }
  627. if (ksession_done(ktpd->session)) {
  628. ktpd->exit = BOOL_TRUE;
  629. status |= KTP_STATUS_EXIT; // Notify client about exiting
  630. }
  631. // Prepare ACK message
  632. ack = ktp_msg_preform(cmd, status);
  633. // Last unfinished word. Common prefix for all entries
  634. prefix = kpargv_last_arg(pargv);
  635. // Fill msg with possible help messages
  636. if (!kpargv_completions_is_empty(pargv)) {
  637. const kentry_t *candidate = NULL;
  638. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  639. faux_list_node_t *help_iter = NULL;
  640. faux_list_t *help_list = NULL;
  641. help_t *help_struct = NULL;
  642. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  643. help_compare, NULL, help_free);
  644. while ((candidate = kpargv_completions_each(&citer))) {
  645. const kentry_t *help = NULL;
  646. const kentry_t *ptype = NULL;
  647. // Get PTYPE of parameter
  648. ptype = kentry_nested_by_purpose(candidate,
  649. KENTRY_PURPOSE_PTYPE);
  650. // Try to get help fn from parameter itself
  651. help = kentry_nested_by_purpose(candidate,
  652. KENTRY_PURPOSE_HELP);
  653. if (!help && ptype)
  654. help = kentry_nested_by_purpose(ptype,
  655. KENTRY_PURPOSE_HELP);
  656. // Generate help with found ACTION
  657. if (help) {
  658. char *out = NULL;
  659. kparg_t *parg = NULL;
  660. int rc = -1;
  661. parg = kparg_new(candidate, prefix);
  662. kpargv_set_candidate_parg(pargv, parg);
  663. ksession_exec_locally(ktpd->session,
  664. help, pargv, &rc, &out);
  665. kparg_free(parg);
  666. if (out) {
  667. const char *str = out;
  668. char *prefix_str = NULL;
  669. char *line_str = NULL;
  670. do {
  671. prefix_str = faux_str_getline(str, &str);
  672. if (!prefix_str)
  673. break;
  674. line_str = faux_str_getline(str, &str);
  675. if (!line_str) {
  676. faux_str_free(prefix_str);
  677. break;
  678. }
  679. help_struct = help_new(prefix_str, line_str);
  680. if (!faux_list_add(help_list, help_struct))
  681. help_free(help_struct);
  682. } while (line_str);
  683. faux_str_free(out);
  684. }
  685. // Generate help with available information
  686. } else {
  687. const char *prefix_str = NULL;
  688. const char *line_str = NULL;
  689. // Prefix_str
  690. if (ptype) {
  691. prefix_str = kentry_help(ptype);
  692. if (!prefix_str)
  693. prefix_str = kentry_value(ptype);
  694. if (!prefix_str)
  695. prefix_str = kentry_name(ptype);
  696. } else {
  697. prefix_str = kentry_value(candidate);
  698. if (!prefix_str)
  699. prefix_str = kentry_name(candidate);
  700. }
  701. assert(prefix_str);
  702. // Line_str
  703. line_str = kentry_help(candidate);
  704. if (!line_str)
  705. line_str = kentry_value(candidate);
  706. if (!line_str)
  707. line_str = kentry_name(candidate);
  708. assert(line_str);
  709. help_struct = help_new(
  710. faux_str_dup(prefix_str),
  711. faux_str_dup(line_str));
  712. if (!faux_list_add(help_list, help_struct))
  713. help_free(help_struct);
  714. }
  715. }
  716. // Put help list to message
  717. help_iter = faux_list_head(help_list);
  718. while ((help_struct = (help_t *)faux_list_each(&help_iter))) {
  719. faux_msg_add_param(ack, KTP_PARAM_PREFIX,
  720. help_struct->prefix, strlen(help_struct->prefix));
  721. faux_msg_add_param(ack, KTP_PARAM_LINE,
  722. help_struct->line, strlen(help_struct->line));
  723. }
  724. faux_list_free(help_list);
  725. }
  726. faux_msg_send_async(ack, ktpd->async);
  727. faux_msg_free(ack);
  728. kpargv_free(pargv);
  729. return BOOL_TRUE;
  730. }
  731. static ssize_t stdin_out(int fd, faux_buf_t *buf)
  732. {
  733. ssize_t total_written = 0;
  734. assert(buf);
  735. if (!buf)
  736. return -1;
  737. assert(fd >= 0);
  738. while (faux_buf_len(buf) > 0) {
  739. ssize_t data_to_write = 0;
  740. ssize_t bytes_written = 0;
  741. void *data = NULL;
  742. data_to_write = faux_buf_dread_lock_easy(buf, &data);
  743. if (data_to_write <= 0)
  744. return -1;
  745. bytes_written = write(fd, data, data_to_write);
  746. if (bytes_written > 0) {
  747. total_written += bytes_written;
  748. faux_buf_dread_unlock_easy(buf, bytes_written);
  749. } else {
  750. faux_buf_dread_unlock_easy(buf, 0);
  751. }
  752. if (bytes_written < 0) {
  753. if ( // Something went wrong
  754. (errno != EINTR) &&
  755. (errno != EAGAIN) &&
  756. (errno != EWOULDBLOCK)
  757. )
  758. return -1;
  759. // Not whole data block was written
  760. } else if (bytes_written != data_to_write) {
  761. break;
  762. }
  763. }
  764. return total_written;
  765. }
  766. static bool_t push_stdin(faux_eloop_t *eloop, faux_eloop_type_e type,
  767. void *associated_data, void *user_data)
  768. {
  769. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  770. faux_buf_t *bufin = NULL;
  771. int fd = -1;
  772. if (!ktpd)
  773. return BOOL_TRUE;
  774. if (!ktpd->exec)
  775. return BOOL_TRUE;
  776. fd = kexec_stdin(ktpd->exec);
  777. if (fd < 0) // Something strange
  778. return BOOL_FALSE;
  779. bufin = kexec_bufin(ktpd->exec);
  780. assert(bufin);
  781. stdin_out(fd, bufin); // Non-blocking write
  782. if (faux_buf_len(bufin) != 0) // Try later
  783. return BOOL_TRUE;
  784. // All data is written
  785. faux_eloop_exclude_fd_event(ktpd->eloop, fd, POLLOUT);
  786. // Happy compiler
  787. eloop = eloop;
  788. type = type;
  789. associated_data = associated_data;
  790. return BOOL_TRUE;
  791. }
  792. static bool_t ktpd_session_process_stdin(ktpd_session_t *ktpd, faux_msg_t *msg)
  793. {
  794. char *line = NULL;
  795. unsigned int len = 0;
  796. faux_buf_t *bufin = NULL;
  797. int fd = -1;
  798. assert(ktpd);
  799. assert(msg);
  800. if (!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