qdevice-ipc.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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. int
  42. qdevice_ipc_init(struct qdevice_instance *instance)
  43. {
  44. if (unix_socket_ipc_init(&instance->local_ipc,
  45. instance->advanced_settings->local_socket_file,
  46. instance->advanced_settings->local_socket_backlog,
  47. instance->advanced_settings->ipc_max_clients,
  48. instance->advanced_settings->ipc_max_receive_size,
  49. instance->advanced_settings->ipc_max_send_size) != 0) {
  50. log_err(LOG_ERR, "Can't create unix socket");
  51. return (-1);
  52. }
  53. return (0);
  54. }
  55. int
  56. qdevice_ipc_close(struct qdevice_instance *instance)
  57. {
  58. int res;
  59. res = unix_socket_ipc_close(&instance->local_ipc);
  60. if (res != 0) {
  61. log_err(LOG_WARNING, "Can't close local IPC");
  62. }
  63. return (res);
  64. }
  65. int
  66. qdevice_ipc_is_closed(struct qdevice_instance *instance)
  67. {
  68. return (unix_socket_ipc_is_closed(&instance->local_ipc));
  69. }
  70. int
  71. qdevice_ipc_destroy(struct qdevice_instance *instance)
  72. {
  73. int res;
  74. struct unix_socket_client *client;
  75. const struct unix_socket_client_list *ipc_client_list;
  76. ipc_client_list = &instance->local_ipc.clients;
  77. TAILQ_FOREACH(client, ipc_client_list, entries) {
  78. free(client->user_data);
  79. }
  80. res = unix_socket_ipc_destroy(&instance->local_ipc);
  81. if (res != 0) {
  82. log_err(LOG_WARNING, "Can't destroy local IPC");
  83. }
  84. return (res);
  85. }
  86. int
  87. qdevice_ipc_accept(struct qdevice_instance *instance, struct unix_socket_client **res_client)
  88. {
  89. int res;
  90. int accept_res;
  91. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  92. switch (accept_res) {
  93. case -1:
  94. log_err(LOG_ERR, "Can't accept local IPC connection");
  95. res = -1;
  96. goto return_res;
  97. break;
  98. case -2:
  99. log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  100. res = -1;
  101. goto return_res;
  102. break;
  103. case -3:
  104. log(LOG_ERR, "Can't add client to list");
  105. res = -1;
  106. goto return_res;
  107. break;
  108. default:
  109. unix_socket_client_read_line(*res_client, 1);
  110. res = 0;
  111. break;
  112. }
  113. (*res_client)->user_data = malloc(sizeof(struct qdevice_ipc_user_data));
  114. if ((*res_client)->user_data == NULL) {
  115. log(LOG_ERR, "Can't alloc IPC client user data");
  116. res = -1;
  117. qdevice_ipc_client_disconnect(instance, *res_client);
  118. } else {
  119. memset((*res_client)->user_data, 0, sizeof(struct qdevice_ipc_user_data));
  120. }
  121. return_res:
  122. return (res);
  123. }
  124. void
  125. qdevice_ipc_client_disconnect(struct qdevice_instance *instance, struct unix_socket_client *client)
  126. {
  127. free(client->user_data);
  128. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  129. }
  130. int
  131. qdevice_ipc_send_error(struct qdevice_instance *instance, struct unix_socket_client *client,
  132. const char *error_fmt, ...)
  133. {
  134. va_list ap;
  135. int res;
  136. va_start(ap, error_fmt);
  137. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  138. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  139. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  140. va_end(ap);
  141. if (res) {
  142. unix_socket_client_write_buffer(client, 1);
  143. } else {
  144. log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  145. }
  146. return (res ? 0 : -1);
  147. }
  148. int
  149. qdevice_ipc_send_buffer(struct qdevice_instance *instance, struct unix_socket_client *client)
  150. {
  151. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  152. log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  153. if (qdevice_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  154. return (-1);
  155. }
  156. return (0);
  157. }
  158. unix_socket_client_write_buffer(client, 1);
  159. return (0);
  160. }
  161. static void
  162. qdevice_ipc_parse_line(struct qdevice_instance *instance, struct unix_socket_client *client)
  163. {
  164. struct dynar_simple_lex lex;
  165. struct dynar *token;
  166. char *str;
  167. struct qdevice_ipc_user_data *ipc_user_data;
  168. int verbose;
  169. ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
  170. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_PLAIN);
  171. token = dynar_simple_lex_token_next(&lex);
  172. verbose = 0;
  173. if (token == NULL) {
  174. log(LOG_ERR, "Can't alloc memory for simple lex");
  175. if (qdevice_ipc_send_error(instance, client, "Command too long") != 0) {
  176. client->schedule_disconnect = 1;
  177. }
  178. return;
  179. }
  180. str = dynar_data(token);
  181. if (strcasecmp(str, "") == 0) {
  182. log(LOG_DEBUG, "IPC client doesn't send command");
  183. if (qdevice_ipc_send_error(instance, client, "No command specified") != 0) {
  184. client->schedule_disconnect = 1;
  185. }
  186. } else if (strcasecmp(str, "shutdown") == 0) {
  187. log(LOG_DEBUG, "IPC client requested shutdown");
  188. ipc_user_data->shutdown_requested = 1;
  189. if (qdevice_ipc_send_buffer(instance, client) != 0) {
  190. client->schedule_disconnect = 1;
  191. }
  192. } else if (strcasecmp(str, "status") == 0) {
  193. token = dynar_simple_lex_token_next(&lex);
  194. if (token != NULL && (str = dynar_data(token), strcmp(str, "")) != 0) {
  195. if (strcasecmp(str, "verbose") == 0) {
  196. verbose = 1;
  197. }
  198. }
  199. if (qdevice_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  200. if (qdevice_ipc_send_error(instance, client, "Can't get QDevice status") != 0) {
  201. client->schedule_disconnect = 1;
  202. }
  203. } else {
  204. if (qdevice_ipc_send_buffer(instance, client) != 0) {
  205. client->schedule_disconnect = 1;
  206. }
  207. }
  208. } else {
  209. log(LOG_DEBUG, "IPC client sent unknown command");
  210. if (qdevice_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  211. client->schedule_disconnect = 1;
  212. }
  213. }
  214. dynar_simple_lex_destroy(&lex);
  215. }
  216. void
  217. qdevice_ipc_io_read(struct qdevice_instance *instance, struct unix_socket_client *client)
  218. {
  219. int res;
  220. res = unix_socket_client_io_read(client);
  221. switch (res) {
  222. case 0:
  223. /*
  224. * Partial read
  225. */
  226. break;
  227. case -1:
  228. log(LOG_DEBUG, "IPC client closed connection");
  229. client->schedule_disconnect = 1;
  230. break;
  231. case -2:
  232. log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  233. client->schedule_disconnect = 1;
  234. break;
  235. case -3:
  236. log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  237. client->schedule_disconnect = 1;
  238. break;
  239. case 1:
  240. /*
  241. * Full message received
  242. */
  243. unix_socket_client_read_line(client, 0);
  244. qdevice_ipc_parse_line(instance, client);
  245. break;
  246. }
  247. }
  248. void
  249. qdevice_ipc_io_write(struct qdevice_instance *instance, struct unix_socket_client *client)
  250. {
  251. int res;
  252. struct qdevice_ipc_user_data *ipc_user_data;
  253. ipc_user_data = (struct qdevice_ipc_user_data *)client->user_data;
  254. res = unix_socket_client_io_write(client);
  255. switch (res) {
  256. case 0:
  257. /*
  258. * Partial send
  259. */
  260. break;
  261. case -1:
  262. log(LOG_DEBUG, "IPC client closed connection");
  263. client->schedule_disconnect = 1;
  264. break;
  265. case -2:
  266. log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  267. client->schedule_disconnect = 1;
  268. break;
  269. case 1:
  270. /*
  271. * Full message sent
  272. */
  273. unix_socket_client_write_buffer(client, 0);
  274. client->schedule_disconnect = 1;
  275. if (ipc_user_data->shutdown_requested) {
  276. qdevice_ipc_close(instance);
  277. }
  278. break;
  279. }
  280. }