qnetd-ipc.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  1. /*
  2. * Copyright (c) 2015-2020 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. /*
  46. * Callbacks
  47. */
  48. /*
  49. * IPC server socket
  50. */
  51. static int
  52. ipc_socket_poll_loop_set_events_cb(int fd, short *events, void *user_data1, void *user_data2)
  53. {
  54. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  55. if (qnetd_ipc_is_closed(instance)) {
  56. log(LOG_DEBUG, "Listening socket is closed");
  57. return (-2);
  58. }
  59. return (0);
  60. }
  61. static int
  62. ipc_socket_poll_loop_read_cb(int fd, void *user_data1, void *user_data2)
  63. {
  64. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  65. struct unix_socket_client *ipc_client;
  66. qnetd_ipc_accept(instance, &ipc_client);
  67. return (0);
  68. }
  69. static int
  70. ipc_socket_poll_loop_write_cb(int fd, void *user_data1, void *user_data2)
  71. {
  72. log(LOG_CRIT, "POLL_WRITE on listening IPC socket");
  73. return (-1);
  74. }
  75. static int
  76. ipc_socket_poll_loop_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  77. {
  78. if (revents != POLLNVAL) {
  79. /*
  80. * Poll ERR on listening socket is fatal error.
  81. * POLL_NVAL is used as a signal to quit poll loop.
  82. */
  83. log(LOG_CRIT, "POLL_ERR (%u) on listening socket", revents);
  84. } else {
  85. log(LOG_DEBUG, "Listening socket is closed");
  86. }
  87. return (-1);
  88. }
  89. /*
  90. * IPC client sockets
  91. */
  92. static int
  93. ipc_client_socket_poll_loop_set_events_cb(int fd, short *events, void *user_data1, void *user_data2)
  94. {
  95. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  96. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  97. if (ipc_client->schedule_disconnect) {
  98. qnetd_ipc_client_disconnect(instance, ipc_client);
  99. if (pr_poll_loop_del_fd(&instance->main_poll_loop, fd) == -1) {
  100. log(LOG_CRIT, "pr_poll_loop_del_fd for ipc client socket failed");
  101. return (-2);
  102. }
  103. return (-1);
  104. }
  105. if (!ipc_client->reading_line && !ipc_client->writing_buffer) {
  106. return (-1);
  107. }
  108. if (ipc_client->reading_line) {
  109. *events |= POLLIN;
  110. }
  111. if (ipc_client->writing_buffer) {
  112. *events |= POLLOUT;
  113. }
  114. return (0);
  115. }
  116. static int
  117. ipc_client_socket_poll_loop_read_cb(int fd, void *user_data1, void *user_data2)
  118. {
  119. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  120. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  121. if (!ipc_client->schedule_disconnect) {
  122. qnetd_ipc_io_read(instance, ipc_client);
  123. }
  124. return (0);
  125. }
  126. static int
  127. ipc_client_socket_poll_loop_write_cb(int fd, void *user_data1, void *user_data2)
  128. {
  129. struct qnetd_instance *instance = (struct qnetd_instance *)user_data1;
  130. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  131. if (!ipc_client->schedule_disconnect) {
  132. qnetd_ipc_io_write(instance, ipc_client);
  133. }
  134. return (0);
  135. }
  136. static int
  137. ipc_client_socket_poll_loop_err_cb(int fd, short revents, void *user_data1, void *user_data2)
  138. {
  139. struct unix_socket_client *ipc_client = (struct unix_socket_client *)user_data2;
  140. if (!ipc_client->schedule_disconnect) {
  141. log(LOG_DEBUG, "POLL_ERR (%u) on ipc client socket."
  142. " Disconnecting.", revents);
  143. ipc_client->schedule_disconnect = 1;
  144. }
  145. return (0);
  146. }
  147. /*
  148. * Exported functions
  149. */
  150. int
  151. qnetd_ipc_init(struct qnetd_instance *instance)
  152. {
  153. if (unix_socket_ipc_init(&instance->local_ipc,
  154. instance->advanced_settings->local_socket_file,
  155. instance->advanced_settings->local_socket_backlog,
  156. instance->advanced_settings->ipc_max_clients,
  157. instance->advanced_settings->ipc_max_receive_size,
  158. instance->advanced_settings->ipc_max_send_size) != 0) {
  159. log_err(LOG_ERR, "Can't create unix socket");
  160. return (-1);
  161. }
  162. if (pr_poll_loop_add_fd(&instance->main_poll_loop, instance->local_ipc.socket, POLLIN,
  163. ipc_socket_poll_loop_set_events_cb,
  164. ipc_socket_poll_loop_read_cb,
  165. ipc_socket_poll_loop_write_cb,
  166. ipc_socket_poll_loop_err_cb, instance, NULL) == -1) {
  167. log_err(LOG_CRIT, "Can't add IPC socket to main poll loop");
  168. return (-1);
  169. }
  170. return (0);
  171. }
  172. int
  173. qnetd_ipc_close(struct qnetd_instance *instance)
  174. {
  175. int res;
  176. res = unix_socket_ipc_close(&instance->local_ipc);
  177. if (res != 0) {
  178. log_err(LOG_WARNING, "Can't close local IPC");
  179. }
  180. return (res);
  181. }
  182. int
  183. qnetd_ipc_is_closed(struct qnetd_instance *instance)
  184. {
  185. return (unix_socket_ipc_is_closed(&instance->local_ipc));
  186. }
  187. int
  188. qnetd_ipc_destroy(struct qnetd_instance *instance)
  189. {
  190. int res;
  191. struct unix_socket_client *client;
  192. const struct unix_socket_client_list *ipc_client_list;
  193. ipc_client_list = &instance->local_ipc.clients;
  194. TAILQ_FOREACH(client, ipc_client_list, entries) {
  195. free(client->user_data);
  196. }
  197. res = unix_socket_ipc_destroy(&instance->local_ipc);
  198. if (res != 0) {
  199. log_err(LOG_WARNING, "Can't destroy local IPC");
  200. }
  201. return (res);
  202. }
  203. int
  204. qnetd_ipc_accept(struct qnetd_instance *instance, struct unix_socket_client **res_client)
  205. {
  206. int res;
  207. int accept_res;
  208. accept_res = unix_socket_ipc_accept(&instance->local_ipc, res_client);
  209. switch (accept_res) {
  210. case -1:
  211. log_err(LOG_ERR, "Can't accept local IPC connection");
  212. res = -1;
  213. goto return_res;
  214. break;
  215. case -2:
  216. log(LOG_ERR, "Maximum IPC clients reached. Not accepting connection");
  217. res = -1;
  218. goto return_res;
  219. break;
  220. case -3:
  221. log(LOG_ERR, "Can't add client to list");
  222. res = -1;
  223. goto return_res;
  224. break;
  225. default:
  226. unix_socket_client_read_line(*res_client, 1);
  227. res = 0;
  228. break;
  229. }
  230. (*res_client)->user_data = malloc(sizeof(struct qnetd_ipc_user_data));
  231. if ((*res_client)->user_data == NULL) {
  232. log(LOG_ERR, "Can't alloc IPC client user data");
  233. res = -1;
  234. qnetd_ipc_client_disconnect(instance, *res_client);
  235. goto return_res;
  236. }
  237. memset((*res_client)->user_data, 0, sizeof(struct qnetd_ipc_user_data));
  238. if (pr_poll_loop_add_fd(&instance->main_poll_loop, (*res_client)->socket, 0,
  239. ipc_client_socket_poll_loop_set_events_cb,
  240. ipc_client_socket_poll_loop_read_cb,
  241. ipc_client_socket_poll_loop_write_cb,
  242. ipc_client_socket_poll_loop_err_cb, instance, *res_client) == -1) {
  243. log_err(LOG_CRIT, "Can't add IPC client socket to main poll loop");
  244. res = -1;
  245. qnetd_ipc_client_disconnect(instance, *res_client);
  246. goto return_res;
  247. }
  248. return_res:
  249. return (res);
  250. }
  251. void
  252. qnetd_ipc_client_disconnect(struct qnetd_instance *instance, struct unix_socket_client *client)
  253. {
  254. free(client->user_data);
  255. unix_socket_ipc_client_disconnect(&instance->local_ipc, client);
  256. }
  257. int
  258. qnetd_ipc_send_error(struct qnetd_instance *instance, struct unix_socket_client *client,
  259. const char *error_fmt, ...)
  260. {
  261. va_list ap;
  262. int res;
  263. va_start(ap, error_fmt);
  264. res = ((dynar_str_cpy(&client->send_buffer, "Error\n") == 0) &&
  265. (dynar_str_vcatf(&client->send_buffer, error_fmt, ap) > 0) &&
  266. (dynar_str_cat(&client->send_buffer, "\n") == 0));
  267. va_end(ap);
  268. if (res) {
  269. unix_socket_client_write_buffer(client, 1);
  270. } else {
  271. log(LOG_ERR, "Can't send ipc error to client (buffer too small)");
  272. }
  273. return (res ? 0 : -1);
  274. }
  275. int
  276. qnetd_ipc_send_buffer(struct qnetd_instance *instance, struct unix_socket_client *client)
  277. {
  278. if (dynar_str_prepend(&client->send_buffer, "OK\n") != 0) {
  279. log(LOG_ERR, "Can't send ipc message to client (buffer too small)");
  280. if (qnetd_ipc_send_error(instance, client, "Internal IPC buffer too small") != 0) {
  281. return (-1);
  282. }
  283. return (0);
  284. }
  285. unix_socket_client_write_buffer(client, 1);
  286. return (0);
  287. }
  288. static void
  289. qnetd_ipc_parse_line(struct qnetd_instance *instance, struct unix_socket_client *client)
  290. {
  291. struct dynar_simple_lex lex;
  292. struct dynar *token;
  293. char *str;
  294. struct qnetd_ipc_user_data *ipc_user_data;
  295. int verbose;
  296. char *cluster_name;
  297. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  298. dynar_simple_lex_init(&lex, &client->receive_buffer, DYNAR_SIMPLE_LEX_TYPE_QUOTE);
  299. token = dynar_simple_lex_token_next(&lex);
  300. verbose = 0;
  301. cluster_name = NULL;
  302. if (token == NULL) {
  303. goto exit_err_low_mem;
  304. }
  305. str = dynar_data(token);
  306. if (strcasecmp(str, "") == 0) {
  307. log(LOG_DEBUG, "IPC client doesn't send command");
  308. if (qnetd_ipc_send_error(instance, client, "No command specified") != 0) {
  309. client->schedule_disconnect = 1;
  310. }
  311. } else if (strcasecmp(str, "shutdown") == 0) {
  312. log(LOG_DEBUG, "IPC client requested shutdown");
  313. ipc_user_data->shutdown_requested = 1;
  314. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  315. client->schedule_disconnect = 1;
  316. }
  317. } else if (strcasecmp(str, "status") == 0) {
  318. token = dynar_simple_lex_token_next(&lex);
  319. if (token == NULL) {
  320. goto exit_err_low_mem;
  321. }
  322. str = dynar_data(token);
  323. if (token != NULL && strcmp(str, "") != 0) {
  324. if (strcasecmp(str, "verbose") == 0) {
  325. verbose = 1;
  326. }
  327. }
  328. if (qnetd_ipc_cmd_status(instance, &client->send_buffer, verbose) != 0) {
  329. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd status") != 0) {
  330. client->schedule_disconnect = 1;
  331. }
  332. } else {
  333. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  334. client->schedule_disconnect = 1;
  335. }
  336. }
  337. } else if (strcasecmp(str, "list") == 0) {
  338. while (((token = dynar_simple_lex_token_next(&lex)) != NULL) &&
  339. (str = dynar_data(token), strcmp(str, "") != 0)) {
  340. if (strcasecmp(str, "verbose") == 0) {
  341. verbose = 1;
  342. } else if (strcasecmp(str, "cluster") == 0) {
  343. token = dynar_simple_lex_token_next(&lex);
  344. if (token == NULL) {
  345. goto exit_err_low_mem;
  346. }
  347. free(cluster_name); cluster_name = NULL;
  348. if ((cluster_name = strdup(dynar_data(token))) == NULL) {
  349. goto exit_err_low_mem;
  350. }
  351. } else {
  352. break;
  353. }
  354. }
  355. if (qnetd_ipc_cmd_list(instance, &client->send_buffer, verbose, cluster_name) != 0) {
  356. if (qnetd_ipc_send_error(instance, client, "Can't get QNetd cluster list") != 0) {
  357. client->schedule_disconnect = 1;
  358. }
  359. } else {
  360. if (qnetd_ipc_send_buffer(instance, client) != 0) {
  361. client->schedule_disconnect = 1;
  362. }
  363. }
  364. free(cluster_name); cluster_name = NULL;
  365. } else {
  366. log(LOG_DEBUG, "IPC client sent unknown command");
  367. if (qnetd_ipc_send_error(instance, client, "Unknown command '%s'", str) != 0) {
  368. client->schedule_disconnect = 1;
  369. }
  370. }
  371. dynar_simple_lex_destroy(&lex);
  372. return ;
  373. exit_err_low_mem:
  374. free(cluster_name); cluster_name = NULL;
  375. log(LOG_ERR, "Can't alloc memory for simple lex");
  376. if (qnetd_ipc_send_error(instance, client, "Command too long") != 0) {
  377. client->schedule_disconnect = 1;
  378. }
  379. }
  380. void
  381. qnetd_ipc_io_read(struct qnetd_instance *instance, struct unix_socket_client *client)
  382. {
  383. int res;
  384. res = unix_socket_client_io_read(client);
  385. switch (res) {
  386. case 0:
  387. /*
  388. * Partial read
  389. */
  390. break;
  391. case -1:
  392. log(LOG_DEBUG, "IPC client closed connection");
  393. client->schedule_disconnect = 1;
  394. break;
  395. case -2:
  396. log(LOG_ERR, "Can't store message from IPC client. Disconnecting client.");
  397. client->schedule_disconnect = 1;
  398. break;
  399. case -3:
  400. log_err(LOG_ERR, "Can't receive message from IPC client. Disconnecting client.");
  401. client->schedule_disconnect = 1;
  402. break;
  403. case 1:
  404. /*
  405. * Full message received
  406. */
  407. unix_socket_client_read_line(client, 0);
  408. qnetd_ipc_parse_line(instance, client);
  409. break;
  410. }
  411. }
  412. void
  413. qnetd_ipc_io_write(struct qnetd_instance *instance, struct unix_socket_client *client)
  414. {
  415. int res;
  416. struct qnetd_ipc_user_data *ipc_user_data;
  417. ipc_user_data = (struct qnetd_ipc_user_data *)client->user_data;
  418. res = unix_socket_client_io_write(client);
  419. switch (res) {
  420. case 0:
  421. /*
  422. * Partial send
  423. */
  424. break;
  425. case -1:
  426. log(LOG_DEBUG, "IPC client closed connection");
  427. client->schedule_disconnect = 1;
  428. break;
  429. case -2:
  430. log_err(LOG_ERR, "Can't send message to IPC client. Disconnecting client");
  431. client->schedule_disconnect = 1;
  432. break;
  433. case 1:
  434. /*
  435. * Full message sent
  436. */
  437. unix_socket_client_write_buffer(client, 0);
  438. client->schedule_disconnect = 1;
  439. if (ipc_user_data->shutdown_requested) {
  440. qnetd_ipc_close(instance);
  441. }
  442. break;
  443. }
  444. }