ktpd_session.c 35 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406
  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. uint32_t client_status = KTP_STATUS_NONE;
  228. assert(ktpd);
  229. assert(msg);
  230. // Get UNIX socket peer information
  231. sock = faux_async_fd(ktpd->async);
  232. if (getsockopt(sock, SOL_SOCKET, SO_PEERCRED, &ucred, &len) < 0) {
  233. const char *err = "Can't get peer credentials";
  234. syslog(LOG_ERR, "%s for connection %d", err, sock);
  235. ack = ktp_msg_preform(cmd, KTP_STATUS_ERROR | KTP_STATUS_EXIT);
  236. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  237. faux_msg_send_async(ack, ktpd->async);
  238. faux_msg_free(ack);
  239. ktpd->exit = BOOL_TRUE;
  240. return BOOL_FALSE;
  241. }
  242. ksession_set_pid(ktpd->session, ucred.pid);
  243. ksession_set_uid(ktpd->session, ucred.uid);
  244. user = faux_sysdb_name_by_uid(ucred.uid);
  245. ksession_set_user(ktpd->session, user);
  246. faux_str_free(user);
  247. // Get tty information from auth message status
  248. client_status = faux_msg_get_status(msg);
  249. ksession_set_isatty_stdin(ktpd->session,
  250. KTP_STATUS_IS_TTY_STDIN(client_status));
  251. ksession_set_isatty_stdout(ktpd->session,
  252. KTP_STATUS_IS_TTY_STDOUT(client_status));
  253. ksession_set_isatty_stderr(ktpd->session,
  254. KTP_STATUS_IS_TTY_STDERR(client_status));
  255. // init session for plugins
  256. scheme = ksession_scheme(ktpd->session);
  257. context = kcontext_new(KCONTEXT_TYPE_PLUGIN_INIT);
  258. kcontext_set_session(context, ktpd->session);
  259. kcontext_set_scheme(context, scheme);
  260. kscheme_init_session_plugins(scheme, context, NULL);
  261. kcontext_free(context);
  262. // Prepare ACK message
  263. ack = ktp_msg_preform(cmd, status);
  264. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  265. // Generate prompt
  266. prompt = generate_prompt(ktpd);
  267. if (prompt) {
  268. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  269. faux_str_free(prompt);
  270. }
  271. add_hotkeys_to_msg(ktpd, ack);
  272. faux_msg_send_async(ack, ktpd->async);
  273. faux_msg_free(ack);
  274. ktpd->state = KTPD_SESSION_STATE_IDLE;
  275. return BOOL_TRUE;
  276. }
  277. static bool_t ktpd_session_process_cmd(ktpd_session_t *ktpd, faux_msg_t *msg)
  278. {
  279. char *line = NULL;
  280. int retcode = -1;
  281. ktp_cmd_e cmd = KTP_CMD_ACK;
  282. faux_error_t *error = NULL;
  283. bool_t rc = BOOL_FALSE;
  284. bool_t dry_run = BOOL_FALSE;
  285. uint32_t status = KTP_STATUS_NONE;
  286. bool_t ret = BOOL_TRUE;
  287. char *prompt = NULL;
  288. bool_t view_was_changed = BOOL_FALSE;
  289. assert(ktpd);
  290. assert(msg);
  291. // Get line from message
  292. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  293. ktp_send_error(ktpd->async, cmd, "The line is not specified");
  294. return BOOL_FALSE;
  295. }
  296. // Get dry-run flag from message
  297. if (KTP_STATUS_IS_DRY_RUN(faux_msg_get_status(msg)))
  298. dry_run = BOOL_TRUE;
  299. error = faux_error_new();
  300. ktpd->exec = NULL;
  301. rc = ktpd_session_exec(ktpd, line, &retcode, error,
  302. dry_run, &view_was_changed);
  303. faux_str_free(line);
  304. // Command is scheduled. Eloop will wait for ACTION completion.
  305. // So inform client about it and about command features like
  306. // interactive/non-interactive.
  307. if (ktpd->exec) {
  308. faux_msg_t *ack = NULL;
  309. ktp_status_e status = KTP_STATUS_INCOMPLETED;
  310. if (kexec_interactive(ktpd->exec))
  311. status |= KTP_STATUS_INTERACTIVE;
  312. if (kexec_need_stdin(ktpd->exec))
  313. status |= KTP_STATUS_NEED_STDIN;
  314. ack = ktp_msg_preform(cmd, status);
  315. faux_msg_send_async(ack, ktpd->async);
  316. faux_msg_free(ack);
  317. faux_error_free(error);
  318. return BOOL_TRUE; // Continue and wait for ACTION
  319. }
  320. // Here we don't need to wait for the action. We have retcode already.
  321. if (ksession_done(ktpd->session)) {
  322. ktpd->exit = BOOL_TRUE;
  323. status |= KTP_STATUS_EXIT;
  324. }
  325. // Prepare ACK message
  326. faux_msg_t *ack = ktp_msg_preform(cmd, status);
  327. if (rc) {
  328. uint8_t retcode8bit = 0;
  329. retcode8bit = (uint8_t)(retcode & 0xff);
  330. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  331. } else {
  332. faux_msg_set_status(ack, KTP_STATUS_ERROR);
  333. char *err = faux_error_cstr(error);
  334. faux_msg_add_param(ack, KTP_PARAM_ERROR, err, strlen(err));
  335. faux_str_free(err);
  336. ret = BOOL_FALSE;
  337. }
  338. // Generate prompt
  339. prompt = generate_prompt(ktpd);
  340. if (prompt) {
  341. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  342. faux_str_free(prompt);
  343. }
  344. // Add hotkeys
  345. if (view_was_changed)
  346. add_hotkeys_to_msg(ktpd, ack);
  347. faux_msg_send_async(ack, ktpd->async);
  348. faux_msg_free(ack);
  349. faux_error_free(error);
  350. return ret;
  351. }
  352. static bool_t ktpd_session_exec(ktpd_session_t *ktpd, const char *line,
  353. int *retcode, faux_error_t *error,
  354. bool_t dry_run, bool_t *view_was_changed_p)
  355. {
  356. kexec_t *exec = NULL;
  357. assert(ktpd);
  358. if (!ktpd)
  359. return BOOL_FALSE;
  360. // Parsing
  361. exec = ksession_parse_for_exec(ktpd->session, line, error);
  362. if (!exec)
  363. return BOOL_FALSE;
  364. // Set dry-run flag
  365. kexec_set_dry_run(exec, dry_run);
  366. // Session status can be changed while parsing
  367. // NOTE: kexec_t is atomic now
  368. // if (ksession_done(ktpd->session)) {
  369. // kexec_free(exec);
  370. // return BOOL_FALSE; // Because action is not completed
  371. // }
  372. // Execute kexec and then wait for completion using global Eloop
  373. if (!kexec_exec(exec)) {
  374. kexec_free(exec);
  375. return BOOL_FALSE; // Something went wrong
  376. }
  377. // If kexec contains only non-exec (for example dry-run) ACTIONs then
  378. // we don't need event loop and can return here.
  379. if (kexec_retcode(exec, retcode)) {
  380. if (view_was_changed_p)
  381. *view_was_changed_p = !kpath_is_equal(
  382. ksession_path(ktpd->session),
  383. kexec_saved_path(exec));
  384. kexec_free(exec);
  385. return BOOL_TRUE;
  386. }
  387. // Save kexec pointer to use later
  388. ktpd->state = KTPD_SESSION_STATE_WAIT_FOR_PROCESS;
  389. ktpd->exec = exec;
  390. faux_eloop_add_fd(ktpd->eloop, kexec_stdout(exec), POLLIN,
  391. action_stdout_ev, ktpd);
  392. faux_eloop_add_fd(ktpd->eloop, kexec_stderr(exec), POLLIN,
  393. action_stderr_ev, ktpd);
  394. return BOOL_TRUE;
  395. }
  396. static bool_t wait_for_actions_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  397. void *associated_data, void *user_data)
  398. {
  399. int wstatus = 0;
  400. pid_t child_pid = -1;
  401. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  402. int retcode = -1;
  403. uint8_t retcode8bit = 0;
  404. faux_msg_t *ack = NULL;
  405. ktp_cmd_e cmd = KTP_CMD_ACK;
  406. uint32_t status = KTP_STATUS_NONE;
  407. char *prompt = NULL;
  408. bool_t view_was_changed = BOOL_FALSE;
  409. if (!ktpd)
  410. return BOOL_FALSE;
  411. // Wait for any child process. Doesn't block.
  412. while ((child_pid = waitpid(-1, &wstatus, WNOHANG)) > 0) {
  413. if (ktpd->exec)
  414. kexec_continue_command_execution(ktpd->exec, child_pid,
  415. wstatus);
  416. }
  417. if (!ktpd->exec)
  418. return BOOL_TRUE;
  419. // Check if kexec is done now
  420. if (!kexec_retcode(ktpd->exec, &retcode))
  421. return BOOL_TRUE; // Continue
  422. // Sometimes SIGCHILD signal can appear before all data were really read
  423. // from process stdout buffer. So read the least data before closing
  424. // file descriptors and send it to client.
  425. get_stream(ktpd, kexec_stdout(ktpd->exec), BOOL_FALSE);
  426. get_stream(ktpd, kexec_stderr(ktpd->exec), BOOL_TRUE);
  427. faux_eloop_del_fd(eloop, kexec_stdout(ktpd->exec));
  428. faux_eloop_del_fd(eloop, kexec_stderr(ktpd->exec));
  429. view_was_changed = !kpath_is_equal(
  430. ksession_path(ktpd->session), kexec_saved_path(ktpd->exec));
  431. kexec_free(ktpd->exec);
  432. ktpd->exec = NULL;
  433. ktpd->state = KTPD_SESSION_STATE_IDLE;
  434. // All kexec_t actions are done so can break the loop if needed.
  435. if (ksession_done(ktpd->session)) {
  436. ktpd->exit = BOOL_TRUE;
  437. status |= KTP_STATUS_EXIT; // Notify client about exiting
  438. }
  439. // Send ACK message
  440. ack = ktp_msg_preform(cmd, status);
  441. retcode8bit = (uint8_t)(retcode & 0xff);
  442. faux_msg_add_param(ack, KTP_PARAM_RETCODE, &retcode8bit, 1);
  443. // Generate prompt
  444. prompt = generate_prompt(ktpd);
  445. if (prompt) {
  446. faux_msg_add_param(ack, KTP_PARAM_PROMPT, prompt, strlen(prompt));
  447. faux_str_free(prompt);
  448. }
  449. // Add hotkeys
  450. if (view_was_changed)
  451. add_hotkeys_to_msg(ktpd, ack);
  452. faux_msg_send_async(ack, ktpd->async);
  453. faux_msg_free(ack);
  454. type = type; // Happy compiler
  455. associated_data = associated_data; // Happy compiler
  456. if (ktpd->exit)
  457. return BOOL_FALSE;
  458. return BOOL_TRUE;
  459. }
  460. static int compl_compare(const void *first, const void *second)
  461. {
  462. const char *f = (const char *)first;
  463. const char *s = (const char *)second;
  464. return strcmp(f, s);
  465. }
  466. static int compl_kcompare(const void *key, const void *list_item)
  467. {
  468. const char *f = (const char *)key;
  469. const char *s = (const char *)list_item;
  470. return strcmp(f, s);
  471. }
  472. static bool_t ktpd_session_process_completion(ktpd_session_t *ktpd, faux_msg_t *msg)
  473. {
  474. char *line = NULL;
  475. faux_msg_t *ack = NULL;
  476. kpargv_t *pargv = NULL;
  477. ktp_cmd_e cmd = KTP_COMPLETION_ACK;
  478. uint32_t status = KTP_STATUS_NONE;
  479. const char *prefix = NULL;
  480. size_t prefix_len = 0;
  481. assert(ktpd);
  482. assert(msg);
  483. // Get line from message
  484. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  485. ktp_send_error(ktpd->async, cmd, NULL);
  486. return BOOL_FALSE;
  487. }
  488. // Parsing
  489. pargv = ksession_parse_for_completion(ktpd->session, line);
  490. faux_str_free(line);
  491. if (!pargv) {
  492. ktp_send_error(ktpd->async, cmd, NULL);
  493. return BOOL_FALSE;
  494. }
  495. kpargv_debug(pargv);
  496. if (ksession_done(ktpd->session)) {
  497. ktpd->exit = BOOL_TRUE;
  498. status |= KTP_STATUS_EXIT; // Notify client about exiting
  499. }
  500. // Prepare ACK message
  501. ack = ktp_msg_preform(cmd, status);
  502. // Last unfinished word. Common prefix for all completions
  503. prefix = kpargv_last_arg(pargv);
  504. if (!faux_str_is_empty(prefix)) {
  505. prefix_len = strlen(prefix);
  506. faux_msg_add_param(ack, KTP_PARAM_PREFIX, prefix, prefix_len);
  507. }
  508. // Fill msg with possible completions
  509. if (!kpargv_completions_is_empty(pargv)) {
  510. const kentry_t *candidate = NULL;
  511. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  512. faux_list_node_t *compl_iter = NULL;
  513. faux_list_t *completions = NULL;
  514. char *compl_str = NULL;
  515. completions = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  516. compl_compare, compl_kcompare,
  517. (void (*)(void *))faux_str_free);
  518. while ((candidate = kpargv_completions_each(&citer))) {
  519. const kentry_t *completion = NULL;
  520. kparg_t *parg = NULL;
  521. int rc = -1;
  522. char *out = NULL;
  523. bool_t res = BOOL_FALSE;
  524. char *l = NULL; // One line of completion
  525. const char *str = NULL;
  526. // Get completion entry from candidate entry
  527. completion = kentry_nested_by_purpose(candidate,
  528. KENTRY_PURPOSE_COMPLETION);
  529. // If candidate entry doesn't contain completion then try
  530. // to get completion from entry's PTYPE
  531. if (!completion) {
  532. const kentry_t *ptype = NULL;
  533. ptype = kentry_nested_by_purpose(candidate,
  534. KENTRY_PURPOSE_PTYPE);
  535. if (!ptype)
  536. continue;
  537. completion = kentry_nested_by_purpose(ptype,
  538. KENTRY_PURPOSE_COMPLETION);
  539. }
  540. if (!completion)
  541. continue;
  542. parg = kparg_new(candidate, prefix);
  543. kpargv_set_candidate_parg(pargv, parg);
  544. res = ksession_exec_locally(ktpd->session, completion,
  545. pargv, &rc, &out);
  546. kparg_free(parg);
  547. if (!res || (rc < 0) || !out) {
  548. if (out)
  549. faux_str_free(out);
  550. continue;
  551. }
  552. // Get all completions one by one
  553. str = out;
  554. while ((l = faux_str_getline(str, &str))) {
  555. // Compare prefix
  556. if ((prefix_len > 0) &&
  557. (faux_str_cmpn(prefix, l, prefix_len) != 0)) {
  558. faux_str_free(l);
  559. continue;
  560. }
  561. compl_str = l + prefix_len;
  562. faux_list_add(completions, faux_str_dup(compl_str));
  563. faux_str_free(l);
  564. }
  565. faux_str_free(out);
  566. }
  567. // Put completion list to message
  568. compl_iter = faux_list_head(completions);
  569. while ((compl_str = faux_list_each(&compl_iter))) {
  570. faux_msg_add_param(ack, KTP_PARAM_LINE,
  571. compl_str, strlen(compl_str));
  572. }
  573. faux_list_free(completions);
  574. }
  575. faux_msg_send_async(ack, ktpd->async);
  576. faux_msg_free(ack);
  577. kpargv_free(pargv);
  578. return BOOL_TRUE;
  579. }
  580. // The most priority source of help is candidate's help ACTION output. Next
  581. // source is candidate's PTYPE help ACTION output.
  582. // Function generates two lines for one resulting help line. The first
  583. // component is a 'prefix' and the second component is 'text'.
  584. // The 'prefix' can be something like 'ip', 'filter' i.e.
  585. // subcommand or '3..89', '<STRING>' i.e. description of type. The 'text'
  586. // field is description of current parameter. For example 'Interface IP
  587. // address'. So the full help can be:
  588. // AAA.BBB.CCC.DDD Interface IP address
  589. // [ first field ] [ second field ]
  590. //
  591. // If not candidate parameter nor PTYPE contains the help functions the engine
  592. // tries to construct help itself.
  593. //
  594. // It uses the following sources for 'prefix':
  595. // * 'help' field of PTYPE
  596. // * 'value' field of PTYPE
  597. // * 'name' field of PTYPE
  598. // * 'value' field of parameter
  599. // * 'name' field of parameter
  600. //
  601. // Engine uses the following sources for 'text':
  602. // * 'help' field of parameter
  603. // * 'value' field of parameter
  604. // * 'name' field of parameter
  605. static bool_t ktpd_session_process_help(ktpd_session_t *ktpd, faux_msg_t *msg)
  606. {
  607. char *line = NULL;
  608. faux_msg_t *ack = NULL;
  609. kpargv_t *pargv = NULL;
  610. ktp_cmd_e cmd = KTP_HELP_ACK;
  611. uint32_t status = KTP_STATUS_NONE;
  612. const char *prefix = NULL;
  613. assert(ktpd);
  614. assert(msg);
  615. // Get line from message
  616. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_LINE))) {
  617. ktp_send_error(ktpd->async, cmd, NULL);
  618. return BOOL_FALSE;
  619. }
  620. // Parsing
  621. pargv = ksession_parse_for_completion(ktpd->session, line);
  622. faux_str_free(line);
  623. if (!pargv) {
  624. ktp_send_error(ktpd->async, cmd, NULL);
  625. return BOOL_FALSE;
  626. }
  627. if (ksession_done(ktpd->session)) {
  628. ktpd->exit = BOOL_TRUE;
  629. status |= KTP_STATUS_EXIT; // Notify client about exiting
  630. }
  631. // Prepare ACK message
  632. ack = ktp_msg_preform(cmd, status);
  633. // Last unfinished word. Common prefix for all entries
  634. prefix = kpargv_last_arg(pargv);
  635. // Fill msg with possible help messages
  636. if (!kpargv_completions_is_empty(pargv)) {
  637. const kentry_t *candidate = NULL;
  638. kpargv_completions_node_t *citer = kpargv_completions_iter(pargv);
  639. faux_list_node_t *help_iter = NULL;
  640. faux_list_t *help_list = NULL;
  641. help_t *help_struct = NULL;
  642. help_list = faux_list_new(FAUX_LIST_SORTED, FAUX_LIST_UNIQUE,
  643. help_compare, NULL, help_free);
  644. while ((candidate = kpargv_completions_each(&citer))) {
  645. const kentry_t *help = NULL;
  646. const kentry_t *ptype = NULL;
  647. // Get PTYPE of parameter
  648. ptype = kentry_nested_by_purpose(candidate,
  649. KENTRY_PURPOSE_PTYPE);
  650. // Try to get help fn from parameter itself
  651. help = kentry_nested_by_purpose(candidate,
  652. KENTRY_PURPOSE_HELP);
  653. if (!help && ptype)
  654. help = kentry_nested_by_purpose(ptype,
  655. KENTRY_PURPOSE_HELP);
  656. // Generate help with found ACTION
  657. if (help) {
  658. char *out = NULL;
  659. kparg_t *parg = NULL;
  660. int rc = -1;
  661. parg = kparg_new(candidate, prefix);
  662. kpargv_set_candidate_parg(pargv, parg);
  663. ksession_exec_locally(ktpd->session,
  664. help, pargv, &rc, &out);
  665. kparg_free(parg);
  666. if (out) {
  667. const char *str = out;
  668. char *prefix_str = NULL;
  669. char *line_str = NULL;
  670. do {
  671. prefix_str = faux_str_getline(str, &str);
  672. if (!prefix_str)
  673. break;
  674. line_str = faux_str_getline(str, &str);
  675. if (!line_str) {
  676. faux_str_free(prefix_str);
  677. break;
  678. }
  679. help_struct = help_new(prefix_str, line_str);
  680. if (!faux_list_add(help_list, help_struct))
  681. help_free(help_struct);
  682. } while (line_str);
  683. faux_str_free(out);
  684. }
  685. // Generate help with available information
  686. } else {
  687. const char *prefix_str = NULL;
  688. const char *line_str = NULL;
  689. // Prefix_str
  690. if (ptype) {
  691. prefix_str = kentry_help(ptype);
  692. if (!prefix_str)
  693. prefix_str = kentry_value(ptype);
  694. if (!prefix_str)
  695. prefix_str = kentry_name(ptype);
  696. } else {
  697. prefix_str = kentry_value(candidate);
  698. if (!prefix_str)
  699. prefix_str = kentry_name(candidate);
  700. }
  701. assert(prefix_str);
  702. // Line_str
  703. line_str = kentry_help(candidate);
  704. if (!line_str)
  705. line_str = kentry_value(candidate);
  706. if (!line_str)
  707. line_str = kentry_name(candidate);
  708. assert(line_str);
  709. help_struct = help_new(
  710. faux_str_dup(prefix_str),
  711. faux_str_dup(line_str));
  712. if (!faux_list_add(help_list, help_struct))
  713. help_free(help_struct);
  714. }
  715. }
  716. // Put help list to message
  717. help_iter = faux_list_head(help_list);
  718. while ((help_struct = (help_t *)faux_list_each(&help_iter))) {
  719. faux_msg_add_param(ack, KTP_PARAM_PREFIX,
  720. help_struct->prefix, strlen(help_struct->prefix));
  721. faux_msg_add_param(ack, KTP_PARAM_LINE,
  722. help_struct->line, strlen(help_struct->line));
  723. }
  724. faux_list_free(help_list);
  725. }
  726. faux_msg_send_async(ack, ktpd->async);
  727. faux_msg_free(ack);
  728. kpargv_free(pargv);
  729. return BOOL_TRUE;
  730. }
  731. static ssize_t stdin_out(int fd, faux_buf_t *buf)
  732. {
  733. ssize_t total_written = 0;
  734. assert(buf);
  735. if (!buf)
  736. return -1;
  737. assert(fd >= 0);
  738. while (faux_buf_len(buf) > 0) {
  739. ssize_t data_to_write = 0;
  740. ssize_t bytes_written = 0;
  741. void *data = NULL;
  742. data_to_write = faux_buf_dread_lock_easy(buf, &data);
  743. if (data_to_write <= 0)
  744. return -1;
  745. bytes_written = write(fd, data, data_to_write);
  746. if (bytes_written > 0) {
  747. total_written += bytes_written;
  748. faux_buf_dread_unlock_easy(buf, bytes_written);
  749. } else {
  750. faux_buf_dread_unlock_easy(buf, 0);
  751. }
  752. if (bytes_written < 0) {
  753. if ( // Something went wrong
  754. (errno != EINTR) &&
  755. (errno != EAGAIN) &&
  756. (errno != EWOULDBLOCK)
  757. )
  758. return -1;
  759. // Not whole data block was written
  760. } else if (bytes_written != data_to_write) {
  761. break;
  762. }
  763. }
  764. return total_written;
  765. }
  766. static bool_t push_stdin(faux_eloop_t *eloop, faux_eloop_type_e type,
  767. void *associated_data, void *user_data)
  768. {
  769. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  770. faux_buf_t *bufin = NULL;
  771. int fd = -1;
  772. if (!ktpd)
  773. return BOOL_TRUE;
  774. if (!ktpd->exec)
  775. return BOOL_TRUE;
  776. fd = kexec_stdin(ktpd->exec);
  777. if (fd < 0) // Something strange
  778. return BOOL_FALSE;
  779. bufin = kexec_bufin(ktpd->exec);
  780. assert(bufin);
  781. stdin_out(fd, bufin); // Non-blocking write
  782. if (faux_buf_len(bufin) != 0) // Try later
  783. return BOOL_TRUE;
  784. // All data is written
  785. faux_eloop_exclude_fd_event(ktpd->eloop, fd, POLLOUT);
  786. // Happy compiler
  787. eloop = eloop;
  788. type = type;
  789. associated_data = associated_data;
  790. return BOOL_TRUE;
  791. }
  792. static bool_t ktpd_session_process_stdin(ktpd_session_t *ktpd, faux_msg_t *msg)
  793. {
  794. char *line = NULL;
  795. unsigned int len = 0;
  796. faux_buf_t *bufin = NULL;
  797. int fd = -1;
  798. bool_t interrupt = BOOL_FALSE;
  799. const kaction_t *action = NULL;
  800. assert(ktpd);
  801. assert(msg);
  802. if (!ktpd->exec)
  803. return BOOL_FALSE;
  804. fd = kexec_stdin(ktpd->exec);
  805. if (fd < 0)
  806. return BOOL_FALSE;
  807. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_LINE, (void **)&line, &len))
  808. return BOOL_TRUE; // It's strange but not a bug
  809. if (len == 0)
  810. return BOOL_TRUE;
  811. bufin = kexec_bufin(ktpd->exec);
  812. assert(bufin);
  813. action = kexec_current_action(ktpd->exec);
  814. if (action)
  815. interrupt = kaction_interrupt(action);
  816. // If current action is non-interruptible and action's stdin is terminal
  817. // then remove ^C (0x03) symbol from stdin stream to don't deliver
  818. // SIGINT to process
  819. if (isatty(fd) && !interrupt) {
  820. // 0x03 is a ^C
  821. const char chars_to_search[] = {0x03, 0};
  822. const char *start = line;
  823. const char *pos = NULL;
  824. size_t cur_len = len;
  825. while ((pos = faux_str_charsn(start, chars_to_search, cur_len))) {
  826. size_t written = pos - start;
  827. faux_buf_write(bufin, start, written);
  828. start = pos + 1;
  829. cur_len = cur_len - written - 1;
  830. }
  831. if (cur_len > 0)
  832. faux_buf_write(bufin, start, cur_len);
  833. } else {
  834. faux_buf_write(bufin, line, len);
  835. }
  836. stdin_out(fd, bufin); // Non-blocking write
  837. if (faux_buf_len(bufin) == 0)
  838. return BOOL_TRUE;
  839. // Non-blocking write can't write all data so plan to write later
  840. faux_eloop_include_fd_event(ktpd->eloop, fd, POLLOUT);
  841. return BOOL_TRUE;
  842. }
  843. static bool_t ktpd_session_process_winch(ktpd_session_t *ktpd, faux_msg_t *msg)
  844. {
  845. char *line = NULL;
  846. char *p = NULL;
  847. unsigned short width = 0;
  848. unsigned short height = 0;
  849. assert(ktpd);
  850. assert(msg);
  851. if (!(line = faux_msg_get_str_param_by_type(msg, KTP_PARAM_WINCH)))
  852. return BOOL_TRUE;
  853. p = strchr(line, ' ');
  854. if (!p || (p == line)) {
  855. faux_str_free(line);
  856. return BOOL_FALSE;
  857. }
  858. if (!faux_conv_atous(line, &width, 0)) {
  859. faux_str_free(line);
  860. return BOOL_FALSE;
  861. }
  862. if (!faux_conv_atous(p + 1, &height, 0)) {
  863. faux_str_free(line);
  864. return BOOL_FALSE;
  865. }
  866. ksession_set_term_width(ktpd->session, width);
  867. ksession_set_term_height(ktpd->session, height);
  868. faux_str_free(line);
  869. if (!ktpd->exec)
  870. return BOOL_TRUE;
  871. // Set pseudo terminal window size
  872. kexec_set_winsize(ktpd->exec);
  873. return BOOL_TRUE;
  874. }
  875. static bool_t ktpd_session_process_notification(ktpd_session_t *ktpd, faux_msg_t *msg)
  876. {
  877. assert(ktpd);
  878. assert(msg);
  879. ktpd_session_process_winch(ktpd, msg);
  880. return BOOL_TRUE;
  881. }
  882. static bool_t ktpd_session_process_stdout_close(ktpd_session_t *ktpd,
  883. faux_msg_t *msg)
  884. {
  885. int fd = -1;
  886. assert(ktpd);
  887. assert(msg);
  888. if (!ktpd->exec)
  889. return BOOL_FALSE;
  890. fd = kexec_stdout(ktpd->exec);
  891. if (fd < 0)
  892. return BOOL_FALSE;
  893. close(fd);
  894. // Remove already generated data from out buffer. This data is not
  895. // needed now
  896. faux_buf_empty(kexec_bufout(ktpd->exec));
  897. return BOOL_TRUE;
  898. }
  899. static bool_t ktpd_session_dispatch(ktpd_session_t *ktpd, faux_msg_t *msg)
  900. {
  901. uint16_t cmd = 0;
  902. const char *err = NULL;
  903. ktp_cmd_e ecmd = KTP_NOTIFICATION; // Answer command if error
  904. assert(ktpd);
  905. if (!ktpd)
  906. return BOOL_FALSE;
  907. assert(msg);
  908. if (!msg)
  909. return BOOL_FALSE;
  910. cmd = faux_msg_get_cmd(msg);
  911. switch (cmd) {
  912. case KTP_AUTH:
  913. if ((ktpd->state != KTPD_SESSION_STATE_UNAUTHORIZED) &&
  914. (ktpd->state != KTPD_SESSION_STATE_IDLE)) {
  915. ecmd = KTP_AUTH_ACK;
  916. err = "Server illegal state for authorization";
  917. break;
  918. }
  919. ktpd_session_process_auth(ktpd, msg);
  920. break;
  921. case KTP_CMD:
  922. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  923. ecmd = KTP_CMD_ACK;
  924. err = "Server illegal state for command execution";
  925. break;
  926. }
  927. ktpd_session_process_cmd(ktpd, msg);
  928. break;
  929. case KTP_COMPLETION:
  930. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  931. ecmd = KTP_COMPLETION_ACK;
  932. err = "Server illegal state for completion";
  933. break;
  934. }
  935. ktpd_session_process_completion(ktpd, msg);
  936. break;
  937. case KTP_HELP:
  938. if (ktpd->state != KTPD_SESSION_STATE_IDLE) {
  939. ecmd = KTP_HELP_ACK;
  940. err = "Server illegal state for help";
  941. break;
  942. }
  943. ktpd_session_process_help(ktpd, msg);
  944. break;
  945. case KTP_STDIN:
  946. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
  947. err = "Nobody is waiting for stdin";
  948. break;
  949. }
  950. ktpd_session_process_stdin(ktpd, msg);
  951. break;
  952. case KTP_NOTIFICATION:
  953. ktpd_session_process_notification(ktpd, msg);
  954. break;
  955. case KTP_STDOUT_CLOSE:
  956. if (ktpd->state != KTPD_SESSION_STATE_WAIT_FOR_PROCESS) {
  957. err = "No active command is running";
  958. break;
  959. }
  960. ktpd_session_process_stdout_close(ktpd, msg);
  961. break;
  962. default:
  963. syslog(LOG_WARNING, "Unsupported command: 0x%04u", cmd);
  964. err = "Unsupported command";
  965. break;
  966. }
  967. // On error
  968. if (err)
  969. ktp_send_error(ktpd->async, ecmd, err);
  970. return BOOL_TRUE;
  971. }
  972. /** @brief Low-level function to receive KTP message.
  973. *
  974. * Firstly function gets the header of message. Then it checks and parses
  975. * header and find out the length of whole message. Then it receives the rest
  976. * of message.
  977. */
  978. static bool_t ktpd_session_read_cb(faux_async_t *async,
  979. faux_buf_t *buf, size_t len, void *user_data)
  980. {
  981. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  982. faux_msg_t *completed_msg = NULL;
  983. char *data = NULL;
  984. assert(async);
  985. assert(buf);
  986. assert(ktpd);
  987. // Linearize buffer
  988. data = malloc(len);
  989. faux_buf_read(buf, data, len);
  990. // Receive header
  991. if (!ktpd->hdr) {
  992. size_t whole_len = 0;
  993. size_t msg_wo_hdr = 0;
  994. ktpd->hdr = (faux_hdr_t *)data;
  995. // Check for broken header
  996. if (!ktp_check_header(ktpd->hdr)) {
  997. faux_free(ktpd->hdr);
  998. ktpd->hdr = NULL;
  999. return BOOL_FALSE;
  1000. }
  1001. whole_len = faux_hdr_len(ktpd->hdr);
  1002. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  1003. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  1004. // Plan to receive message body
  1005. if (msg_wo_hdr > 0) {
  1006. faux_async_set_read_limits(async,
  1007. msg_wo_hdr, msg_wo_hdr);
  1008. return BOOL_TRUE;
  1009. }
  1010. // Here message is completed (msg body has zero length)
  1011. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, NULL, 0);
  1012. // Receive message body
  1013. } else {
  1014. completed_msg = faux_msg_deserialize_parts(ktpd->hdr, data, len);
  1015. faux_free(data);
  1016. }
  1017. // Plan to receive msg header
  1018. faux_async_set_read_limits(ktpd->async,
  1019. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  1020. faux_free(ktpd->hdr);
  1021. ktpd->hdr = NULL; // Ready to recv new header
  1022. // Here message is completed
  1023. ktpd_session_dispatch(ktpd, completed_msg);
  1024. faux_msg_free(completed_msg);
  1025. return BOOL_TRUE;
  1026. }
  1027. bool_t ktpd_session_connected(ktpd_session_t *ktpd)
  1028. {
  1029. assert(ktpd);
  1030. if (!ktpd)
  1031. return BOOL_FALSE;
  1032. if (KTPD_SESSION_STATE_DISCONNECTED == ktpd->state)
  1033. return BOOL_FALSE;
  1034. return BOOL_TRUE;
  1035. }
  1036. int ktpd_session_fd(const ktpd_session_t *ktpd)
  1037. {
  1038. assert(ktpd);
  1039. if (!ktpd)
  1040. return BOOL_FALSE;
  1041. return faux_async_fd(ktpd->async);
  1042. }
  1043. static bool_t get_stream(ktpd_session_t *ktpd, int fd, bool_t is_stderr)
  1044. {
  1045. ssize_t r = -1;
  1046. faux_buf_t *faux_buf = NULL;
  1047. char *buf = NULL;
  1048. ssize_t len = 0;
  1049. faux_msg_t *ack = NULL;
  1050. if (!ktpd)
  1051. return BOOL_TRUE;
  1052. if (!ktpd->exec)
  1053. return BOOL_TRUE;
  1054. if (is_stderr)
  1055. faux_buf = kexec_buferr(ktpd->exec);
  1056. else
  1057. faux_buf = kexec_bufout(ktpd->exec);
  1058. assert(faux_buf);
  1059. do {
  1060. void *linear_buf = NULL;
  1061. ssize_t really_readed = 0;
  1062. ssize_t linear_len =
  1063. faux_buf_dwrite_lock_easy(faux_buf, &linear_buf);
  1064. // Non-blocked read. The fd became non-blocked while
  1065. // kexec_prepare().
  1066. r = read(fd, linear_buf, linear_len);
  1067. if (r > 0)
  1068. really_readed = r;
  1069. faux_buf_dwrite_unlock_easy(faux_buf, really_readed);
  1070. } while (r > 0);
  1071. len = faux_buf_len(faux_buf);
  1072. if (0 == len)
  1073. return BOOL_TRUE;
  1074. buf = malloc(len);
  1075. faux_buf_read(faux_buf, buf, len);
  1076. // Create KTP_STDOUT/KTP_STDERR message to send to client
  1077. ack = ktp_msg_preform(is_stderr ? KTP_STDERR : KTP_STDOUT, KTP_STATUS_NONE);
  1078. faux_msg_add_param(ack, KTP_PARAM_LINE, buf, len);
  1079. faux_msg_send_async(ack, ktpd->async);
  1080. faux_msg_free(ack);
  1081. free(buf);
  1082. return BOOL_TRUE;
  1083. }
  1084. static bool_t action_stdout_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1085. void *associated_data, void *user_data)
  1086. {
  1087. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1088. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1089. // Interactive command use these function as callback not only for
  1090. // getting stdout but for writing stdin too. Because pseudo-terminal
  1091. // uses the same fd for in and out.
  1092. if (info->revents & POLLOUT)
  1093. push_stdin(eloop, type, associated_data, user_data);
  1094. if (info->revents & POLLIN)
  1095. get_stream(ktpd, info->fd, BOOL_FALSE);
  1096. // Some errors or fd is closed so remove it from polling
  1097. // EOF || POLERR || POLLNVAL
  1098. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1099. faux_eloop_del_fd(eloop, info->fd);
  1100. return BOOL_TRUE;
  1101. }
  1102. static bool_t action_stderr_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1103. void *associated_data, void *user_data)
  1104. {
  1105. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1106. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1107. if (info->revents & POLLIN)
  1108. get_stream(ktpd, info->fd, BOOL_TRUE);
  1109. // Some errors or fd is closed so remove it from polling
  1110. // EOF || POLERR || POLLNVAL
  1111. if (info->revents & (POLLHUP | POLLERR | POLLNVAL))
  1112. faux_eloop_del_fd(eloop, info->fd);
  1113. type = type; // Happy compiler
  1114. return BOOL_TRUE;
  1115. }
  1116. bool_t client_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  1117. void *associated_data, void *user_data)
  1118. {
  1119. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  1120. ktpd_session_t *ktpd = (ktpd_session_t *)user_data;
  1121. faux_async_t *async = ktpd->async;
  1122. assert(async);
  1123. // Write data
  1124. if (info->revents & POLLOUT) {
  1125. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  1126. if (faux_async_out(async) < 0) {
  1127. // Someting went wrong
  1128. faux_eloop_del_fd(eloop, info->fd);
  1129. syslog(LOG_ERR, "Can't send data to client");
  1130. return BOOL_FALSE; // Stop event loop
  1131. }
  1132. }
  1133. // Read data
  1134. if (info->revents & POLLIN) {
  1135. if (faux_async_in(async) < 0) {
  1136. // Someting went wrong
  1137. faux_eloop_del_fd(eloop, info->fd);
  1138. syslog(LOG_ERR, "Can't get data from client");
  1139. return BOOL_FALSE; // Stop event loop
  1140. }
  1141. }
  1142. // EOF
  1143. if (info->revents & POLLHUP) {
  1144. faux_eloop_del_fd(eloop, info->fd);
  1145. syslog(LOG_DEBUG, "Connection %d is closed by client", info->fd);
  1146. return BOOL_FALSE; // Stop event loop
  1147. }
  1148. // POLLERR
  1149. if (info->revents & POLLERR) {
  1150. faux_eloop_del_fd(eloop, info->fd);
  1151. syslog(LOG_DEBUG, "POLLERR received %d", info->fd);
  1152. return BOOL_FALSE; // Stop event loop
  1153. }
  1154. // POLLNVAL
  1155. if (info->revents & POLLNVAL) {
  1156. faux_eloop_del_fd(eloop, info->fd);
  1157. syslog(LOG_DEBUG, "POLLNVAL received %d", info->fd);
  1158. return BOOL_FALSE; // Stop event loop
  1159. }
  1160. type = type; // Happy compiler
  1161. // Session can be really finished here. Note KTPD session can't be
  1162. // stopped immediately so it's only two places within code to really
  1163. // break the loop. This one and within wait_for_action_ev().
  1164. if (ktpd->exit)
  1165. return BOOL_FALSE;
  1166. return BOOL_TRUE;
  1167. }
  1168. #if 0
  1169. static void ktpd_session_bad_socket(ktpd_session_t *ktpd)
  1170. {
  1171. assert(ktpd);
  1172. if (!ktpd)
  1173. return;
  1174. ktpd->state = KTPD_SESSION_STATE_DISCONNECTED;
  1175. }
  1176. #endif