4
0

corosync-cpgtool.c 7.1 KB

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