corosync-qdevice-tool.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  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 "qdevice-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 qdevice_tool_operation {
  47. QDEVICE_TOOL_OPERATION_NONE,
  48. QDEVICE_TOOL_OPERATION_SHUTDOWN,
  49. QDEVICE_TOOL_OPERATION_STATUS,
  50. };
  51. enum qdevice_tool_exit_code {
  52. QDEVICE_TOOL_EXIT_CODE_NO_ERROR = 0,
  53. QDEVICE_TOOL_EXIT_CODE_USAGE = 1,
  54. QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR = 2,
  55. QDEVICE_TOOL_EXIT_CODE_SOCKET_CONNECT = 3,
  56. QDEVICE_TOOL_EXIT_CODE_QDEVICE_RETURNED_ERROR = 4,
  57. };
  58. static void
  59. usage(void)
  60. {
  61. printf("usage: %s [-Hhsv] [-p qdevice_ipc_socket_path]\n",
  62. QDEVICE_TOOL_PROGRAM_NAME);
  63. }
  64. static void
  65. cli_parse(int argc, char * const argv[], enum qdevice_tool_operation *operation,
  66. int *verbose, char **socket_path)
  67. {
  68. int ch;
  69. *operation = QDEVICE_TOOL_OPERATION_NONE;
  70. *verbose = 0;
  71. *socket_path = strdup(QDEVICE_DEFAULT_LOCAL_SOCKET_FILE);
  72. if (*socket_path == NULL) {
  73. errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR,
  74. "Can't alloc memory for socket path string");
  75. }
  76. while ((ch = getopt(argc, argv, "Hhsvp:")) != -1) {
  77. switch (ch) {
  78. case 'H':
  79. *operation = QDEVICE_TOOL_OPERATION_SHUTDOWN;
  80. break;
  81. case 's':
  82. *operation = QDEVICE_TOOL_OPERATION_STATUS;
  83. break;
  84. case 'v':
  85. *verbose = 1;
  86. break;
  87. case 'p':
  88. free(*socket_path);
  89. *socket_path = strdup(optarg);
  90. if (*socket_path == NULL) {
  91. errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR,
  92. "Can't alloc memory for socket path string");
  93. }
  94. break;
  95. case 'h':
  96. case '?':
  97. usage();
  98. exit(QDEVICE_TOOL_EXIT_CODE_USAGE);
  99. break;
  100. }
  101. }
  102. if (*operation == QDEVICE_TOOL_OPERATION_NONE) {
  103. usage();
  104. exit(QDEVICE_TOOL_EXIT_CODE_USAGE);
  105. }
  106. }
  107. static int
  108. store_command(struct dynar *str, enum qdevice_tool_operation operation, int verbose)
  109. {
  110. const char *nline = "\n\0";
  111. const int nline_len = 2;
  112. switch (operation) {
  113. case QDEVICE_TOOL_OPERATION_NONE:
  114. errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR, "Unhandled operation none");
  115. break;
  116. case QDEVICE_TOOL_OPERATION_SHUTDOWN:
  117. if (dynar_str_cat(str, "shutdown ") != 0) {
  118. return (-1);
  119. }
  120. break;
  121. case QDEVICE_TOOL_OPERATION_STATUS:
  122. if (dynar_str_cat(str, "status ") != 0) {
  123. return (-1);
  124. }
  125. break;
  126. }
  127. if (verbose) {
  128. if (dynar_str_cat(str, "verbose ") != 0) {
  129. return (-1);
  130. }
  131. }
  132. if (dynar_cat(str, nline, nline_len) != 0) {
  133. return (-1);
  134. }
  135. return (0);
  136. }
  137. /*
  138. * -1 - Internal error (can't alloc memory)
  139. * 0 - No error
  140. * 1 - IPC returned error
  141. * 2 - Unknown status line
  142. */
  143. static int
  144. read_ipc_reply(FILE *f)
  145. {
  146. struct dynar read_str;
  147. int ch;
  148. int status_readed;
  149. int res;
  150. static const char *ok_str = "OK";
  151. static const char *err_str = "Error";
  152. int err_set;
  153. char c;
  154. dynar_init(&read_str, IPC_READ_BUF_SIZE);
  155. status_readed = 0;
  156. err_set = 0;
  157. res = 0;
  158. while ((ch = fgetc(f)) != EOF) {
  159. if (status_readed) {
  160. putc(ch, (err_set ? stderr : stdout));
  161. } else {
  162. if (ch == '\r') {
  163. } else if (ch == '\n') {
  164. status_readed = 1;
  165. c = '\0';
  166. if (dynar_cat(&read_str, &c, sizeof(c)) != 0) {
  167. res = -1;
  168. goto exit_destroy;
  169. }
  170. if (strcasecmp(dynar_data(&read_str), ok_str) == 0) {
  171. } else if (strcasecmp(dynar_data(&read_str), err_str) == 0) {
  172. err_set = 1;
  173. res = 1;
  174. fprintf(stderr, "Error: ");
  175. } else {
  176. res = 2;
  177. goto exit_destroy;
  178. }
  179. } else {
  180. c = ch;
  181. if (dynar_cat(&read_str, &c, sizeof(c)) != 0) {
  182. res = -1;
  183. goto exit_destroy;
  184. }
  185. }
  186. }
  187. }
  188. exit_destroy:
  189. dynar_destroy(&read_str);
  190. return (res);
  191. }
  192. int
  193. main(int argc, char * const argv[])
  194. {
  195. enum qdevice_tool_operation operation;
  196. int verbose;
  197. char *socket_path;
  198. int sock_fd;
  199. FILE *sock;
  200. struct dynar send_str;
  201. int res;
  202. int exit_code;
  203. exit_code = QDEVICE_TOOL_EXIT_CODE_NO_ERROR;
  204. cli_parse(argc, argv, &operation, &verbose, &socket_path);
  205. dynar_init(&send_str, QDEVICE_DEFAULT_IPC_MAX_RECEIVE_SIZE);
  206. sock_fd = unix_socket_client_create(socket_path, 0);
  207. if (sock_fd == -1) {
  208. err(QDEVICE_TOOL_EXIT_CODE_SOCKET_CONNECT,
  209. "Can't connect to QDevice socket (is QDevice running?)");
  210. }
  211. sock = fdopen(sock_fd, "w+t");
  212. if (sock == NULL) {
  213. err(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR, "Can't open QDevice socket fd");
  214. }
  215. if (store_command(&send_str, operation, verbose) != 0) {
  216. errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR, "Can't store command");
  217. }
  218. res = fprintf(sock, "%s", dynar_data(&send_str));
  219. if (res < 0 || (size_t)res != strlen(dynar_data(&send_str)) ||
  220. fflush(sock) != 0) {
  221. errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR, "Can't send command");
  222. }
  223. res = read_ipc_reply(sock);
  224. switch (res) {
  225. case -1:
  226. errx(QDEVICE_TOOL_EXIT_CODE_INTERNAL_ERROR, "Internal error during IPC status line read");
  227. break;
  228. case 0:
  229. break;
  230. case 1:
  231. exit_code = QDEVICE_TOOL_EXIT_CODE_QDEVICE_RETURNED_ERROR;
  232. break;
  233. case 2:
  234. errx(QDEVICE_TOOL_EXIT_CODE_SOCKET_CONNECT, "Unknown status line returned by IPC server");
  235. break;
  236. }
  237. if (fclose(sock) != 0) {
  238. warn("Can't close QDevice socket");
  239. }
  240. free(socket_path);
  241. dynar_destroy(&send_str);
  242. return (exit_code);
  243. }