ktpd_session.c 34 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370
  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. // Remove already generated data from out buffer. This data is not
  862. // needed now
  863. faux_buf_empty(kexec_bufout(ktpd->exec));
  864. return BOOL_TRUE;
  865. }
  866. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  867. {
  868. uint16_t cmd = 0;
  869. const char *err = NULL;
  870. ktp_cmd_e ecmd = KTP_NOTIFICATION; // Answer command if error
  871. assert(ktpd);
  872. if (!ktpd)
  873. return BOOL_FALSE;
  874. assert(msg);
  875. if (!msg)
  876. return BOOL_FALSE;
  877. cmd = faux_msg_get_cmd(msg);
  878. switch (cmd) {
  879. case KTP_AUTH:
  880. if ((ktpd->state != KTPD_SESSION_STATE_UNAUTHORIZED) &&
  881. (ktpd->state != KTPD_SESSION_STATE_IDLE)) {
  882. ecmd = KTP_AUTH_ACK;
  883. err = "Server illegal state for authorization";
  884. break;
  885. }
  886. ktpd_session_process_auth(ktpd, msg);
  887. break;
  888. case KTP_CMD:
  889. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  890. ecmd = KTP_CMD_ACK;
  891. err = "Server illegal state for command execution";
  892. break;
  893. }
  894. ktpd_session_process_cmd(ktpd, msg);
  895. break;
  896. case KTP_COMPLETION:
  897. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  898. ecmd = KTP_COMPLETION_ACK;
  899. err = "Server illegal state for completion";
  900. break;
  901. }
  902. ktpd_session_process_completion(ktpd, msg);
  903. break;
  904. case KTP_HELP:
  905. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  906. ecmd = KTP_HELP_ACK;
  907. err = "Server illegal state for help";
  908. break;
  909. }
  910. ktpd_session_process_help(ktpd, msg);
  911. break;
  912. case KTP_STDIN:
  913. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
  914. err = "Nobody is waiting for stdin";
  915. break;
  916. }
  917. ktpd_session_process_stdin(ktpd, msg);
  918. break;
  919. case KTP_NOTIFICATION:
  920. ktpd_session_process_notification(ktpd, msg);
  921. break;
  922. case KTP_STDOUT_CLOSE:
  923. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
  924. err = "No active command is running";
  925. break;
  926. }
  927. ktpd_session_process_stdout_close(ktpd, msg);
  928. break;
  929. default:
  930. syslog(LOG_WARNING, "Unsupported command: 0x%04u", cmd);
  931. err = "Unsupported command";
  932. break;
  933. }
  934. // On error
  935. if (err)
  936. ktp_send_error(ktpd->async, ecmd, err);
  937. return BOOL_TRUE;
  938. }
  939. /** @brief Low-level function to receive KTP message.
  940. *
  941. * Firstly function gets the header of message. Then it checks and parses
  942. * header and find out the length of whole message. Then it receives the rest
  943. * of message.
  944. */
  945. static bool_t ktpd_session_read_cb(faux_async_t *async,
  946. faux_buf_t *buf, size_t len, void *user_data)
  947. {
  948. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  949. faux_msg_t *completed_msg = NULL;
  950. char *data = NULL;
  951. assert(async);
  952. assert(buf);
  953. assert(ktpd);
  954. // Linearize buffer
  955. data = malloc(len);
  956. faux_buf_read(buf, data, len);
  957. // Receive header
  958. if (!ktpd->hdr) {
  959. size_t whole_len = 0;
  960. size_t msg_wo_hdr = 0;
  961. ktpd->hdr = (faux_hdr_t *)data;
  962. // Check for broken header
  963. if (!ktp_check_header(ktpd->hdr)) {
  964. faux_free(ktpd->hdr);
  965. ktpd->hdr = NULL;
  966. return BOOL_FALSE;
  967. }
  968. whole_len = faux_hdr_len(ktpd->hdr);
  969. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  970. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  971. // Plan to receive message body
  972. if (msg_wo_hdr > 0) {
  973. faux_async_set_read_limits(async,
  974. msg_wo_hdr, msg_wo_hdr);
  975. return BOOL_TRUE;
  976. }
  977. // Here message is completed (msg body has zero length)
  978. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  979. // Receive message body
  980. } else {
  981. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  982. faux_free(data);
  983. }
  984. // Plan to receive msg header
  985. faux_async_set_read_limits(ktpd->async,
  986. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  987. faux_free(ktpd->hdr);
  988. ktpd->hdr = NULL; // Ready to recv new header
  989. // Here message is completed
  990. ktpd_session_dispatch(ktpd, completed_msg);
  991. faux_msg_free(completed_msg);
  992. return BOOL_TRUE;
  993. }
  994. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  995. {
  996. assert(ktpd);
  997. if (!ktpd)
  998. return BOOL_FALSE;
  999. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  1000. return BOOL_FALSE;
  1001. return BOOL_TRUE;
  1002. }
  1003. int ktpd_session_fd(const ktpd_session_t *ktpd)
  1004. {
  1005. assert(ktpd);
  1006. if (!ktpd)
  1007. return BOOL_FALSE;
  1008. return faux_async_fd(ktpd->async);
  1009. }
  1010. static bool_t get_stream(ktpd_session_t *ktpd, int fd, bool_t is_stderr)
  1011. {
  1012. ssize_t r = -1;
  1013. faux_buf_t *faux_buf = NULL;
  1014. char *buf = NULL;
  1015. ssize_t len = 0;
  1016. faux_msg_t *ack = NULL;
  1017. if (!ktpd)
  1018. return BOOL_TRUE;
  1019. if (!ktpd->exec)
  1020. return BOOL_TRUE;
  1021. if (is_stderr)
  1022. faux_buf = kexec_buferr(ktpd->exec);
  1023. else
  1024. faux_buf = kexec_bufout(ktpd->exec);
  1025. assert(faux_buf);
  1026. do {
  1027. void *linear_buf = NULL;
  1028. ssize_t really_readed = 0;
  1029. ssize_t linear_len =
  1030. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  1031. // Non-blocked read. The fd became non-blocked while
  1032. // kexec_prepare().
  1033. r = read(fd, linear_buf, linear_len);
  1034. if (r > 0)
  1035. really_readed = r;
  1036. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  1037. } while (r > 0);
  1038. len = faux_buf_len(faux_buf);
  1039. if (0 == len)
  1040. return BOOL_TRUE;
  1041. buf = malloc(len);
  1042. faux_buf_read(faux_buf, buf, len);
  1043. // Create KTP_STDOUT/KTP_STDERR message to send to client
  1044. ack = ktp_msg_preform(is_stderr ? KTP_STDERR : KTP_STDOUT, KTP_STATUS_NONE);
  1045. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  1046. faux_msg_send_async(ack, ktpd->async);
  1047. faux_msg_free(ack);
  1048. free(buf);
  1049. return BOOL_TRUE;
  1050. }
  1051. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1052. void *associated_data, void *user_data)
  1053. {
  1054. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1055. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1056. // Interactive command use these function as callback not only for
  1057. // getting stdout but for writing stdin too. Because pseudo-terminal
  1058. // uses the same fd for in and out.
  1059. if (info->revents & POLLOUT)
  1060. push_stdin(eloop, type, associated_data, user_data);
  1061. if (info->revents & POLLIN)
  1062. get_stream(ktpd, info->fd, BOOL_FALSE);
  1063. // Some errors or fd is closed so remove it from polling
  1064. // EOF || POLERR || POLLNVAL
  1065. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1066. faux_eloop_del_fd(eloop, info->fd);
  1067. return BOOL_TRUE;
  1068. }
  1069. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1070. void *associated_data, void *user_data)
  1071. {
  1072. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1073. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1074. if (info->revents & POLLIN)
  1075. get_stream(ktpd, info->fd, BOOL_TRUE);
  1076. // Some errors or fd is closed so remove it from polling
  1077. // EOF || POLERR || POLLNVAL
  1078. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1079. faux_eloop_del_fd(eloop, info->fd);
  1080. type = type; // Happy compiler
  1081. return BOOL_TRUE;
  1082. }
  1083. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1084. void *associated_data, void *user_data)
  1085. {
  1086. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1087. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1088. faux_async_t *async = ktpd->async;
  1089. assert(async);
  1090. // Write data
  1091. if (info->revents & POLLOUT) {
  1092. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  1093. if (faux_async_out(async) < 0) {
  1094. // Someting went wrong
  1095. faux_eloop_del_fd(eloop, info->fd);
  1096. syslog(LOG_ERR, "Can't send data to client");
  1097. return BOOL_FALSE; // Stop event loop
  1098. }
  1099. }
  1100. // Read data
  1101. if (info->revents & POLLIN) {
  1102. if (faux_async_in(async) < 0) {
  1103. // Someting went wrong
  1104. faux_eloop_del_fd(eloop, info->fd);
  1105. syslog(LOG_ERR, "Can't get data from client");
  1106. return BOOL_FALSE; // Stop event loop
  1107. }
  1108. }
  1109. // EOF
  1110. if (info->revents & POLLHUP) {
  1111. faux_eloop_del_fd(eloop, info->fd);
  1112. syslog(LOG_DEBUG, "Connection %d is closed by client", info->fd);
  1113. return BOOL_FALSE; // Stop event loop
  1114. }
  1115. // POLLERR
  1116. if (info->revents & POLLERR) {
  1117. faux_eloop_del_fd(eloop, info->fd);
  1118. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  1119. return BOOL_FALSE; // Stop event loop
  1120. }
  1121. // POLLNVAL
  1122. if (info->revents & POLLNVAL) {
  1123. faux_eloop_del_fd(eloop, info->fd);
  1124. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  1125. return BOOL_FALSE; // Stop event loop
  1126. }
  1127. type = type; // Happy compiler
  1128. // Session can be really finished here. Note KTPD session can't be
  1129. // stopped immediately so it's only two places within code to really
  1130. // break the loop. This one and within wait_for_action_ev().
  1131. if (ktpd->exit)
  1132. return BOOL_FALSE;
  1133. return BOOL_TRUE;
  1134. }
  1135. #if 0
  1136. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  1137. {
  1138. assert(ktpd);
  1139. if (!ktpd)
  1140. return;
  1141. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  1142. }
  1143. #endif