testcpg.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2006 Red Hat Inc
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Patrick Caulfield <pcaulfie@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 <stdio.h>
  35. #include <stdlib.h>
  36. #include <errno.h>
  37. #include <signal.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 "saAis.h"
  45. #include "cpg.h"
  46. void print_cpgname (struct cpg_name *name)
  47. {
  48. int i;
  49. for (i = 0; i < name->length; i++) {
  50. printf ("%c", name->value[i]);
  51. }
  52. }
  53. void DeliverCallback (
  54. cpg_handle_t handle,
  55. struct cpg_name *groupName,
  56. uint32_t nodeid,
  57. uint32_t pid,
  58. void *msg,
  59. int msg_len)
  60. {
  61. printf("DeliverCallback: message (len=%d)from node/pid %d/%d: '%s'\n", msg_len, nodeid, pid, (char *)msg);
  62. }
  63. void ConfchgCallback (
  64. cpg_handle_t handle,
  65. struct cpg_name *groupName,
  66. struct cpg_address *member_list, int member_list_entries,
  67. struct cpg_address *left_list, int left_list_entries,
  68. struct cpg_address *joined_list, int joined_list_entries)
  69. {
  70. int i;
  71. printf("\nConfchgCallback: group '"); print_cpgname(groupName); printf("'\n");
  72. for (i=0; i<joined_list_entries; i++)
  73. printf("joined node/pid: %d/%d reason: %d\n", joined_list[i].nodeId, joined_list[i].pid, joined_list[i].reason);
  74. for (i=0; i<left_list_entries; i++)
  75. printf("left node/pid: %d/%d reason: %d\n", left_list[i].nodeId, left_list[i].pid, left_list[i].reason);
  76. printf("nodes in group now %d\n", member_list_entries);
  77. for (i=0; i<member_list_entries; i++) {
  78. printf("node/pid: %d/%d\n", member_list[i].nodeId, member_list[i].pid);
  79. }
  80. /* Is it us??
  81. NOTE: in reality we should also check the nodeid */
  82. if (left_list_entries && left_list[0].pid == getpid()) {
  83. printf("We have left the building\n");
  84. exit(0);
  85. }
  86. }
  87. cpg_callbacks_t callbacks = {
  88. .cpg_deliver_fn = DeliverCallback,
  89. .cpg_confchg_fn = ConfchgCallback,
  90. };
  91. void sigintr_handler (int signum) {
  92. exit (0);
  93. }
  94. static struct cpg_name group_name;
  95. int main (int argc, char *argv[]) {
  96. cpg_handle_t handle;
  97. fd_set read_fds;
  98. int select_fd;
  99. int result;
  100. if (argc > 1) {
  101. strcpy(group_name.value, argv[1]);
  102. group_name.length = strlen(argv[1])+1;
  103. }
  104. else {
  105. strcpy(group_name.value, "GROUP");
  106. group_name.length = 6;
  107. }
  108. result = cpg_initialize (&handle, &callbacks);
  109. if (result != SA_AIS_OK) {
  110. printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
  111. exit (1);
  112. }
  113. result = cpg_join(handle, &group_name);
  114. if (result != SA_AIS_OK) {
  115. printf ("Could not join process group, error %d\n", result);
  116. exit (1);
  117. }
  118. FD_ZERO (&read_fds);
  119. cpg_fd_get(handle, &select_fd);
  120. printf ("Type EXIT to finish\n");
  121. do {
  122. FD_SET (select_fd, &read_fds);
  123. FD_SET (STDIN_FILENO, &read_fds);
  124. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  125. if (result == -1) {
  126. perror ("select\n");
  127. }
  128. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  129. char inbuf[132];
  130. struct iovec iov;
  131. fgets(inbuf, sizeof(inbuf), stdin);
  132. if (strncmp(inbuf, "EXIT", 4) == 0) {
  133. cpg_leave(handle, &group_name);
  134. }
  135. else {
  136. iov.iov_base = inbuf;
  137. iov.iov_len = strlen(inbuf)+1;
  138. cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1);
  139. }
  140. }
  141. if (FD_ISSET (select_fd, &read_fds)) {
  142. if (cpg_dispatch (handle, CPG_DISPATCH_ALL) != SA_AIS_OK)
  143. exit(1);
  144. }
  145. } while (result);
  146. result = cpg_finalize (handle);
  147. printf ("Finalize result is %d (should be 1)\n", result);
  148. return (0);
  149. }