corosync-cpgtool.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276
  1. /*
  2. * Copyright (c) 2009-2011 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 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 <libgen.h>
  48. #include <corosync/corotypes.h>
  49. #include <corosync/totem/totem.h>
  50. #include <corosync/cfg.h>
  51. #include <corosync/cpg.h>
  52. static corosync_cfg_handle_t cfg_handle;
  53. static cpg_handle_t cpg_handle;
  54. typedef enum {
  55. OPER_NAMES_ONLY = 1,
  56. OPER_FULL_OUTPUT = 2,
  57. } operation_t;
  58. static void fprint_addrs(FILE *f, unsigned int nodeid)
  59. {
  60. int numaddrs;
  61. int i;
  62. corosync_cfg_node_address_t addrs[INTERFACE_MAX];
  63. if (corosync_cfg_get_node_addrs(cfg_handle, nodeid, INTERFACE_MAX, &numaddrs, addrs) == CS_OK) {
  64. for (i=0; i<numaddrs; i++) {
  65. char buf[INET6_ADDRSTRLEN];
  66. struct sockaddr_storage *ss = (struct sockaddr_storage *)addrs[i].address;
  67. struct sockaddr_in *sin = (struct sockaddr_in *)addrs[i].address;
  68. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)addrs[i].address;
  69. void *saddr;
  70. if (!ss->ss_family) {
  71. continue;
  72. }
  73. if (ss->ss_family == AF_INET6) {
  74. saddr = &sin6->sin6_addr;
  75. } else {
  76. saddr = &sin->sin_addr;
  77. }
  78. inet_ntop(ss->ss_family, saddr, buf, sizeof(buf));
  79. if (i != 0) {
  80. fprintf(f, " ");
  81. }
  82. fprintf(f, "%s", buf);
  83. }
  84. }
  85. }
  86. static void fprint_group (FILE *f, int escape, const struct cpg_name *group) {
  87. int i;
  88. char c;
  89. for (i = 0; i < group->length; i++) {
  90. c = group->value[i];
  91. if (!escape || (c >= ' ' && c < 0x7f && c != '\\')) {
  92. fputc (c, f);
  93. } else {
  94. if (c == '\\')
  95. fprintf (f, "\\\\");
  96. else
  97. fprintf (f, "\\x%02X", c);
  98. }
  99. }
  100. }
  101. static int display_groups (char delimiter, int escape)
  102. {
  103. cs_error_t res;
  104. cpg_iteration_handle_t iter_handle;
  105. struct cpg_iteration_description_t description;
  106. res = cpg_iteration_initialize (cpg_handle, CPG_ITERATION_NAME_ONLY, NULL, &iter_handle);
  107. if (res != CS_OK) {
  108. fprintf (stderr, "Cannot initialize cpg iterator error %d\n", res);
  109. return 0;
  110. }
  111. while ((res = cpg_iteration_next (iter_handle, &description)) == CS_OK) {
  112. fprint_group (stdout, escape, &description.group);
  113. fputc ((delimiter ? delimiter : '\n'), stdout);
  114. }
  115. if (delimiter)
  116. putc ('\n', stdout);
  117. cpg_iteration_finalize (iter_handle);
  118. return 1;
  119. }
  120. static inline int group_name_compare (
  121. const struct cpg_name *g1,
  122. const struct cpg_name *g2)
  123. {
  124. return (g1->length == g2->length?
  125. memcmp (g1->value, g2->value, g1->length):
  126. g1->length - g2->length);
  127. }
  128. static int display_groups_with_members (char delimiter, int escape) {
  129. cs_error_t res;
  130. cpg_iteration_handle_t iter_handle;
  131. struct cpg_iteration_description_t description;
  132. struct cpg_name old_group;
  133. res = cpg_iteration_initialize (cpg_handle, CPG_ITERATION_ALL, NULL, &iter_handle);
  134. if (res != CS_OK) {
  135. fprintf (stderr, "Cannot initialize cpg iterator error %d\n", res);
  136. return 0;
  137. }
  138. memset (&old_group, 0, sizeof (struct cpg_name));
  139. if (delimiter) {
  140. fprintf (stdout, "GRP_NAME%cPID%cNODEID\n", delimiter, delimiter);
  141. } else {
  142. fprintf (stdout, "Group Name\t%10s\t%10s\n", "PID", "Node ID");
  143. }
  144. while ((res = cpg_iteration_next (iter_handle, &description)) == CS_OK) {
  145. if (!delimiter && group_name_compare (&old_group, &description.group) != 0) {
  146. fprint_group (stdout, escape, &description.group);
  147. fprintf (stdout, "\n");
  148. memcpy (&old_group, &description.group, sizeof (struct cpg_name));
  149. }
  150. if (!delimiter) {
  151. fprintf (stdout, "\t\t%10u\t%10u (", description.pid, description.nodeid);
  152. fprint_addrs (stdout, description.nodeid);
  153. fprintf (stdout, ")\n");
  154. } else {
  155. fprint_group (stdout, escape, &description.group);
  156. fprintf (stdout, "%c%u%c%u\n", delimiter, description.pid, delimiter, description.nodeid);
  157. }
  158. }
  159. if (res != CS_ERR_NO_SECTIONS) {
  160. fprintf (stderr, "cpg iteration error %d\n", res);
  161. return 0;
  162. }
  163. cpg_iteration_finalize (iter_handle);
  164. return 1;
  165. }
  166. static void usage_do (const char *prog_name)
  167. {
  168. printf ("%s [-d delimiter] [-e] [-n] [-h]\n\n", prog_name);
  169. printf ("A tool for displaying cpg groups and members.\n");
  170. printf ("options:\n");
  171. printf ("\t-d\tDelimiter between fields.\n");
  172. printf ("\t-e\tDon't escape unprintable characters in group name.\n");
  173. printf ("\t-n\tDisplay only all existing group names.\n");
  174. printf ("\t-h\tDisplay this help.\n");
  175. }
  176. int main (int argc, char *argv[]) {
  177. const char *options = "hd:ne";
  178. int opt;
  179. const char *prog_name = basename(argv[0]);
  180. char delimiter = 0;
  181. int escape = 1;
  182. operation_t operation = OPER_FULL_OUTPUT;
  183. int result;
  184. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  185. switch (opt) {
  186. case 'd':
  187. if (strlen (optarg) > 0) {
  188. delimiter = optarg[0];
  189. }
  190. break;
  191. case 'n':
  192. operation = OPER_NAMES_ONLY;
  193. break;
  194. case 'e':
  195. escape = 0;
  196. break;
  197. case 'h':
  198. usage_do (prog_name);
  199. return (EXIT_SUCCESS);
  200. break;
  201. case '?':
  202. case ':':
  203. return (EXIT_FAILURE);
  204. break;
  205. }
  206. }
  207. result = cpg_initialize (&cpg_handle, NULL);
  208. if (result != CS_OK) {
  209. fprintf (stderr, "Could not initialize corosync cpg API error %d\n", result);
  210. return (EXIT_FAILURE);
  211. }
  212. result = corosync_cfg_initialize (&cfg_handle, NULL);
  213. if (result != CS_OK) {
  214. fprintf (stderr, "Could not initialize corosync configuration API error %d\n", result);
  215. return (EXIT_FAILURE);
  216. }
  217. switch (operation) {
  218. case OPER_NAMES_ONLY:
  219. result = display_groups (delimiter, escape);
  220. break;
  221. case OPER_FULL_OUTPUT:
  222. result = display_groups_with_members (delimiter, escape);
  223. break;
  224. }
  225. cpg_finalize (cpg_handle);
  226. corosync_cfg_finalize (cfg_handle);
  227. return (result ? EXIT_SUCCESS : EXIT_FAILURE);
  228. }