testcfg.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Copyright (c) 2021 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 <errno.h>
  38. #include <pthread.h>
  39. #include <corosync/corotypes.h>
  40. #include <corosync/cfg.h>
  41. static void shutdown_callback(corosync_cfg_handle_t handle, corosync_cfg_shutdown_flags_t flags)
  42. {
  43. /* Prevent shutdown */
  44. printf("In shutdown callback - denying corosync shutdown\n");
  45. corosync_cfg_replyto_shutdown(handle, COROSYNC_CFG_SHUTDOWN_FLAG_NO);
  46. }
  47. static void *dispatch_thread(void *arg)
  48. {
  49. corosync_cfg_handle_t handle = (corosync_cfg_handle_t) arg;
  50. int res = CS_OK;
  51. while (res == CS_OK) {
  52. res = corosync_cfg_dispatch(handle, CS_DISPATCH_ONE);
  53. }
  54. fprintf(stderr, "ERROR: Corosync shut down\n");
  55. return (void*)0;
  56. }
  57. int main (int argc, char *argv[]) {
  58. corosync_cfg_handle_t cfg_handle;
  59. corosync_cfg_handle_t cfg_handle1;
  60. unsigned int local_nodeid;
  61. int i;
  62. int res;
  63. struct corosync_cfg_node_status_v1 ns;
  64. pthread_t thread;
  65. corosync_cfg_callbacks_t callbacks = {
  66. .corosync_cfg_shutdown_callback = shutdown_callback
  67. };
  68. res = corosync_cfg_initialize(&cfg_handle, &callbacks);
  69. if (res != CS_OK) {
  70. fprintf(stderr, "corosync_Cfg_initialize(0) failed: %d\n", res);
  71. return 1;
  72. }
  73. /* Start a new handle & thread to prevent the shutdown we request later on */
  74. res = corosync_cfg_initialize(&cfg_handle1, &callbacks);
  75. if (res != CS_OK) {
  76. fprintf(stderr, "corosync_cfg_initialize(1) failed: %d\n", res);
  77. return 1;
  78. }
  79. res = corosync_cfg_trackstart(cfg_handle1, 0);
  80. if (res != CS_OK) {
  81. fprintf(stderr, "corosync_cfg_initialize(1) failed: %d\n", res);
  82. return 1;
  83. }
  84. res = pthread_create(&thread, NULL, dispatch_thread, (void*)cfg_handle1);
  85. if (res != 0) {
  86. perror("pthread_create failed");
  87. return 1;
  88. }
  89. /* Exercise a few functions */
  90. res = corosync_cfg_local_get(cfg_handle, &local_nodeid);
  91. if (res != CS_OK) {
  92. fprintf(stderr, "corosync_cfg_local_get failed: %d\n", res);
  93. return 1;
  94. }
  95. printf("Local nodeid is %d\n", local_nodeid);
  96. /*
  97. * Test node_status_get.
  98. * node status for the local node looks odd (cos it's the loopback connection), so
  99. * we try for a node ID one less or more than us just to get output that looks
  100. * sensible to the user.
  101. */
  102. res = corosync_cfg_node_status_get(cfg_handle, local_nodeid-1, CFG_NODE_STATUS_V1, &ns);
  103. if (res != CS_OK) {
  104. res = corosync_cfg_node_status_get(cfg_handle, local_nodeid+1, CFG_NODE_STATUS_V1, &ns);
  105. }
  106. if (res != CS_OK) {
  107. fprintf(stderr, "corosync_cfg_node_status_get failed: %d\n", res);
  108. return 1;
  109. }
  110. printf("Node Status for nodeid %d\n", ns.nodeid);
  111. printf(" reachable: %d\n", ns.reachable);
  112. printf(" remote: %d\n", ns.remote);
  113. printf(" onwire_min: %d\n", ns.onwire_min);
  114. printf(" onwire_max: %d\n", ns.onwire_max);
  115. printf(" onwire_ver: %d\n", ns.onwire_ver);
  116. for (i = 0; i<CFG_MAX_LINKS; i++) {
  117. if (ns.link_status[i].enabled) {
  118. printf(" Link %d\n", i);
  119. printf(" connected: %d\n", ns.link_status[i].connected);
  120. printf(" mtu: %d\n", ns.link_status[i].mtu);
  121. printf(" src: %s\n", ns.link_status[i].src_ipaddr);
  122. printf(" dst: %s\n", ns.link_status[i].dst_ipaddr);
  123. }
  124. }
  125. /* This shutdown request should be denied by the thread */
  126. res = corosync_cfg_try_shutdown(cfg_handle, COROSYNC_CFG_SHUTDOWN_FLAG_REQUEST);
  127. if (res == CS_OK) {
  128. fprintf(stderr, "ERROR: corosync_cfg_try_shutdown suceeded. should have been prevented\n");
  129. }
  130. if (res != CS_ERR_BUSY && res != CS_OK) {
  131. fprintf(stderr, "corosync_cfg_try_shutdown failed: %d\n", res);
  132. return 1;
  133. }
  134. /*
  135. * Test bug presented in 3.1.1 and 3.1.2 that makes trackstop blocks forever
  136. */
  137. res = corosync_cfg_trackstop(cfg_handle1);
  138. if (res != CS_OK) {
  139. fprintf(stderr, "corosync_cfg_trackstop failed: %d\n", res);
  140. return 1;
  141. }
  142. return 0;
  143. }