qnetd-ipc.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  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. char *cluster_name;
  186. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  187. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  188. token = dynar_simple_lex_token_next(&lex);
  189. verbose = 0;
  190. cluster_name = NULL;
  191. if (token == NULL) {
  192. goto exit_err_low_mem;
  193. }
  194. str = dynar_data(token);
  195. if (strcasecmp(str, "") == 0) {
  196. qnetd_log(LOG_DEBUG, "IPC client doesn't send command");
  197. if (qnetd_ipc_send_error(instance, client, "No command specified") != 0) {
  198. client->schedule_disconnect = 1;
  199. }
  200. } else if (strcasecmp(str, "shutdown") == 0) {
  201. qnetd_log(LOG_DEBUG, "IPC client requested shutdown");
  202. ipc_user_data->shutdown_requested = 1;
  203. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  204. client->schedule_disconnect = 1;
  205. }
  206. } else if (strcasecmp(str, "status") == 0) {
  207. token = dynar_simple_lex_token_next(&lex);
  208. if (token == NULL) {
  209. goto exit_err_low_mem;
  210. }
  211. str = dynar_data(token);
  212. if (token != NULL && strcmp(str, "") != 0) {
  213. if (strcasecmp(str, "verbose") == 0) {
  214. verbose = 1;
  215. }
  216. }
  217. if (qnetd_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  218. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd status") != 0) {
  219. client->schedule_disconnect = 1;
  220. }
  221. } else {
  222. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  223. client->schedule_disconnect = 1;
  224. }
  225. }
  226. } else if (strcasecmp(str, "list") == 0) {
  227. while (((token = dynar_simple_lex_token_next(&lex)) != NULL) &&
  228. (str = dynar_data(token), strcmp(str, "") != 0)) {
  229. if (strcasecmp(str, "verbose") == 0) {
  230. verbose = 1;
  231. } else if (strcasecmp(str, "cluster") == 0) {
  232. token = dynar_simple_lex_token_next(&lex);
  233. if (token == NULL) {
  234. goto exit_err_low_mem;
  235. }
  236. if ((cluster_name = strdup(dynar_data(token))) == NULL) {
  237. goto exit_err_low_mem;
  238. }
  239. } else {
  240. break;
  241. }
  242. }
  243. if (qnetd_ipc_cmd_list(instance, &client->send_buffer, verbose, cluster_name) != 0) {
  244. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd cluster list") != 0) {
  245. client->schedule_disconnect = 1;
  246. }
  247. } else {
  248. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  249. client->schedule_disconnect = 1;
  250. }
  251. }
  252. free(cluster_name); cluster_name = NULL;
  253. } else {
  254. qnetd_log(LOG_DEBUG, "IPC client sent unknown command");
  255. if (qnetd_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  256. client->schedule_disconnect = 1;
  257. }
  258. }
  259. dynar_simple_lex_destroy(&lex);
  260. return ;
  261. exit_err_low_mem:
  262. qnetd_log(LOG_ERR, "Can't alloc memory for simple lex");
  263. if (qnetd_ipc_send_error(instance, client, "Command too long") != 0) {
  264. client->schedule_disconnect = 1;
  265. }
  266. }
  267. void
  268. qnetd_ipc_io_read(struct qnetd_instance *instance, struct unix_socket_client *client)
  269. {
  270. int res;
  271. res = unix_socket_client_io_read(client);
  272. switch (res) {
  273. case 0:
  274. /*
  275. * Partial read
  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(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  284. client->schedule_disconnect = 1;
  285. break;
  286. case -3:
  287. qnetd_log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  288. client->schedule_disconnect = 1;
  289. break;
  290. case 1:
  291. /*
  292. * Full message received
  293. */
  294. unix_socket_client_read_line(client, 0);
  295. qnetd_ipc_parse_line(instance, client);
  296. break;
  297. }
  298. }
  299. void
  300. qnetd_ipc_io_write(struct qnetd_instance *instance, struct unix_socket_client *client)
  301. {
  302. int res;
  303. struct qnetd_ipc_user_data *ipc_user_data;
  304. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  305. res = unix_socket_client_io_write(client);
  306. switch (res) {
  307. case 0:
  308. /*
  309. * Partial send
  310. */
  311. break;
  312. case -1:
  313. qnetd_log(LOG_DEBUG, "IPC client closed connection");
  314. client->schedule_disconnect = 1;
  315. break;
  316. case -2:
  317. qnetd_log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  318. client->schedule_disconnect = 1;
  319. break;
  320. case 1:
  321. /*
  322. * Full message sent
  323. */
  324. unix_socket_client_write_buffer(client, 0);
  325. client->schedule_disconnect = 1;
  326. if (ipc_user_data->shutdown_requested) {
  327. qnetd_ipc_close(instance);
  328. }
  329. break;
  330. }
  331. }