qdevice-ipc.c 8.5 KB

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