4
0

qdevice-ipc.c 7.6 KB

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