corosync-cfgtool.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275
  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/saAis.h>
  48. #include <corosync/cfg.h>
  49. static void ringstatusget_do (void)
  50. {
  51. SaAisErrorT result;
  52. corosync_cfg_handle_t handle;
  53. unsigned int interface_count;
  54. char **interface_names;
  55. char **interface_status;
  56. unsigned int i;
  57. printf ("Printing ring status.\n");
  58. result = corosync_cfg_initialize (&handle, NULL);
  59. if (result != SA_AIS_OK) {
  60. printf ("Could not initialize corosync configuration API error %d\n", result);
  61. exit (1);
  62. }
  63. corosync_cfg_ring_status_get (handle,
  64. &interface_names,
  65. &interface_status,
  66. &interface_count);
  67. for (i = 0; i < interface_count; i++) {
  68. printf ("RING ID %d\n", i);
  69. printf ("\tid\t= %s\n", interface_names[i]);
  70. printf ("\tstatus\t= %s\n", interface_status[i]);
  71. }
  72. corosync_cfg_finalize (handle);
  73. }
  74. static void ringreenable_do (void)
  75. {
  76. SaAisErrorT result;
  77. corosync_cfg_handle_t handle;
  78. printf ("Re-enabling all failed rings.\n");
  79. result = corosync_cfg_initialize (&handle, NULL);
  80. if (result != SA_AIS_OK) {
  81. printf ("Could not initialize corosync configuration API error %d\n", result);
  82. exit (1);
  83. }
  84. result = corosync_cfg_ring_reenable (handle);
  85. if (result != SA_AIS_OK) {
  86. printf ("Could not reenable ring error %d\n", result);
  87. }
  88. corosync_cfg_finalize (handle);
  89. }
  90. void service_load_do (char *service, unsigned int version)
  91. {
  92. SaAisErrorT result;
  93. corosync_cfg_handle_t handle;
  94. printf ("Loading service '%s' version '%d'\n", service, version);
  95. result = corosync_cfg_initialize (&handle, NULL);
  96. if (result != SA_AIS_OK) {
  97. printf ("Could not initialize corosync configuration API error %d\n", result);
  98. exit (1);
  99. }
  100. result = corosync_cfg_service_load (handle, service, version);
  101. if (result != SA_AIS_OK) {
  102. printf ("Could not load service (error = %d)\n", result);
  103. }
  104. corosync_cfg_finalize (handle);
  105. }
  106. void service_unload_do (char *service, unsigned int version)
  107. {
  108. SaAisErrorT result;
  109. corosync_cfg_handle_t handle;
  110. printf ("Unloading service '%s' version '%d'\n", service, version);
  111. result = corosync_cfg_initialize (&handle, NULL);
  112. if (result != SA_AIS_OK) {
  113. printf ("Could not initialize corosync configuration API error %d\n", result);
  114. exit (1);
  115. }
  116. result = corosync_cfg_service_unload (handle, service, version);
  117. if (result != SA_AIS_OK) {
  118. printf ("Could not unload service (error = %d)\n", result);
  119. }
  120. corosync_cfg_finalize (handle);
  121. }
  122. void shutdown_callback (corosync_cfg_handle_t cfg_handle, CorosyncCfgShutdownFlagsT flags)
  123. {
  124. printf("shutdown callback called, flags = %d\n",flags);
  125. corosync_cfg_replyto_shutdown (cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_YES);
  126. }
  127. void *shutdown_dispatch_thread(void *arg)
  128. {
  129. int res = SA_AIS_OK;
  130. corosync_cfg_handle_t *handle = arg;
  131. while (res == SA_AIS_OK) {
  132. res = corosync_cfg_dispatch(*handle, SA_DISPATCH_ALL);
  133. if (res != SA_AIS_OK)
  134. printf ("Could not dispatch cfg messages: %d\n", res);
  135. }
  136. return NULL;
  137. }
  138. void shutdown_do()
  139. {
  140. SaAisErrorT result;
  141. corosync_cfg_handle_t handle;
  142. CorosyncCfgCallbacksT callbacks;
  143. CorosyncCfgStateNotificationT notificationBuffer;
  144. pthread_t dispatch_thread;
  145. printf ("Shutting down corosync\n");
  146. callbacks.corosyncCfgShutdownCallback = shutdown_callback;
  147. result = corosync_cfg_initialize (&handle, &callbacks);
  148. if (result != SA_AIS_OK) {
  149. printf ("Could not initialize corosync configuration API error %d\n", result);
  150. exit (1);
  151. }
  152. pthread_create(&dispatch_thread, NULL, shutdown_dispatch_thread, &handle);
  153. result = corosync_cfg_state_track (handle,
  154. 0,
  155. &notificationBuffer);
  156. if (result != SA_AIS_OK) {
  157. printf ("Could not start corosync cfg tracking error %d\n", result);
  158. exit (1);
  159. }
  160. result = corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  161. if (result != SA_AIS_OK) {
  162. printf ("Could not shutdown (error = %d)\n", result);
  163. }
  164. corosync_cfg_finalize (handle);
  165. }
  166. void killnode_do(unsigned int nodeid)
  167. {
  168. SaAisErrorT result;
  169. corosync_cfg_handle_t handle;
  170. printf ("Killing node %d\n", nodeid);
  171. result = corosync_cfg_initialize (&handle, NULL);
  172. if (result != SA_AIS_OK) {
  173. printf ("Could not initialize corosync configuration API error %d\n", result);
  174. exit (1);
  175. }
  176. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  177. if (result != SA_AIS_OK) {
  178. printf ("Could not kill node (error = %d)\n", result);
  179. }
  180. corosync_cfg_finalize (handle);
  181. }
  182. void usage_do (void)
  183. {
  184. printf ("corosync-cfgtool [-s] [-r] [-l] [-u] [service_name] [-v] [version] [-k] [nodeid]\n\n");
  185. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  186. printf ("options:\n");
  187. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  188. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  189. printf ("\t\tre-enable redundant ring operation.\n");
  190. printf ("\t-l\tLoad a service identified by name.\n");
  191. printf ("\t-u\tUnload a service identified by name.\n");
  192. printf ("\t-k\tKill a node identified by node id.\n");
  193. printf ("\t-h\tShutdown corosync cleanly on this node.\n");
  194. }
  195. int main (int argc, char *argv[]) {
  196. const char *options = "srl:u:v:k:h";
  197. int opt;
  198. int service_load = 0;
  199. unsigned int nodeid;
  200. int service_unload = 0;
  201. char *service = NULL;
  202. unsigned int version = 0;
  203. if (argc == 1) {
  204. usage_do ();
  205. }
  206. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  207. switch (opt) {
  208. case 's':
  209. ringstatusget_do ();
  210. break;
  211. case 'r':
  212. ringreenable_do ();
  213. break;
  214. case 'l':
  215. service_load = 1;
  216. service = strdup (optarg);
  217. break;
  218. case 'u':
  219. service_unload = 1;
  220. service = strdup (optarg);
  221. break;
  222. case 'k':
  223. nodeid = atoi (optarg);
  224. killnode_do(nodeid);
  225. break;
  226. case 'h':
  227. shutdown_do();
  228. break;
  229. case 'v':
  230. version = atoi (optarg);
  231. break;
  232. }
  233. }
  234. if (service_load) {
  235. service_load_do (service, version);
  236. } else
  237. if (service_unload) {
  238. service_unload_do (service, version);
  239. }
  240. return (0);
  241. }