qdevice-ipc.c 8.4 KB

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