testcpg.c 6.3 KB

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