sam.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250
  1. /*
  2. * Copyright (c) 2009-2011 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. /**
  41. * @brief sam_recovery_policy_t enum
  42. */
  43. typedef enum {
  44. SAM_RECOVERY_POLICY_QUIT = 1,
  45. SAM_RECOVERY_POLICY_RESTART = 2,
  46. SAM_RECOVERY_POLICY_QUORUM = 0x08,
  47. SAM_RECOVERY_POLICY_QUORUM_QUIT = SAM_RECOVERY_POLICY_QUORUM | SAM_RECOVERY_POLICY_QUIT,
  48. SAM_RECOVERY_POLICY_QUORUM_RESTART = SAM_RECOVERY_POLICY_QUORUM | SAM_RECOVERY_POLICY_RESTART,
  49. SAM_RECOVERY_POLICY_CMAP = 0x10,
  50. SAM_RECOVERY_POLICY_CONFDB = 0x10,
  51. } sam_recovery_policy_t;
  52. /**
  53. * @brief Callback definition for event driven checking
  54. */
  55. typedef int (*sam_hc_callback_t)(void);
  56. /**
  57. * @brief Create a new SAM connection.
  58. *
  59. * This function must be called before any other.
  60. * It is recommended to call it as one of first in application.
  61. *
  62. * @param time_interval Time interval in milliseconds of healthcheck. After this time, application
  63. * will be killed and recovery policy will be taken. This can be zero, which means,
  64. * that there is no time limit (only fall of application is checked and only then
  65. * recovery action is taken)
  66. * @param recovery_policy One of SAM_RECOVERY_POLICY_RESTART, which means, that after
  67. * timeout application will be killed and new instance will be started.
  68. * SAM_RECOVERY_POLICY_QUIT will just stop application
  69. *
  70. * @retval CS_OK in case no problem appeared
  71. * @retval CS_ERR_BAD_HANDLE in case user is trying to initialize initialized instance
  72. * @retval CS_ERR_INVALID_PARAM in case recovery_policy had bad value
  73. */
  74. cs_error_t sam_initialize (
  75. int time_interval,
  76. sam_recovery_policy_t recovery_policy);
  77. /**
  78. * @brief Close the SAM handle.
  79. *
  80. * This function should be called as late as possible.
  81. * (in reality, if you plan just quit, and checking is stopped, there is no need
  82. * to call it). Function will stop healtchecking and put library to state, where
  83. * no new start is possible.
  84. *
  85. * @retval CS_OK in case no problem appeared
  86. * @retval CS_ERR_BAD_HANDLE library was not initialized by #sam_initialize
  87. */
  88. cs_error_t sam_finalize (void);
  89. /**
  90. * @brief Start healthchecking.
  91. *
  92. * From this time, you should call every time_interval
  93. * sam_hc_send, otherwise, recovery action will be taken.
  94. *
  95. * @retval CS_OK in case no problem appeared
  96. * @retval CS_ERR_BAD_HANDLE component was not registered by #sam_register
  97. */
  98. cs_error_t sam_start (void);
  99. /**
  100. * @brief Stop healthchecking.
  101. *
  102. * Oposite of #sam_start. You can call sam_start and sam_stop how many
  103. * times you want.
  104. *
  105. * @retval CS_OK in case no problem appeared
  106. * @retval CS_ERR_BAD_HANDLE healthchecking is not in running state (no sam_start
  107. * was called)
  108. */
  109. cs_error_t sam_stop (void);
  110. /**
  111. * @brief Set warning signal to be sent.
  112. *
  113. * Default signal is SIGTERM. You can use SIGKILL to emulate NOT sending
  114. * warning signal and just send SIGKILL.
  115. *
  116. * @retval CS_OK in case no problem appeared
  117. * @retval CS_ERR_BAD_HANDLE library was not initialized by #sam_initialize or
  118. * is finalized
  119. */
  120. cs_error_t sam_warn_signal_set (int warn_signal);
  121. /**
  122. * @brief Register application.
  123. *
  124. * This is one of most crucial function. In case, your
  125. * application will be restarted, you will always return to point after calling
  126. * this function. This function can be called only once, and SAM must be initialized
  127. * by sam_initialize. You can choose any place in your application, where to call
  128. * this function.
  129. *
  130. * @param instance_id NULL or pointer to int memory, where current instance
  131. * of application will be returned. It's always safe to suppose, that first instance
  132. * (this means, no recovery action was taken yet) will be always 1 and instance_id
  133. * will be raising up to MAX_INT (after this, it will fall to 0).
  134. *
  135. * @retval CS_OK in case no problem appeared
  136. * @retval CS_ERR_BAD_HANDLE in case, you call this function twice, or before sam_init
  137. * @retval CS_ERR_LIBRARY internal library call failed. This can be one of pipe or fork
  138. * creation. You can get more information from errno
  139. */
  140. cs_error_t sam_register (
  141. unsigned int *instance_id);
  142. /**
  143. * @brief Send healthcheck confirmation.
  144. *
  145. * This should be called after #sam_start
  146. *
  147. * @retval CS_OK in case no problem appeared
  148. * @retval CS_ERR_BAD_HANDLE healthchecking is not in running state (no sam_start was
  149. * called, or called after sam_stop/sam_finalize)
  150. */
  151. cs_error_t sam_hc_send (void);
  152. /**
  153. * @brief Register healtcheck callback.
  154. *
  155. * After you will call this function, and set
  156. * cb to something else then NULL, SAM is automatically switched from
  157. * application driven healtchecking to event driven healtchecking. In other
  158. * words, is not longer needed to call sam_hc_send, but your callback function
  159. * must return 0 in case of healtchecking is correct, or value different then
  160. * 0, in case something happend. After next hc iteration, warning signal and
  161. * after that kill signal is sent back to your application.
  162. *
  163. * @param cb Pointer to healtcheck function, or NULL to switch back to application driven hc
  164. *
  165. * @retval CS_OK in case no problem appeared
  166. * @retval CS_ERR_BAD_HANDLE in case, you call this function before sam_init or after sam_start
  167. * @retval CS_ERR_LIBRARY internal library call failed. This can be one of pipe or pthread
  168. * creation.
  169. */
  170. cs_error_t sam_hc_callback_register (sam_hc_callback_t cb);
  171. /**
  172. * @brief Return size of stored data.
  173. *
  174. * @param size Pointer to variable, where stored data size is returned. If
  175. * nothing or NULL is stored, then 0 is returned.
  176. *
  177. * @retval CS_OK in case no problem appeared
  178. * @retval CS_ERR_BAD_HANDLE in case you call this function before sam_init or after
  179. * sam_finalize
  180. * @retval CS_ERR_INVALID_PARAM if size parameter is NULL
  181. */
  182. cs_error_t sam_data_getsize (size_t *size);
  183. /**
  184. * @brief Return stored data.
  185. *
  186. * @param data Pointer to place, where to store data
  187. * @param size Allocated size of data
  188. *
  189. * @retval CS_OK if no problem appeared
  190. * @retval CS_ERR_BAD_HANDLE if you call this function before sam_init or after sam_finalize
  191. * @retval CS_ERR_INVALID_PARAM if data is NULL or size is less then currently saved user data length
  192. */
  193. cs_error_t sam_data_restore (
  194. void *data,
  195. size_t size);
  196. /**
  197. * @brief Store user data.
  198. *
  199. * Such stored data survives restart of child.
  200. *
  201. * @param data Data to store. You can use NULL to delete data
  202. * @param size Size of data to store.
  203. *
  204. * @retval CS_OK in case no problem appeared
  205. * @retval CS_ERR_BAD_HANDLE if you call this function before sam_init or
  206. * after sam_finalize
  207. * @retval CS_ERR_NO_MEMORY if data is too large and malloc/realloc was not
  208. * succesfull
  209. * @retval CS_ERR_LIBRARY if some internal error appeared (communication with parent
  210. * process)
  211. */
  212. cs_error_t sam_data_store (
  213. const void *data,
  214. size_t size);
  215. /**
  216. * @brief Marks child as failed.
  217. *
  218. * This can be called only with SAM_RECOVERY_POLICY_CMAP flag set and
  219. * makes sense only for SAM_RECOVERY_POLICY_RESTART. This will kill child without sending warning
  220. * signal. Cmap state key will be set to failed.
  221. *
  222. * @retval CS_OK in case no problem appeared
  223. * @retval CS_ERR_BAD_HANDLE library was not initialized or was already finalized
  224. * @retval CS_ERR_INVALID_PARAM recovery policy doesn't have SAM_RECOVERY_POLICY_CMAP flag set
  225. * @retval CS_ERR_LIBRARY if some internal error appeared (communication with parent
  226. * process)
  227. */
  228. cs_error_t sam_mark_failed (void);
  229. #ifdef __cplusplus
  230. }
  231. #endif
  232. #endif /* COROSYNC_SAM_H_DEFINED */