qnetd-ipc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412
  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,
  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. qnetd_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. qnetd_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. qnetd_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. qnetd_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. qnetd_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. qnetd_log_err(LOG_ERR, "Can't accept local IPC connection");
  107. res = -1;
  108. goto return_res;
  109. break;
  110. case -2:
  111. qnetd_log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  112. res = -1;
  113. goto return_res;
  114. break;
  115. case -3:
  116. qnetd_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. qnetd_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. qnetd_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 (PR_DestroySocketPollFd(
  148. ((struct qnetd_ipc_user_data *)(client)->user_data)->nspr_poll_fd) != PR_SUCCESS) {
  149. qnetd_log_nss(LOG_WARNING, "Unable to destroy client IPC poll socket fd");
  150. }
  151. free(client->user_data);
  152. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  153. }
  154. int
  155. qnetd_ipc_send_error(struct qnetd_instance *instance, struct unix_socket_client *client,
  156. const char *error_fmt, ...)
  157. {
  158. va_list ap;
  159. int res;
  160. va_start(ap, error_fmt);
  161. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  162. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  163. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  164. va_end(ap);
  165. if (res) {
  166. unix_socket_client_write_buffer(client, 1);
  167. } else {
  168. qnetd_log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  169. }
  170. return (res ? 0 : -1);
  171. }
  172. int
  173. qnetd_ipc_send_buffer(struct qnetd_instance *instance, struct unix_socket_client *client)
  174. {
  175. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  176. qnetd_log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  177. if (qnetd_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  178. return (-1);
  179. }
  180. return (0);
  181. }
  182. unix_socket_client_write_buffer(client, 1);
  183. return (0);
  184. }
  185. static void
  186. qnetd_ipc_parse_line(struct qnetd_instance *instance, struct unix_socket_client *client)
  187. {
  188. struct dynar_simple_lex lex;
  189. struct dynar *token;
  190. char *str;
  191. struct qnetd_ipc_user_data *ipc_user_data;
  192. int verbose;
  193. char *cluster_name;
  194. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  195. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  196. token = dynar_simple_lex_token_next(&lex);
  197. verbose = 0;
  198. cluster_name = NULL;
  199. if (token == NULL) {
  200. goto exit_err_low_mem;
  201. }
  202. str = dynar_data(token);
  203. if (strcasecmp(str, "") == 0) {
  204. qnetd_log(LOG_DEBUG, "IPC client doesn't send command");
  205. if (qnetd_ipc_send_error(instance, client, "No command specified") != 0) {
  206. client->schedule_disconnect = 1;
  207. }
  208. } else if (strcasecmp(str, "shutdown") == 0) {
  209. qnetd_log(LOG_DEBUG, "IPC client requested shutdown");
  210. ipc_user_data->shutdown_requested = 1;
  211. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  212. client->schedule_disconnect = 1;
  213. }
  214. } else if (strcasecmp(str, "status") == 0) {
  215. token = dynar_simple_lex_token_next(&lex);
  216. if (token == NULL) {
  217. goto exit_err_low_mem;
  218. }
  219. str = dynar_data(token);
  220. if (token != NULL && strcmp(str, "") != 0) {
  221. if (strcasecmp(str, "verbose") == 0) {
  222. verbose = 1;
  223. }
  224. }
  225. if (qnetd_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  226. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd status") != 0) {
  227. client->schedule_disconnect = 1;
  228. }
  229. } else {
  230. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  231. client->schedule_disconnect = 1;
  232. }
  233. }
  234. } else if (strcasecmp(str, "list") == 0) {
  235. while (((token = dynar_simple_lex_token_next(&lex)) != NULL) &&
  236. (str = dynar_data(token), strcmp(str, "") != 0)) {
  237. if (strcasecmp(str, "verbose") == 0) {
  238. verbose = 1;
  239. } else if (strcasecmp(str, "cluster") == 0) {
  240. token = dynar_simple_lex_token_next(&lex);
  241. if (token == NULL) {
  242. goto exit_err_low_mem;
  243. }
  244. free(cluster_name); cluster_name = NULL;
  245. if ((cluster_name = strdup(dynar_data(token))) == NULL) {
  246. goto exit_err_low_mem;
  247. }
  248. } else {
  249. break;
  250. }
  251. }
  252. if (qnetd_ipc_cmd_list(instance, &client->send_buffer, verbose, cluster_name) != 0) {
  253. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd cluster list") != 0) {
  254. client->schedule_disconnect = 1;
  255. }
  256. } else {
  257. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  258. client->schedule_disconnect = 1;
  259. }
  260. }
  261. free(cluster_name); cluster_name = NULL;
  262. } else {
  263. qnetd_log(LOG_DEBUG, "IPC client sent unknown command");
  264. if (qnetd_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  265. client->schedule_disconnect = 1;
  266. }
  267. }
  268. dynar_simple_lex_destroy(&lex);
  269. return ;
  270. exit_err_low_mem:
  271. free(cluster_name); cluster_name = NULL;
  272. qnetd_log(LOG_ERR, "Can't alloc memory for simple lex");
  273. if (qnetd_ipc_send_error(instance, client, "Command too long") != 0) {
  274. client->schedule_disconnect = 1;
  275. }
  276. }
  277. void
  278. qnetd_ipc_io_read(struct qnetd_instance *instance, struct unix_socket_client *client)
  279. {
  280. int res;
  281. res = unix_socket_client_io_read(client);
  282. switch (res) {
  283. case 0:
  284. /*
  285. * Partial read
  286. */
  287. break;
  288. case -1:
  289. qnetd_log(LOG_DEBUG, "IPC client closed connection");
  290. client->schedule_disconnect = 1;
  291. break;
  292. case -2:
  293. qnetd_log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  294. client->schedule_disconnect = 1;
  295. break;
  296. case -3:
  297. qnetd_log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  298. client->schedule_disconnect = 1;
  299. break;
  300. case 1:
  301. /*
  302. * Full message received
  303. */
  304. unix_socket_client_read_line(client, 0);
  305. qnetd_ipc_parse_line(instance, client);
  306. break;
  307. }
  308. }
  309. void
  310. qnetd_ipc_io_write(struct qnetd_instance *instance, struct unix_socket_client *client)
  311. {
  312. int res;
  313. struct qnetd_ipc_user_data *ipc_user_data;
  314. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  315. res = unix_socket_client_io_write(client);
  316. switch (res) {
  317. case 0:
  318. /*
  319. * Partial send
  320. */
  321. break;
  322. case -1:
  323. qnetd_log(LOG_DEBUG, "IPC client closed connection");
  324. client->schedule_disconnect = 1;
  325. break;
  326. case -2:
  327. qnetd_log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  328. client->schedule_disconnect = 1;
  329. break;
  330. case 1:
  331. /*
  332. * Full message sent
  333. */
  334. unix_socket_client_write_buffer(client, 0);
  335. client->schedule_disconnect = 1;
  336. if (ipc_user_data->shutdown_requested) {
  337. qnetd_ipc_close(instance);
  338. }
  339. break;
  340. }
  341. }