icmap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565
  1. /*
  2. * Copyright (c) 2011-2017 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. * @brief icmap type.
  102. *
  103. * icmap.c contains global variable (icmap_global_map) of this type. This
  104. * is used in every non-reentant call. Also only in this table are implemented
  105. * operations like set_ro and tracking of values. Other tables (created by
  106. * icmap_init_r) are simple map tables with get/set/iter operations.
  107. */
  108. typedef struct icmap_map *icmap_map_t;
  109. /**
  110. * @brief Itterator type
  111. */
  112. typedef qb_map_iter_t *icmap_iter_t;
  113. /**
  114. * @brief Track type
  115. */
  116. typedef struct icmap_track *icmap_track_t;
  117. /**
  118. * @brief Initialize global icmap
  119. * @return
  120. */
  121. extern cs_error_t icmap_init(void);
  122. /**
  123. * @brief Initialize additional (local, reentrant) icmap_map. Content of variable
  124. * result is undefined if return code is not CS_OK.
  125. * @param result
  126. * @return
  127. */
  128. extern cs_error_t icmap_init_r(icmap_map_t *result);
  129. /**
  130. * @brief Finalize global icmap
  131. */
  132. extern void icmap_fini(void);
  133. /**
  134. * @brief Finalize local, reentrant icmap
  135. * @param map
  136. */
  137. extern void icmap_fini_r(const icmap_map_t map);
  138. /**
  139. * @brief Return global icmap
  140. * @return
  141. */
  142. extern icmap_map_t icmap_get_global_map(void);
  143. /**
  144. * @brief Compare value of key with name key_name1 in map1 with key with name key_name2
  145. * in map2.
  146. *
  147. * Two values must have same type, length and value to be considered equal.
  148. * Function returns 0 when any of map1, key_name1, map2, key_name2 are NULL, or
  149. * key_name is not found in map, or keys are not equal. != 0 is returned when
  150. * values are equal.
  151. *
  152. * @param map1
  153. * @param key_name1
  154. * @param map2
  155. * @param key_name2
  156. * @return
  157. */
  158. extern int icmap_key_value_eq(
  159. const icmap_map_t map1,
  160. const char *key_name1,
  161. const icmap_map_t map2,
  162. const char *key_name2);
  163. /**
  164. * @brief Store value with value_len length and type as key_name name in global icmap.
  165. * @param key_name
  166. * @param value
  167. * @param value_len
  168. * @param type
  169. * @return
  170. */
  171. extern cs_error_t icmap_set(
  172. const char *key_name,
  173. const void *value,
  174. size_t value_len,
  175. icmap_value_types_t type);
  176. /**
  177. * @brief Reentrant version of icmap_set
  178. * @param map
  179. * @param key_name
  180. * @param value
  181. * @param value_len
  182. * @param type
  183. * @return
  184. */
  185. extern cs_error_t icmap_set_r(
  186. const icmap_map_t map,
  187. const char *key_name,
  188. const void *value,
  189. size_t value_len,
  190. icmap_value_types_t type);
  191. /*
  192. * Shortcuts for setting values
  193. */
  194. extern cs_error_t icmap_set_int8(const char *key_name, int8_t value);
  195. extern cs_error_t icmap_set_uint8(const char *key_name, uint8_t value);
  196. extern cs_error_t icmap_set_int16(const char *key_name, int16_t value);
  197. extern cs_error_t icmap_set_uint16(const char *key_name, uint16_t value);
  198. extern cs_error_t icmap_set_int32(const char *key_name, int32_t value);
  199. extern cs_error_t icmap_set_uint32(const char *key_name, uint32_t value);
  200. extern cs_error_t icmap_set_int64(const char *key_name, int64_t value);
  201. extern cs_error_t icmap_set_uint64(const char *key_name, uint64_t value);
  202. extern cs_error_t icmap_set_float(const char *key_name, float value);
  203. extern cs_error_t icmap_set_double(const char *key_name, double value);
  204. extern cs_error_t icmap_set_string(const char *key_name, const char *value);
  205. extern cs_error_t icmap_set_int8_r(const icmap_map_t map, const char *key_name, int8_t value);
  206. extern cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value);
  207. extern cs_error_t icmap_set_int16_r(const icmap_map_t map, const char *key_name, int16_t value);
  208. extern cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value);
  209. extern cs_error_t icmap_set_int32_r(const icmap_map_t map, const char *key_name, int32_t value);
  210. extern cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value);
  211. extern cs_error_t icmap_set_int64_r(const icmap_map_t map, const char *key_name, int64_t value);
  212. extern cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value);
  213. extern cs_error_t icmap_set_float_r(const icmap_map_t map, const char *key_name, float value);
  214. extern cs_error_t icmap_set_double_r(const icmap_map_t map, const char *key_name, double value);
  215. extern cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value);
  216. /**
  217. * @brief Delete key from map
  218. * @param key_name
  219. * @return
  220. */
  221. extern cs_error_t icmap_delete(const char *key_name);
  222. /**
  223. * @brief icmap_delete_r
  224. * @param map
  225. * @param key_name
  226. * @return
  227. */
  228. extern cs_error_t icmap_delete_r(const icmap_map_t map, const char *key_name);
  229. /**
  230. * @brief Retrieve value of key key_name and store it in user preallocated value pointer.
  231. *
  232. * Value can be NULL, and then only value_len and/or type is returned (both of them
  233. * can also be NULL). If value is not NULL, actual length of value in map is checked
  234. * against value_len. If *value_len is shorter then length of value in map, error
  235. * CS_ERR_INVALID_PARAM is returned. After successful copy of value, value_len is
  236. * set to actual length of value in map.
  237. *
  238. * @param key_name
  239. * @param value
  240. * @param value_len
  241. * @param type
  242. * @return
  243. */
  244. extern cs_error_t icmap_get(
  245. const char *key_name,
  246. void *value,
  247. size_t *value_len,
  248. icmap_value_types_t *type);
  249. /**
  250. * @brief Same as icmap_get but it's reentrant and operates on given icmap_map
  251. * @param map
  252. * @param key_name
  253. * @param value
  254. * @param value_len
  255. * @param type
  256. * @return
  257. */
  258. extern cs_error_t icmap_get_r(
  259. const icmap_map_t map,
  260. const char *key_name,
  261. void *value,
  262. size_t *value_len,
  263. icmap_value_types_t *type);
  264. /*
  265. * Shortcuts for icmap_get
  266. */
  267. extern cs_error_t icmap_get_int8(const char *key_name, int8_t *i8);
  268. extern cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8);
  269. extern cs_error_t icmap_get_int16(const char *key_name, int16_t *i16);
  270. extern cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16);
  271. extern cs_error_t icmap_get_int32(const char *key_name, int32_t *i32);
  272. extern cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32);
  273. extern cs_error_t icmap_get_int64(const char *key_name, int64_t *i64);
  274. extern cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64);
  275. extern cs_error_t icmap_get_float(const char *key_name, float *flt);
  276. extern cs_error_t icmap_get_double(const char *key_name, double *dbl);
  277. /*
  278. * Shortcuts for icmap_get_r
  279. */
  280. extern cs_error_t icmap_get_int8_r(const icmap_map_t map, const char *key_name, int8_t *i8);
  281. extern cs_error_t icmap_get_uint8_r(const icmap_map_t map, const char *key_name, uint8_t *u8);
  282. extern cs_error_t icmap_get_int16_r(const icmap_map_t map, const char *key_name, int16_t *i16);
  283. extern cs_error_t icmap_get_uint16_r(const icmap_map_t map, const char *key_name, uint16_t *u16);
  284. extern cs_error_t icmap_get_int32_r(const icmap_map_t map, const char *key_name, int32_t *i32);
  285. extern cs_error_t icmap_get_uint32_r(const icmap_map_t map, const char *key_name, uint32_t *u32);
  286. extern cs_error_t icmap_get_int64_r(const icmap_map_t map, const char *key_name, int64_t *i64);
  287. extern cs_error_t icmap_get_uint64_r(const icmap_map_t map, const char *key_name, uint64_t *u64);
  288. extern cs_error_t icmap_get_float_r(const icmap_map_t map, const char *key_name, float *flt);
  289. extern cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, double *dbl);
  290. extern cs_error_t icmap_get_string_r(const icmap_map_t map, const char *key_name, char **str);
  291. /**
  292. * @brief Shortcut for icmap_get for string type.
  293. *
  294. * Returned string is newly allocated and
  295. * caller is responsible for freeing memory
  296. *
  297. * @param key_name
  298. * @param str
  299. * @return
  300. */
  301. extern cs_error_t icmap_get_string(const char *key_name, char **str);
  302. /**
  303. * @brief icmap_adjust_int
  304. *
  305. * Defined only for [u]int* values. It adds step to current value.
  306. *
  307. * @param key_name
  308. * @param step
  309. * @return
  310. */
  311. extern cs_error_t icmap_adjust_int(const char *key_name, int32_t step);
  312. /**
  313. * @brief icmap_adjust_int_r
  314. * @param map
  315. * @param key_name
  316. * @param step
  317. * @return
  318. */
  319. extern cs_error_t icmap_adjust_int_r(const icmap_map_t map, const char *key_name, int32_t step);
  320. /**
  321. * @brief icmap_fast_adjust_int
  322. *
  323. * Defined only for [u]int* values. It adds step to current value. Difference
  324. * between this function and icmap_adjust_int is given in fact, that in
  325. * tracking callback, old value is undefined, but whole process is done
  326. * without malloc/memcpy.
  327. *
  328. * @param key_name
  329. * @param step
  330. * @return
  331. */
  332. extern cs_error_t icmap_fast_adjust_int(const char *key_name, int32_t step);
  333. /**
  334. * @brief icmap_fast_adjust_int_r
  335. * @param map
  336. * @param key_name
  337. * @param step
  338. * @return
  339. */
  340. extern cs_error_t icmap_fast_adjust_int_r(const icmap_map_t map, const char *key_name, int32_t step);
  341. /**
  342. * @brief Increase stored value by one
  343. * @param key_name
  344. * @return
  345. */
  346. extern cs_error_t icmap_inc(const char *key_name);
  347. /**
  348. * @brief icmap_inc_r
  349. * @param map
  350. * @param key_name
  351. * @return
  352. */
  353. extern cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name);
  354. /**
  355. * @brief Decrease stored value by one
  356. * @param key_name
  357. * @return
  358. */
  359. extern cs_error_t icmap_dec(const char *key_name);
  360. /**
  361. * @brief icmap_dec_r
  362. * @param map
  363. * @param key_name
  364. * @return
  365. */
  366. extern cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name);
  367. /**
  368. * @brief Increase stored value by one.
  369. *
  370. * Difference between this function and icmap_inc
  371. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  372. *
  373. * @param key_name
  374. * @return
  375. */
  376. extern cs_error_t icmap_fast_inc(const char *key_name);
  377. /**
  378. * @brief icmap_fast_inc_r
  379. * @param map
  380. * @param key_name
  381. * @return
  382. */
  383. extern cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name);
  384. /**
  385. * @brief Decrease stored value by one.
  386. *
  387. * Difference between this function and icmap_dec
  388. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  389. *
  390. * @param key_name
  391. * @return
  392. */
  393. extern cs_error_t icmap_fast_dec(const char *key_name);
  394. /**
  395. * @brief icmap_fast_dec_r
  396. * @param map
  397. * @param key_name
  398. * @return
  399. */
  400. extern cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name);
  401. /**
  402. * @brief Initialize iterator with given prefix
  403. * @param prefix
  404. * @return
  405. */
  406. extern icmap_iter_t icmap_iter_init(const char *prefix);
  407. /**
  408. * @brief icmap_iter_init_r
  409. * @param map
  410. * @param prefix
  411. * @return
  412. */
  413. extern icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix);
  414. /**
  415. * @brief Return next item in iterator iter.
  416. *
  417. * value_len and type are optional (= can be NULL), but if set, length of returned value
  418. * and/or type is returned. Function returns following key_name or NULL if iteration is over.
  419. *
  420. * @param iter
  421. * @param value_len
  422. * @param type
  423. * @return
  424. */
  425. extern const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type);
  426. /**
  427. * @brief Finalize iterator
  428. * @param iter
  429. */
  430. extern void icmap_iter_finalize(icmap_iter_t iter);
  431. /**
  432. * @brief Add tracking function for given key_name.
  433. *
  434. * Tracked changes (add|modify|delete) depend on track_type, which is bitwise or of ICMAP_TRACK_* values.
  435. * notify_fn is called on change, where user_data pointer is passed (unchanged).
  436. * Value which can be used to delete tracking is passed as icmap_track.
  437. *
  438. * @param key_name
  439. * @param track_type
  440. * @param notify_fn
  441. * @param user_data
  442. * @param icmap_track
  443. * @return
  444. */
  445. extern cs_error_t icmap_track_add(
  446. const char *key_name,
  447. int32_t track_type,
  448. icmap_notify_fn_t notify_fn,
  449. void *user_data,
  450. icmap_track_t *icmap_track);
  451. /**
  452. * @brief Return user data associated with given track
  453. * @param icmap_track
  454. * @return
  455. */
  456. extern void *icmap_track_get_user_data(icmap_track_t icmap_track);
  457. /**
  458. * @brief Remove previously added track
  459. * @param icmap_track
  460. * @return
  461. */
  462. extern cs_error_t icmap_track_delete(icmap_track_t icmap_track);
  463. /**
  464. * @brief Set read-only access for given key (key_name) or prefix,
  465. * If prefix is set. ro_access can be !0, which means, that old information
  466. * about ro of this key is deleted. Read-only access is used only in CMAP service!
  467. * (in other word it prevents users from deleting/changing key, but doesn't
  468. * guarantee anything for internal icmap users.)
  469. * @param key_name
  470. * @param prefix
  471. * @param ro_access
  472. * @return
  473. */
  474. extern cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access);
  475. /**
  476. * @brief Check in given key is read only. Returns !0 if so, otherwise (key is rw) 0.
  477. * @param key_name
  478. * @return
  479. */
  480. extern int icmap_is_key_ro(const char *key_name);
  481. /**
  482. * @brief Converts given key_name to valid key name (replacing all prohibited characters by _)
  483. * @param key_name
  484. */
  485. extern void icmap_convert_name_to_valid_name(char *key_name);
  486. /**
  487. * @brief Copy content of src_map icmap to dst_map icmap.
  488. * @param dst_map
  489. * @param src_map
  490. * @return
  491. */
  492. extern cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map);
  493. /*
  494. * Returns length of value of given type, or 0 for string and binary data type
  495. */
  496. size_t icmap_get_valuetype_len(icmap_value_types_t type);
  497. /*
  498. * Converts track type of icmap to qb
  499. */
  500. int32_t icmap_tt_to_qbtt(int32_t track_type);
  501. /*
  502. * Convert track type of qb to icmap
  503. */
  504. int32_t icmap_qbtt_to_tt(int32_t track_type);
  505. #ifdef __cplusplus
  506. }
  507. #endif
  508. #endif /* ICMAP_H_DEFINED */