ktpd_session.c 32 KB

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