testcpgzc.c 6.7 KB

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