4
0

testcpgzc.c 6.8 KB

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