ktpd_session.c 32 KB

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