corosync-cfgtool.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 <sys/types.h>
  41. #include <sys/socket.h>
  42. #include <sys/select.h>
  43. #include <sys/un.h>
  44. #include <netinet/in.h>
  45. #include <arpa/inet.h>
  46. #include <corosync/saAis.h>
  47. #include <corosync/cfg.h>
  48. static void ringstatusget_do (void)
  49. {
  50. SaAisErrorT result;
  51. corosync_cfg_handle_t handle;
  52. unsigned int interface_count;
  53. char **interface_names;
  54. char **interface_status;
  55. unsigned int i;
  56. printf ("Printing ring status.\n");
  57. result = corosync_cfg_initialize (&handle, NULL);
  58. if (result != SA_AIS_OK) {
  59. printf ("Could not initialize corosync configuration API error %d\n", result);
  60. exit (1);
  61. }
  62. corosync_cfg_ring_status_get (handle,
  63. &interface_names,
  64. &interface_status,
  65. &interface_count);
  66. for (i = 0; i < interface_count; i++) {
  67. printf ("RING ID %d\n", i);
  68. printf ("\tid\t= %s\n", interface_names[i]);
  69. printf ("\tstatus\t= %s\n", interface_status[i]);
  70. }
  71. corosync_cfg_finalize (handle);
  72. }
  73. static void ringreenable_do (void)
  74. {
  75. SaAisErrorT result;
  76. corosync_cfg_handle_t handle;
  77. printf ("Re-enabling all failed rings.\n");
  78. result = corosync_cfg_initialize (&handle, NULL);
  79. if (result != SA_AIS_OK) {
  80. printf ("Could not initialize corosync configuration API error %d\n", result);
  81. exit (1);
  82. }
  83. result = corosync_cfg_ring_reenable (handle);
  84. if (result != SA_AIS_OK) {
  85. printf ("Could not reenable ring error %d\n", result);
  86. }
  87. corosync_cfg_finalize (handle);
  88. }
  89. void service_load_do (char *service, unsigned int version)
  90. {
  91. SaAisErrorT result;
  92. corosync_cfg_handle_t handle;
  93. printf ("Loading service '%s' version '%d'\n", service, version);
  94. result = corosync_cfg_initialize (&handle, NULL);
  95. if (result != SA_AIS_OK) {
  96. printf ("Could not initialize corosync configuration API error %d\n", result);
  97. exit (1);
  98. }
  99. result = corosync_cfg_service_load (handle, service, version);
  100. if (result != SA_AIS_OK) {
  101. printf ("Could not load service (error = %d)\n", result);
  102. }
  103. corosync_cfg_finalize (handle);
  104. }
  105. void service_unload_do (char *service, unsigned int version)
  106. {
  107. SaAisErrorT result;
  108. corosync_cfg_handle_t handle;
  109. printf ("Unloading service '%s' version '%d'\n", service, version);
  110. result = corosync_cfg_initialize (&handle, NULL);
  111. if (result != SA_AIS_OK) {
  112. printf ("Could not initialize corosync configuration API error %d\n", result);
  113. exit (1);
  114. }
  115. result = corosync_cfg_service_unload (handle, service, version);
  116. if (result != SA_AIS_OK) {
  117. printf ("Could not unload service (error = %d)\n", result);
  118. }
  119. corosync_cfg_finalize (handle);
  120. }
  121. void usage_do (void)
  122. {
  123. printf ("corosync-cfgtool [-s] [-r] [-l] [-u] [service_name] [-v] [version]\n\n");
  124. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  125. printf ("options:\n");
  126. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  127. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  128. printf ("\t\tre-enable redundant ring operation.\n");
  129. printf ("\t-l\tLoad a service identified by name.\n");
  130. printf ("\t-u\tUnload a service identified by name.\n");
  131. }
  132. int main (int argc, char *argv[]) {
  133. const char *options = "srl:u:v:";
  134. int opt;
  135. int service_load = 0;
  136. int service_unload = 0;
  137. char *service;
  138. unsigned int version;
  139. if (argc == 1) {
  140. usage_do ();
  141. }
  142. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  143. switch (opt) {
  144. case 's':
  145. ringstatusget_do ();
  146. break;
  147. case 'r':
  148. ringreenable_do ();
  149. break;
  150. case 'l':
  151. service_load = 1;
  152. service = strdup (optarg);
  153. break;
  154. case 'u':
  155. service_unload = 1;
  156. service = strdup (optarg);
  157. break;
  158. case 'v':
  159. version = atoi (optarg);
  160. }
  161. }
  162. if (service_load) {
  163. service_load_do (service, version);
  164. } else
  165. if (service_unload) {
  166. service_unload_do (service, version);
  167. }
  168. return (0);
  169. }