ktpd_session.c 32 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301
  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. bool_t help_added = BOOL_FALSE;
  616. // Get PTYPE of parameter
  617. ptype = kentry_nested_by_purpose(candidate,
  618. KENTRY_PURPOSE_PTYPE);
  619. // Try to get help fn from parameter itself
  620. help = kentry_nested_by_purpose(candidate,
  621. KENTRY_PURPOSE_HELP);
  622. if (!help && ptype)
  623. help = kentry_nested_by_purpose(ptype,
  624. KENTRY_PURPOSE_HELP);
  625. // Generate help with found ACTION
  626. if (help) {
  627. char *out = NULL;
  628. kparg_t *parg = NULL;
  629. int rc = -1;
  630. parg = kparg_new(candidate, prefix);
  631. kpargv_set_candidate_parg(pargv, parg);
  632. ksession_exec_locally(ktpd->session,
  633. help, pargv, &rc, &out);
  634. kparg_free(parg);
  635. if (out) {
  636. const char *str = out;
  637. char *prefix_str = NULL;
  638. char *line_str = NULL;
  639. do {
  640. prefix_str = faux_str_getline(str, &str);
  641. if (!prefix_str)
  642. break;
  643. line_str = faux_str_getline(str, &str);
  644. if (!line_str) {
  645. faux_str_free(prefix_str);
  646. break;
  647. }
  648. help_struct = help_new(prefix_str, line_str);
  649. if (faux_list_add(help_list, help_struct))
  650. help_added = BOOL_TRUE;
  651. else
  652. help_free(help_struct);
  653. } while (line_str);
  654. faux_str_free(out);
  655. }
  656. }
  657. // Generate help with available information
  658. if (!help_added) {
  659. const char *prefix_str = NULL;
  660. const char *line_str = NULL;
  661. // Prefix_str
  662. if (ptype) {
  663. prefix_str = kentry_help(ptype);
  664. if (!prefix_str)
  665. prefix_str = kentry_value(ptype);
  666. if (!prefix_str)
  667. prefix_str = kentry_name(ptype);
  668. } else {
  669. prefix_str = kentry_value(candidate);
  670. if (!prefix_str)
  671. prefix_str = kentry_name(candidate);
  672. }
  673. assert(prefix_str);
  674. // Line_str
  675. line_str = kentry_help(candidate);
  676. if (!line_str)
  677. line_str = kentry_value(candidate);
  678. if (!line_str)
  679. line_str = kentry_name(candidate);
  680. assert(line_str);
  681. help_struct = help_new(
  682. faux_str_dup(prefix_str),
  683. faux_str_dup(line_str));
  684. if (!faux_list_add(help_list, help_struct))
  685. help_free(help_struct);
  686. }
  687. }
  688. // Put help list to message
  689. help_iter = faux_list_head(help_list);
  690. while ((help_struct = (help_t *)faux_list_each(&help_iter))) {
  691. faux_msg_add_param(ack, KTP_PARAM_PREFIX,
  692. help_struct->prefix, strlen(help_struct->prefix));
  693. faux_msg_add_param(ack, KTP_PARAM_LINE,
  694. help_struct->line, strlen(help_struct->line));
  695. }
  696. faux_list_free(help_list);
  697. }
  698. faux_msg_send_async(ack, ktpd->async);
  699. faux_msg_free(ack);
  700. kpargv_free(pargv);
  701. return BOOL_TRUE;
  702. }
  703. static ssize_t stdin_out(int fd, faux_buf_t *buf)
  704. {
  705. ssize_t total_written = 0;
  706. assert(buf);
  707. if (!buf)
  708. return -1;
  709. assert(fd >= 0);
  710. while (faux_buf_len(buf) > 0) {
  711. ssize_t data_to_write = 0;
  712. ssize_t bytes_written = 0;
  713. void *data = NULL;
  714. data_to_write = faux_buf_dread_lock_easy(buf, &data);
  715. if (data_to_write <= 0)
  716. return -1;
  717. bytes_written = write(fd, data, data_to_write);
  718. if (bytes_written > 0) {
  719. total_written += bytes_written;
  720. faux_buf_dread_unlock_easy(buf, bytes_written);
  721. } else {
  722. faux_buf_dread_unlock_easy(buf, 0);
  723. }
  724. if (bytes_written < 0) {
  725. if ( // Something went wrong
  726. (errno != EINTR) &&
  727. (errno != EAGAIN) &&
  728. (errno != EWOULDBLOCK)
  729. )
  730. return -1;
  731. // Not whole data block was written
  732. } else if (bytes_written != data_to_write) {
  733. break;
  734. }
  735. }
  736. return total_written;
  737. }
  738. static bool_t push_stdin(faux_eloop_t *eloop, faux_eloop_type_e type,
  739. void *associated_data, void *user_data)
  740. {
  741. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  742. faux_buf_t *bufin = NULL;
  743. int fd = -1;
  744. if (!ktpd)
  745. return BOOL_TRUE;
  746. if (!ktpd->exec)
  747. return BOOL_TRUE;
  748. fd = kexec_stdin(ktpd->exec);
  749. if (fd < 0) // Something strange
  750. return BOOL_FALSE;
  751. bufin = kexec_bufin(ktpd->exec);
  752. assert(bufin);
  753. stdin_out(fd, bufin); // Non-blocking write
  754. if (faux_buf_len(bufin) != 0) // Try later
  755. return BOOL_TRUE;
  756. // All data is written
  757. faux_eloop_exclude_fd_event(ktpd->eloop, fd, POLLOUT);
  758. // Happy compiler
  759. eloop = eloop;
  760. type = type;
  761. associated_data = associated_data;
  762. return BOOL_TRUE;
  763. }
  764. static bool_t ktpd_session_process_stdin(ktpd_session_t *ktpd, faux_msg_t *msg)
  765. {
  766. char *line = NULL;
  767. unsigned int len = 0;
  768. faux_buf_t *bufin = NULL;
  769. int fd = -1;
  770. assert(ktpd);
  771. assert(msg);
  772. if (!ktpd->exec)
  773. return BOOL_FALSE;
  774. if (!kexec_interactive(ktpd->exec))
  775. return BOOL_FALSE;
  776. fd = kexec_stdin(ktpd->exec);
  777. if (fd < 0)
  778. return BOOL_FALSE;
  779. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_LINE, (void **)&line, &len))
  780. return BOOL_TRUE; // It's strange but not a bug
  781. if (len == 0)
  782. return BOOL_TRUE;
  783. bufin = kexec_bufin(ktpd->exec);
  784. assert(bufin);
  785. faux_buf_write(bufin, line, len);
  786. stdin_out(fd, bufin); // Non-blocking write
  787. if (faux_buf_len(bufin) == 0)
  788. return BOOL_TRUE;
  789. // Non-blocking write can't write all data so plan to write later
  790. faux_eloop_include_fd_event(ktpd->eloop, fd, POLLOUT);
  791. return BOOL_TRUE;
  792. }
  793. static bool_t ktpd_session_process_winch(ktpd_session_t *ktpd, faux_msg_t *msg)
  794. {
  795. char *line = NULL;
  796. char *p = NULL;
  797. unsigned short width = 0;
  798. unsigned short height = 0;
  799. assert(ktpd);
  800. assert(msg);
  801. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_WINCH)))
  802. return BOOL_TRUE;
  803. p = strchr(line, ' ');
  804. if (!p || (p == line)) {
  805. faux_str_free(line);
  806. return BOOL_FALSE;
  807. }
  808. if (!faux_conv_atous(line, &width, 0)) {
  809. faux_str_free(line);
  810. return BOOL_FALSE;
  811. }
  812. if (!faux_conv_atous(p + 1, &height, 0)) {
  813. faux_str_free(line);
  814. return BOOL_FALSE;
  815. }
  816. ksession_set_term_width(ktpd->session, width);
  817. ksession_set_term_height(ktpd->session, height);
  818. faux_str_free(line);
  819. if (!ktpd->exec)
  820. return BOOL_TRUE;
  821. // Set pseudo terminal window size
  822. kexec_set_winsize(ktpd->exec);
  823. return BOOL_TRUE;
  824. }
  825. static bool_t ktpd_session_process_notification(ktpd_session_t *ktpd, faux_msg_t *msg)
  826. {
  827. assert(ktpd);
  828. assert(msg);
  829. ktpd_session_process_winch(ktpd, msg);
  830. return BOOL_TRUE;
  831. }
  832. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  833. {
  834. uint16_t cmd = 0;
  835. assert(ktpd);
  836. if (!ktpd)
  837. return BOOL_FALSE;
  838. assert(msg);
  839. if (!msg)
  840. return BOOL_FALSE;
  841. cmd = faux_msg_get_cmd(msg);
  842. switch (cmd) {
  843. case KTP_AUTH:
  844. if ((ktpd->state != KTPD_SESSION_STATE_UNAUTHORIZED) &&
  845. (ktpd->state != KTPD_SESSION_STATE_IDLE))
  846. break;
  847. ktpd_session_process_auth(ktpd, msg);
  848. break;
  849. case KTP_CMD:
  850. if (ktpd->state != KTPD_SESSION_STATE_IDLE)
  851. break;
  852. ktpd_session_process_cmd(ktpd, msg);
  853. break;
  854. case KTP_COMPLETION:
  855. if (ktpd->state != KTPD_SESSION_STATE_IDLE)
  856. break;
  857. ktpd_session_process_completion(ktpd, msg);
  858. break;
  859. case KTP_HELP:
  860. if (ktpd->state != KTPD_SESSION_STATE_IDLE)
  861. break;
  862. ktpd_session_process_help(ktpd, msg);
  863. break;
  864. case KTP_STDIN:
  865. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS)
  866. break;
  867. ktpd_session_process_stdin(ktpd, msg);
  868. break;
  869. case KTP_NOTIFICATION:
  870. ktpd_session_process_notification(ktpd, msg);
  871. break;
  872. default:
  873. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd);
  874. break;
  875. }
  876. return BOOL_TRUE;
  877. }
  878. /** @brief Low-level function to receive KTP message.
  879. *
  880. * Firstly function gets the header of message. Then it checks and parses
  881. * header and find out the length of whole message. Then it receives the rest
  882. * of message.
  883. */
  884. static bool_t ktpd_session_read_cb(faux_async_t *async,
  885. faux_buf_t *buf, size_t len, void *user_data)
  886. {
  887. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  888. faux_msg_t *completed_msg = NULL;
  889. char *data = NULL;
  890. assert(async);
  891. assert(buf);
  892. assert(ktpd);
  893. // Linearize buffer
  894. data = malloc(len);
  895. faux_buf_read(buf, data, len);
  896. // Receive header
  897. if (!ktpd->hdr) {
  898. size_t whole_len = 0;
  899. size_t msg_wo_hdr = 0;
  900. ktpd->hdr = (faux_hdr_t *)data;
  901. // Check for broken header
  902. if (!ktp_check_header(ktpd->hdr)) {
  903. faux_free(ktpd->hdr);
  904. ktpd->hdr = NULL;
  905. return BOOL_FALSE;
  906. }
  907. whole_len = faux_hdr_len(ktpd->hdr);
  908. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  909. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  910. // Plan to receive message body
  911. if (msg_wo_hdr > 0) {
  912. faux_async_set_read_limits(async,
  913. msg_wo_hdr, msg_wo_hdr);
  914. return BOOL_TRUE;
  915. }
  916. // Here message is completed (msg body has zero length)
  917. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  918. // Receive message body
  919. } else {
  920. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  921. faux_free(data);
  922. }
  923. // Plan to receive msg header
  924. faux_async_set_read_limits(ktpd->async,
  925. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  926. faux_free(ktpd->hdr);
  927. ktpd->hdr = NULL; // Ready to recv new header
  928. // Here message is completed
  929. ktpd_session_dispatch(ktpd, completed_msg);
  930. faux_msg_free(completed_msg);
  931. return BOOL_TRUE;
  932. }
  933. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  934. {
  935. assert(ktpd);
  936. if (!ktpd)
  937. return BOOL_FALSE;
  938. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  939. return BOOL_FALSE;
  940. return BOOL_TRUE;
  941. }
  942. int ktpd_session_fd(const ktpd_session_t *ktpd)
  943. {
  944. assert(ktpd);
  945. if (!ktpd)
  946. return BOOL_FALSE;
  947. return faux_async_fd(ktpd->async);
  948. }
  949. static bool_t get_stream(ktpd_session_t *ktpd, int fd, bool_t is_stderr)
  950. {
  951. ssize_t r = -1;
  952. faux_buf_t *faux_buf = NULL;
  953. char *buf = NULL;
  954. ssize_t len = 0;
  955. faux_msg_t *ack = NULL;
  956. if (!ktpd)
  957. return BOOL_TRUE;
  958. if (!ktpd->exec)
  959. return BOOL_TRUE;
  960. if (is_stderr)
  961. faux_buf = kexec_buferr(ktpd->exec);
  962. else
  963. faux_buf = kexec_bufout(ktpd->exec);
  964. assert(faux_buf);
  965. do {
  966. void *linear_buf = NULL;
  967. ssize_t really_readed = 0;
  968. ssize_t linear_len =
  969. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  970. // Non-blocked read. The fd became non-blocked while
  971. // kexec_prepare().
  972. r = read(fd, linear_buf, linear_len);
  973. if (r > 0)
  974. really_readed = r;
  975. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  976. } while (r > 0);
  977. len = faux_buf_len(faux_buf);
  978. if (0 == len)
  979. return BOOL_TRUE;
  980. buf = malloc(len);
  981. faux_buf_read(faux_buf, buf, len);
  982. // Create KTP_STDOUT/KTP_STDERR message to send to client
  983. ack = ktp_msg_preform(is_stderr ? KTP_STDERR : KTP_STDOUT, KTP_STATUS_NONE);
  984. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  985. faux_msg_send_async(ack, ktpd->async);
  986. faux_msg_free(ack);
  987. free(buf);
  988. return BOOL_TRUE;
  989. }
  990. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  991. void *associated_data, void *user_data)
  992. {
  993. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  994. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  995. // Interactive command use these function as callback not only for
  996. // getting stdout but for writing stdin too. Because pseudo-terminal
  997. // uses the same fd for in and out.
  998. if (info->revents & POLLOUT)
  999. push_stdin(eloop, type, associated_data, user_data);
  1000. if (info->revents & POLLIN)
  1001. get_stream(ktpd, info->fd, BOOL_FALSE);
  1002. // Some errors or fd is closed so remove it from polling
  1003. // EOF || POLERR || POLLNVAL
  1004. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1005. faux_eloop_del_fd(eloop, info->fd);
  1006. return BOOL_TRUE;
  1007. }
  1008. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1009. void *associated_data, void *user_data)
  1010. {
  1011. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1012. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1013. if (info->revents & POLLIN)
  1014. get_stream(ktpd, info->fd, BOOL_TRUE);
  1015. // Some errors or fd is closed so remove it from polling
  1016. // EOF || POLERR || POLLNVAL
  1017. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1018. faux_eloop_del_fd(eloop, info->fd);
  1019. type = type; // Happy compiler
  1020. return BOOL_TRUE;
  1021. }
  1022. bool_t client_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. faux_async_t *async = ktpd->async;
  1028. assert(async);
  1029. // Write data
  1030. if (info->revents & POLLOUT) {
  1031. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  1032. if (faux_async_out(async) < 0) {
  1033. // Someting went wrong
  1034. faux_eloop_del_fd(eloop, info->fd);
  1035. syslog(LOG_ERR, "Can't send data to client");
  1036. return BOOL_FALSE; // Stop event loop
  1037. }
  1038. }
  1039. // Read data
  1040. if (info->revents & POLLIN) {
  1041. if (faux_async_in(async) < 0) {
  1042. // Someting went wrong
  1043. faux_eloop_del_fd(eloop, info->fd);
  1044. syslog(LOG_ERR, "Can't get data from client");
  1045. return BOOL_FALSE; // Stop event loop
  1046. }
  1047. }
  1048. // EOF
  1049. if (info->revents & POLLHUP) {
  1050. faux_eloop_del_fd(eloop, info->fd);
  1051. syslog(LOG_DEBUG, "Connection %d is closed by client", info->fd);
  1052. return BOOL_FALSE; // Stop event loop
  1053. }
  1054. // POLLERR
  1055. if (info->revents & POLLERR) {
  1056. faux_eloop_del_fd(eloop, info->fd);
  1057. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  1058. return BOOL_FALSE; // Stop event loop
  1059. }
  1060. // POLLNVAL
  1061. if (info->revents & POLLNVAL) {
  1062. faux_eloop_del_fd(eloop, info->fd);
  1063. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  1064. return BOOL_FALSE; // Stop event loop
  1065. }
  1066. type = type; // Happy compiler
  1067. // Session can be really finished here. Note KTPD session can't be
  1068. // stopped immediately so it's only two places within code to really
  1069. // break the loop. This one and within wait_for_action_ev().
  1070. if (ktpd->exit)
  1071. return BOOL_FALSE;
  1072. return BOOL_TRUE;
  1073. }
  1074. #if 0
  1075. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  1076. {
  1077. assert(ktpd);
  1078. if (!ktpd)
  1079. return;
  1080. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  1081. }
  1082. #endif