icmap.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283
  1. /*
  2. * Copyright (c) 2011-2012 Red Hat, Inc.
  3. *
  4. * Author: Jan Friesse (jfriesse@redhat.com)
  5. *
  6. * All rights reserved.
  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 ICMAP_H_DEFINED
  35. #define ICMAP_H_DEFINED
  36. #include <stdlib.h>
  37. #include <corosync/corotypes.h>
  38. #include <qb/qbmap.h>
  39. #ifdef __cplusplus
  40. extern "C" {
  41. #endif
  42. /*
  43. * Maximum length of key in icmap
  44. */
  45. #define ICMAP_KEYNAME_MAXLEN 255
  46. /*
  47. * Minimum lenght of key in icmap
  48. */
  49. #define ICMAP_KEYNAME_MINLEN 3
  50. /*
  51. * Possible types of value. Binary is raw data without trailing zero with given length
  52. */
  53. typedef enum {
  54. ICMAP_VALUETYPE_INT8 = 1,
  55. ICMAP_VALUETYPE_UINT8 = 2,
  56. ICMAP_VALUETYPE_INT16 = 3,
  57. ICMAP_VALUETYPE_UINT16 = 4,
  58. ICMAP_VALUETYPE_INT32 = 5,
  59. ICMAP_VALUETYPE_UINT32 = 6,
  60. ICMAP_VALUETYPE_INT64 = 7,
  61. ICMAP_VALUETYPE_UINT64 = 8,
  62. ICMAP_VALUETYPE_FLOAT = 9,
  63. ICMAP_VALUETYPE_DOUBLE = 10,
  64. ICMAP_VALUETYPE_STRING = 11,
  65. ICMAP_VALUETYPE_BINARY = 12,
  66. } icmap_value_types_t;
  67. /*
  68. * Tracking values.
  69. */
  70. #define ICMAP_TRACK_ADD 4
  71. #define ICMAP_TRACK_DELETE 1
  72. #define ICMAP_TRACK_MODIFY 2
  73. /*
  74. * Whole prefix is tracked, instead of key only (so "totem." tracking means that
  75. * "totem.nodeid", "totem.version", ... applies). This value is also never returned
  76. * inside of callback and is used only in adding track
  77. */
  78. #define ICMAP_TRACK_PREFIX 8
  79. /*
  80. * Structure passed as new_value and old_value in change callback. It contains type of
  81. * key, length of key and pointer to value of key
  82. */
  83. struct icmap_notify_value {
  84. icmap_value_types_t type;
  85. size_t len;
  86. const void *data;
  87. };
  88. /*
  89. * Prototype for notify callback function. Even is one of ICMAP_TRACK_* event, key_name is
  90. * changed key, new and old_value contains values or are zeroed (in other words, type is non
  91. * existing 0 type) if there were no old (creating of key) or new (deleting of key) value.
  92. * user_data are passed when adding tracking.
  93. */
  94. typedef void (*icmap_notify_fn_t) (
  95. int32_t event,
  96. const char *key_name,
  97. struct icmap_notify_value new_value,
  98. struct icmap_notify_value old_value,
  99. void *user_data);
  100. /*
  101. * Itterator type
  102. */
  103. typedef qb_map_iter_t *icmap_iter_t;
  104. /*
  105. * Track type
  106. */
  107. typedef struct icmap_track *icmap_track_t;
  108. /*
  109. * Initialize icmap
  110. */
  111. extern cs_error_t icmap_init(void);
  112. extern void icmap_fini(void);
  113. /*
  114. * Store value with value_len length and type as key_name name in icmap.
  115. */
  116. extern cs_error_t icmap_set(
  117. const char *key_name,
  118. const void *value,
  119. size_t value_len,
  120. icmap_value_types_t type);
  121. /*
  122. * Shortcuts for setting values
  123. */
  124. extern cs_error_t icmap_set_int8(const char *key_name, int8_t value);
  125. extern cs_error_t icmap_set_uint8(const char *key_name, uint8_t value);
  126. extern cs_error_t icmap_set_int16(const char *key_name, int16_t value);
  127. extern cs_error_t icmap_set_uint16(const char *key_name, uint16_t value);
  128. extern cs_error_t icmap_set_int32(const char *key_name, int32_t value);
  129. extern cs_error_t icmap_set_uint32(const char *key_name, uint32_t value);
  130. extern cs_error_t icmap_set_int64(const char *key_name, int64_t value);
  131. extern cs_error_t icmap_set_uint64(const char *key_name, uint64_t value);
  132. extern cs_error_t icmap_set_float(const char *key_name, float value);
  133. extern cs_error_t icmap_set_double(const char *key_name, double value);
  134. extern cs_error_t icmap_set_string(const char *key_name, const char *value);
  135. /*
  136. * Delete key from map
  137. */
  138. extern cs_error_t icmap_delete(const char *key_name);
  139. /*
  140. * Retrieve value of key key_name and store it in user preallocated value pointer.
  141. * value can be NULL, and then only value_len and/or type is returned (both of them
  142. * can also be NULL). If value is not NULL, actual length of value in map is checked
  143. * against value_len. If *value_len is shorter then length of value in map, error
  144. * CS_ERR_INVALID_PARAM is returned. After successful copy of value, value_len is
  145. * set to actual length of value in map.
  146. */
  147. extern cs_error_t icmap_get(
  148. const char *key_name,
  149. void *value,
  150. size_t *value_len,
  151. icmap_value_types_t *type);
  152. /*
  153. * Shortcuts for icmap_get
  154. */
  155. extern cs_error_t icmap_get_int8(const char *key_name, int8_t *i8);
  156. extern cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8);
  157. extern cs_error_t icmap_get_int16(const char *key_name, int16_t *i16);
  158. extern cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16);
  159. extern cs_error_t icmap_get_int32(const char *key_name, int32_t *i32);
  160. extern cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32);
  161. extern cs_error_t icmap_get_int64(const char *key_name, int64_t *i64);
  162. extern cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64);
  163. extern cs_error_t icmap_get_float(const char *key_name, float *flt);
  164. extern cs_error_t icmap_get_double(const char *key_name, double *dbl);
  165. /*
  166. * Shortcut for icmap_get for string type. Returned string is newly allocated and
  167. * caller is responsible for freeing memory
  168. */
  169. extern cs_error_t icmap_get_string(const char *key_name, char **str);
  170. /*
  171. * Defined only for [u]int* values. It adds step to current value.
  172. */
  173. extern cs_error_t icmap_adjust_int(const char *key_name, int32_t step);
  174. /*
  175. * Defined only for [u]int* values. It adds step to current value. Difference
  176. * between this function and icmap_adjust_int is given in fact, that in
  177. * tracking callback, old value is undefined, but whole process is done
  178. * without malloc/memcpy.
  179. */
  180. extern cs_error_t icmap_fast_adjust_int(const char *key_name, int32_t step);
  181. /*
  182. * Increase stored value by one
  183. */
  184. extern cs_error_t icmap_inc(const char *key_name);
  185. /*
  186. * Decrease stored value by one
  187. */
  188. extern cs_error_t icmap_dec(const char *key_name);
  189. /*
  190. * Increase stored value by one. Difference between this function and icmap_inc
  191. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  192. */
  193. extern cs_error_t icmap_fast_inc(const char *key_name);
  194. /*
  195. * Decrease stored value by one. Difference between this function and icmap_dec
  196. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  197. */
  198. extern cs_error_t icmap_fast_dec(const char *key_name);
  199. /*
  200. * Initialize iterator with given prefix
  201. */
  202. extern icmap_iter_t icmap_iter_init(const char *prefix);
  203. /*
  204. * Return next item in iterator iter. value_len and type are optional (= can be NULL), but if set,
  205. * length of returned value and/or type is returned. Function returns following key_name or NULL if
  206. * iteration is over.
  207. */
  208. extern const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type);
  209. /*
  210. * Finalize iterator
  211. */
  212. extern void icmap_iter_finalize(icmap_iter_t iter);
  213. /*
  214. * Add tracking function for given key_name. Tracked changes (add|modify|delete) depend on track_type,
  215. * which is bitwise or of ICMAP_TRACK_* values. notify_fn is called on change, where user_data pointer
  216. * is passed (unchanged). Value which can be used to delete tracking is passed as icmap_track.
  217. */
  218. extern cs_error_t icmap_track_add(
  219. const char *key_name,
  220. int32_t track_type,
  221. icmap_notify_fn_t notify_fn,
  222. void *user_data,
  223. icmap_track_t *icmap_track);
  224. /*
  225. * Return user data associated with given track
  226. */
  227. extern void *icmap_track_get_user_data(icmap_track_t icmap_track);
  228. /*
  229. * Remove previously added track
  230. */
  231. extern cs_error_t icmap_track_delete(icmap_track_t icmap_track);
  232. /*
  233. * Set read-only access for given key (key_name) or prefix, if prefix is set. ro_access
  234. * can be !0, which means, that old information about ro of this key is deleted.
  235. * Read-only access is used only in CMAP service! (in other word it prevents users
  236. * from deleting/changing key, but doesn't guarantee anything for internal icmap users.
  237. */
  238. extern cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access);
  239. /*
  240. * Check in given key is read only. Returns !0 if so, otherwise (key is rw) 0.
  241. */
  242. extern int icmap_is_key_ro(const char *key_name);
  243. /*
  244. * Converts given key_name to valid key name (replacing all prohibited characters by _)
  245. */
  246. extern void icmap_convert_name_to_valid_name(char *key_name);
  247. #ifdef __cplusplus
  248. }
  249. #endif
  250. #endif /* ICMAP_H_DEFINED */