testcpg.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  1. /*
  2. * Copyright (c) 2006-2008 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield <ccaulfie@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/corotypes.h>
  47. #include <corosync/cpg.h>
  48. static int quit = 0;
  49. static int show_ip = 0;
  50. void print_cpgname (struct cpg_name *name)
  51. {
  52. int i;
  53. for (i = 0; i < name->length; i++) {
  54. printf ("%c", name->value[i]);
  55. }
  56. }
  57. void DeliverCallback (
  58. cpg_handle_t handle,
  59. struct cpg_name *groupName,
  60. uint32_t nodeid,
  61. uint32_t pid,
  62. void *msg,
  63. int msg_len)
  64. {
  65. if (show_ip) {
  66. struct in_addr saddr;
  67. saddr.s_addr = nodeid;
  68. printf("DeliverCallback: message (len=%d)from node/pid %s/%d: '%s'\n",
  69. msg_len, inet_ntoa(saddr), pid, (char *)msg);
  70. }
  71. else {
  72. printf("DeliverCallback: message (len=%d)from node/pid %d/%d: '%s'\n", msg_len, nodeid, pid, (char *)msg);
  73. }
  74. }
  75. void ConfchgCallback (
  76. cpg_handle_t handle,
  77. struct cpg_name *groupName,
  78. struct cpg_address *member_list, int member_list_entries,
  79. struct cpg_address *left_list, int left_list_entries,
  80. struct cpg_address *joined_list, int joined_list_entries)
  81. {
  82. int i;
  83. struct in_addr saddr;
  84. printf("\nConfchgCallback: group '");
  85. print_cpgname(groupName);
  86. printf("'\n");
  87. for (i=0; i<joined_list_entries; i++) {
  88. if (show_ip) {
  89. saddr.s_addr = joined_list[i].nodeid;
  90. printf("joined node/pid: %s/%d reason: %d\n",
  91. inet_ntoa (saddr), joined_list[i].pid,
  92. joined_list[i].reason);
  93. }
  94. else {
  95. printf("joined node/pid: %d/%d reason: %d\n",
  96. joined_list[i].nodeid, joined_list[i].pid,
  97. joined_list[i].reason);
  98. }
  99. }
  100. for (i=0; i<left_list_entries; i++) {
  101. if (show_ip) {
  102. saddr.s_addr = left_list[i].nodeid;
  103. printf("left node/pid: %s/%d reason: %d\n",
  104. inet_ntoa (saddr), left_list[i].pid,
  105. left_list[i].reason);
  106. }
  107. else {
  108. printf("left node/pid: %d/%d reason: %d\n",
  109. left_list[i].nodeid, left_list[i].pid,
  110. left_list[i].reason);
  111. }
  112. }
  113. printf("nodes in group now %d\n", member_list_entries);
  114. for (i=0; i<member_list_entries; i++) {
  115. if (show_ip) {
  116. saddr.s_addr = member_list[i].nodeid;
  117. printf("node/pid: %s/%d\n",
  118. inet_ntoa (saddr), member_list[i].pid);
  119. }
  120. else {
  121. printf("node/pid: %d/%d\n",
  122. member_list[i].nodeid, member_list[i].pid);
  123. }
  124. }
  125. /* Is it us??
  126. NOTE: in reality we should also check the nodeid */
  127. if (left_list_entries && left_list[0].pid == getpid()) {
  128. printf("We have left the building\n");
  129. quit = 1;
  130. }
  131. }
  132. void GroupsGetCallback(cpg_handle_t handle,
  133. uint32_t groupnum,
  134. uint32_t groupmax,
  135. struct cpg_name *group_name,
  136. struct cpg_address *member_list, int member_list_entries)
  137. {
  138. int i;
  139. struct in_addr saddr;
  140. printf("Groups List Callback %d/%d: ", groupnum, groupmax);
  141. print_cpgname(group_name);
  142. printf("\n");
  143. for (i=0; i<member_list_entries; i++) {
  144. if (show_ip) {
  145. saddr.s_addr = member_list[i].nodeid;
  146. printf("node/pid: %s/%d\n",
  147. inet_ntoa (saddr), member_list[i].pid);
  148. }
  149. else {
  150. printf("node/pid: %d/%d\n",
  151. member_list[i].nodeid, member_list[i].pid);
  152. }
  153. }
  154. }
  155. cpg_callbacks_t callbacks = {
  156. .cpg_deliver_fn = DeliverCallback,
  157. .cpg_confchg_fn = ConfchgCallback,
  158. .cpg_groups_get_fn = GroupsGetCallback
  159. };
  160. void sigintr_handler (int signum) {
  161. exit (0);
  162. }
  163. static struct cpg_name group_name;
  164. int main (int argc, char *argv[]) {
  165. cpg_handle_t handle;
  166. fd_set read_fds;
  167. int select_fd;
  168. int result;
  169. const char *options = "i";
  170. int opt;
  171. unsigned int nodeid;
  172. unsigned int num_groups;
  173. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  174. switch (opt) {
  175. case 'i':
  176. show_ip = 1;
  177. break;
  178. }
  179. }
  180. if (argc > optind) {
  181. strcpy(group_name.value, argv[optind]);
  182. group_name.length = strlen(argv[optind])+1;
  183. }
  184. else {
  185. strcpy(group_name.value, "GROUP");
  186. group_name.length = 6;
  187. }
  188. result = cpg_initialize (&handle, &callbacks);
  189. if (result != CS_OK) {
  190. printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
  191. exit (1);
  192. }
  193. result = cpg_local_get (handle, &nodeid);
  194. if (result != CS_OK) {
  195. printf ("Could not get local node id\n");
  196. exit (1);
  197. }
  198. printf ("Local node id is %x\n", nodeid);
  199. result = cpg_join(handle, &group_name);
  200. if (result != CS_OK) {
  201. printf ("Could not join process group, error %d\n", result);
  202. exit (1);
  203. }
  204. cpg_groups_get(handle, &num_groups);
  205. if (result != CS_OK) {
  206. printf ("Could not get list of groups, error %d\n", result);
  207. exit (1);
  208. }
  209. printf("%d groups known to this node\n", num_groups);
  210. FD_ZERO (&read_fds);
  211. cpg_fd_get(handle, &select_fd);
  212. printf ("Type EXIT to finish\n");
  213. do {
  214. FD_SET (select_fd, &read_fds);
  215. FD_SET (STDIN_FILENO, &read_fds);
  216. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  217. if (result == -1) {
  218. perror ("select\n");
  219. }
  220. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  221. char inbuf[132];
  222. struct iovec iov;
  223. fgets(inbuf, sizeof(inbuf), stdin);
  224. if (strncmp(inbuf, "EXIT", 4) == 0) {
  225. cpg_leave(handle, &group_name);
  226. }
  227. else {
  228. iov.iov_base = inbuf;
  229. iov.iov_len = strlen(inbuf)+1;
  230. cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1);
  231. }
  232. }
  233. if (FD_ISSET (select_fd, &read_fds)) {
  234. if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK)
  235. exit(1);
  236. }
  237. } while (result && !quit);
  238. result = cpg_finalize (handle);
  239. printf ("Finalize result is %d (should be 1)\n", result);
  240. return (0);
  241. }