qdevice-ipc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493
  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->set_local_socket_umask,
  146. instance->advanced_settings->local_socket_umask,
  147. instance->advanced_settings->local_socket_gid,
  148. instance->advanced_settings->local_socket_backlog,
  149. instance->advanced_settings->ipc_max_clients,
  150. instance->advanced_settings->ipc_max_receive_size,
  151. instance->advanced_settings->ipc_max_send_size) != 0) {
  152. log_err(LOG_ERR, "Can't create unix socket");
  153. return (-1);
  154. }
  155. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->local_ipc.socket, POLLIN,
  156. ipc_socket_poll_loop_set_events_cb,
  157. ipc_socket_poll_loop_read_cb,
  158. NULL,
  159. ipc_socket_poll_loop_err_cb, instance, NULL) == -1) {
  160. log(LOG_ERR, "Can't add IPC socket to main poll loop");
  161. return (-1);
  162. }
  163. return (0);
  164. }
  165. int
  166. qdevice_ipc_close(struct qdevice_instance *instance)
  167. {
  168. int res;
  169. res = unix_socket_ipc_close(&instance->local_ipc);
  170. if (res != 0) {
  171. log_err(LOG_WARNING, "Can't close local IPC");
  172. }
  173. return (res);
  174. }
  175. int
  176. qdevice_ipc_is_closed(struct qdevice_instance *instance)
  177. {
  178. return (unix_socket_ipc_is_closed(&instance->local_ipc));
  179. }
  180. int
  181. qdevice_ipc_destroy(struct qdevice_instance *instance)
  182. {
  183. int res;
  184. struct unix_socket_client *client;
  185. const struct unix_socket_client_list *ipc_client_list;
  186. ipc_client_list = &instance->local_ipc.clients;
  187. TAILQ_FOREACH(client, ipc_client_list, entries) {
  188. free(client->user_data);
  189. }
  190. res = unix_socket_ipc_destroy(&instance->local_ipc);
  191. if (res != 0) {
  192. log_err(LOG_WARNING, "Can't destroy local IPC");
  193. }
  194. return (res);
  195. }
  196. int
  197. qdevice_ipc_accept(struct qdevice_instance *instance, struct unix_socket_client **res_client)
  198. {
  199. int res;
  200. int accept_res;
  201. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  202. switch (accept_res) {
  203. case -1:
  204. log_err(LOG_ERR, "Can't accept local IPC connection");
  205. res = -1;
  206. goto return_res;
  207. break;
  208. case -2:
  209. log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  210. res = -1;
  211. goto return_res;
  212. break;
  213. case -3:
  214. log(LOG_ERR, "Can't add client to list");
  215. res = -1;
  216. goto return_res;
  217. break;
  218. default:
  219. unix_socket_client_read_line(*res_client, 1);
  220. res = 0;
  221. break;
  222. }
  223. (*res_client)->user_data = malloc(sizeof(struct qdevice_ipc_user_data));
  224. if ((*res_client)->user_data == NULL) {
  225. log(LOG_ERR, "Can't alloc IPC client user data");
  226. res = -1;
  227. qdevice_ipc_client_disconnect(instance, *res_client);
  228. goto return_res;
  229. } else {
  230. memset((*res_client)->user_data, 0, sizeof(struct qdevice_ipc_user_data));
  231. }
  232. if (pr_poll_loop_add_fd(&instance->main_poll_loop, (*res_client)->socket, 0,
  233. ipc_client_socket_poll_loop_set_events_cb,
  234. ipc_client_socket_poll_loop_read_cb,
  235. ipc_client_socket_poll_loop_write_cb,
  236. ipc_client_socket_poll_loop_err_cb, instance, *res_client) == -1) {
  237. log(LOG_ERR, "Can't add IPC client socket to main poll loop");
  238. res = -1;
  239. qdevice_ipc_client_disconnect(instance, *res_client);
  240. goto return_res;
  241. }
  242. return_res:
  243. return (res);
  244. }
  245. void
  246. qdevice_ipc_client_disconnect(struct qdevice_instance *instance, struct unix_socket_client *client)
  247. {
  248. free(client->user_data);
  249. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  250. }
  251. int
  252. qdevice_ipc_send_error(struct qdevice_instance *instance, struct unix_socket_client *client,
  253. const char *error_fmt, ...)
  254. {
  255. va_list ap;
  256. int res;
  257. va_start(ap, error_fmt);
  258. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  259. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  260. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  261. va_end(ap);
  262. if (res) {
  263. unix_socket_client_write_buffer(client, 1);
  264. } else {
  265. log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  266. }
  267. return (res ? 0 : -1);
  268. }
  269. int
  270. qdevice_ipc_send_buffer(struct qdevice_instance *instance, struct unix_socket_client *client)
  271. {
  272. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  273. log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  274. if (qdevice_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  275. return (-1);
  276. }
  277. return (0);
  278. }
  279. unix_socket_client_write_buffer(client, 1);
  280. return (0);
  281. }
  282. static void
  283. qdevice_ipc_parse_line(struct qdevice_instance *instance, struct unix_socket_client *client)
  284. {
  285. struct dynar_simple_lex lex;
  286. struct dynar *token;
  287. char *str;
  288. struct qdevice_ipc_user_data *ipc_user_data;
  289. int verbose;
  290. ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
  291. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_PLAIN);
  292. token = dynar_simple_lex_token_next(&lex);
  293. verbose = 0;
  294. if (token == NULL) {
  295. log(LOG_ERR, "Can't alloc memory for simple lex");
  296. if (qdevice_ipc_send_error(instance, client, "Command too long") != 0) {
  297. client->schedule_disconnect = 1;
  298. }
  299. return;
  300. }
  301. str = dynar_data(token);
  302. if (strcasecmp(str, "") == 0) {
  303. log(LOG_DEBUG, "IPC client doesn't send command");
  304. if (qdevice_ipc_send_error(instance, client, "No command specified") != 0) {
  305. client->schedule_disconnect = 1;
  306. }
  307. } else if (strcasecmp(str, "shutdown") == 0) {
  308. log(LOG_DEBUG, "IPC client requested shutdown");
  309. ipc_user_data->shutdown_requested = 1;
  310. if (qdevice_ipc_send_buffer(instance, client) != 0) {
  311. client->schedule_disconnect = 1;
  312. }
  313. } else if (strcasecmp(str, "status") == 0) {
  314. token = dynar_simple_lex_token_next(&lex);
  315. if (token != NULL && (str = dynar_data(token), strcmp(str, "")) != 0) {
  316. if (strcasecmp(str, "verbose") == 0) {
  317. verbose = 1;
  318. }
  319. }
  320. if (qdevice_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  321. if (qdevice_ipc_send_error(instance, client, "Can't get QDevice status") != 0) {
  322. client->schedule_disconnect = 1;
  323. }
  324. } else {
  325. if (qdevice_ipc_send_buffer(instance, client) != 0) {
  326. client->schedule_disconnect = 1;
  327. }
  328. }
  329. } else {
  330. log(LOG_DEBUG, "IPC client sent unknown command");
  331. if (qdevice_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  332. client->schedule_disconnect = 1;
  333. }
  334. }
  335. dynar_simple_lex_destroy(&lex);
  336. }
  337. void
  338. qdevice_ipc_io_read(struct qdevice_instance *instance, struct unix_socket_client *client)
  339. {
  340. int res;
  341. res = unix_socket_client_io_read(client);
  342. switch (res) {
  343. case 0:
  344. /*
  345. * Partial read
  346. */
  347. break;
  348. case -1:
  349. log(LOG_DEBUG, "IPC client closed connection");
  350. client->schedule_disconnect = 1;
  351. break;
  352. case -2:
  353. log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  354. client->schedule_disconnect = 1;
  355. break;
  356. case -3:
  357. log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  358. client->schedule_disconnect = 1;
  359. break;
  360. case 1:
  361. /*
  362. * Full message received
  363. */
  364. unix_socket_client_read_line(client, 0);
  365. qdevice_ipc_parse_line(instance, client);
  366. break;
  367. }
  368. }
  369. void
  370. qdevice_ipc_io_write(struct qdevice_instance *instance, struct unix_socket_client *client)
  371. {
  372. int res;
  373. struct qdevice_ipc_user_data *ipc_user_data;
  374. ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
  375. res = unix_socket_client_io_write(client);
  376. switch (res) {
  377. case 0:
  378. /*
  379. * Partial send
  380. */
  381. break;
  382. case -1:
  383. log(LOG_DEBUG, "IPC client closed connection");
  384. client->schedule_disconnect = 1;
  385. break;
  386. case -2:
  387. log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  388. client->schedule_disconnect = 1;
  389. break;
  390. case 1:
  391. /*
  392. * Full message sent
  393. */
  394. unix_socket_client_write_buffer(client, 0);
  395. client->schedule_disconnect = 1;
  396. if (ipc_user_data->shutdown_requested) {
  397. qdevice_ipc_close(instance);
  398. }
  399. break;
  400. }
  401. }