4
0

corosync-cfgtool.c 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. /*
  2. * Copyright (c) 2006-2009 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 <config.h>
  35. #include <stdio.h>
  36. #include <stdlib.h>
  37. #include <errno.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. #define cs_repeat(result, max, code) \
  51. do { \
  52. int counter = 0; \
  53. do { \
  54. result = code; \
  55. if (result == CS_ERR_TRY_AGAIN) { \
  56. sleep(1); \
  57. counter++; \
  58. } else { \
  59. break; \
  60. } \
  61. } while (counter < max); \
  62. } while (0)
  63. static void ringstatusget_do (void)
  64. {
  65. cs_error_t result;
  66. corosync_cfg_handle_t handle;
  67. unsigned int interface_count;
  68. char **interface_names;
  69. char **interface_status;
  70. unsigned int i;
  71. unsigned int nodeid;
  72. printf ("Printing ring status.\n");
  73. result = corosync_cfg_initialize (&handle, NULL);
  74. if (result != CS_OK) {
  75. printf ("Could not initialize corosync configuration API error %d\n", result);
  76. exit (1);
  77. }
  78. result = corosync_cfg_local_get(handle, &nodeid);
  79. if (result != CS_OK) {
  80. printf ("Could not get the local node id, the error is: %d\n", result);
  81. }
  82. else {
  83. printf ("Local node ID %d\n", nodeid);
  84. }
  85. result = corosync_cfg_ring_status_get (handle,
  86. &interface_names,
  87. &interface_status,
  88. &interface_count);
  89. if (result != CS_OK) {
  90. printf ("Could not get the ring status, the error is: %d\n", result);
  91. } else {
  92. for (i = 0; i < interface_count; i++) {
  93. printf ("RING ID %d\n", i);
  94. printf ("\tid\t= %s\n", interface_names[i]);
  95. printf ("\tstatus\t= %s\n", interface_status[i]);
  96. }
  97. }
  98. (void)corosync_cfg_finalize (handle);
  99. }
  100. static void ringreenable_do (void)
  101. {
  102. cs_error_t result;
  103. corosync_cfg_handle_t handle;
  104. printf ("Re-enabling all failed rings.\n");
  105. result = corosync_cfg_initialize (&handle, NULL);
  106. if (result != CS_OK) {
  107. printf ("Could not initialize corosync configuration API error %d\n", result);
  108. exit (1);
  109. }
  110. result = corosync_cfg_ring_reenable (handle);
  111. if (result != CS_OK) {
  112. printf ("Could not reenable ring error %d\n", result);
  113. }
  114. (void)corosync_cfg_finalize (handle);
  115. }
  116. static void service_load_do (const char *service, unsigned int version)
  117. {
  118. cs_error_t result;
  119. corosync_cfg_handle_t handle;
  120. printf ("Loading service '%s' version '%d'\n", service, version);
  121. result = corosync_cfg_initialize (&handle, NULL);
  122. if (result != CS_OK) {
  123. printf ("Could not initialize corosync configuration API error %d\n", result);
  124. exit (1);
  125. }
  126. result = corosync_cfg_service_load (handle, service, version);
  127. if (result != CS_OK) {
  128. printf ("Could not load service (error = %d)\n", result);
  129. }
  130. (void)corosync_cfg_finalize (handle);
  131. }
  132. static void service_unload_do (const char *service, unsigned int version)
  133. {
  134. cs_error_t result;
  135. corosync_cfg_handle_t handle;
  136. printf ("Unloading service '%s' version '%d'\n", service, version);
  137. result = corosync_cfg_initialize (&handle, NULL);
  138. if (result != CS_OK) {
  139. printf ("Could not initialize corosync configuration API error %d\n", result);
  140. exit (1);
  141. }
  142. result = corosync_cfg_service_unload (handle, service, version);
  143. if (result != CS_OK) {
  144. printf ("Could not unload service (error = %d)\n", result);
  145. }
  146. (void)corosync_cfg_finalize (handle);
  147. }
  148. static void shutdown_do(void)
  149. {
  150. cs_error_t result;
  151. corosync_cfg_handle_t handle;
  152. corosync_cfg_callbacks_t callbacks;
  153. callbacks.corosync_cfg_shutdown_callback = NULL;
  154. result = corosync_cfg_initialize (&handle, &callbacks);
  155. if (result != CS_OK) {
  156. printf ("Could not initialize corosync configuration API error %d\n", result);
  157. exit (1);
  158. }
  159. printf ("Shutting down corosync\n");
  160. cs_repeat(result, 30, corosync_cfg_try_shutdown (handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST));
  161. if (result != CS_OK) {
  162. printf ("Could not shutdown (error = %d)\n", result);
  163. }
  164. (void)corosync_cfg_finalize (handle);
  165. }
  166. static void showaddrs_do(int nodeid)
  167. {
  168. cs_error_t result;
  169. corosync_cfg_handle_t handle;
  170. corosync_cfg_callbacks_t callbacks;
  171. int numaddrs;
  172. int i;
  173. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  174. result = corosync_cfg_initialize (&handle, &callbacks);
  175. if (result != CS_OK) {
  176. printf ("Could not initialize corosync configuration API error %d\n", result);
  177. exit (1);
  178. }
  179. if (corosync_cfg_get_node_addrs(handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  180. for (i=0; i<numaddrs; i++) {
  181. char buf[INET6_ADDRSTRLEN];
  182. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  183. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  184. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  185. void *saddr;
  186. if (ss->ss_family == AF_INET6)
  187. saddr = &sin6->sin6_addr;
  188. else
  189. saddr = &sin->sin_addr;
  190. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  191. if (i != 0) {
  192. printf(" ");
  193. }
  194. printf("%s", buf);
  195. }
  196. printf("\n");
  197. }
  198. (void)corosync_cfg_finalize (handle);
  199. }
  200. static void crypto_do(unsigned int type)
  201. {
  202. cs_error_t result;
  203. corosync_cfg_handle_t handle;
  204. printf ("Setting crypto to mode %d\n", type);
  205. result = corosync_cfg_initialize (&handle, NULL);
  206. if (result != CS_OK) {
  207. printf ("Could not initialize corosync configuration API error %d\n", result);
  208. exit (1);
  209. }
  210. result = corosync_cfg_crypto_set (handle, type);
  211. if (result != CS_OK) {
  212. printf ("Could not set crypto mode (error = %d)\n", result);
  213. }
  214. (void)corosync_cfg_finalize (handle);
  215. }
  216. static void killnode_do(unsigned int nodeid)
  217. {
  218. cs_error_t result;
  219. corosync_cfg_handle_t handle;
  220. printf ("Killing node %d\n", nodeid);
  221. result = corosync_cfg_initialize (&handle, NULL);
  222. if (result != CS_OK) {
  223. printf ("Could not initialize corosync configuration API error %d\n", result);
  224. exit (1);
  225. }
  226. result = corosync_cfg_kill_node (handle, nodeid, "Killed by corosync-cfgtool");
  227. if (result != CS_OK) {
  228. printf ("Could not kill node (error = %d)\n", result);
  229. }
  230. (void)corosync_cfg_finalize (handle);
  231. }
  232. static void usage_do (void)
  233. {
  234. printf ("corosync-cfgtool [-s] [-r] [-l] [-u] [-H] [service_name] [-v] [version] [-k] [nodeid] [-a] [nodeid]\n\n");
  235. printf ("A tool for displaying and configuring active parameters within corosync.\n");
  236. printf ("options:\n");
  237. printf ("\t-s\tDisplays the status of the current rings on this node.\n");
  238. printf ("\t-r\tReset redundant ring state cluster wide after a fault to\n");
  239. printf ("\t\tre-enable redundant ring operation.\n");
  240. printf ("\t-l\tLoad a service identified by name.\n");
  241. printf ("\t-u\tUnload a service identified by name.\n");
  242. printf ("\t-a\tDisplay the IP address(es) of a node\n");
  243. printf ("\t-c\tSet the cryptography mode of cluster communications\n");
  244. printf ("\t-k\tKill a node identified by node id.\n");
  245. printf ("\t-H\tShutdown corosync cleanly on this node.\n");
  246. }
  247. static char *
  248. xstrdup (char const *s)
  249. {
  250. char *p = strdup (s);
  251. if (p)
  252. return (char *) p;
  253. printf ("exhausted virtual memory\n");
  254. exit (1);
  255. }
  256. int main (int argc, char *argv[]) {
  257. const char *options = "srl:u:v:k:a:c:hH";
  258. int opt;
  259. int service_load = 0;
  260. unsigned int nodeid;
  261. int service_unload = 0;
  262. char *service = NULL;
  263. unsigned int version = 0;
  264. if (argc == 1) {
  265. usage_do ();
  266. }
  267. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  268. switch (opt) {
  269. case 's':
  270. ringstatusget_do ();
  271. break;
  272. case 'r':
  273. ringreenable_do ();
  274. break;
  275. case 'l':
  276. service_load = 1;
  277. service = xstrdup (optarg);
  278. break;
  279. case 'u':
  280. service_unload = 1;
  281. service = xstrdup (optarg);
  282. break;
  283. case 'k':
  284. nodeid = atoi (optarg);
  285. killnode_do(nodeid);
  286. break;
  287. case 'H':
  288. shutdown_do();
  289. break;
  290. case 'a':
  291. showaddrs_do( atoi(optarg) );
  292. break;
  293. case 'c':
  294. crypto_do( atoi(optarg) );
  295. break;
  296. case 'v':
  297. version = atoi (optarg);
  298. break;
  299. case 'h':
  300. usage_do();
  301. break;
  302. }
  303. }
  304. if (service_load) {
  305. service_load_do (service, version);
  306. } else
  307. if (service_unload) {
  308. service_unload_do (service, version);
  309. }
  310. return (0);
  311. }