sam.h 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Copyright (c) 2009 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@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 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. #ifndef COROSYNC_SAM_H_DEFINED
  35. #define COROSYNC_SAM_H_DEFINED
  36. #include <corosync/corotypes.h>
  37. #ifdef __cplusplus
  38. extern "C" {
  39. #endif
  40. typedef enum {
  41. SAM_RECOVERY_POLICY_QUIT = 1,
  42. SAM_RECOVERY_POLICY_RESTART = 2,
  43. } sam_recovery_policy_t;
  44. /*
  45. * Callback definition for event driven checking
  46. */
  47. typedef int (*sam_hc_callback_t)(void);
  48. /*
  49. * Create a new SAM connection. This function must be called before any other.
  50. * It is recommended to call it as one of first in application.
  51. *
  52. * @param time_interval Time interval in miliseconds of healthcheck. After this time, application
  53. * will be killed and recovery policy will be taken. This can be zero, which means,
  54. * that there is no time limit (only fall of application is checked and only then
  55. * recovery action is taken)
  56. * @param recovery_policy One of SAM_RECOVERY_POLICY_RESTART, which means, that after
  57. * timeout application will be killed and new instance will be started.
  58. * SAM_RECOVERY_POLICY_QUIT will just stop application
  59. * @return
  60. * - CS_OK in case no problem appeared
  61. * - CS_ERR_BAD_HANDLE in case user is trying to initialize initialized instance
  62. * - CS_ERR_INVALID_PARAM in case recovery_policy had bad value
  63. */
  64. cs_error_t sam_initialize (
  65. int time_interval,
  66. sam_recovery_policy_t recovery_policy);
  67. /*
  68. * Close the SAM handle. This function should be called as late as possible
  69. * (in reality, if you plan just quit, and checking is stopped, there is no need
  70. * to call it). Function will stop healtchecking and put library to state, where
  71. * no new start is possible.
  72. *
  73. * @return
  74. * - CS_OK in case no problem appeared
  75. * - CS_ERR_BAD_HANDLE library was not initialized by #sam_initialize
  76. */
  77. cs_error_t sam_finalize (void);
  78. /*
  79. * Start healthchecking. From this time, you should call every time_interval
  80. * sam_hc_send, otherwise, recovery action will be taken.
  81. * @return
  82. * - CS_OK in case no problem appeared
  83. * - CS_ERR_BAD_HANDLE component was not registered by #sam_register
  84. */
  85. cs_error_t sam_start (void);
  86. /*
  87. * Stop healthchecking. Oposite of #sam_start. You can call sam_start and
  88. * sam_stop how many times you want.
  89. *
  90. * @return
  91. * - CS_OK in case no problem appeared
  92. * - CS_ERR_BAD_HANDLE healthchecking is not in running state (no sam_start
  93. * was called)
  94. */
  95. cs_error_t sam_stop (void);
  96. /*
  97. * Register application. This is one of most crucial function. In case, your
  98. * application will be restarted, you will always return to point after calling
  99. * this function. This function can be called only once, and SAM must be initialized
  100. * by sam_initialize. You can choose any place in your application, where to call
  101. * this function.
  102. *
  103. * @param instance_id NULL or pointer to int memory, where current instance
  104. * of application will be returned. It's always safe to suppose, that first instance
  105. * (this means, no recovery action was taken yet) will be always 1 and instance_id
  106. * will be raising up to MAX_INT (after this, it will fall to 0).
  107. * @return
  108. * - CS_OK in case no problem appeared
  109. * - CS_ERR_BAD_HANDLE in case, you call this function twice, or before sam_init
  110. * - CS_ERR_LIBRARY internal library call failed. This can be one of pipe or fork
  111. * creation. You can get more information from errno
  112. */
  113. cs_error_t sam_register (
  114. unsigned int *instance_id);
  115. /*
  116. * Send healthcheck confirmation. This should be called after #sam_start
  117. *
  118. * - CS_OK in case no problem appeared
  119. * - CS_ERR_BAD_HANDLE healthchecking is not in running state (no sam_start was
  120. * called, or called after sam_stop/sam_finalize)
  121. */
  122. cs_error_t sam_hc_send (void);
  123. /*
  124. * Register healtcheck callback. After you will call this function, and set
  125. * cb to something else then NULL, SAM is automatically switched from
  126. * application driven healtchecking to event driven healtchecking. In other
  127. * words, is not longer needed to call sam_hc_send, but your callback function
  128. * must return 0 in case of healtchecking is correct, or value different then
  129. * 0, in case something happend. After next hc iteration, warning signal and
  130. * after that kill signal is sent back to your application.
  131. * @param cb Pointer to healtcheck function, or NULL to switch back to application driven hc
  132. * @return
  133. * - CS_OK in case no problem appeared
  134. * - CS_ERR_BAD_HANDLE in case, you call this function before sam_init or after sam_start
  135. * - CS_ERR_LIBRARY internal library call failed. This can be one of pipe or pthread
  136. * creation.
  137. */
  138. cs_error_t sam_hc_callback_register (sam_hc_callback_t cb);
  139. #ifdef __cplusplus
  140. }
  141. #endif
  142. #endif /* COROSYNC_SAM_H_DEFINED */