ktp_session.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4. #include <assert.h>
  5. #include <unistd.h>
  6. #include <errno.h>
  7. #include <sys/types.h>
  8. #include <sys/stat.h>
  9. #include <fcntl.h>
  10. #include <sys/socket.h>
  11. #include <sys/un.h>
  12. #include <syslog.h>
  13. #include <faux/str.h>
  14. #include <klish/ktp_session.h>
  15. typedef enum {
  16. KTP_SESSION_STATE_DISCONNECTED = 'd',
  17. KTP_SESSION_STATE_UNAUTHORIZED = 'a',
  18. KTP_SESSION_STATE_IDLE = 'i',
  19. KTP_SESSION_STATE_WAIT_FOR_COMPLETION = 'v',
  20. KTP_SESSION_STATE_WAIT_FOR_HELP = 'h',
  21. KTP_SESSION_STATE_WAIT_FOR_CMD = 'c',
  22. } ktp_session_state_e;
  23. struct ktp_session_s {
  24. ktp_session_state_e state;
  25. faux_async_t *async;
  26. faux_hdr_t *hdr; // Service var: engine will receive header and then msg
  27. bool_t done;
  28. faux_eloop_t *eloop;
  29. ktp_session_stdout_cb_fn stdout_cb;
  30. void *stdout_udata;
  31. ktp_session_stdout_cb_fn stderr_cb;
  32. void *stderr_udata;
  33. faux_error_t *error; // Internal
  34. bool_t request_done;
  35. int cmd_retcode; // Internal
  36. bool_t cmd_retcode_available;
  37. ktp_status_e cmd_features;
  38. bool_t cmd_features_available;
  39. };
  40. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  41. void *associated_data, void *user_data);
  42. static bool_t server_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  43. void *associated_data, void *user_data);
  44. static bool_t ktp_session_read_cb(faux_async_t *async,
  45. faux_buf_t *buf, size_t len, void *user_data);
  46. ktp_session_t *ktp_session_new(int sock)
  47. {
  48. ktp_session_t *ktp = NULL;
  49. if (sock < 0)
  50. return NULL;
  51. ktp = faux_zmalloc(sizeof(*ktp));
  52. assert(ktp);
  53. if (!ktp)
  54. return NULL;
  55. // Init
  56. ktp->state = KTP_SESSION_STATE_IDLE;
  57. ktp->done = BOOL_FALSE;
  58. // Event loop
  59. ktp->eloop = faux_eloop_new(NULL);
  60. // Async object
  61. ktp->async = faux_async_new(sock);
  62. assert(ktp->async);
  63. // Receive message header first
  64. faux_async_set_read_limits(ktp->async,
  65. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  66. faux_async_set_read_cb(ktp->async, ktp_session_read_cb, ktp);
  67. ktp->hdr = NULL;
  68. faux_async_set_stall_cb(ktp->async, ktp_stall_cb, ktp->eloop);
  69. // Event loop handlers
  70. faux_eloop_add_signal(ktp->eloop, SIGINT, stop_loop_ev, ktp);
  71. faux_eloop_add_signal(ktp->eloop, SIGTERM, stop_loop_ev, ktp);
  72. faux_eloop_add_signal(ktp->eloop, SIGQUIT, stop_loop_ev, ktp);
  73. faux_eloop_add_fd(ktp->eloop, ktp_session_fd(ktp), POLLIN,
  74. server_ev, ktp);
  75. // Callbacks
  76. ktp->stdout_cb = NULL;
  77. ktp->stdout_udata = NULL;
  78. ktp->stderr_cb = NULL;
  79. ktp->stderr_udata = NULL;
  80. ktp->error = NULL;
  81. ktp->cmd_retcode = -1;
  82. ktp->cmd_retcode_available = BOOL_FALSE;
  83. ktp->request_done = BOOL_FALSE;
  84. ktp->cmd_features = KTP_STATUS_NONE;
  85. ktp->cmd_features_available = BOOL_FALSE;
  86. return ktp;
  87. }
  88. void ktp_session_free(ktp_session_t *ktp)
  89. {
  90. if (!ktp)
  91. return;
  92. faux_free(ktp->hdr);
  93. close(ktp_session_fd(ktp));
  94. faux_async_free(ktp->async);
  95. faux_eloop_free(ktp->eloop);
  96. faux_free(ktp);
  97. }
  98. bool_t ktp_session_done(const ktp_session_t *ktp)
  99. {
  100. assert(ktp);
  101. if (!ktp)
  102. return BOOL_TRUE; // Done flag
  103. return ktp->done;
  104. }
  105. bool_t ktp_session_set_done(ktp_session_t *ktp, bool_t done)
  106. {
  107. assert(ktp);
  108. if (!ktp)
  109. return BOOL_FALSE;
  110. ktp->done = done;
  111. return BOOL_TRUE;
  112. }
  113. ktp_status_e ktp_session_cmd_features(const ktp_session_t *ktp)
  114. {
  115. assert(ktp);
  116. if (!ktp)
  117. return KTP_STATUS_NONE;
  118. return ktp->cmd_features;
  119. }
  120. faux_error_t *ktp_session_error(const ktp_session_t *ktp)
  121. {
  122. assert(ktp);
  123. if (!ktp)
  124. return BOOL_FALSE;
  125. return ktp->error;
  126. }
  127. bool_t ktp_session_set_stdout_cb(ktp_session_t *ktp,
  128. ktp_session_stdout_cb_fn stdout_cb, void *stdout_udata)
  129. {
  130. assert(ktp);
  131. if (!ktp)
  132. return BOOL_FALSE;
  133. ktp->stdout_cb = stdout_cb;
  134. ktp->stdout_udata = stdout_udata;
  135. return BOOL_TRUE;
  136. }
  137. bool_t ktp_session_set_stderr_cb(ktp_session_t *ktp,
  138. ktp_session_stdout_cb_fn stderr_cb, void *stderr_udata)
  139. {
  140. assert(ktp);
  141. if (!ktp)
  142. return BOOL_FALSE;
  143. ktp->stderr_cb = stderr_cb;
  144. ktp->stderr_udata = stderr_udata;
  145. return BOOL_TRUE;
  146. }
  147. bool_t ktp_session_connected(ktp_session_t *ktp)
  148. {
  149. assert(ktp);
  150. if (!ktp)
  151. return BOOL_FALSE;
  152. if (KTP_SESSION_STATE_DISCONNECTED == ktp->state)
  153. return BOOL_FALSE;
  154. return BOOL_TRUE;
  155. }
  156. int ktp_session_fd(const ktp_session_t *ktp)
  157. {
  158. assert(ktp);
  159. if (!ktp)
  160. return BOOL_FALSE;
  161. return faux_async_fd(ktp->async);
  162. }
  163. #if 0
  164. static void ktp_session_bad_socket(ktp_session_t *ktp)
  165. {
  166. assert(ktp);
  167. if (!ktp)
  168. return;
  169. ktp->state = KTP_SESSION_STATE_DISCONNECTED;
  170. }
  171. #endif
  172. static bool_t stop_loop_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  173. void *associated_data, void *user_data)
  174. {
  175. ktp_session_t *ktp = (ktp_session_t *)user_data;
  176. if (!ktp)
  177. return BOOL_FALSE;
  178. ktp_session_set_done(ktp, BOOL_TRUE);
  179. // Happy compiler
  180. eloop = eloop;
  181. type = type;
  182. associated_data = associated_data;
  183. return BOOL_FALSE; // Stop Event Loop
  184. }
  185. static bool_t server_ev(faux_eloop_t *eloop, faux_eloop_type_e type,
  186. void *associated_data, void *user_data)
  187. {
  188. faux_eloop_info_fd_t *info = (faux_eloop_info_fd_t *)associated_data;
  189. ktp_session_t *ktp = (ktp_session_t *)user_data;
  190. assert(ktp);
  191. // Write data
  192. if (info->revents & POLLOUT) {
  193. faux_eloop_exclude_fd_event(eloop, info->fd, POLLOUT);
  194. if (faux_async_out(ktp->async) < 0) {
  195. // Someting went wrong
  196. faux_eloop_del_fd(eloop, info->fd);
  197. syslog(LOG_ERR, "Problem with async output");
  198. return BOOL_FALSE; // Stop event loop
  199. }
  200. }
  201. // Read data
  202. if (info->revents & POLLIN) {
  203. if (faux_async_in(ktp->async) < 0) {
  204. // Someting went wrong
  205. faux_eloop_del_fd(eloop, info->fd);
  206. syslog(LOG_ERR, "Problem with async input");
  207. return BOOL_FALSE; // Stop event loop
  208. }
  209. }
  210. // EOF
  211. if (info->revents & POLLHUP) {
  212. faux_eloop_del_fd(eloop, info->fd);
  213. syslog(LOG_DEBUG, "Close connection %d", info->fd);
  214. return BOOL_FALSE; // Stop event loop
  215. }
  216. type = type; // Happy compiler
  217. if (ktp->request_done)
  218. return BOOL_FALSE; // Stop event loop on receiving answer
  219. return BOOL_TRUE;
  220. }
  221. static bool_t ktp_session_process_stdout(ktp_session_t *ktp, const faux_msg_t *msg)
  222. {
  223. char *line = NULL;
  224. unsigned int len = 0;
  225. assert(ktp);
  226. assert(msg);
  227. if (!ktp->stdout_cb)
  228. return BOOL_TRUE; // Just ignore stdout. It's not a bug
  229. if (!ktp->cmd_features_available)
  230. return BOOL_TRUE; // Drop message
  231. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_LINE, (void **)&line, &len))
  232. return BOOL_TRUE; // It's strange but not a bug
  233. return ktp->stdout_cb(ktp, line, len, ktp->stdout_udata);
  234. }
  235. static bool_t ktp_session_process_stderr(ktp_session_t *ktp, const faux_msg_t *msg)
  236. {
  237. char *line = NULL;
  238. unsigned int len = 0;
  239. assert(ktp);
  240. assert(msg);
  241. if (!ktp->stderr_cb)
  242. return BOOL_TRUE; // Just ignore message. It's not a bug
  243. if (!ktp->cmd_features_available)
  244. return BOOL_TRUE; // Drop message
  245. if (!faux_msg_get_param_by_type(msg, KTP_PARAM_LINE,
  246. (void **)&line, &len))
  247. return BOOL_TRUE; // It's strange but not a bug
  248. return ktp->stderr_cb(ktp, line, len, ktp->stderr_udata);
  249. }
  250. static bool_t ktp_session_process_cmd_ack(ktp_session_t *ktp, const faux_msg_t *msg)
  251. {
  252. uint8_t *retcode8bit = NULL;
  253. ktp_status_e status = KTP_STATUS_NONE;
  254. char *error_str = NULL;
  255. assert(ktp);
  256. assert(msg);
  257. status = faux_msg_get_status(msg);
  258. // cmd_ack with flag 'incompleted'
  259. if (KTP_STATUS_IS_INCOMPLETED(status)) {
  260. // Only first 'incompleted' cmd ack sets cmd features
  261. if (!ktp->cmd_features_available) {
  262. ktp->cmd_features_available = BOOL_TRUE;
  263. ktp->cmd_features = status & KTP_STATUS_INTERACTIVE;
  264. }
  265. return BOOL_TRUE;
  266. }
  267. if (faux_msg_get_param_by_type(msg, KTP_PARAM_RETCODE,
  268. (void **)&retcode8bit, NULL))
  269. ktp->cmd_retcode = (int)(*retcode8bit);
  270. error_str = faux_msg_get_str_param_by_type(msg, KTP_PARAM_ERROR);
  271. if (error_str) {
  272. faux_error_add(ktp->error, error_str);
  273. faux_str_free(error_str);
  274. }
  275. ktp->cmd_retcode_available = BOOL_TRUE; // Answer from server was received
  276. ktp->request_done = BOOL_TRUE; // Stop the loop
  277. return BOOL_TRUE;
  278. }
  279. static bool_t ktp_session_dispatch(ktp_session_t *ktp, faux_msg_t *msg)
  280. {
  281. uint16_t cmd = 0;
  282. bool_t rc = BOOL_TRUE;
  283. assert(ktp);
  284. if (!ktp)
  285. return BOOL_FALSE;
  286. assert(msg);
  287. if (!msg)
  288. return BOOL_FALSE;
  289. cmd = faux_msg_get_cmd(msg);
  290. switch (cmd) {
  291. case KTP_CMD_ACK:
  292. rc = ktp_session_process_cmd_ack(ktp, msg);
  293. break;
  294. case KTP_STDOUT:
  295. rc = ktp_session_process_stdout(ktp, msg);
  296. break;
  297. case KTP_STDERR:
  298. rc = ktp_session_process_stderr(ktp, msg);
  299. break;
  300. default:
  301. syslog(LOG_WARNING, "Unsupported command: 0x%04u\n", cmd); // Ignore
  302. break;
  303. }
  304. return rc;
  305. }
  306. static bool_t ktp_session_read_cb(faux_async_t *async,
  307. faux_buf_t *buf, size_t len, void *user_data)
  308. {
  309. ktp_session_t *ktp = (ktp_session_t *)user_data;
  310. faux_msg_t *completed_msg = NULL;
  311. char *data = NULL;
  312. assert(async);
  313. assert(buf);
  314. assert(ktp);
  315. // Linearize buffer
  316. data = malloc(len);
  317. faux_buf_read(buf, data, len);
  318. // Receive header
  319. if (!ktp->hdr) {
  320. size_t whole_len = 0;
  321. size_t msg_wo_hdr = 0;
  322. ktp->hdr = (faux_hdr_t *)data;
  323. // Check for broken header
  324. if (!ktp_check_header(ktp->hdr)) {
  325. faux_free(ktp->hdr);
  326. ktp->hdr = NULL;
  327. return BOOL_FALSE;
  328. }
  329. whole_len = faux_hdr_len(ktp->hdr);
  330. // msg_wo_hdr >= 0 because ktp_check_header() validates whole_len
  331. msg_wo_hdr = whole_len - sizeof(faux_hdr_t);
  332. // Plan to receive message body
  333. if (msg_wo_hdr > 0) {
  334. faux_async_set_read_limits(async,
  335. msg_wo_hdr, msg_wo_hdr);
  336. return BOOL_TRUE;
  337. }
  338. // Here message is completed (msg body has zero length)
  339. completed_msg = faux_msg_deserialize_parts(ktp->hdr, NULL, 0);
  340. // Receive message body
  341. } else {
  342. completed_msg = faux_msg_deserialize_parts(ktp->hdr, data, len);
  343. faux_free(data);
  344. }
  345. // Plan to receive msg header
  346. faux_async_set_read_limits(ktp->async,
  347. sizeof(faux_hdr_t), sizeof(faux_hdr_t));
  348. faux_free(ktp->hdr);
  349. ktp->hdr = NULL; // Ready to recv new header
  350. // Here message is completed
  351. #ifdef DEBUG
  352. // faux_msg_debug(completed_msg);
  353. #endif
  354. ktp_session_dispatch(ktp, completed_msg);
  355. faux_msg_free(completed_msg);
  356. return BOOL_TRUE;
  357. }
  358. bool_t ktp_session_req_cmd(ktp_session_t *ktp, const char *line,
  359. int *retcode, faux_error_t *error)
  360. {
  361. faux_msg_t *req = NULL;
  362. assert(ktp);
  363. if (!ktp)
  364. return BOOL_FALSE;
  365. if (ktp->state != KTP_SESSION_STATE_IDLE) {
  366. faux_error_sprintf(error,
  367. "Can't execute command. Session state is not suitable");
  368. return BOOL_FALSE;
  369. }
  370. req = ktp_msg_preform(KTP_CMD, KTP_STATUS_NONE);
  371. faux_msg_add_param(req, KTP_PARAM_LINE, line, strlen(line));
  372. faux_msg_send_async(req, ktp->async);
  373. faux_msg_free(req);
  374. // Prepare for loop
  375. ktp->state = KTP_SESSION_STATE_WAIT_FOR_CMD;
  376. ktp->error = error;
  377. ktp->cmd_retcode = -1;
  378. ktp->cmd_retcode_available = BOOL_FALSE;
  379. ktp->request_done = BOOL_FALSE; // Be pessimistic
  380. ktp->cmd_features = KTP_STATUS_NONE;
  381. ktp->cmd_features_available = BOOL_FALSE;
  382. faux_eloop_loop(ktp->eloop);
  383. ktp->state = KTP_SESSION_STATE_IDLE;
  384. ktp->error = NULL;
  385. if (ktp->cmd_retcode_available && retcode)
  386. *retcode = ktp->cmd_retcode;
  387. return ktp->cmd_retcode_available; // Sign of server answer
  388. }