ktpd_session.c 34 KB

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