ktpd_session.c 32 KB

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