qnetd-ipc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  1. /*
  2. * Copyright (c) 2015-2019 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 "log.h"
  35. #include "qnet-config.h"
  36. #include "qnetd-ipc.h"
  37. #include "qnetd-ipc-cmd.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,
  49. instance->advanced_settings->local_socket_file,
  50. instance->advanced_settings->local_socket_backlog,
  51. instance->advanced_settings->ipc_max_clients,
  52. instance->advanced_settings->ipc_max_receive_size,
  53. instance->advanced_settings->ipc_max_send_size) != 0) {
  54. log_err(LOG_ERR, "Can't create unix socket");
  55. return (-1);
  56. }
  57. if ((instance->ipc_socket_poll_fd = PR_CreateSocketPollFd(instance->local_ipc.socket)) == NULL) {
  58. log_nss(LOG_CRIT, "Can't create NSPR IPC socket poll fd");
  59. return (-1);
  60. }
  61. return (0);
  62. }
  63. int
  64. qnetd_ipc_close(struct qnetd_instance *instance)
  65. {
  66. int res;
  67. res = unix_socket_ipc_close(&instance->local_ipc);
  68. if (res != 0) {
  69. log_err(LOG_WARNING, "Can't close local IPC");
  70. }
  71. return (res);
  72. }
  73. int
  74. qnetd_ipc_is_closed(struct qnetd_instance *instance)
  75. {
  76. return (unix_socket_ipc_is_closed(&instance->local_ipc));
  77. }
  78. int
  79. qnetd_ipc_destroy(struct qnetd_instance *instance)
  80. {
  81. int res;
  82. struct unix_socket_client *client;
  83. const struct unix_socket_client_list *ipc_client_list;
  84. ipc_client_list = &instance->local_ipc.clients;
  85. TAILQ_FOREACH(client, ipc_client_list, entries) {
  86. free(client->user_data);
  87. }
  88. if (PR_DestroySocketPollFd(instance->ipc_socket_poll_fd) != PR_SUCCESS) {
  89. log_nss(LOG_WARNING, "Unable to destroy IPC poll socket fd");
  90. }
  91. res = unix_socket_ipc_destroy(&instance->local_ipc);
  92. if (res != 0) {
  93. log_err(LOG_WARNING, "Can't destroy local IPC");
  94. }
  95. return (res);
  96. }
  97. int
  98. qnetd_ipc_accept(struct qnetd_instance *instance, struct unix_socket_client **res_client)
  99. {
  100. int res;
  101. int accept_res;
  102. PRFileDesc *prfd;
  103. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  104. switch (accept_res) {
  105. case -1:
  106. log_err(LOG_ERR, "Can't accept local IPC connection");
  107. res = -1;
  108. goto return_res;
  109. break;
  110. case -2:
  111. log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  112. res = -1;
  113. goto return_res;
  114. break;
  115. case -3:
  116. log(LOG_ERR, "Can't add client to list");
  117. res = -1;
  118. goto return_res;
  119. break;
  120. default:
  121. unix_socket_client_read_line(*res_client, 1);
  122. res = 0;
  123. break;
  124. }
  125. (*res_client)->user_data = malloc(sizeof(struct qnetd_ipc_user_data));
  126. if ((*res_client)->user_data == NULL) {
  127. log(LOG_ERR, "Can't alloc IPC client user data");
  128. res = -1;
  129. qnetd_ipc_client_disconnect(instance, *res_client);
  130. goto return_res;
  131. }
  132. memset((*res_client)->user_data, 0, sizeof(struct qnetd_ipc_user_data));
  133. prfd = PR_CreateSocketPollFd((*res_client)->socket);
  134. if (prfd == NULL) {
  135. log_nss(LOG_CRIT, "Can't create NSPR poll fd for IPC client. Disconnecting client");
  136. qnetd_ipc_client_disconnect(instance, *res_client);
  137. res = -1;
  138. goto return_res;
  139. }
  140. ((struct qnetd_ipc_user_data *)(*res_client)->user_data)->nspr_poll_fd = prfd;
  141. return_res:
  142. return (res);
  143. }
  144. void
  145. qnetd_ipc_client_disconnect(struct qnetd_instance *instance, struct unix_socket_client *client)
  146. {
  147. if ((struct qnetd_ipc_user_data *)(client)->user_data != NULL &&
  148. PR_DestroySocketPollFd(
  149. ((struct qnetd_ipc_user_data *)(client)->user_data)->nspr_poll_fd) != PR_SUCCESS) {
  150. log_nss(LOG_WARNING, "Unable to destroy client IPC poll socket fd");
  151. }
  152. free(client->user_data);
  153. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  154. }
  155. int
  156. qnetd_ipc_send_error(struct qnetd_instance *instance, struct unix_socket_client *client,
  157. const char *error_fmt, ...)
  158. {
  159. va_list ap;
  160. int res;
  161. va_start(ap, error_fmt);
  162. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  163. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  164. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  165. va_end(ap);
  166. if (res) {
  167. unix_socket_client_write_buffer(client, 1);
  168. } else {
  169. log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  170. }
  171. return (res ? 0 : -1);
  172. }
  173. int
  174. qnetd_ipc_send_buffer(struct qnetd_instance *instance, struct unix_socket_client *client)
  175. {
  176. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  177. log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  178. if (qnetd_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  179. return (-1);
  180. }
  181. return (0);
  182. }
  183. unix_socket_client_write_buffer(client, 1);
  184. return (0);
  185. }
  186. static void
  187. qnetd_ipc_parse_line(struct qnetd_instance *instance, struct unix_socket_client *client)
  188. {
  189. struct dynar_simple_lex lex;
  190. struct dynar *token;
  191. char *str;
  192. struct qnetd_ipc_user_data *ipc_user_data;
  193. int verbose;
  194. char *cluster_name;
  195. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  196. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  197. token = dynar_simple_lex_token_next(&lex);
  198. verbose = 0;
  199. cluster_name = NULL;
  200. if (token == NULL) {
  201. goto exit_err_low_mem;
  202. }
  203. str = dynar_data(token);
  204. if (strcasecmp(str, "") == 0) {
  205. log(LOG_DEBUG, "IPC client doesn't send command");
  206. if (qnetd_ipc_send_error(instance, client, "No command specified") != 0) {
  207. client->schedule_disconnect = 1;
  208. }
  209. } else if (strcasecmp(str, "shutdown") == 0) {
  210. log(LOG_DEBUG, "IPC client requested shutdown");
  211. ipc_user_data->shutdown_requested = 1;
  212. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  213. client->schedule_disconnect = 1;
  214. }
  215. } else if (strcasecmp(str, "status") == 0) {
  216. token = dynar_simple_lex_token_next(&lex);
  217. if (token == NULL) {
  218. goto exit_err_low_mem;
  219. }
  220. str = dynar_data(token);
  221. if (token != NULL && strcmp(str, "") != 0) {
  222. if (strcasecmp(str, "verbose") == 0) {
  223. verbose = 1;
  224. }
  225. }
  226. if (qnetd_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  227. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd status") != 0) {
  228. client->schedule_disconnect = 1;
  229. }
  230. } else {
  231. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  232. client->schedule_disconnect = 1;
  233. }
  234. }
  235. } else if (strcasecmp(str, "list") == 0) {
  236. while (((token = dynar_simple_lex_token_next(&lex)) != NULL) &&
  237. (str = dynar_data(token), strcmp(str, "") != 0)) {
  238. if (strcasecmp(str, "verbose") == 0) {
  239. verbose = 1;
  240. } else if (strcasecmp(str, "cluster") == 0) {
  241. token = dynar_simple_lex_token_next(&lex);
  242. if (token == NULL) {
  243. goto exit_err_low_mem;
  244. }
  245. free(cluster_name); cluster_name = NULL;
  246. if ((cluster_name = strdup(dynar_data(token))) == NULL) {
  247. goto exit_err_low_mem;
  248. }
  249. } else {
  250. break;
  251. }
  252. }
  253. if (qnetd_ipc_cmd_list(instance, &client->send_buffer, verbose, cluster_name) != 0) {
  254. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd cluster list") != 0) {
  255. client->schedule_disconnect = 1;
  256. }
  257. } else {
  258. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  259. client->schedule_disconnect = 1;
  260. }
  261. }
  262. free(cluster_name); cluster_name = NULL;
  263. } else {
  264. log(LOG_DEBUG, "IPC client sent unknown command");
  265. if (qnetd_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  266. client->schedule_disconnect = 1;
  267. }
  268. }
  269. dynar_simple_lex_destroy(&lex);
  270. return ;
  271. exit_err_low_mem:
  272. free(cluster_name); cluster_name = NULL;
  273. log(LOG_ERR, "Can't alloc memory for simple lex");
  274. if (qnetd_ipc_send_error(instance, client, "Command too long") != 0) {
  275. client->schedule_disconnect = 1;
  276. }
  277. }
  278. void
  279. qnetd_ipc_io_read(struct qnetd_instance *instance, struct unix_socket_client *client)
  280. {
  281. int res;
  282. res = unix_socket_client_io_read(client);
  283. switch (res) {
  284. case 0:
  285. /*
  286. * Partial read
  287. */
  288. break;
  289. case -1:
  290. log(LOG_DEBUG, "IPC client closed connection");
  291. client->schedule_disconnect = 1;
  292. break;
  293. case -2:
  294. log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  295. client->schedule_disconnect = 1;
  296. break;
  297. case -3:
  298. log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  299. client->schedule_disconnect = 1;
  300. break;
  301. case 1:
  302. /*
  303. * Full message received
  304. */
  305. unix_socket_client_read_line(client, 0);
  306. qnetd_ipc_parse_line(instance, client);
  307. break;
  308. }
  309. }
  310. void
  311. qnetd_ipc_io_write(struct qnetd_instance *instance, struct unix_socket_client *client)
  312. {
  313. int res;
  314. struct qnetd_ipc_user_data *ipc_user_data;
  315. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  316. res = unix_socket_client_io_write(client);
  317. switch (res) {
  318. case 0:
  319. /*
  320. * Partial send
  321. */
  322. break;
  323. case -1:
  324. log(LOG_DEBUG, "IPC client closed connection");
  325. client->schedule_disconnect = 1;
  326. break;
  327. case -2:
  328. log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  329. client->schedule_disconnect = 1;
  330. break;
  331. case 1:
  332. /*
  333. * Full message sent
  334. */
  335. unix_socket_client_write_buffer(client, 0);
  336. client->schedule_disconnect = 1;
  337. if (ipc_user_data->shutdown_requested) {
  338. qnetd_ipc_close(instance);
  339. }
  340. break;
  341. }
  342. }