4
0

testzcgc.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * Copyright (c) 2006-2009 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 <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. static void print_cpgname (const 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. static void DeliverCallback (
  58. cpg_handle_t handle,
  59. const struct cpg_name *groupName,
  60. uint32_t nodeid,
  61. uint32_t pid,
  62. void *msg,
  63. size_t msg_len)
  64. {
  65. if (show_ip) {
  66. struct in_addr saddr;
  67. saddr.s_addr = nodeid;
  68. printf("DeliverCallback: message (len=%lu)from node/pid %s/%d: '%s'\n",
  69. (unsigned long int) msg_len,
  70. inet_ntoa(saddr), pid, (const char *)msg);
  71. }
  72. else {
  73. printf("DeliverCallback: message (len=%lu)from node/pid %d/%d: '%s'\n",
  74. (unsigned long int) msg_len, nodeid, pid,
  75. (const char *)msg);
  76. }
  77. }
  78. static void ConfchgCallback (
  79. cpg_handle_t handle,
  80. const struct cpg_name *groupName,
  81. const struct cpg_address *member_list, size_t member_list_entries,
  82. const struct cpg_address *left_list, size_t left_list_entries,
  83. const struct cpg_address *joined_list, size_t joined_list_entries)
  84. {
  85. int i;
  86. struct in_addr saddr;
  87. printf("\nConfchgCallback: group '");
  88. print_cpgname(groupName);
  89. printf("'\n");
  90. for (i=0; i<joined_list_entries; i++) {
  91. if (show_ip) {
  92. saddr.s_addr = joined_list[i].nodeid;
  93. printf("joined node/pid: %s/%d reason: %d\n",
  94. inet_ntoa (saddr), joined_list[i].pid,
  95. joined_list[i].reason);
  96. }
  97. else {
  98. printf("joined node/pid: %d/%d reason: %d\n",
  99. joined_list[i].nodeid, joined_list[i].pid,
  100. joined_list[i].reason);
  101. }
  102. }
  103. for (i=0; i<left_list_entries; i++) {
  104. if (show_ip) {
  105. saddr.s_addr = left_list[i].nodeid;
  106. printf("left node/pid: %s/%d reason: %d\n",
  107. inet_ntoa (saddr), left_list[i].pid,
  108. left_list[i].reason);
  109. }
  110. else {
  111. printf("left node/pid: %d/%d reason: %d\n",
  112. left_list[i].nodeid, left_list[i].pid,
  113. left_list[i].reason);
  114. }
  115. }
  116. printf("nodes in group now %lu\n",
  117. (unsigned long int) member_list_entries);
  118. for (i=0; i<member_list_entries; i++) {
  119. if (show_ip) {
  120. saddr.s_addr = member_list[i].nodeid;
  121. printf("node/pid: %s/%d\n",
  122. inet_ntoa (saddr), member_list[i].pid);
  123. }
  124. else {
  125. printf("node/pid: %d/%d\n",
  126. member_list[i].nodeid, member_list[i].pid);
  127. }
  128. }
  129. /* Is it us??
  130. NOTE: in reality we should also check the nodeid */
  131. if (left_list_entries && left_list[0].pid == getpid()) {
  132. printf("We have left the building\n");
  133. quit = 1;
  134. }
  135. }
  136. static cpg_callbacks_t callbacks = {
  137. .cpg_deliver_fn = DeliverCallback,
  138. .cpg_confchg_fn = ConfchgCallback,
  139. };
  140. static void sigintr_handler (int signum) __attribute__((noreturn));
  141. static void sigintr_handler (int signum) {
  142. exit (0);
  143. }
  144. static struct cpg_name group_name;
  145. int main (int argc, char *argv[]) {
  146. cpg_handle_t handle;
  147. int result;
  148. void *buffer;
  149. unsigned int i;
  150. strcpy(group_name.value, "GROUP");
  151. group_name.length = 6;
  152. result = cpg_initialize (&handle, &callbacks);
  153. if (result != CS_OK) {
  154. printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
  155. exit (1);
  156. }
  157. for (i = 0; i < 100; i++) {
  158. cpg_zcb_alloc (handle, 1024*1024, &buffer);
  159. }
  160. return (0);
  161. }