testcpg.c 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305
  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 <inttypes.h>
  36. #include <stdio.h>
  37. #include <stdlib.h>
  38. #include <errno.h>
  39. #include <unistd.h>
  40. #include <string.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. #include <corosync/swab.h>
  50. #include <qb/qblog.h>
  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 char * node_pid_format(unsigned int nodeid,int pid) {
  61. static char buffer[100];
  62. if (show_ip) {
  63. struct in_addr saddr;
  64. #if __BYTE_ORDER == __BIG_ENDIAN
  65. saddr.s_addr = swab32(nodeid);
  66. #else
  67. saddr.s_addr = nodeid;
  68. #endif
  69. sprintf(buffer, "node/pid %s/%d", inet_ntoa(saddr),pid);
  70. }
  71. else {
  72. sprintf(buffer, "node/pid %d/%d", nodeid, pid);
  73. }
  74. return buffer;
  75. }
  76. static void DeliverCallback (
  77. cpg_handle_t handle,
  78. const struct cpg_name *groupName,
  79. uint32_t nodeid,
  80. uint32_t pid,
  81. void *msg,
  82. size_t msg_len)
  83. {
  84. printf("DeliverCallback: message (len=%lu)from %s: '%s'\n",
  85. (unsigned long int) msg_len, node_pid_format(nodeid, pid),
  86. (const char *)msg);
  87. }
  88. static void ConfchgCallback (
  89. cpg_handle_t handle,
  90. const struct cpg_name *groupName,
  91. const struct cpg_address *member_list, size_t member_list_entries,
  92. const struct cpg_address *left_list, size_t left_list_entries,
  93. const struct cpg_address *joined_list, size_t joined_list_entries)
  94. {
  95. int i;
  96. printf("\nConfchgCallback: group '");
  97. print_cpgname(groupName);
  98. printf("'\n");
  99. for (i=0; i<joined_list_entries; i++) {
  100. printf("joined %s reason: %d\n",
  101. node_pid_format(joined_list[i].nodeid, joined_list[i].pid),
  102. joined_list[i].reason);
  103. }
  104. for (i=0; i<left_list_entries; i++) {
  105. printf("left %s reason: %d\n",
  106. node_pid_format(left_list[i].nodeid, left_list[i].pid),
  107. left_list[i].reason);
  108. }
  109. printf("nodes in group now %lu\n",
  110. (unsigned long int) member_list_entries);
  111. for (i=0; i<member_list_entries; i++) {
  112. printf("%s\n",
  113. node_pid_format(member_list[i].nodeid, member_list[i].pid));
  114. }
  115. /* Is it us??
  116. NOTE: in reality we should also check the nodeid */
  117. if (left_list_entries && left_list[0].pid == getpid()) {
  118. printf("We have left the building\n");
  119. quit = 1;
  120. }
  121. }
  122. static void TotemConfchgCallback (
  123. cpg_handle_t handle,
  124. struct cpg_ring_id ring_id,
  125. uint32_t member_list_entries,
  126. const uint32_t *member_list)
  127. {
  128. int i;
  129. printf ("\nTotemConfchgCallback: ringid (%u.%"PRIu64")\n",
  130. ring_id.nodeid, ring_id.seq);
  131. printf("active processors %lu: ",
  132. (unsigned long int) member_list_entries);
  133. for (i=0; i<member_list_entries; i++) {
  134. printf("%d ", member_list[i]);
  135. }
  136. printf ("\n");
  137. }
  138. static cpg_model_v1_data_t model_data = {
  139. .cpg_deliver_fn = DeliverCallback,
  140. .cpg_confchg_fn = ConfchgCallback,
  141. .cpg_totem_confchg_fn = TotemConfchgCallback,
  142. .flags = CPG_MODEL_V1_DELIVER_INITIAL_TOTEM_CONF,
  143. };
  144. static void sigintr_handler (int signum) __attribute__((noreturn));
  145. static void sigintr_handler (int signum) {
  146. exit (0);
  147. }
  148. static struct cpg_name group_name;
  149. #define cs_repeat_init(counter, max, code) do { \
  150. code; \
  151. if (result == CS_ERR_TRY_AGAIN || result == CS_ERR_QUEUE_FULL || result == CS_ERR_LIBRARY) { \
  152. counter++; \
  153. printf("Retrying operation after %ds\n", counter); \
  154. sleep(counter); \
  155. } else { \
  156. break; \
  157. } \
  158. } while (counter < max)
  159. #define cs_repeat(counter, max, code) do { \
  160. code; \
  161. if (result == CS_ERR_TRY_AGAIN || result == CS_ERR_QUEUE_FULL) { \
  162. counter++; \
  163. printf("Retrying operation after %ds\n", counter); \
  164. sleep(counter); \
  165. } else { \
  166. break; \
  167. } \
  168. } while (counter < max)
  169. int main (int argc, char *argv[]) {
  170. cpg_handle_t handle;
  171. fd_set read_fds;
  172. int select_fd;
  173. int result;
  174. int retries;
  175. const char *options = "i";
  176. int opt;
  177. unsigned int nodeid;
  178. char *fgets_res;
  179. struct cpg_address member_list[64];
  180. int member_list_entries;
  181. int i;
  182. qb_log_init("testcpg", LOG_USER, LOG_ERR);
  183. qb_log_ctl(QB_LOG_SYSLOG, QB_LOG_CONF_ENABLED, QB_FALSE);
  184. qb_log_filter_ctl(QB_LOG_STDERR, QB_LOG_FILTER_ADD,
  185. QB_LOG_FILTER_FILE, "*", LOG_TRACE);
  186. qb_log_ctl(QB_LOG_STDERR, QB_LOG_CONF_ENABLED, QB_TRUE);
  187. qb_log_format_set(QB_LOG_STDERR, "[%p] %f %b");
  188. while ( (opt = getopt(argc, argv, options)) != -1 ) {
  189. switch (opt) {
  190. case 'i':
  191. show_ip = 1;
  192. break;
  193. }
  194. }
  195. if (argc > optind) {
  196. strcpy(group_name.value, argv[optind]);
  197. group_name.length = strlen(argv[optind]);
  198. }
  199. else {
  200. strcpy(group_name.value, "GROUP");
  201. group_name.length = 6;
  202. }
  203. retries = 0;
  204. cs_repeat_init(retries, 30, result = cpg_model_initialize (&handle, CPG_MODEL_V1, (cpg_model_data_t *)&model_data, NULL));
  205. if (result != CS_OK) {
  206. printf ("Could not initialize Cluster Process Group API instance error %d\n", result);
  207. exit (1);
  208. }
  209. retries = 0;
  210. cs_repeat(retries, 30, result = cpg_local_get(handle, &nodeid));
  211. if (result != CS_OK) {
  212. printf ("Could not get local node id\n");
  213. exit (1);
  214. }
  215. printf ("Local node id is %x\n", nodeid);
  216. retries = 0;
  217. cs_repeat(retries, 30, result = cpg_join(handle, &group_name));
  218. if (result != CS_OK) {
  219. printf ("Could not join process group, error %d\n", result);
  220. exit (1);
  221. }
  222. retries = 0;
  223. cs_repeat(retries, 30, result = cpg_membership_get (handle, &group_name,
  224. (struct cpg_address *)&member_list, &member_list_entries));
  225. if (result != CS_OK) {
  226. printf ("Could not get current membership list %d\n", result);
  227. exit (1);
  228. }
  229. printf ("membership list\n");
  230. for (i = 0; i < member_list_entries; i++) {
  231. printf ("node id %d pid %d\n", member_list[i].nodeid,
  232. member_list[i].pid);
  233. }
  234. FD_ZERO (&read_fds);
  235. cpg_fd_get(handle, &select_fd);
  236. printf ("Type EXIT to finish\n");
  237. do {
  238. FD_SET (select_fd, &read_fds);
  239. FD_SET (STDIN_FILENO, &read_fds);
  240. result = select (select_fd + 1, &read_fds, 0, 0, 0);
  241. if (result == -1) {
  242. perror ("select\n");
  243. }
  244. if (FD_ISSET (STDIN_FILENO, &read_fds)) {
  245. char inbuf[132];
  246. struct iovec iov;
  247. fgets_res = fgets(inbuf, sizeof(inbuf), stdin);
  248. if (fgets_res == NULL) {
  249. cpg_leave(handle, &group_name);
  250. }
  251. if (strncmp(inbuf, "EXIT", 4) == 0) {
  252. cpg_leave(handle, &group_name);
  253. }
  254. else {
  255. iov.iov_base = inbuf;
  256. iov.iov_len = strlen(inbuf)+1;
  257. cpg_mcast_joined(handle, CPG_TYPE_AGREED, &iov, 1);
  258. }
  259. }
  260. if (FD_ISSET (select_fd, &read_fds)) {
  261. if (cpg_dispatch (handle, CS_DISPATCH_ALL) != CS_OK)
  262. exit(1);
  263. }
  264. } while (result && !quit);
  265. result = cpg_finalize (handle);
  266. printf ("Finalize result is %d (should be 1)\n", result);
  267. return (0);
  268. }