ktpd_session.c 32 KB

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