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. faux_async_t *async; // Object for data exchange with client (KTP)
  34. faux_hdr_t *hdr; // Engine will receive header and then msg
  35. faux_eloop_t *eloop; // External link, dont's free()
  36. kexec_t *exec;
  37. bool_t exit;
  38. };
  39. // Static declarations
  40. static bool_t ktpd_session_read_cb(faux_async_t *async,
  41. faux_buf_t *buf, size_t len, void *user_data);
  42. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  43. void *associated_data, void *user_data);
  44. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  45. void *associated_data, void *user_data);
  46. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  47. int *retcode, faux_error_t *error,
  48. bool_t dry_run, bool_t *view_was_changed);
  49. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  50. void *associated_data, void *user_data);
  51. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  52. void *associated_data, void *user_data);
  53. static bool_t get_stream(ktpd_session_t *ktpd, int fd, bool_t is_stderr);
  54. ktpd_session_t *ktpd_session_new(int sock, kscheme_t *scheme,
  55. const char *start_entry, faux_eloop_t *eloop)
  56. {
  57. ktpd_session_t *ktpd = NULL;
  58. if (sock < 0)
  59. return NULL;
  60. if (!eloop)
  61. return NULL;
  62. ktpd = faux_zmalloc(sizeof(*ktpd));
  63. assert(ktpd);
  64. if (!ktpd)
  65. return NULL;
  66. // Init
  67. ktpd->state = KTPD_SESSION_STATE_IDLE;
  68. ktpd->eloop = eloop;
  69. ktpd->session = ksession_new(scheme, start_entry);
  70. if (!ktpd->session) {
  71. faux_free(ktpd);
  72. return NULL;
  73. }
  74. ktpd->exec = NULL;
  75. // Exit flag. It differs from ksession done flag because KTPD session
  76. // can't exit immediately. It must finish current command processing
  77. // before really stop the event loop. Note: User defined plugin
  78. // function must use ksession done flag. This exit flag is internal
  79. // feature of KTPD session.
  80. ktpd->exit = BOOL_FALSE;
  81. // Async object
  82. ktpd->async = faux_async_new(sock);
  83. assert(ktpd->async);
  84. // Receive message header first
  85. faux_async_set_read_limits(ktpd->async,
  86. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  87. faux_async_set_read_cb(ktpd->async, ktpd_session_read_cb, ktpd);
  88. ktpd->hdr = NULL;
  89. faux_async_set_stall_cb(ktpd->async, ktp_stall_cb, ktpd->eloop);
  90. // Eloop callbacks
  91. faux_eloop_add_fd(ktpd->eloop, ktpd_session_fd(ktpd), POLLIN,
  92. client_ev, ktpd);
  93. faux_eloop_add_signal(ktpd->eloop, SIGCHLD, wait_for_actions_ev, ktpd);
  94. return ktpd;
  95. }
  96. void ktpd_session_free(ktpd_session_t *ktpd)
  97. {
  98. if (!ktpd)
  99. return;
  100. kexec_free(ktpd->exec);
  101. ksession_free(ktpd->session);
  102. faux_free(ktpd->hdr);
  103. close(ktpd_session_fd(ktpd));
  104. faux_async_free(ktpd->async);
  105. faux_free(ktpd);
  106. }
  107. static char *generate_prompt(ktpd_session_t *ktpd)
  108. {
  109. kpath_levels_node_t *iter = NULL;
  110. klevel_t *level = NULL;
  111. char *prompt = NULL;
  112. iter = kpath_iterr(ksession_path(ktpd->session));
  113. while ((level = kpath_eachr(&iter))) {
  114. const kentry_t *view = klevel_entry(level);
  115. kentry_t *prompt_entry = kentry_nested_by_purpose(view,
  116. KENTRY_PURPOSE_PROMPT);
  117. if (!prompt_entry)
  118. continue;
  119. if (kentry_actions_len(prompt_entry) > 0) {
  120. int rc = -1;
  121. bool_t res = BOOL_FALSE;
  122. res = ksession_exec_locally(ktpd->session,
  123. prompt_entry, NULL, &rc, &prompt);
  124. if (!res || (rc < 0) || !prompt) {
  125. if (prompt)
  126. faux_str_free(prompt);
  127. prompt = NULL;
  128. }
  129. }
  130. if (!prompt) {
  131. if (kentry_value(prompt_entry))
  132. prompt = faux_str_dup(kentry_value(prompt_entry));
  133. }
  134. if (prompt)
  135. break;
  136. }
  137. return prompt;
  138. }
  139. // Format: <key>'\0'<cmd>
  140. static bool_t add_hotkey(faux_msg_t *msg, khotkey_t *hotkey)
  141. {
  142. const char *key = NULL;
  143. const char *cmd = NULL;
  144. char *whole_str = NULL;
  145. size_t key_s = 0;
  146. size_t cmd_s = 0;
  147. key = khotkey_key(hotkey);
  148. key_s = strlen(key);
  149. cmd = khotkey_cmd(hotkey);
  150. cmd_s = strlen(cmd);
  151. whole_str = faux_zmalloc(key_s + 1 + cmd_s);
  152. memcpy(whole_str, key, key_s);
  153. memcpy(whole_str + key_s + 1, cmd, cmd_s);
  154. faux_msg_add_param(msg, KTP_PARAM_HOTKEY, whole_str, key_s + 1 + cmd_s);
  155. faux_free(whole_str);
  156. return BOOL_TRUE;
  157. }
  158. static bool_t add_hotkeys_to_msg(ktpd_session_t *ktpd, faux_msg_t *msg)
  159. {
  160. faux_list_t *list = NULL;
  161. kpath_t *path = NULL;
  162. kentry_hotkeys_node_t *l_iter = NULL;
  163. khotkey_t *hotkey = NULL;
  164. assert(ktpd);
  165. assert(msg);
  166. path = ksession_path(ktpd->session);
  167. assert(path);
  168. if (kpath_len(path) == 1) {
  169. // We don't need additional list because there is only one
  170. // VIEW in the path so hotkey's list is only one too. Get it.
  171. list = kentry_hotkeys(klevel_entry(
  172. (klevel_t *)faux_list_data(kpath_iter(path))));
  173. } else {
  174. faux_list_node_t *iterr = NULL;
  175. klevel_t *level = NULL;
  176. // Create temp hotkeys list to add hotkeys from all VIEWs in
  177. // the path and exclude duplications. Don't free elements
  178. // because they are just a references.
  179. list = faux_list_new(FAUX_LIST_UNSORTED, FAUX_LIST_UNIQUE,
  180. kentry_hotkey_compare, NULL, NULL);
  181. // Begin with the end. Because hotkeys from nested VIEWs has
  182. // higher priority.
  183. iterr = kpath_iterr(path);
  184. while ((level = kpath_eachr(&iterr))) {
  185. const kentry_t *entry = klevel_entry(level);
  186. kentry_hotkeys_node_t *hk_iter = kentry_hotkeys_iter(entry);
  187. while ((hotkey = kentry_hotkeys_each(&hk_iter)))
  188. faux_list_add(list, hotkey);
  189. }
  190. }
  191. // Add found hotkeys to msg
  192. l_iter = faux_list_head(list);
  193. while ((hotkey = (khotkey_t *)faux_list_each(&l_iter)))
  194. add_hotkey(msg, hotkey);
  195. if (kpath_len(path) != 1)
  196. faux_list_free(list);
  197. return BOOL_TRUE;
  198. }
  199. // Now it's not really an auth function. Just a hand-shake with client and
  200. // passing prompt to client.
  201. static bool_t ktpd_session_process_auth(ktpd_session_t *ktpd, faux_msg_t *msg)
  202. {
  203. ktp_cmd_e cmd = KTP_AUTH_ACK;
  204. uint32_t status = KTP_STATUS_NONE;
  205. char *prompt = NULL;
  206. uint8_t retcode8bit = 0;
  207. assert(ktpd);
  208. assert(msg);
  209. // Prepare ACK message
  210. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  211. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  212. // Generate prompt
  213. prompt = generate_prompt(ktpd);
  214. if (prompt) {
  215. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  216. faux_str_free(prompt);
  217. }
  218. add_hotkeys_to_msg(ktpd, ack);
  219. faux_msg_send_async(ack, ktpd->async);
  220. faux_msg_free(ack);
  221. return BOOL_TRUE;
  222. }
  223. static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
  224. {
  225. char *line = NULL;
  226. int retcode = -1;
  227. ktp_cmd_e cmd = KTP_CMD_ACK;
  228. faux_error_t *error = NULL;
  229. bool_t rc = BOOL_FALSE;
  230. bool_t dry_run = BOOL_FALSE;
  231. uint32_t status = KTP_STATUS_NONE;
  232. bool_t ret = BOOL_TRUE;
  233. char *prompt = NULL;
  234. bool_t view_was_changed = BOOL_FALSE;
  235. assert(ktpd);
  236. assert(msg);
  237. // Get line from message
  238. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  239. ktp_send_error(ktpd->async, cmd, "The line is not specified");
  240. return BOOL_FALSE;
  241. }
  242. // Get dry-run flag from message
  243. if (KTP_STATUS_IS_DRY_RUN(faux_msg_get_status(msg)))
  244. dry_run = BOOL_TRUE;
  245. error = faux_error_new();
  246. ktpd->exec = NULL;
  247. rc = ktpd_session_exec(ktpd, line, &retcode, error,
  248. dry_run, &view_was_changed);
  249. faux_str_free(line);
  250. // Command is scheduled. Eloop will wait for ACTION completion.
  251. // So inform client about it and about command features like
  252. // interactive/non-interactive.
  253. if (ktpd->exec) {
  254. faux_msg_t *ack = NULL;
  255. ktp_status_e status = KTP_STATUS_INCOMPLETED;
  256. if (kexec_interactive(ktpd->exec))
  257. status |= KTP_STATUS_INTERACTIVE;
  258. ack = ktp_msg_preform(cmd, status);
  259. faux_msg_send_async(ack, ktpd->async);
  260. faux_msg_free(ack);
  261. faux_error_free(error);
  262. return BOOL_TRUE; // Continue and wait for ACTION
  263. }
  264. // Here we don't need to wait for the action. We have retcode already.
  265. if (ksession_done(ktpd->session)) {
  266. ktpd->exit = BOOL_TRUE;
  267. status |= KTP_STATUS_EXIT;
  268. }
  269. // Prepare ACK message
  270. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  271. if (rc) {
  272. uint8_t retcode8bit = 0;
  273. retcode8bit = (uint8_t)(retcode & 0xff);
  274. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  275. } else {
  276. faux_msg_set_status(ack, KTP_STATUS_ERROR);
  277. char *err = faux_error_cstr(error);
  278. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  279. faux_str_free(err);
  280. ret = BOOL_FALSE;
  281. }
  282. // Generate prompt
  283. prompt = generate_prompt(ktpd);
  284. if (prompt) {
  285. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  286. faux_str_free(prompt);
  287. }
  288. // Add hotkeys
  289. if (view_was_changed)
  290. add_hotkeys_to_msg(ktpd, ack);
  291. faux_msg_send_async(ack, ktpd->async);
  292. faux_msg_free(ack);
  293. faux_error_free(error);
  294. return ret;
  295. }
  296. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  297. int *retcode, faux_error_t *error,
  298. bool_t dry_run, bool_t *view_was_changed_p)
  299. {
  300. kexec_t *exec = NULL;
  301. assert(ktpd);
  302. if (!ktpd)
  303. return BOOL_FALSE;
  304. // Parsing
  305. exec = ksession_parse_for_exec(ktpd->session, line, error);
  306. if (!exec)
  307. return BOOL_FALSE;
  308. // Set dry-run flag
  309. kexec_set_dry_run(exec, dry_run);
  310. // Session status can be changed while parsing
  311. // NOTE: kexec_t is atomic now
  312. // if (ksession_done(ktpd->session)) {
  313. // kexec_free(exec);
  314. // return BOOL_FALSE; // Because action is not completed
  315. // }
  316. // Execute kexec and then wait for completion using global Eloop
  317. if (!kexec_exec(exec)) {
  318. kexec_free(exec);
  319. return BOOL_FALSE; // Something went wrong
  320. }
  321. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  322. // we don't need event loop and can return here.
  323. if (kexec_retcode(exec, retcode)) {
  324. if (view_was_changed_p)
  325. *view_was_changed_p = !kpath_is_equal(
  326. ksession_path(ktpd->session),
  327. kexec_saved_path(exec));
  328. kexec_free(exec);
  329. return BOOL_TRUE;
  330. }
  331. // Save kexec pointer to use later
  332. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  333. ktpd->exec = exec;
  334. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  335. action_stdout_ev, ktpd);
  336. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  337. action_stderr_ev, ktpd);
  338. return BOOL_TRUE;
  339. }
  340. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  341. void *associated_data, void *user_data)
  342. {
  343. int wstatus = 0;
  344. pid_t child_pid = -1;
  345. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  346. int retcode = -1;
  347. uint8_t retcode8bit = 0;
  348. faux_msg_t *ack = NULL;
  349. ktp_cmd_e cmd = KTP_CMD_ACK;
  350. uint32_t status = KTP_STATUS_NONE;
  351. char *prompt = NULL;
  352. bool_t view_was_changed = BOOL_FALSE;
  353. if (!ktpd)
  354. return BOOL_FALSE;
  355. // Wait for any child process. Doesn't block.
  356. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  357. if (ktpd->exec)
  358. kexec_continue_command_execution(ktpd->exec, child_pid,
  359. wstatus);
  360. }
  361. if (!ktpd->exec)
  362. return BOOL_TRUE;
  363. // Check if kexec is done now
  364. if (!kexec_retcode(ktpd->exec, &retcode))
  365. return BOOL_TRUE; // Continue
  366. // Sometimes SIGCHILD signal can appear before all data were really read
  367. // from process stdout buffer. So read the least data before closing
  368. // file descriptors and send it to client.
  369. get_stream(ktpd, kexec_stdout(ktpd->exec), BOOL_FALSE);
  370. get_stream(ktpd, kexec_stderr(ktpd->exec), BOOL_TRUE);
  371. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  372. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  373. view_was_changed = !kpath_is_equal(
  374. ksession_path(ktpd->session), kexec_saved_path(ktpd->exec));
  375. kexec_free(ktpd->exec);
  376. ktpd->exec = NULL;
  377. ktpd->state = KTPD_SESSION_STATE_IDLE;
  378. // All kexec_t actions are done so can break the loop if needed.
  379. if (ksession_done(ktpd->session)) {
  380. ktpd->exit = BOOL_TRUE;
  381. status |= KTP_STATUS_EXIT; // Notify client about exiting
  382. }
  383. // Send ACK message
  384. ack = ktp_msg_preform(cmd, status);
  385. retcode8bit = (uint8_t)(retcode & 0xff);
  386. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  387. // Generate prompt
  388. prompt = generate_prompt(ktpd);
  389. if (prompt) {
  390. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  391. faux_str_free(prompt);
  392. }
  393. // Add hotkeys
  394. if (view_was_changed)
  395. add_hotkeys_to_msg(ktpd, ack);
  396. faux_msg_send_async(ack, ktpd->async);
  397. faux_msg_free(ack);
  398. type = type; // Happy compiler
  399. associated_data = associated_data; // Happy compiler
  400. if (ktpd->exit)
  401. return BOOL_FALSE;
  402. return BOOL_TRUE;
  403. }
  404. static int compl_compare(const void *first, const void *second)
  405. {
  406. const char *f = (const char *)first;
  407. const char *s = (const char *)second;
  408. return strcmp(f, s);
  409. }
  410. static int compl_kcompare(const void *key, const void *list_item)
  411. {
  412. const char *f = (const char *)key;
  413. const char *s = (const char *)list_item;
  414. return strcmp(f, s);
  415. }
  416. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  417. {
  418. char *line = NULL;
  419. faux_msg_t *ack = NULL;
  420. kpargv_t *pargv = NULL;
  421. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  422. uint32_t status = KTP_STATUS_NONE;
  423. const char *prefix = NULL;
  424. size_t prefix_len = 0;
  425. assert(ktpd);
  426. assert(msg);
  427. // Get line from message
  428. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  429. ktp_send_error(ktpd->async, cmd, NULL);
  430. return BOOL_FALSE;
  431. }
  432. // Parsing
  433. pargv = ksession_parse_for_completion(ktpd->session, line);
  434. faux_str_free(line);
  435. if (!pargv) {
  436. ktp_send_error(ktpd->async, cmd, NULL);
  437. return BOOL_FALSE;
  438. }
  439. kpargv_debug(pargv);
  440. if (ksession_done(ktpd->session)) {
  441. ktpd->exit = BOOL_TRUE;
  442. status |= KTP_STATUS_EXIT; // Notify client about exiting
  443. }
  444. // Prepare ACK message
  445. ack = ktp_msg_preform(cmd, status);
  446. // Last unfinished word. Common prefix for all completions
  447. prefix = kpargv_last_arg(pargv);
  448. if (!faux_str_is_empty(prefix)) {
  449. prefix_len = strlen(prefix);
  450. faux_msg_add_param(ack, KTP_PARAM_PREFIX, prefix, prefix_len);
  451. }
  452. // Fill msg with possible completions
  453. if (!kpargv_completions_is_empty(pargv)) {
  454. const kentry_t *candidate = NULL;
  455. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  456. faux_list_node_t *compl_iter = NULL;
  457. faux_list_t *completions = NULL;
  458. char *compl_str = NULL;
  459. completions = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  460. compl_compare, compl_kcompare,
  461. (void (*)(void *))faux_str_free);
  462. while ((candidate = kpargv_completions_each(&citer))) {
  463. const kentry_t *completion = NULL;
  464. kparg_t *parg = NULL;
  465. int rc = -1;
  466. char *out = NULL;
  467. bool_t res = BOOL_FALSE;
  468. char *l = NULL; // One line of completion
  469. const char *str = NULL;
  470. // Get completion entry from candidate entry
  471. completion = kentry_nested_by_purpose(candidate,
  472. KENTRY_PURPOSE_COMPLETION);
  473. // If candidate entry doesn't contain completion then try
  474. // to get completion from entry's PTYPE
  475. if (!completion) {
  476. const kentry_t *ptype = NULL;
  477. ptype = kentry_nested_by_purpose(candidate,
  478. KENTRY_PURPOSE_PTYPE);
  479. if (!ptype)
  480. continue;
  481. completion = kentry_nested_by_purpose(ptype,
  482. KENTRY_PURPOSE_COMPLETION);
  483. }
  484. if (!completion)
  485. continue;
  486. parg = kparg_new(candidate, prefix);
  487. kpargv_set_candidate_parg(pargv, parg);
  488. res = ksession_exec_locally(ktpd->session, completion,
  489. pargv, &rc, &out);
  490. kparg_free(parg);
  491. if (!res || (rc < 0) || !out) {
  492. if (out)
  493. faux_str_free(out);
  494. continue;
  495. }
  496. // Get all completions one by one
  497. str = out;
  498. while ((l = faux_str_getline(str, &str))) {
  499. // Compare prefix
  500. if ((prefix_len > 0) &&
  501. (faux_str_cmpn(prefix, l, prefix_len) != 0)) {
  502. faux_str_free(l);
  503. continue;
  504. }
  505. compl_str = l + prefix_len;
  506. faux_list_add(completions, faux_str_dup(compl_str));
  507. faux_str_free(l);
  508. }
  509. faux_str_free(out);
  510. }
  511. // Put completion list to message
  512. compl_iter = faux_list_head(completions);
  513. while ((compl_str = faux_list_each(&compl_iter))) {
  514. faux_msg_add_param(ack, KTP_PARAM_LINE,
  515. compl_str, strlen(compl_str));
  516. }
  517. faux_list_free(completions);
  518. }
  519. faux_msg_send_async(ack, ktpd->async);
  520. faux_msg_free(ack);
  521. kpargv_free(pargv);
  522. return BOOL_TRUE;
  523. }
  524. // The most priority source of help is candidate's help ACTION output. Next
  525. // source is candidate's PTYPE help ACTION output.
  526. // Function generates two lines for one resulting help line. The first
  527. // component is a 'prefix' and the second component is 'text'.
  528. // The 'prefix' can be something like 'ip', 'filter' i.e.
  529. // subcommand or '3..89', '<STRING>' i.e. description of type. The 'text'
  530. // field is description of current parameter. For example 'Interface IP
  531. // address'. So the full help can be:
  532. // AAA.BBB.CCC.DDD Interface IP address
  533. // [ first field ] [ second field ]
  534. //
  535. // If not candidate parameter nor PTYPE contains the help functions the engine
  536. // tries to construct help itself.
  537. //
  538. // It uses the following sources for 'prefix':
  539. // * 'help' field of PTYPE
  540. // * 'value' field of PTYPE
  541. // * 'name' field of PTYPE
  542. // * 'value' field of parameter
  543. // * 'name' field of parameter
  544. //
  545. // Engine uses the following sources for 'text':
  546. // * 'help' field of parameter
  547. // * 'value' field of parameter
  548. // * 'name' field of parameter
  549. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  550. {
  551. char *line = NULL;
  552. faux_msg_t *ack = NULL;
  553. kpargv_t *pargv = NULL;
  554. ktp_cmd_e cmd = KTP_HELP_ACK;
  555. uint32_t status = KTP_STATUS_NONE;
  556. const char *prefix = NULL;
  557. assert(ktpd);
  558. assert(msg);
  559. // Get line from message
  560. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  561. ktp_send_error(ktpd->async, cmd, NULL);
  562. return BOOL_FALSE;
  563. }
  564. // Parsing
  565. pargv = ksession_parse_for_completion(ktpd->session, line);
  566. faux_str_free(line);
  567. if (!pargv) {
  568. ktp_send_error(ktpd->async, cmd, NULL);
  569. return BOOL_FALSE;
  570. }
  571. if (ksession_done(ktpd->session)) {
  572. ktpd->exit = BOOL_TRUE;
  573. status |= KTP_STATUS_EXIT; // Notify client about exiting
  574. }
  575. // Prepare ACK message
  576. ack = ktp_msg_preform(cmd, status);
  577. // Last unfinished word. Common prefix for all entries
  578. prefix = kpargv_last_arg(pargv);
  579. // Fill msg with possible help messages
  580. if (!kpargv_completions_is_empty(pargv)) {
  581. const kentry_t *candidate = NULL;
  582. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  583. faux_list_node_t *help_iter = NULL;
  584. faux_list_t *help_list = NULL;
  585. help_t *help_struct = NULL;
  586. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  587. help_compare, NULL, help_free);
  588. while ((candidate = kpargv_completions_each(&citer))) {
  589. const kentry_t *help = NULL;
  590. const kentry_t *ptype = NULL;
  591. bool_t help_added = BOOL_FALSE;
  592. // Get PTYPE of parameter
  593. ptype = kentry_nested_by_purpose(candidate,
  594. KENTRY_PURPOSE_PTYPE);
  595. // Try to get help fn from parameter itself
  596. help = kentry_nested_by_purpose(candidate,
  597. KENTRY_PURPOSE_HELP);
  598. if (!help && ptype)
  599. help = kentry_nested_by_purpose(ptype,
  600. KENTRY_PURPOSE_HELP);
  601. // Generate help with found ACTION
  602. if (help) {
  603. char *out = NULL;
  604. kparg_t *parg = NULL;
  605. int rc = -1;
  606. parg = kparg_new(candidate, prefix);
  607. kpargv_set_candidate_parg(pargv, parg);
  608. ksession_exec_locally(ktpd->session,
  609. help, pargv, &rc, &out);
  610. kparg_free(parg);
  611. if (out) {
  612. const char *str = out;
  613. char *prefix_str = NULL;
  614. char *line_str = NULL;
  615. do {
  616. prefix_str = faux_str_getline(str, &str);
  617. if (!prefix_str)
  618. break;
  619. line_str = faux_str_getline(str, &str);
  620. if (!line_str) {
  621. faux_str_free(prefix_str);
  622. break;
  623. }
  624. help_struct = help_new(prefix_str, line_str);
  625. if (faux_list_add(help_list, help_struct))
  626. help_added = BOOL_TRUE;
  627. else
  628. help_free(help_struct);
  629. } while (line_str);
  630. faux_str_free(out);
  631. }
  632. }
  633. // Generate help with available information
  634. if (!help_added) {
  635. const char *prefix_str = NULL;
  636. const char *line_str = NULL;
  637. // Prefix_str
  638. if (ptype) {
  639. prefix_str = kentry_help(ptype);
  640. if (!prefix_str)
  641. prefix_str = kentry_value(ptype);
  642. if (!prefix_str)
  643. prefix_str = kentry_name(ptype);
  644. } else {
  645. prefix_str = kentry_value(candidate);
  646. if (!prefix_str)
  647. prefix_str = kentry_name(candidate);
  648. }
  649. assert(prefix_str);
  650. // Line_str
  651. line_str = kentry_help(candidate);
  652. if (!line_str)
  653. line_str = kentry_value(candidate);
  654. if (!line_str)
  655. line_str = kentry_name(candidate);
  656. assert(line_str);
  657. help_struct = help_new(
  658. faux_str_dup(prefix_str),
  659. faux_str_dup(line_str));
  660. if (!faux_list_add(help_list, help_struct))
  661. help_free(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