qnetd-ipc.c 13 KB

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