cmap.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378
  1. /*
  2. * Copyright (c) 2011-2017 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_CMAP_H_DEFINED
  35. #define COROSYNC_CMAP_H_DEFINED
  36. #include <corosync/corotypes.h>
  37. #include <corosync/hdb.h>
  38. #ifdef __cplusplus
  39. extern "C" {
  40. #endif
  41. /**
  42. * @addtogroup cmap_corosync
  43. *
  44. * @{
  45. */
  46. /*
  47. * Handle for cmap service connection
  48. */
  49. typedef uint64_t cmap_handle_t;
  50. /*
  51. * Handle for cmap iterator
  52. */
  53. typedef uint64_t cmap_iter_handle_t;
  54. /*
  55. * Handle for cmap tracking function
  56. */
  57. typedef uint64_t cmap_track_handle_t;
  58. /*
  59. * Maximum length of key in cmap
  60. */
  61. #define CMAP_KEYNAME_MAXLEN 255
  62. /*
  63. * Minumum length of key in cmap
  64. */
  65. #define CMAP_KEYNAME_MINLEN 3
  66. /*
  67. * Tracking values.
  68. */
  69. #define CMAP_TRACK_ADD 4
  70. #define CMAP_TRACK_DELETE 1
  71. #define CMAP_TRACK_MODIFY 2
  72. /**
  73. * Whole prefix is tracked, instead of key only (so "totem." tracking means that
  74. * "totem.nodeid", "totem.version", ... applies). This value is also never returned
  75. * inside of callback and is used only in adding track
  76. */
  77. #define CMAP_TRACK_PREFIX 8
  78. /**
  79. * Possible types of value. Binary is raw data without trailing zero with given length
  80. */
  81. typedef enum {
  82. CMAP_VALUETYPE_INT8 = 1,
  83. CMAP_VALUETYPE_UINT8 = 2,
  84. CMAP_VALUETYPE_INT16 = 3,
  85. CMAP_VALUETYPE_UINT16 = 4,
  86. CMAP_VALUETYPE_INT32 = 5,
  87. CMAP_VALUETYPE_UINT32 = 6,
  88. CMAP_VALUETYPE_INT64 = 7,
  89. CMAP_VALUETYPE_UINT64 = 8,
  90. CMAP_VALUETYPE_FLOAT = 9,
  91. CMAP_VALUETYPE_DOUBLE = 10,
  92. CMAP_VALUETYPE_STRING = 11,
  93. CMAP_VALUETYPE_BINARY = 12,
  94. } cmap_value_types_t;
  95. typedef enum {
  96. CMAP_MAP_DEFAULT = 0,
  97. CMAP_MAP_ICMAP = 0,
  98. CMAP_MAP_STATS = 1,
  99. } cmap_map_t;
  100. /**
  101. * Structure passed as new_value and old_value in change callback. It contains type of
  102. * key, length of key and pointer to value of key
  103. */
  104. struct cmap_notify_value {
  105. cmap_value_types_t type;
  106. size_t len;
  107. const void *data;
  108. };
  109. /**
  110. * Prototype for notify callback function. Even is one of CMAP_TRACK_* event, key_name is
  111. * changed key, new and old_value contains values or are zeroed (in other words, type is non
  112. * existing 0 type) if there were no old (creating of key) or new (deleting of key) value.
  113. * user_data are passed when adding tracking.
  114. */
  115. typedef void (*cmap_notify_fn_t) (
  116. cmap_handle_t cmap_handle,
  117. cmap_track_handle_t cmap_track_handle,
  118. int32_t event,
  119. const char *key_name,
  120. struct cmap_notify_value new_value,
  121. struct cmap_notify_value old_value,
  122. void *user_data);
  123. /**
  124. * Create a new cmap connection
  125. *
  126. * @param handle will be filled with handle to be used for all following
  127. * operations with cht.
  128. */
  129. extern cs_error_t cmap_initialize (
  130. cmap_handle_t *handle);
  131. /**
  132. * Create a new cmap connection on a specified map
  133. *
  134. * @param handle will be filled with handle to be used for all following
  135. * operations with cht.
  136. * @param map is the 'map' to use for this connection
  137. */
  138. extern cs_error_t cmap_initialize_map (
  139. cmap_handle_t *handle,
  140. cmap_map_t map);
  141. /**
  142. * Close the cmap handle
  143. * @param handle cmap handle
  144. */
  145. extern cs_error_t cmap_finalize (
  146. cmap_handle_t handle);
  147. /**
  148. * Get a file descriptor on which to poll. cmap_handle_t is NOT a
  149. * file descriptor and may not be used directly.
  150. * @param handle cmap handle initialized by cmap_initialize
  151. * @param fd file descriptor for poll
  152. */
  153. extern cs_error_t cmap_fd_get (
  154. cmap_handle_t handle,
  155. int *fd);
  156. /**
  157. * Dispatch data from service.
  158. * @param handle cmap handle initialized by cmap_initialize
  159. * @param dispatch_types one of standard corosync dispatch values
  160. */
  161. extern cs_error_t cmap_dispatch (
  162. cmap_handle_t handle,
  163. cs_dispatch_flags_t dispatch_types);
  164. /**
  165. * @brief cmap_context_get
  166. * @param handle
  167. * @param context
  168. * @return
  169. */
  170. extern cs_error_t cmap_context_get (
  171. cmap_handle_t handle,
  172. const void **context);
  173. /**
  174. * @brief cmap_context_set
  175. * @param handle
  176. * @param context
  177. * @return
  178. */
  179. extern cs_error_t cmap_context_set (
  180. cmap_handle_t handle,
  181. const void *context);
  182. /**
  183. * Store value in cmap
  184. * @param handle cmap handle
  185. * @param key_name name of key where to store value
  186. * @param value value to store
  187. * @param value_len length of value to store
  188. * @param type type to store
  189. */
  190. extern cs_error_t cmap_set(
  191. cmap_handle_t handle,
  192. const char *key_name,
  193. const void *value,
  194. size_t value_len,
  195. cmap_value_types_t type);
  196. /*
  197. * Shortcuts for cmap_set with given type
  198. */
  199. extern cs_error_t cmap_set_int8(cmap_handle_t handle, const char *key_name, int8_t value);
  200. extern cs_error_t cmap_set_uint8(cmap_handle_t handle, const char *key_name, uint8_t value);
  201. extern cs_error_t cmap_set_int16(cmap_handle_t handle, const char *key_name, int16_t value);
  202. extern cs_error_t cmap_set_uint16(cmap_handle_t handle, const char *key_name, uint16_t value);
  203. extern cs_error_t cmap_set_int32(cmap_handle_t handle, const char *key_name, int32_t value);
  204. extern cs_error_t cmap_set_uint32(cmap_handle_t handle, const char *key_name, uint32_t value);
  205. extern cs_error_t cmap_set_int64(cmap_handle_t handle, const char *key_name, int64_t value);
  206. extern cs_error_t cmap_set_uint64(cmap_handle_t handle, const char *key_name, uint64_t value);
  207. extern cs_error_t cmap_set_float(cmap_handle_t handle, const char *key_name, float value);
  208. extern cs_error_t cmap_set_double(cmap_handle_t handle, const char *key_name, double value);
  209. extern cs_error_t cmap_set_string(cmap_handle_t handle, const char *key_name, const char *value);
  210. /**
  211. * Deletes key from cmap database
  212. * @param handle cmap handle
  213. * @param key_name name of key to delete
  214. */
  215. extern cs_error_t cmap_delete(cmap_handle_t handle, const char *key_name);
  216. /**
  217. * @brief Retrieve value of key key_name and store it in user preallocated value pointer.
  218. *
  219. * value can be NULL, and then only value_len and/or type is returned (both of them
  220. * can also be NULL). If value is not NULL, actual length of value in map is checked
  221. * against value_len. If *value_len is shorter then length of value in map, error
  222. * CS_ERR_INVALID_PARAM is returned. After successful copy of value, value_len is
  223. * set to actual length of value in map.
  224. *
  225. * @param handle cmap handle
  226. * @param key_name name of key where to get value
  227. * @param value pointer to store data (or NULL)
  228. * @param value_len pointer with length of value (value != NULL), or pointer where value length
  229. * will be returned (value == NULL) or NULL.
  230. * @param type type of value in cmap
  231. */
  232. extern cs_error_t cmap_get(
  233. cmap_handle_t handle,
  234. const char *key_name,
  235. void *value,
  236. size_t *value_len,
  237. cmap_value_types_t *type);
  238. /*
  239. * Shortcuts for cmap_get.
  240. */
  241. extern cs_error_t cmap_get_int8(cmap_handle_t handle, const char *key_name, int8_t *i8);
  242. extern cs_error_t cmap_get_uint8(cmap_handle_t handle, const char *key_name, uint8_t *u8);
  243. extern cs_error_t cmap_get_int16(cmap_handle_t handle, const char *key_name, int16_t *i16);
  244. extern cs_error_t cmap_get_uint16(cmap_handle_t handle, const char *key_name, uint16_t *u16);
  245. extern cs_error_t cmap_get_int32(cmap_handle_t handle, const char *key_name, int32_t *i32);
  246. extern cs_error_t cmap_get_uint32(cmap_handle_t handle, const char *key_name, uint32_t *u32);
  247. extern cs_error_t cmap_get_int64(cmap_handle_t handle, const char *key_name, int64_t *i64);
  248. extern cs_error_t cmap_get_uint64(cmap_handle_t handle, const char *key_name, uint64_t *u64);
  249. extern cs_error_t cmap_get_float(cmap_handle_t handle, const char *key_name, float *flt);
  250. extern cs_error_t cmap_get_double(cmap_handle_t handle, const char *key_name, double *dbl);
  251. /**
  252. * @brief Shortcut for cmap_get for string type.
  253. *
  254. * Returned string is newly allocated and caller is responsible for freeing memory
  255. *
  256. * @param handle cmap handle
  257. * @param key_name name of key to get value from
  258. * @param str pointer where char pointer will be stored
  259. */
  260. extern cs_error_t cmap_get_string(cmap_handle_t handle, const char *key_name, char **str);
  261. /**
  262. * @brief Increment value of key_name if it is [u]int* type
  263. *
  264. * @param handle cmap handle
  265. * @param key_name key name
  266. */
  267. extern cs_error_t cmap_inc(cmap_handle_t handle, const char *key_name);
  268. /**
  269. * @brief Decrement value of key_name if it is [u]int* type
  270. *
  271. * @param handle cmap handle
  272. * @param key_name key name
  273. */
  274. extern cs_error_t cmap_dec(cmap_handle_t handle, const char *key_name);
  275. /**
  276. * @brief Initialize iterator with given prefix
  277. *
  278. * @param handle cmap handle
  279. * @param prefix prefix to iterate on
  280. * @param cmap_iter_handle value used for getting next value of iterator and/or deleting iteration
  281. */
  282. extern cs_error_t cmap_iter_init(cmap_handle_t handle, const char *prefix, cmap_iter_handle_t *cmap_iter_handle);
  283. /**
  284. * @brief Return next item in iterator iter.
  285. *
  286. * value_len and type are optional (= can be NULL), but if set,
  287. * length of returned value and/or type is returned.
  288. *
  289. * @param handle cmap handle
  290. * @param iter_handle handle of iteration returned by cmap_iter_init
  291. * @param key_name place to store name of key. Maximum length is CMAP_KEYNAME_MAXLEN and
  292. * trailing zero is always added so size of the buffer has to be at least
  293. * CMAP_KEYNAME_MAXLEN + 1.
  294. * @param value_len length of value
  295. * @param type type of value
  296. * @return CS_NO_SECTION if there are no more sections to iterate
  297. */
  298. extern cs_error_t cmap_iter_next(
  299. cmap_handle_t handle,
  300. cmap_iter_handle_t iter_handle,
  301. char key_name[],
  302. size_t *value_len,
  303. cmap_value_types_t *type);
  304. /**
  305. * @brief Finalize iterator
  306. * @param handle
  307. * @param iter_handle
  308. * @return
  309. */
  310. extern cs_error_t cmap_iter_finalize(cmap_handle_t handle, cmap_iter_handle_t iter_handle);
  311. /**
  312. * @brief Add tracking function for given key_name.
  313. *
  314. * Tracked changes (add|modify|delete) depend on track_type,
  315. * which is bitwise or of CMAP_TRACK_* values. notify_fn is called on change, where user_data pointer
  316. * is passed (unchanged). Value which can be used to delete tracking is passed as cmap_track.
  317. *
  318. * @param handle cmap handle
  319. * @param key_name name of key to track changes on
  320. * @param track_type bitwise-or of CMAP_TRACK_* values
  321. * @param notify_fn function to be called on change of key
  322. * @param user_data given pointer is unchanged passed to notify_fn
  323. * @param cmap_track_handle handle used for removing of newly created track
  324. */
  325. extern cs_error_t cmap_track_add(
  326. cmap_handle_t handle,
  327. const char *key_name,
  328. int32_t track_type,
  329. cmap_notify_fn_t notify_fn,
  330. void *user_data,
  331. cmap_track_handle_t *cmap_track_handle);
  332. /**
  333. * Delete track created previously by cmap_track_add
  334. * @param handle cmap handle
  335. * @param track_handle Track handle
  336. */
  337. extern cs_error_t cmap_track_delete(cmap_handle_t handle, cmap_track_handle_t track_handle);
  338. /** @} */
  339. #ifdef __cplusplus
  340. }
  341. #endif
  342. #endif /* COROSYNC_CMAP_H_DEFINED */