ktpd_session.c 33 KB

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