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