ktpd_session.c 32 KB

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