qnetd-ipc.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  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 "qnet-config.h"
  35. #include "qnetd-ipc.h"
  36. #include "qnetd-ipc-cmd.h"
  37. #include "qnetd-log.h"
  38. #include "unix-socket-ipc.h"
  39. #include "dynar-simple-lex.h"
  40. #include "dynar-str.h"
  41. /*
  42. * Needed for creating nspr handle from unix fd
  43. */
  44. #include <private/pprio.h>
  45. int
  46. qnetd_ipc_init(struct qnetd_instance *instance)
  47. {
  48. if (unix_socket_ipc_init(&instance->local_ipc, QNETD_LOCAL_SOCKET_FILE,
  49. QNETD_LOCAL_SOCKET_BACKLOG, QNETD_IPC_MAX_CLIENTS, QNETD_IPC_MAX_RECEIVE_SIZE,
  50. QNETD_IPC_MAX_SEND_SIZE) != 0) {
  51. qnetd_log_err(LOG_ERR, "Can't create unix socket");
  52. return (-1);
  53. }
  54. if ((instance->ipc_socket_poll_fd = PR_CreateSocketPollFd(instance->local_ipc.socket)) == NULL) {
  55. qnetd_log_nss(LOG_CRIT, "Can't create NSPR IPC socket poll fd");
  56. return (-1);
  57. }
  58. return (0);
  59. }
  60. int
  61. qnetd_ipc_close(struct qnetd_instance *instance)
  62. {
  63. int res;
  64. res = unix_socket_ipc_close(&instance->local_ipc);
  65. if (res != 0) {
  66. qnetd_log_err(LOG_WARNING, "Can't close local IPC");
  67. }
  68. return (res);
  69. }
  70. int
  71. qnetd_ipc_destroy(struct qnetd_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. if (PR_DestroySocketPollFd(instance->ipc_socket_poll_fd) != PR_SUCCESS) {
  81. qnetd_log_nss(LOG_WARNING, "Unable to destroy IPC poll socket fd");
  82. }
  83. res = unix_socket_ipc_destroy(&instance->local_ipc);
  84. if (res != 0) {
  85. qnetd_log_err(LOG_WARNING, "Can't destroy local IPC");
  86. }
  87. return (res);
  88. }
  89. int
  90. qnetd_ipc_accept(struct qnetd_instance *instance, struct unix_socket_client **res_client)
  91. {
  92. int res;
  93. int accept_res;
  94. PRFileDesc *prfd;
  95. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  96. switch (accept_res) {
  97. case -1:
  98. qnetd_log_err(LOG_ERR, "Can't accept local IPC connection");
  99. res = -1;
  100. goto return_res;
  101. break;
  102. case -2:
  103. qnetd_log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  104. res = -1;
  105. goto return_res;
  106. break;
  107. case -3:
  108. qnetd_log(LOG_ERR, "Can't add client to list");
  109. res = -1;
  110. goto return_res;
  111. break;
  112. default:
  113. unix_socket_client_read_line(*res_client, 1);
  114. res = 0;
  115. break;
  116. }
  117. (*res_client)->user_data = malloc(sizeof(struct qnetd_ipc_user_data));
  118. if ((*res_client)->user_data == NULL) {
  119. qnetd_log(LOG_ERR, "Can't alloc IPC client user data");
  120. res = -1;
  121. qnetd_ipc_client_disconnect(instance, *res_client);
  122. goto return_res;
  123. }
  124. memset((*res_client)->user_data, 0, sizeof(struct qnetd_ipc_user_data));
  125. prfd = PR_CreateSocketPollFd((*res_client)->socket);
  126. if (prfd == NULL) {
  127. qnetd_log_nss(LOG_CRIT, "Can't create NSPR poll fd for IPC client. Disconnecting client");
  128. qnetd_ipc_client_disconnect(instance, *res_client);
  129. res = -1;
  130. goto return_res;
  131. }
  132. ((struct qnetd_ipc_user_data *)(*res_client)->user_data)->nspr_poll_fd = prfd;
  133. return_res:
  134. return (res);
  135. }
  136. void
  137. qnetd_ipc_client_disconnect(struct qnetd_instance *instance, struct unix_socket_client *client)
  138. {
  139. if (PR_DestroySocketPollFd(
  140. ((struct qnetd_ipc_user_data *)(client)->user_data)->nspr_poll_fd) != PR_SUCCESS) {
  141. qnetd_log_nss(LOG_WARNING, "Unable to destroy client IPC poll socket fd");
  142. }
  143. free(client->user_data);
  144. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  145. }
  146. int
  147. qnetd_ipc_send_error(struct qnetd_instance *instance, struct unix_socket_client *client,
  148. const char *error_fmt, ...)
  149. {
  150. va_list ap;
  151. int res;
  152. va_start(ap, error_fmt);
  153. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  154. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  155. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  156. va_end(ap);
  157. if (res) {
  158. unix_socket_client_write_buffer(client, 1);
  159. } else {
  160. qnetd_log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  161. }
  162. return (res ? 0 : -1);
  163. }
  164. int
  165. qnetd_ipc_send_buffer(struct qnetd_instance *instance, struct unix_socket_client *client)
  166. {
  167. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  168. qnetd_log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  169. if (qnetd_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  170. return (-1);
  171. }
  172. return (0);
  173. }
  174. unix_socket_client_write_buffer(client, 1);
  175. return (0);
  176. }
  177. static void
  178. qnetd_ipc_parse_line(struct qnetd_instance *instance, struct unix_socket_client *client)
  179. {
  180. struct dynar_simple_lex lex;
  181. struct dynar *token;
  182. char *str;
  183. struct qnetd_ipc_user_data *ipc_user_data;
  184. int verbose;
  185. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  186. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  187. token = dynar_simple_lex_token_next(&lex);
  188. verbose = 0;
  189. if (token == NULL) {
  190. qnetd_log(LOG_ERR, "Can't alloc memory for simple lex");
  191. if (qnetd_ipc_send_error(instance, client, "Command too long") != 0) {
  192. client->schedule_disconnect = 1;
  193. }
  194. return;
  195. }
  196. str = dynar_data(token);
  197. if (strcasecmp(str, "") == 0) {
  198. qnetd_log(LOG_DEBUG, "IPC client doesn't send command");
  199. if (qnetd_ipc_send_error(instance, client, "No command specified") != 0) {
  200. client->schedule_disconnect = 1;
  201. }
  202. } else if (strcasecmp(str, "shutdown") == 0) {
  203. qnetd_log(LOG_DEBUG, "IPC client requested shutdown");
  204. ipc_user_data->shutdown_requested = 1;
  205. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  206. client->schedule_disconnect = 1;
  207. }
  208. } else if (strcasecmp(str, "status") == 0) {
  209. token = dynar_simple_lex_token_next(&lex);
  210. str = dynar_data(token);
  211. if (token != NULL && strcmp(str, "") != 0) {
  212. if (strcasecmp(str, "verbose") == 0) {
  213. verbose = 1;
  214. }
  215. }
  216. if (qnetd_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  217. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd status") != 0) {
  218. client->schedule_disconnect = 1;
  219. }
  220. } else {
  221. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  222. client->schedule_disconnect = 1;
  223. }
  224. }
  225. } else {
  226. qnetd_log(LOG_DEBUG, "IPC client sent unknown command");
  227. if (qnetd_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  228. client->schedule_disconnect = 1;
  229. }
  230. }
  231. dynar_simple_lex_destroy(&lex);
  232. }
  233. void
  234. qnetd_ipc_io_read(struct qnetd_instance *instance, struct unix_socket_client *client)
  235. {
  236. int res;
  237. res = unix_socket_client_io_read(client);
  238. switch (res) {
  239. case 0:
  240. /*
  241. * Partial read
  242. */
  243. break;
  244. case -1:
  245. qnetd_log(LOG_DEBUG, "IPC client closed connection");
  246. client->schedule_disconnect = 1;
  247. break;
  248. case -2:
  249. qnetd_log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  250. client->schedule_disconnect = 1;
  251. break;
  252. case -3:
  253. qnetd_log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  254. client->schedule_disconnect = 1;
  255. break;
  256. case 1:
  257. /*
  258. * Full message received
  259. */
  260. unix_socket_client_read_line(client, 0);
  261. qnetd_ipc_parse_line(instance, client);
  262. break;
  263. }
  264. }
  265. void
  266. qnetd_ipc_io_write(struct qnetd_instance *instance, struct unix_socket_client *client)
  267. {
  268. int res;
  269. struct qnetd_ipc_user_data *ipc_user_data;
  270. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  271. res = unix_socket_client_io_write(client);
  272. switch (res) {
  273. case 0:
  274. /*
  275. * Partial send
  276. */
  277. break;
  278. case -1:
  279. qnetd_log(LOG_DEBUG, "IPC client closed connection");
  280. client->schedule_disconnect = 1;
  281. break;
  282. case -2:
  283. qnetd_log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  284. client->schedule_disconnect = 1;
  285. break;
  286. case 1:
  287. /*
  288. * Full message sent
  289. */
  290. unix_socket_client_write_buffer(client, 0);
  291. client->schedule_disconnect = 1;
  292. if (ipc_user_data->shutdown_requested) {
  293. qnetd_ipc_close(instance);
  294. }
  295. break;
  296. }
  297. }