qdevice-ipc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490
  1. /*
  2. * Copyright (c) 2015-2019 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "log.h"
  35. #include "qdevice-config.h"
  36. #include "qdevice-ipc.h"
  37. #include "unix-socket-ipc.h"
  38. #include "dynar-simple-lex.h"
  39. #include "dynar-str.h"
  40. #include "qdevice-ipc-cmd.h"
  41. /*
  42. * Callbacks
  43. */
  44. /*
  45. * IPC server socket
  46. */
  47. static int
  48. ipc_socket_poll_loop_set_events_cb(int fd, short *events, void *user_data1, void *user_data2)
  49. {
  50. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  51. if (qdevice_ipc_is_closed(instance)) {
  52. log(LOG_DEBUG, "Listening socket is closed");
  53. return (-2);
  54. }
  55. return (0);
  56. }
  57. static int
  58. ipc_socket_poll_loop_read_cb(int fd, void *user_data1, void *user_data2)
  59. {
  60. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  61. struct unix_socket_client *ipc_client;
  62. qdevice_ipc_accept(instance, &ipc_client);
  63. return (0);
  64. }
  65. static int
  66. ipc_socket_poll_loop_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  67. {
  68. if (revents != POLLNVAL) {
  69. /*
  70. * Poll ERR on listening socket is fatal error.
  71. * POLL_NVAL is used as a signal to quit poll loop.
  72. */
  73. log(LOG_CRIT, "POLL_ERR (%u) on listening socket", revents);
  74. } else {
  75. log(LOG_DEBUG, "Listening socket is closed");
  76. }
  77. return (-1);
  78. }
  79. /*
  80. * IPC client sockets
  81. */
  82. static int
  83. ipc_client_socket_poll_loop_set_events_cb(int fd, short *events, void *user_data1, void *user_data2)
  84. {
  85. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  86. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  87. if (ipc_client->schedule_disconnect) {
  88. qdevice_ipc_client_disconnect(instance, ipc_client);
  89. if (pr_poll_loop_del_fd(&instance->main_poll_loop, fd) == -1) {
  90. log(LOG_ERR, "pr_poll_loop_del_fd for ipc client socket failed");
  91. return (-2);
  92. }
  93. return (-1);
  94. }
  95. if (!ipc_client->reading_line && !ipc_client->writing_buffer) {
  96. return (-1);
  97. }
  98. if (ipc_client->reading_line) {
  99. *events |= POLLIN;
  100. }
  101. if (ipc_client->writing_buffer) {
  102. *events |= POLLOUT;
  103. }
  104. return (0);
  105. }
  106. static int
  107. ipc_client_socket_poll_loop_read_cb(int fd, void *user_data1, void *user_data2)
  108. {
  109. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  110. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  111. if (!ipc_client->schedule_disconnect) {
  112. qdevice_ipc_io_read(instance, ipc_client);
  113. }
  114. return (0);
  115. }
  116. static int
  117. ipc_client_socket_poll_loop_write_cb(int fd, void *user_data1, void *user_data2)
  118. {
  119. struct qdevice_instance *instance = (struct qdevice_instance *)user_data1;
  120. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  121. if (!ipc_client->schedule_disconnect) {
  122. qdevice_ipc_io_write(instance, ipc_client);
  123. }
  124. return (0);
  125. }
  126. static int
  127. ipc_client_socket_poll_loop_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  128. {
  129. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  130. if (!ipc_client->schedule_disconnect) {
  131. log(LOG_DEBUG, "POLL_ERR (%u) on ipc client socket."
  132. " Disconnecting.", revents);
  133. ipc_client->schedule_disconnect = 1;
  134. }
  135. return (0);
  136. }
  137. /*
  138. * Exported functions
  139. */
  140. int
  141. qdevice_ipc_init(struct qdevice_instance *instance)
  142. {
  143. if (unix_socket_ipc_init(&instance->local_ipc,
  144. instance->advanced_settings->local_socket_file,
  145. instance->advanced_settings->local_socket_backlog,
  146. instance->advanced_settings->ipc_max_clients,
  147. instance->advanced_settings->ipc_max_receive_size,
  148. instance->advanced_settings->ipc_max_send_size) != 0) {
  149. log_err(LOG_ERR, "Can't create unix socket");
  150. return (-1);
  151. }
  152. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->local_ipc.socket, POLLIN,
  153. ipc_socket_poll_loop_set_events_cb,
  154. ipc_socket_poll_loop_read_cb,
  155. NULL,
  156. ipc_socket_poll_loop_err_cb, instance, NULL) == -1) {
  157. log(LOG_ERR, "Can't add IPC socket to main poll loop");
  158. return (-1);
  159. }
  160. return (0);
  161. }
  162. int
  163. qdevice_ipc_close(struct qdevice_instance *instance)
  164. {
  165. int res;
  166. res = unix_socket_ipc_close(&instance->local_ipc);
  167. if (res != 0) {
  168. log_err(LOG_WARNING, "Can't close local IPC");
  169. }
  170. return (res);
  171. }
  172. int
  173. qdevice_ipc_is_closed(struct qdevice_instance *instance)
  174. {
  175. return (unix_socket_ipc_is_closed(&instance->local_ipc));
  176. }
  177. int
  178. qdevice_ipc_destroy(struct qdevice_instance *instance)
  179. {
  180. int res;
  181. struct unix_socket_client *client;
  182. const struct unix_socket_client_list *ipc_client_list;
  183. ipc_client_list = &instance->local_ipc.clients;
  184. TAILQ_FOREACH(client, ipc_client_list, entries) {
  185. free(client->user_data);
  186. }
  187. res = unix_socket_ipc_destroy(&instance->local_ipc);
  188. if (res != 0) {
  189. log_err(LOG_WARNING, "Can't destroy local IPC");
  190. }
  191. return (res);
  192. }
  193. int
  194. qdevice_ipc_accept(struct qdevice_instance *instance, struct unix_socket_client **res_client)
  195. {
  196. int res;
  197. int accept_res;
  198. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  199. switch (accept_res) {
  200. case -1:
  201. log_err(LOG_ERR, "Can't accept local IPC connection");
  202. res = -1;
  203. goto return_res;
  204. break;
  205. case -2:
  206. log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  207. res = -1;
  208. goto return_res;
  209. break;
  210. case -3:
  211. log(LOG_ERR, "Can't add client to list");
  212. res = -1;
  213. goto return_res;
  214. break;
  215. default:
  216. unix_socket_client_read_line(*res_client, 1);
  217. res = 0;
  218. break;
  219. }
  220. (*res_client)->user_data = malloc(sizeof(struct qdevice_ipc_user_data));
  221. if ((*res_client)->user_data == NULL) {
  222. log(LOG_ERR, "Can't alloc IPC client user data");
  223. res = -1;
  224. qdevice_ipc_client_disconnect(instance, *res_client);
  225. goto return_res;
  226. } else {
  227. memset((*res_client)->user_data, 0, sizeof(struct qdevice_ipc_user_data));
  228. }
  229. if (pr_poll_loop_add_fd(&instance->main_poll_loop, (*res_client)->socket, 0,
  230. ipc_client_socket_poll_loop_set_events_cb,
  231. ipc_client_socket_poll_loop_read_cb,
  232. ipc_client_socket_poll_loop_write_cb,
  233. ipc_client_socket_poll_loop_err_cb, instance, *res_client) == -1) {
  234. log(LOG_ERR, "Can't add IPC client socket to main poll loop");
  235. res = -1;
  236. qdevice_ipc_client_disconnect(instance, *res_client);
  237. goto return_res;
  238. }
  239. return_res:
  240. return (res);
  241. }
  242. void
  243. qdevice_ipc_client_disconnect(struct qdevice_instance *instance, struct unix_socket_client *client)
  244. {
  245. free(client->user_data);
  246. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  247. }
  248. int
  249. qdevice_ipc_send_error(struct qdevice_instance *instance, struct unix_socket_client *client,
  250. const char *error_fmt, ...)
  251. {
  252. va_list ap;
  253. int res;
  254. va_start(ap, error_fmt);
  255. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  256. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  257. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  258. va_end(ap);
  259. if (res) {
  260. unix_socket_client_write_buffer(client, 1);
  261. } else {
  262. log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  263. }
  264. return (res ? 0 : -1);
  265. }
  266. int
  267. qdevice_ipc_send_buffer(struct qdevice_instance *instance, struct unix_socket_client *client)
  268. {
  269. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  270. log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  271. if (qdevice_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  272. return (-1);
  273. }
  274. return (0);
  275. }
  276. unix_socket_client_write_buffer(client, 1);
  277. return (0);
  278. }
  279. static void
  280. qdevice_ipc_parse_line(struct qdevice_instance *instance, struct unix_socket_client *client)
  281. {
  282. struct dynar_simple_lex lex;
  283. struct dynar *token;
  284. char *str;
  285. struct qdevice_ipc_user_data *ipc_user_data;
  286. int verbose;
  287. ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
  288. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_PLAIN);
  289. token = dynar_simple_lex_token_next(&lex);
  290. verbose = 0;
  291. if (token == NULL) {
  292. log(LOG_ERR, "Can't alloc memory for simple lex");
  293. if (qdevice_ipc_send_error(instance, client, "Command too long") != 0) {
  294. client->schedule_disconnect = 1;
  295. }
  296. return;
  297. }
  298. str = dynar_data(token);
  299. if (strcasecmp(str, "") == 0) {
  300. log(LOG_DEBUG, "IPC client doesn't send command");
  301. if (qdevice_ipc_send_error(instance, client, "No command specified") != 0) {
  302. client->schedule_disconnect = 1;
  303. }
  304. } else if (strcasecmp(str, "shutdown") == 0) {
  305. log(LOG_DEBUG, "IPC client requested shutdown");
  306. ipc_user_data->shutdown_requested = 1;
  307. if (qdevice_ipc_send_buffer(instance, client) != 0) {
  308. client->schedule_disconnect = 1;
  309. }
  310. } else if (strcasecmp(str, "status") == 0) {
  311. token = dynar_simple_lex_token_next(&lex);
  312. if (token != NULL && (str = dynar_data(token), strcmp(str, "")) != 0) {
  313. if (strcasecmp(str, "verbose") == 0) {
  314. verbose = 1;
  315. }
  316. }
  317. if (qdevice_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  318. if (qdevice_ipc_send_error(instance, client, "Can't get QDevice status") != 0) {
  319. client->schedule_disconnect = 1;
  320. }
  321. } else {
  322. if (qdevice_ipc_send_buffer(instance, client) != 0) {
  323. client->schedule_disconnect = 1;
  324. }
  325. }
  326. } else {
  327. log(LOG_DEBUG, "IPC client sent unknown command");
  328. if (qdevice_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  329. client->schedule_disconnect = 1;
  330. }
  331. }
  332. dynar_simple_lex_destroy(&lex);
  333. }
  334. void
  335. qdevice_ipc_io_read(struct qdevice_instance *instance, struct unix_socket_client *client)
  336. {
  337. int res;
  338. res = unix_socket_client_io_read(client);
  339. switch (res) {
  340. case 0:
  341. /*
  342. * Partial read
  343. */
  344. break;
  345. case -1:
  346. log(LOG_DEBUG, "IPC client closed connection");
  347. client->schedule_disconnect = 1;
  348. break;
  349. case -2:
  350. log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  351. client->schedule_disconnect = 1;
  352. break;
  353. case -3:
  354. log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  355. client->schedule_disconnect = 1;
  356. break;
  357. case 1:
  358. /*
  359. * Full message received
  360. */
  361. unix_socket_client_read_line(client, 0);
  362. qdevice_ipc_parse_line(instance, client);
  363. break;
  364. }
  365. }
  366. void
  367. qdevice_ipc_io_write(struct qdevice_instance *instance, struct unix_socket_client *client)
  368. {
  369. int res;
  370. struct qdevice_ipc_user_data *ipc_user_data;
  371. ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
  372. res = unix_socket_client_io_write(client);
  373. switch (res) {
  374. case 0:
  375. /*
  376. * Partial send
  377. */
  378. break;
  379. case -1:
  380. log(LOG_DEBUG, "IPC client closed connection");
  381. client->schedule_disconnect = 1;
  382. break;
  383. case -2:
  384. log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  385. client->schedule_disconnect = 1;
  386. break;
  387. case 1:
  388. /*
  389. * Full message sent
  390. */
  391. unix_socket_client_write_buffer(client, 0);
  392. client->schedule_disconnect = 1;
  393. if (ipc_user_data->shutdown_requested) {
  394. qdevice_ipc_close(instance);
  395. }
  396. break;
  397. }
  398. }