icmap.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. /**
  291. * @brief Shortcut for icmap_get for string type.
  292. *
  293. * Returned string is newly allocated and
  294. * caller is responsible for freeing memory
  295. *
  296. * @param key_name
  297. * @param str
  298. * @return
  299. */
  300. extern cs_error_t icmap_get_string(const char *key_name, char **str);
  301. /**
  302. * @brief icmap_adjust_int
  303. *
  304. * Defined only for [u]int* values. It adds step to current value.
  305. *
  306. * @param key_name
  307. * @param step
  308. * @return
  309. */
  310. extern cs_error_t icmap_adjust_int(const char *key_name, int32_t step);
  311. /**
  312. * @brief icmap_adjust_int_r
  313. * @param map
  314. * @param key_name
  315. * @param step
  316. * @return
  317. */
  318. extern cs_error_t icmap_adjust_int_r(const icmap_map_t map, const char *key_name, int32_t step);
  319. /**
  320. * @brief icmap_fast_adjust_int
  321. *
  322. * Defined only for [u]int* values. It adds step to current value. Difference
  323. * between this function and icmap_adjust_int is given in fact, that in
  324. * tracking callback, old value is undefined, but whole process is done
  325. * without malloc/memcpy.
  326. *
  327. * @param key_name
  328. * @param step
  329. * @return
  330. */
  331. extern cs_error_t icmap_fast_adjust_int(const char *key_name, int32_t step);
  332. /**
  333. * @brief icmap_fast_adjust_int_r
  334. * @param map
  335. * @param key_name
  336. * @param step
  337. * @return
  338. */
  339. extern cs_error_t icmap_fast_adjust_int_r(const icmap_map_t map, const char *key_name, int32_t step);
  340. /**
  341. * @brief Increase stored value by one
  342. * @param key_name
  343. * @return
  344. */
  345. extern cs_error_t icmap_inc(const char *key_name);
  346. /**
  347. * @brief icmap_inc_r
  348. * @param map
  349. * @param key_name
  350. * @return
  351. */
  352. extern cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name);
  353. /**
  354. * @brief Decrease stored value by one
  355. * @param key_name
  356. * @return
  357. */
  358. extern cs_error_t icmap_dec(const char *key_name);
  359. /**
  360. * @brief icmap_dec_r
  361. * @param map
  362. * @param key_name
  363. * @return
  364. */
  365. extern cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name);
  366. /**
  367. * @brief Increase stored value by one.
  368. *
  369. * Difference between this function and icmap_inc
  370. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  371. *
  372. * @param key_name
  373. * @return
  374. */
  375. extern cs_error_t icmap_fast_inc(const char *key_name);
  376. /**
  377. * @brief icmap_fast_inc_r
  378. * @param map
  379. * @param key_name
  380. * @return
  381. */
  382. extern cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name);
  383. /**
  384. * @brief Decrease stored value by one.
  385. *
  386. * Difference between this function and icmap_dec
  387. * is same as between icmap_adjust_int and icmap_fast_adjust_int.
  388. *
  389. * @param key_name
  390. * @return
  391. */
  392. extern cs_error_t icmap_fast_dec(const char *key_name);
  393. /**
  394. * @brief icmap_fast_dec_r
  395. * @param map
  396. * @param key_name
  397. * @return
  398. */
  399. extern cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name);
  400. /**
  401. * @brief Initialize iterator with given prefix
  402. * @param prefix
  403. * @return
  404. */
  405. extern icmap_iter_t icmap_iter_init(const char *prefix);
  406. /**
  407. * @brief icmap_iter_init_r
  408. * @param map
  409. * @param prefix
  410. * @return
  411. */
  412. extern icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix);
  413. /**
  414. * @brief Return next item in iterator iter.
  415. *
  416. * value_len and type are optional (= can be NULL), but if set, length of returned value
  417. * and/or type is returned. Function returns following key_name or NULL if iteration is over.
  418. *
  419. * @param iter
  420. * @param value_len
  421. * @param type
  422. * @return
  423. */
  424. extern const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type);
  425. /**
  426. * @brief Finalize iterator
  427. * @param iter
  428. */
  429. extern void icmap_iter_finalize(icmap_iter_t iter);
  430. /**
  431. * @brief Add tracking function for given key_name.
  432. *
  433. * Tracked changes (add|modify|delete) depend on track_type, which is bitwise or of ICMAP_TRACK_* values.
  434. * notify_fn is called on change, where user_data pointer is passed (unchanged).
  435. * Value which can be used to delete tracking is passed as icmap_track.
  436. *
  437. * @param key_name
  438. * @param track_type
  439. * @param notify_fn
  440. * @param user_data
  441. * @param icmap_track
  442. * @return
  443. */
  444. extern cs_error_t icmap_track_add(
  445. const char *key_name,
  446. int32_t track_type,
  447. icmap_notify_fn_t notify_fn,
  448. void *user_data,
  449. icmap_track_t *icmap_track);
  450. /**
  451. * @brief Return user data associated with given track
  452. * @param icmap_track
  453. * @return
  454. */
  455. extern void *icmap_track_get_user_data(icmap_track_t icmap_track);
  456. /**
  457. * @brief Remove previously added track
  458. * @param icmap_track
  459. * @return
  460. */
  461. extern cs_error_t icmap_track_delete(icmap_track_t icmap_track);
  462. /**
  463. * @brief Set read-only access for given key (key_name) or prefix,
  464. * If prefix is set. ro_access can be !0, which means, that old information
  465. * about ro of this key is deleted. Read-only access is used only in CMAP service!
  466. * (in other word it prevents users from deleting/changing key, but doesn't
  467. * guarantee anything for internal icmap users.)
  468. * @param key_name
  469. * @param prefix
  470. * @param ro_access
  471. * @return
  472. */
  473. extern cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access);
  474. /**
  475. * @brief Check in given key is read only. Returns !0 if so, otherwise (key is rw) 0.
  476. * @param key_name
  477. * @return
  478. */
  479. extern int icmap_is_key_ro(const char *key_name);
  480. /**
  481. * @brief Converts given key_name to valid key name (replacing all prohibited characters by _)
  482. * @param key_name
  483. */
  484. extern void icmap_convert_name_to_valid_name(char *key_name);
  485. /**
  486. * @brief Copy content of src_map icmap to dst_map icmap.
  487. * @param dst_map
  488. * @param src_map
  489. * @return
  490. */
  491. extern cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map);
  492. /*
  493. * Returns length of value of given type, or 0 for string and binary data type
  494. */
  495. size_t icmap_get_valuetype_len(icmap_value_types_t type);
  496. /*
  497. * Converts track type of icmap to qb
  498. */
  499. int32_t icmap_tt_to_qbtt(int32_t track_type);
  500. /*
  501. * Convert track type of qb to icmap
  502. */
  503. int32_t icmap_qbtt_to_tt(int32_t track_type);
  504. #ifdef __cplusplus
  505. }
  506. #endif
  507. #endif /* ICMAP_H_DEFINED */