4
0

corosync-cpgtool.c 7.2 KB

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