corosync-cfgtool.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321
  1. /*
  2. * Copyright (c) 2006-2007 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Steven Dake <sdake@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 MontaVista Software, 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 <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <signal.h>
  38. #include <unistd.h>
  39. #include <string.h>
  40. #include <pthread.h>
  41. #include <sys/types.h>
  42. #include <sys/socket.h>
  43. #include <sys/select.h>
  44. #include <sys/un.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <corosync/corotypes.h>
  48. #include <corosync/totem/totem.h>
  49. #include <corosync/cfg.h>
  50. static void ringstatusget_do (void)
  51. {
  52. cs_error_t result;
  53. corosync_cfg_handle_t handle;
  54. unsigned int interface_count;
  55. char **interface_names;
  56. char **interface_status;
  57. unsigned int i;
  58. printf ("Printing ring status.\n");
  59. result = corosync_cfg_initialize (&handle, NULL);
  60. if (result != CS_OK) {
  61. printf ("Could not initialize corosync configuration API error %d\n", result);
  62. exit (1);
  63. }
  64. result = corosync_cfg_ring_status_get (handle,
  65. &interface_names,
  66. &interface_status,
  67. &interface_count);
  68. if (result != CS_OK) {
  69. printf ("Could not get the ring status, the error is: %d\n", result);
  70. } else {
  71. for (i = 0; i < interface_count; i++) {
  72. printf ("RING ID %d\n", i);
  73. printf ("\tid\t= %s\n", interface_names[i]);
  74. printf ("\tstatus\t= %s\n", interface_status[i]);
  75. }
  76. }
  77. (void)corosync_cfg_finalize (handle);
  78. }
  79. static void ringreenable_do (void)
  80. {
  81. cs_error_t result;
  82. corosync_cfg_handle_t handle;
  83. printf ("Re-enabling all failed rings.\n");
  84. result = corosync_cfg_initialize (&handle, NULL);
  85. if (result != CS_OK) {
  86. printf ("Could not initialize corosync configuration API error %d\n", result);
  87. exit (1);
  88. }
  89. result = corosync_cfg_ring_reenable (handle);
  90. if (result != CS_OK) {
  91. printf ("Could not reenable ring error %d\n", result);
  92. }
  93. (void)corosync_cfg_finalize (handle);
  94. }
  95. void service_load_do (char *service, unsigned int version)
  96. {
  97. cs_error_t result;
  98. corosync_cfg_handle_t handle;
  99. printf ("Loading service '%s' version '%d'\n", service, version);
  100. result = corosync_cfg_initialize (&handle, NULL);
  101. if (result != CS_OK) {
  102. printf ("Could not initialize corosync configuration API error %d\n", result);
  103. exit (1);
  104. }
  105. result = corosync_cfg_service_load (handle, service, version);
  106. if (result != CS_OK) {
  107. printf ("Could not load service (error = %d)\n", result);
  108. }
  109. (void)corosync_cfg_finalize (handle);
  110. }
  111. void service_unload_do (char *service, unsigned int version)
  112. {
  113. cs_error_t result;
  114. corosync_cfg_handle_t handle;
  115. printf ("Unloading service '%s' version '%d'\n", service, version);
  116. result = corosync_cfg_initialize (&handle, NULL);
  117. if (result != CS_OK) {
  118. printf ("Could not initialize corosync configuration API error %d\n", result);
  119. exit (1);
  120. }
  121. result = corosync_cfg_service_unload (handle, service, version);
  122. if (result != CS_OK) {
  123. printf ("Could not unload service (error = %d)\n", result);
  124. }
  125. (void)corosync_cfg_finalize (handle);
  126. }
  127. void shutdown_callback (corosync_cfg_handle_t cfg_handle, CorosyncCfgShutdownFlagsT flags)
  128. {
  129. printf("shutdown callback called, flags = %d\n",flags);
  130. (void)corosync_cfg_replyto_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_YES);
  131. }
  132. void *shutdown_dispatch_thread(void *arg)
  133. {
  134. int res = CS_OK;
  135. corosync_cfg_handle_t *handle = arg;
  136. while (res == CS_OK) {
  137. res = corosync_cfg_dispatch(*handle, CS_DISPATCH_ALL);
  138. if (res != CS_OK)
  139. printf ("Could not dispatch cfg messages: %d\n", res);
  140. }
  141. return NULL;
  142. }
  143. void shutdown_do()
  144. {
  145. cs_error_t result;
  146. corosync_cfg_handle_t handle;
  147. CorosyncCfgCallbacksT callbacks;
  148. CorosyncCfgStateNotificationT notificationBuffer;
  149. pthread_t dispatch_thread;
  150. printf ("Shutting down corosync\n");
  151. callbacks.corosyncCfgShutdownCallback = shutdown_callback;
  152. result = corosync_cfg_initialize (&handle, &callbacks);
  153. if (result != CS_OK) {
  154. printf ("Could not initialize corosync configuration API error %d\n", result);
  155. exit (1);
  156. }
  157. pthread_create(&dispatch_thread, NULL, shutdown_dispatch_thread, &handle);
  158. result = corosync_cfg_state_track (handle,
  159. 0,
  160. &notificationBuffer);
  161. if (result != CS_OK) {
  162. printf ("Could not start corosync cfg tracking error %d\n", result);
  163. exit (1);
  164. }
  165. result = corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  166. if (result != CS_OK) {
  167. printf ("Could not shutdown (error = %d)\n", result);
  168. }
  169. (void)corosync_cfg_finalize (handle);
  170. }
  171. void showaddrs_do(int nodeid)
  172. {
  173. cs_error_t result;
  174. corosync_cfg_handle_t handle;
  175. CorosyncCfgCallbacksT callbacks;
  176. int numaddrs;
  177. int i;
  178. CorosyncCfgNodeAddressT addrs[INTERFACE_MAX];
  179. result = corosync_cfg_initialize (&handle, &callbacks);
  180. if (result != CS_OK) {
  181. printf ("Could not initialize corosync configuration API error %d\n", result);
  182. exit (1);
  183. }
  184. if (!corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  185. for (i=0; i<numaddrs; i++) {
  186. char buf[INET6_ADDRSTRLEN];
  187. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  188. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  189. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  190. void *saddr;
  191. if (ss->ss_family == AF_INET6)
  192. saddr = &sin6->sin6_addr;
  193. else
  194. saddr = &sin->sin_addr;
  195. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  196. printf("%s", buf);
  197. }
  198. printf("\n");
  199. }
  200. (void)corosync_cfg_finalize (handle);
  201. }
  202. void killnode_do(unsigned int nodeid)
  203. {
  204. cs_error_t result;
  205. corosync_cfg_handle_t handle;
  206. printf ("Killing node %d\n", nodeid);
  207. result = corosync_cfg_initialize (&handle, NULL);
  208. if (result != CS_OK) {
  209. printf ("Could not initialize corosync configuration API error %d\n", result);
  210. exit (1);
  211. }
  212. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  213. if (result != CS_OK) {
  214. printf ("Could not kill node (error = %d)\n", result);
  215. }
  216. (void)corosync_cfg_finalize (handle);
  217. }
  218. void usage_do (void)
  219. {
  220. printf ("corosync-cfgtool [-s] [-r] [-l] [-u] [service_name] [-v] [version] [-k] [nodeid] [-a] [nodeid]\n\n");
  221. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  222. printf ("options:\n");
  223. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  224. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  225. printf ("\t\tre-enable redundant ring operation.\n");
  226. printf ("\t-l\tLoad a service identified by name.\n");
  227. printf ("\t-u\tUnload a service identified by name.\n");
  228. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  229. printf ("\t-k\tKill a node identified by node id.\n");
  230. printf ("\t-h\tShutdown corosync cleanly on this node.\n");
  231. }
  232. int main (int argc, char *argv[]) {
  233. const char *options = "srl:u:v:k:a:h";
  234. int opt;
  235. int service_load = 0;
  236. unsigned int nodeid;
  237. int service_unload = 0;
  238. char *service = NULL;
  239. unsigned int version = 0;
  240. if (argc == 1) {
  241. usage_do ();
  242. }
  243. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  244. switch (opt) {
  245. case 's':
  246. ringstatusget_do ();
  247. break;
  248. case 'r':
  249. ringreenable_do ();
  250. break;
  251. case 'l':
  252. service_load = 1;
  253. service = strdup (optarg);
  254. break;
  255. case 'u':
  256. service_unload = 1;
  257. service = strdup (optarg);
  258. break;
  259. case 'k':
  260. nodeid = atoi (optarg);
  261. killnode_do(nodeid);
  262. break;
  263. case 'h':
  264. shutdown_do();
  265. break;
  266. case 'a':
  267. showaddrs_do( atoi(optarg) );
  268. break;
  269. case 'v':
  270. version = atoi (optarg);
  271. break;
  272. }
  273. }
  274. if (service_load) {
  275. service_load_do (service, version);
  276. } else
  277. if (service_unload) {
  278. service_unload_do (service, version);
  279. }
  280. return (0);
  281. }