corosync-qnetd-tool.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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 <err.h>
  35. #include <errno.h>
  36. #include <getopt.h>
  37. #include <stdio.h>
  38. #include <stdlib.h>
  39. #include <string.h>
  40. #include "qnet-config.h"
  41. #include "dynar.h"
  42. #include "dynar-str.h"
  43. #include "utils.h"
  44. #include "unix-socket.h"
  45. #define IPC_READ_BUF_SIZE 512
  46. enum qnetd_tool_operation {
  47. QNETD_TOOL_OPERATION_NONE,
  48. QNETD_TOOL_OPERATION_SHUTDOWN,
  49. QNETD_TOOL_OPERATION_STATUS,
  50. QNETD_TOOL_OPERATION_LIST,
  51. };
  52. enum qnetd_tool_exit_code {
  53. QNETD_TOOL_EXIT_CODE_NO_ERROR = 0,
  54. QNETD_TOOL_EXIT_CODE_USAGE = 1,
  55. QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR = 2,
  56. QNETD_TOOL_EXIT_CODE_SOCKET_CONNECT = 3,
  57. QNETD_TOOL_EXIT_CODE_QNETD_RETURNED_ERROR = 4,
  58. };
  59. static void
  60. usage(void)
  61. {
  62. printf("usage: %s [-Hhlsv] [-c cluster_name] [-p qnetd_ipc_socket_path]\n",
  63. QNETD_TOOL_PROGRAM_NAME);
  64. }
  65. static void
  66. cli_parse(int argc, char * const argv[], enum qnetd_tool_operation *operation,
  67. int *verbose, char **cluster_name, char **socket_path)
  68. {
  69. int ch;
  70. *operation = QNETD_TOOL_OPERATION_NONE;
  71. *verbose = 0;
  72. *cluster_name = NULL;
  73. *socket_path = strdup(QNETD_DEFAULT_LOCAL_SOCKET_FILE);
  74. if (*socket_path == NULL) {
  75. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR,
  76. "Can't alloc memory for socket path string");
  77. }
  78. while ((ch = getopt(argc, argv, "Hhlsvc:p:")) != -1) {
  79. switch (ch) {
  80. case 'H':
  81. *operation = QNETD_TOOL_OPERATION_SHUTDOWN;
  82. break;
  83. case 'l':
  84. *operation = QNETD_TOOL_OPERATION_LIST;
  85. break;
  86. case 's':
  87. *operation = QNETD_TOOL_OPERATION_STATUS;
  88. break;
  89. case 'v':
  90. *verbose = 1;
  91. break;
  92. case 'c':
  93. free(*cluster_name);
  94. *cluster_name = strdup(optarg);
  95. if (*cluster_name == NULL) {
  96. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR,
  97. "Can't alloc memory for cluster name string");
  98. }
  99. break;
  100. case 'p':
  101. free(*socket_path);
  102. *socket_path = strdup(optarg);
  103. if (*socket_path == NULL) {
  104. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR,
  105. "Can't alloc memory for socket path string");
  106. }
  107. break;
  108. case 'h':
  109. case '?':
  110. usage();
  111. exit(QNETD_TOOL_EXIT_CODE_USAGE);
  112. break;
  113. }
  114. }
  115. if (*operation == QNETD_TOOL_OPERATION_NONE) {
  116. usage();
  117. exit(QNETD_TOOL_EXIT_CODE_USAGE);
  118. }
  119. }
  120. static int
  121. store_command(struct dynar *str, enum qnetd_tool_operation operation, int verbose,
  122. const char *cluster_name)
  123. {
  124. const char *nline = "\n\0";
  125. const int nline_len = 2;
  126. switch (operation) {
  127. case QNETD_TOOL_OPERATION_NONE:
  128. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR, "Unhandled operation none");
  129. break;
  130. case QNETD_TOOL_OPERATION_SHUTDOWN:
  131. if (dynar_str_cat(str, "shutdown ") != 0) {
  132. return (-1);
  133. }
  134. break;
  135. case QNETD_TOOL_OPERATION_STATUS:
  136. if (dynar_str_cat(str, "status ") != 0) {
  137. return (-1);
  138. }
  139. break;
  140. case QNETD_TOOL_OPERATION_LIST:
  141. if (dynar_str_cat(str, "list ") != 0) {
  142. return (-1);
  143. }
  144. break;
  145. }
  146. if (verbose) {
  147. if (dynar_str_cat(str, "verbose ") != 0) {
  148. return (-1);
  149. }
  150. }
  151. if (cluster_name != NULL) {
  152. if (dynar_str_cat(str, "cluster ") != 0 ||
  153. dynar_str_quote_cat(str, cluster_name) != 0 ||
  154. dynar_str_cat(str, " ") != 0) {
  155. return (-1);
  156. }
  157. }
  158. if (dynar_cat(str, nline, nline_len) != 0) {
  159. return (-1);
  160. }
  161. return (0);
  162. }
  163. /*
  164. * -1 - Internal error (can't alloc memory)
  165. * 0 - No error
  166. * 1 - IPC returned error
  167. * 2 - Unknown status line
  168. */
  169. static int
  170. read_ipc_reply(FILE *f)
  171. {
  172. struct dynar read_str;
  173. int ch;
  174. int status_readed;
  175. int res;
  176. static const char *ok_str = "OK";
  177. static const char *err_str = "Error";
  178. int err_set;
  179. char c;
  180. dynar_init(&read_str, IPC_READ_BUF_SIZE);
  181. status_readed = 0;
  182. err_set = 0;
  183. res = 0;
  184. while ((ch = fgetc(f)) != EOF) {
  185. if (status_readed) {
  186. putc(ch, (err_set ? stderr : stdout));
  187. } else {
  188. if (ch == '\r') {
  189. } else if (ch == '\n') {
  190. status_readed = 1;
  191. c = '\0';
  192. if (dynar_cat(&read_str, &c, sizeof(c)) != 0) {
  193. res = -1;
  194. goto exit_destroy;
  195. }
  196. if (strcasecmp(dynar_data(&read_str), ok_str) == 0) {
  197. } else if (strcasecmp(dynar_data(&read_str), err_str) == 0) {
  198. err_set = 1;
  199. res = 1;
  200. fprintf(stderr, "Error: ");
  201. } else {
  202. res = 2;
  203. goto exit_destroy;
  204. }
  205. } else {
  206. c = ch;
  207. if (dynar_cat(&read_str, &c, sizeof(c)) != 0) {
  208. res = -1;
  209. goto exit_destroy;
  210. }
  211. }
  212. }
  213. }
  214. exit_destroy:
  215. dynar_destroy(&read_str);
  216. return (res);
  217. }
  218. int
  219. main(int argc, char * const argv[])
  220. {
  221. enum qnetd_tool_operation operation;
  222. int verbose;
  223. char *cluster_name;
  224. char *socket_path;
  225. int sock_fd;
  226. FILE *sock;
  227. struct dynar send_str;
  228. int res;
  229. int exit_code;
  230. exit_code = QNETD_TOOL_EXIT_CODE_NO_ERROR;
  231. cli_parse(argc, argv, &operation, &verbose, &cluster_name, &socket_path);
  232. dynar_init(&send_str, QNETD_DEFAULT_IPC_MAX_RECEIVE_SIZE);
  233. sock_fd = unix_socket_client_create(socket_path, 0);
  234. if (sock_fd == -1) {
  235. err(QNETD_TOOL_EXIT_CODE_SOCKET_CONNECT,
  236. "Can't connect to qnetd socket (is QNetd running?)");
  237. }
  238. sock = fdopen(sock_fd, "w+t");
  239. if (sock == NULL) {
  240. err(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR, "Can't open QNetd socket fd");
  241. }
  242. if (store_command(&send_str, operation, verbose, cluster_name) != 0) {
  243. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR, "Can't store command");
  244. }
  245. res = fprintf(sock, "%s", dynar_data(&send_str));
  246. if (res < 0 || (size_t)res != strlen(dynar_data(&send_str)) ||
  247. fflush(sock) != 0) {
  248. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR, "Can't send command");
  249. }
  250. res = read_ipc_reply(sock);
  251. switch (res) {
  252. case -1:
  253. errx(QNETD_TOOL_EXIT_CODE_INTERNAL_ERROR, "Internal error during IPC status line read");
  254. break;
  255. case 0:
  256. break;
  257. case 1:
  258. exit_code = QNETD_TOOL_EXIT_CODE_QNETD_RETURNED_ERROR;
  259. break;
  260. case 2:
  261. errx(QNETD_TOOL_EXIT_CODE_SOCKET_CONNECT, "Unknown status line returned by IPC server");
  262. break;
  263. }
  264. if (fclose(sock) != 0) {
  265. warn("Can't close QNetd socket");
  266. }
  267. free(cluster_name);
  268. free(socket_path);
  269. dynar_destroy(&send_str);
  270. return (exit_code);
  271. }