icmap.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  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. * icmap type.
  102. * icmap.c contains global variable (icmap_global_map) of this type. This
  103. * is used in every non-reentant call. Also only in this table are implemented
  104. * operations like set_ro and tracking of values. Other tables (created by
  105. * icmap_init_r) are simple map tables with get/set/iter operations.
  106. */
  107. typedef struct icmap_map *icmap_map_t;
  108. /*
  109. * Itterator type
  110. */
  111. typedef qb_map_iter_t *icmap_iter_t;
  112. /*
  113. * Track type
  114. */
  115. typedef struct icmap_track *icmap_track_t;
  116. /*
  117. * Initialize global icmap
  118. */
  119. extern cs_error_t icmap_init(void);
  120. /*
  121. * Initialize additional (local, reentrant) icmap_map. Content of variable
  122. * result is undefined if return code is not CS_OK.
  123. */
  124. extern cs_error_t icmap_init_r(icmap_map_t *result);
  125. /*
  126. * Finalize global icmap
  127. */
  128. extern void icmap_fini(void);
  129. /*
  130. * Finalize local, reentrant icmap
  131. */
  132. extern void icmap_fini_r(const icmap_map_t map);
  133. /*
  134. * Return global icmap
  135. */
  136. extern icmap_map_t icmap_get_global_map(void);
  137. /*
  138. * Compare value of key with name key_name1 in map1 with key with name key_name2
  139. * in map2. Two values must have same type, length and value to be considered equal.
  140. * Function returns 0 when any of map1, key_name1, map2, key_name2 are NULL, or
  141. * key_name is not found in map, or keys are not equal. != 0 is returned when
  142. * values are equal.
  143. */
  144. extern int icmap_key_value_eq(
  145. const icmap_map_t map1,
  146. const char *key_name1,
  147. const icmap_map_t map2,
  148. const char *key_name2);
  149. /*
  150. * Store value with value_len length and type as key_name name in global icmap.
  151. */
  152. extern cs_error_t icmap_set(
  153. const char *key_name,
  154. const void *value,
  155. size_t value_len,
  156. icmap_value_types_t type);
  157. /*
  158. * Reentrant version of icmap_set
  159. */
  160. extern cs_error_t icmap_set_r(
  161. const icmap_map_t map,
  162. const char *key_name,
  163. const void *value,
  164. size_t value_len,
  165. icmap_value_types_t type);
  166. /*
  167. * Shortcuts for setting values
  168. */
  169. extern cs_error_t icmap_set_int8(const char *key_name, int8_t value);
  170. extern cs_error_t icmap_set_uint8(const char *key_name, uint8_t value);
  171. extern cs_error_t icmap_set_int16(const char *key_name, int16_t value);
  172. extern cs_error_t icmap_set_uint16(const char *key_name, uint16_t value);
  173. extern cs_error_t icmap_set_int32(const char *key_name, int32_t value);
  174. extern cs_error_t icmap_set_uint32(const char *key_name, uint32_t value);
  175. extern cs_error_t icmap_set_int64(const char *key_name, int64_t value);
  176. extern cs_error_t icmap_set_uint64(const char *key_name, uint64_t value);
  177. extern cs_error_t icmap_set_float(const char *key_name, float value);
  178. extern cs_error_t icmap_set_double(const char *key_name, double value);
  179. extern cs_error_t icmap_set_string(const char *key_name, const char *value);
  180. extern cs_error_t icmap_set_int8_r(const icmap_map_t map, const char *key_name, int8_t value);
  181. extern cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value);
  182. extern cs_error_t icmap_set_int16_r(const icmap_map_t map, const char *key_name, int16_t value);
  183. extern cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value);
  184. extern cs_error_t icmap_set_int32_r(const icmap_map_t map, const char *key_name, int32_t value);
  185. extern cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value);
  186. extern cs_error_t icmap_set_int64_r(const icmap_map_t map, const char *key_name, int64_t value);
  187. extern cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value);
  188. extern cs_error_t icmap_set_float_r(const icmap_map_t map, const char *key_name, float value);
  189. extern cs_error_t icmap_set_double_r(const icmap_map_t map, const char *key_name, double value);
  190. extern cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value);
  191. /*
  192. * Delete key from map
  193. */
  194. extern cs_error_t icmap_delete(const char *key_name);
  195. extern cs_error_t icmap_delete_r(const icmap_map_t map, const char *key_name);
  196. /*
  197. * Retrieve value of key key_name and store it in user preallocated value pointer.
  198. * value can be NULL, and then only value_len and/or type is returned (both of them
  199. * can also be NULL). If value is not NULL, actual length of value in map is checked
  200. * against value_len. If *value_len is shorter then length of value in map, error
  201. * CS_ERR_INVALID_PARAM is returned. After successful copy of value, value_len is
  202. * set to actual length of value in map.
  203. */
  204. extern cs_error_t icmap_get(
  205. const char *key_name,
  206. void *value,
  207. size_t *value_len,
  208. icmap_value_types_t *type);
  209. /*
  210. * Same as icmap_get but it's reentrant and operates on given icmap_map
  211. */
  212. extern cs_error_t icmap_get_r(
  213. const icmap_map_t map,
  214. const char *key_name,
  215. void *value,
  216. size_t *value_len,
  217. icmap_value_types_t *type);
  218. /*
  219. * Shortcuts for icmap_get
  220. */
  221. extern cs_error_t icmap_get_int8(const char *key_name, int8_t *i8);
  222. extern cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8);
  223. extern cs_error_t icmap_get_int16(const char *key_name, int16_t *i16);
  224. extern cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16);
  225. extern cs_error_t icmap_get_int32(const char *key_name, int32_t *i32);
  226. extern cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32);
  227. extern cs_error_t icmap_get_int64(const char *key_name, int64_t *i64);
  228. extern cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64);
  229. extern cs_error_t icmap_get_float(const char *key_name, float *flt);
  230. extern cs_error_t icmap_get_double(const char *key_name, double *dbl);
  231. /*
  232. * Shortcuts for icmap_get_r
  233. */
  234. extern cs_error_t icmap_get_int8_r(const icmap_map_t map, const char *key_name, int8_t *i8);
  235. extern cs_error_t icmap_get_uint8_r(const icmap_map_t map, const char *key_name, uint8_t *u8);
  236. extern cs_error_t icmap_get_int16_r(const icmap_map_t map, const char *key_name, int16_t *i16);
  237. extern cs_error_t icmap_get_uint16_r(const icmap_map_t map, const char *key_name, uint16_t *u16);
  238. extern cs_error_t icmap_get_int32_r(const icmap_map_t map, const char *key_name, int32_t *i32);
  239. extern cs_error_t icmap_get_uint32_r(const icmap_map_t map, const char *key_name, uint32_t *u32);
  240. extern cs_error_t icmap_get_int64_r(const icmap_map_t map, const char *key_name, int64_t *i64);
  241. extern cs_error_t icmap_get_uint64_r(const icmap_map_t map, const char *key_name, uint64_t *u64);
  242. extern cs_error_t icmap_get_float_r(const icmap_map_t map, const char *key_name, float *flt);
  243. extern cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, double *dbl);
  244. /*
  245. * Shortcut for icmap_get for string type. Returned string is newly allocated and
  246. * caller is responsible for freeing memory
  247. */
  248. extern cs_error_t icmap_get_string(const char *key_name, char **str);
  249. /*
  250. * Defined only for [u]int* values. It adds step to current value.
  251. */
  252. extern cs_error_t icmap_adjust_int(const char *key_name, int32_t step);
  253. extern cs_error_t icmap_adjust_int_r(const icmap_map_t map, const char *key_name, int32_t step);
  254. /*
  255. * Defined only for [u]int* values. It adds step to current value. Difference
  256. * between this function and icmap_adjust_int is given in fact, that in
  257. * tracking callback, old value is undefined, but whole process is done
  258. * without malloc/memcpy.
  259. */
  260. extern cs_error_t icmap_fast_adjust_int(const char *key_name, int32_t step);
  261. extern cs_error_t icmap_fast_adjust_int_r(const icmap_map_t map, const char *key_name, int32_t step);
  262. /*
  263. * Increase stored value by one
  264. */
  265. extern cs_error_t icmap_inc(const char *key_name);
  266. extern cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name);
  267. /*
  268. * Decrease stored value by one
  269. */
  270. extern cs_error_t icmap_dec(const char *key_name);
  271. extern cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name);
  272. /*
  273. * Increase stored value by one. Difference between this function and icmap_inc
  274. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  275. */
  276. extern cs_error_t icmap_fast_inc(const char *key_name);
  277. extern cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name);
  278. /*
  279. * Decrease stored value by one. Difference between this function and icmap_dec
  280. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  281. */
  282. extern cs_error_t icmap_fast_dec(const char *key_name);
  283. extern cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name);
  284. /*
  285. * Initialize iterator with given prefix
  286. */
  287. extern icmap_iter_t icmap_iter_init(const char *prefix);
  288. extern icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix);
  289. /*
  290. * Return next item in iterator iter. value_len and type are optional (= can be NULL), but if set,
  291. * length of returned value and/or type is returned. Function returns following key_name or NULL if
  292. * iteration is over.
  293. */
  294. extern const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type);
  295. /*
  296. * Finalize iterator
  297. */
  298. extern void icmap_iter_finalize(icmap_iter_t iter);
  299. /*
  300. * Add tracking function for given key_name. Tracked changes (add|modify|delete) depend on track_type,
  301. * which is bitwise or of ICMAP_TRACK_* values. notify_fn is called on change, where user_data pointer
  302. * is passed (unchanged). Value which can be used to delete tracking is passed as icmap_track.
  303. */
  304. extern cs_error_t icmap_track_add(
  305. const char *key_name,
  306. int32_t track_type,
  307. icmap_notify_fn_t notify_fn,
  308. void *user_data,
  309. icmap_track_t *icmap_track);
  310. /*
  311. * Return user data associated with given track
  312. */
  313. extern void *icmap_track_get_user_data(icmap_track_t icmap_track);
  314. /*
  315. * Remove previously added track
  316. */
  317. extern cs_error_t icmap_track_delete(icmap_track_t icmap_track);
  318. /*
  319. * Set read-only access for given key (key_name) or prefix, if prefix is set. ro_access
  320. * can be !0, which means, that old information about ro of this key is deleted.
  321. * Read-only access is used only in CMAP service! (in other word it prevents users
  322. * from deleting/changing key, but doesn't guarantee anything for internal icmap users.
  323. */
  324. extern cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access);
  325. /*
  326. * Check in given key is read only. Returns !0 if so, otherwise (key is rw) 0.
  327. */
  328. extern int icmap_is_key_ro(const char *key_name);
  329. /*
  330. * Converts given key_name to valid key name (replacing all prohibited characters by _)
  331. */
  332. extern void icmap_convert_name_to_valid_name(char *key_name);
  333. /*
  334. * Copy content of src_map icmap to dst_map icmap.
  335. */
  336. extern cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map);
  337. #ifdef __cplusplus
  338. }
  339. #endif
  340. #endif /* ICMAP_H_DEFINED */