icmap.h 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282
  1. /*
  2. * Copyright (c) 2011 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. /*
  113. * Store value with value_len length and type as key_name name in icmap.
  114. */
  115. extern cs_error_t icmap_set(
  116. const char *key_name,
  117. const void *value,
  118. size_t value_len,
  119. icmap_value_types_t type);
  120. /*
  121. * Shortcuts for setting values
  122. */
  123. extern cs_error_t icmap_set_int8(const char *key_name, int8_t value);
  124. extern cs_error_t icmap_set_uint8(const char *key_name, uint8_t value);
  125. extern cs_error_t icmap_set_int16(const char *key_name, int16_t value);
  126. extern cs_error_t icmap_set_uint16(const char *key_name, uint16_t value);
  127. extern cs_error_t icmap_set_int32(const char *key_name, int32_t value);
  128. extern cs_error_t icmap_set_uint32(const char *key_name, uint32_t value);
  129. extern cs_error_t icmap_set_int64(const char *key_name, int64_t value);
  130. extern cs_error_t icmap_set_uint64(const char *key_name, uint64_t value);
  131. extern cs_error_t icmap_set_float(const char *key_name, float value);
  132. extern cs_error_t icmap_set_double(const char *key_name, double value);
  133. extern cs_error_t icmap_set_string(const char *key_name, const char *value);
  134. /*
  135. * Delete key from map
  136. */
  137. extern cs_error_t icmap_delete(const char *key_name);
  138. /*
  139. * Retrieve value of key key_name and store it in user preallocated value pointer.
  140. * value can be NULL, and then only value_len and/or type is returned (both of them
  141. * can also be NULL). If value is not NULL, actual length of value in map is checked
  142. * against value_len. If *value_len is shorter then length of value in map, error
  143. * CS_ERR_INVALID_PARAM is returned. After successful copy of value, value_len is
  144. * set to actual length of value in map.
  145. */
  146. extern cs_error_t icmap_get(
  147. const char *key_name,
  148. void *value,
  149. size_t *value_len,
  150. icmap_value_types_t *type);
  151. /*
  152. * Shortcuts for icmap_get
  153. */
  154. extern cs_error_t icmap_get_int8(const char *key_name, int8_t *i8);
  155. extern cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8);
  156. extern cs_error_t icmap_get_int16(const char *key_name, int16_t *i16);
  157. extern cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16);
  158. extern cs_error_t icmap_get_int32(const char *key_name, int32_t *i32);
  159. extern cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32);
  160. extern cs_error_t icmap_get_int64(const char *key_name, int64_t *i64);
  161. extern cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64);
  162. extern cs_error_t icmap_get_float(const char *key_name, float *flt);
  163. extern cs_error_t icmap_get_double(const char *key_name, double *dbl);
  164. /*
  165. * Shortcut for icmap_get for string type. Returned string is newly allocated and
  166. * caller is responsible for freeing memory
  167. */
  168. extern cs_error_t icmap_get_string(const char *key_name, char **str);
  169. /*
  170. * Defined only for [u]int* values. It adds step to current value.
  171. */
  172. extern cs_error_t icmap_adjust_int(const char *key_name, int32_t step);
  173. /*
  174. * Defined only for [u]int* values. It adds step to current value. Difference
  175. * between this function and icmap_adjust_int is given in fact, that in
  176. * tracking callback, old value is undefined, but whole process is done
  177. * without malloc/memcpy.
  178. */
  179. extern cs_error_t icmap_fast_adjust_int(const char *key_name, int32_t step);
  180. /*
  181. * Increase stored value by one
  182. */
  183. extern cs_error_t icmap_inc(const char *key_name);
  184. /*
  185. * Decrease stored value by one
  186. */
  187. extern cs_error_t icmap_dec(const char *key_name);
  188. /*
  189. * Increase stored value by one. Difference between this function and icmap_inc
  190. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  191. */
  192. extern cs_error_t icmap_fast_inc(const char *key_name);
  193. /*
  194. * Decrease stored value by one. Difference between this function and icmap_dec
  195. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  196. */
  197. extern cs_error_t icmap_fast_dec(const char *key_name);
  198. /*
  199. * Initialize iterator with given prefix
  200. */
  201. extern icmap_iter_t icmap_iter_init(const char *prefix);
  202. /*
  203. * Return next item in iterator iter. value_len and type are optional (= can be NULL), but if set,
  204. * length of returned value and/or type is returned. Function returns following key_name or NULL if
  205. * iteration is over.
  206. */
  207. extern const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type);
  208. /*
  209. * Finalize iterator
  210. */
  211. extern void icmap_iter_finalize(icmap_iter_t iter);
  212. /*
  213. * Add tracking function for given key_name. Tracked changes (add|modify|delete) depend on track_type,
  214. * which is bitwise or of ICMAP_TRACK_* values. notify_fn is called on change, where user_data pointer
  215. * is passed (unchanged). Value which can be used to delete tracking is passed as icmap_track.
  216. */
  217. extern cs_error_t icmap_track_add(
  218. const char *key_name,
  219. int32_t track_type,
  220. icmap_notify_fn_t notify_fn,
  221. void *user_data,
  222. icmap_track_t *icmap_track);
  223. /*
  224. * Return user data associated with given track
  225. */
  226. extern void *icmap_track_get_user_data(icmap_track_t icmap_track);
  227. /*
  228. * Remove previously added track
  229. */
  230. extern cs_error_t icmap_track_delete(icmap_track_t icmap_track);
  231. /*
  232. * Set read-only access for given key (key_name) or prefix, if prefix is set. ro_access
  233. * can be !0, which means, that old information about ro of this key is deleted.
  234. * Read-only access is used only in CMAP service! (in other word it prevents users
  235. * from deleting/changing key, but doesn't guarantee anything for internal icmap users.
  236. */
  237. extern cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access);
  238. /*
  239. * Check in given key is read only. Returns !0 if so, otherwise (key is rw) 0.
  240. */
  241. extern int icmap_is_key_ro(const char *key_name);
  242. /*
  243. * Converts given key_name to valid key name (replacing all prohibited characters by _)
  244. */
  245. extern void icmap_convert_name_to_valid_name(char *key_name);
  246. #ifdef __cplusplus
  247. }
  248. #endif
  249. #endif /* ICMAP_H_DEFINED */