testquorum.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /*
  2. * Copyright (c) 2008, 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 Red Hat, 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 <pwd.h>
  36. #include <grp.h>
  37. #include <sys/types.h>
  38. #include <sys/poll.h>
  39. #include <sys/uio.h>
  40. #include <sys/mman.h>
  41. #include <sys/socket.h>
  42. #include <sys/un.h>
  43. #include <sys/time.h>
  44. #include <sys/resource.h>
  45. #include <netinet/in.h>
  46. #include <arpa/inet.h>
  47. #include <unistd.h>
  48. #include <fcntl.h>
  49. #include <stdlib.h>
  50. #include <stdio.h>
  51. #include <errno.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <corosync/corotypes.h>
  55. #include <qb/qbipc_common.h>
  56. #include <corosync/corodefs.h>
  57. #include <corosync/logsys.h>
  58. #include <corosync/icmap.h>
  59. #include <corosync/mar_gen.h>
  60. #include <corosync/lcr/lcr_comp.h>
  61. #include <corosync/coroapi.h>
  62. #include <corosync/engine/quorum.h>
  63. LOGSYS_DECLARE_SUBSYS ("TEST");
  64. static void test_init(struct corosync_api_v1 *api, quorum_set_quorate_fn_t report);
  65. /*
  66. * lcrso object definition
  67. */
  68. static struct quorum_services_api_ver1 test_quorum_iface_ver0 = {
  69. .init = test_init
  70. };
  71. static struct lcr_iface corosync_test_quorum_ver0[1] = {
  72. {
  73. .name = "testquorum",
  74. .version = 0,
  75. .versions_replace = 0,
  76. .versions_replace_count = 0,
  77. .dependencies = 0,
  78. .dependency_count = 0,
  79. .constructor = NULL,
  80. .destructor = NULL,
  81. .interfaces = (void **)(void *)&test_quorum_iface_ver0,
  82. },
  83. };
  84. static struct lcr_comp test_quorum_comp_ver0 = {
  85. .iface_count = 1,
  86. .ifaces = corosync_test_quorum_ver0
  87. };
  88. #ifdef COROSYNC_SOLARIS
  89. void corosync_lcr_component_register (void);
  90. void corosync_lcr_component_register (void) {
  91. logsys_subsys_init();
  92. #else
  93. __attribute__ ((constructor)) static void corosync_lcr_component_register (void) {
  94. #endif
  95. lcr_interfaces_set (&corosync_test_quorum_ver0[0], &test_quorum_iface_ver0);
  96. lcr_component_register (&test_quorum_comp_ver0);
  97. }
  98. /* -------------------------------------------------- */
  99. static quorum_set_quorate_fn_t set_quorum;
  100. static void key_change_notify(
  101. int32_t event,
  102. const char *key_name,
  103. struct icmap_notify_value new_val,
  104. struct icmap_notify_value old_val,
  105. void *user_data)
  106. {
  107. struct memb_ring_id ring_id;
  108. unsigned int members[1];
  109. uint8_t u8;
  110. memset(&ring_id, 0, sizeof(ring_id));
  111. if (icmap_get_uint8(key_name, &u8) == CS_OK) {
  112. set_quorum(members, 0, u8, &ring_id);
  113. }
  114. }
  115. static void quorum_callback(int quorate, void *context)
  116. {
  117. log_printf(LOGSYS_LEVEL_DEBUG, "quorum callback: quorate = %d\n", quorate);
  118. }
  119. static void test_init(struct corosync_api_v1 *api,
  120. quorum_set_quorate_fn_t report)
  121. {
  122. icmap_track_t icmap_track;
  123. set_quorum = report;
  124. /*
  125. * Register for icmap changes on quorum.quorate
  126. */
  127. icmap_track_add("quorum.quorate",
  128. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY,
  129. key_change_notify,
  130. NULL,
  131. &icmap_track);
  132. /* Register for quorum changes too! */
  133. api->quorum_register_callback(quorum_callback, NULL);
  134. }