icmap.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282
  1. /*
  2. * Copyright (c) 2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  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. #include <config.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <corosync/corotypes.h>
  38. #include <qb/qbdefs.h>
  39. #include <corosync/list.h>
  40. #include <corosync/icmap.h>
  41. #define ICMAP_MAX_VALUE_LEN (16*1024)
  42. struct icmap_item {
  43. char *key_name;
  44. icmap_value_types_t type;
  45. size_t value_len;
  46. char value[];
  47. };
  48. struct icmap_map {
  49. qb_map_t *qb_map;
  50. };
  51. static icmap_map_t icmap_global_map;
  52. struct icmap_track {
  53. char *key_name;
  54. int32_t track_type;
  55. icmap_notify_fn_t notify_fn;
  56. void *user_data;
  57. struct list_head list;
  58. };
  59. struct icmap_ro_access_item {
  60. char *key_name;
  61. int prefix;
  62. struct list_head list;
  63. };
  64. DECLARE_LIST_INIT(icmap_ro_access_item_list_head);
  65. DECLARE_LIST_INIT(icmap_track_list_head);
  66. /*
  67. * Static functions declarations
  68. */
  69. /*
  70. * Check if key_name is valid icmap key name. Returns 0 on success, and -1 on fail
  71. */
  72. static int icmap_check_key_name(const char *key_name);
  73. /*
  74. * Check that value with given type has correct length value_len. Returns 0 on success,
  75. * and -1 on fail
  76. */
  77. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type);
  78. /*
  79. * Returns length of value of given type, or 0 for string and binary data type
  80. */
  81. static size_t icmap_get_valuetype_len(icmap_value_types_t type);
  82. /*
  83. * Converts track type of icmap to qb
  84. */
  85. static int32_t icmap_tt_to_qbtt(int32_t track_type);
  86. /*
  87. * Convert track type of qb to icmap
  88. */
  89. static int32_t icmap_qbtt_to_tt(int32_t track_type);
  90. /*
  91. * Checks if item has same value as value with value_len and given type. Returns 0 if not, otherwise !0.
  92. */
  93. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type);
  94. /*
  95. * Checks if given character is valid in key name. Returns 0 if not, otherwise !0.
  96. */
  97. static int icmap_is_valid_name_char(char c);
  98. /*
  99. * Helper for getting integer and float value with given type for key key_name and store it in value.
  100. */
  101. static cs_error_t icmap_get_int_r(
  102. const icmap_map_t map,
  103. const char *key_name,
  104. void *value,
  105. icmap_value_types_t type);
  106. /*
  107. * Return raw item value data. Internal function used by icmap_get_r which does most
  108. * of arguments validity checks but doesn't copy data (it returns raw item data
  109. * pointer). It's not very safe tho it's static.
  110. */
  111. static cs_error_t icmap_get_ref_r(
  112. const icmap_map_t map,
  113. const char *key_name,
  114. void **value,
  115. size_t *value_len,
  116. icmap_value_types_t *type);
  117. /*
  118. * Function implementation
  119. */
  120. static int32_t icmap_tt_to_qbtt(int32_t track_type)
  121. {
  122. int32_t res = 0;
  123. if (track_type & ICMAP_TRACK_DELETE) {
  124. res |= QB_MAP_NOTIFY_DELETED;
  125. }
  126. if (track_type & ICMAP_TRACK_MODIFY) {
  127. res |= QB_MAP_NOTIFY_REPLACED;
  128. }
  129. if (track_type & ICMAP_TRACK_ADD) {
  130. res |= QB_MAP_NOTIFY_INSERTED;
  131. }
  132. if (track_type & ICMAP_TRACK_PREFIX) {
  133. res |= QB_MAP_NOTIFY_RECURSIVE;
  134. }
  135. return (res);
  136. }
  137. static int32_t icmap_qbtt_to_tt(int32_t track_type)
  138. {
  139. int32_t res = 0;
  140. if (track_type & QB_MAP_NOTIFY_DELETED) {
  141. res |= ICMAP_TRACK_DELETE;
  142. }
  143. if (track_type & QB_MAP_NOTIFY_REPLACED) {
  144. res |= ICMAP_TRACK_MODIFY;
  145. }
  146. if (track_type & QB_MAP_NOTIFY_INSERTED) {
  147. res |= ICMAP_TRACK_ADD;
  148. }
  149. if (track_type & QB_MAP_NOTIFY_RECURSIVE) {
  150. res |= ICMAP_TRACK_PREFIX;
  151. }
  152. return (res);
  153. }
  154. static void icmap_map_free_cb(uint32_t event,
  155. char* key, void* old_value,
  156. void* value, void* user_data)
  157. {
  158. struct icmap_item *item = (struct icmap_item *)old_value;
  159. /*
  160. * value == old_value -> fast_adjust_int was used, don't free data
  161. */
  162. if (item != NULL && value != old_value) {
  163. free(item->key_name);
  164. free(item);
  165. }
  166. }
  167. cs_error_t icmap_init_r(icmap_map_t *result)
  168. {
  169. int32_t err;
  170. *result = malloc(sizeof(struct icmap_map));
  171. if (*result == NULL) {
  172. return (CS_ERR_NO_MEMORY);
  173. }
  174. (*result)->qb_map = qb_trie_create();
  175. if ((*result)->qb_map == NULL)
  176. return (CS_ERR_INIT);
  177. err = qb_map_notify_add((*result)->qb_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  178. return (qb_to_cs_error(err));
  179. }
  180. cs_error_t icmap_init(void)
  181. {
  182. return (icmap_init_r(&icmap_global_map));
  183. }
  184. static void icmap_set_ro_access_free(void)
  185. {
  186. struct list_head *iter = icmap_ro_access_item_list_head.next;
  187. struct icmap_ro_access_item *icmap_ro_ai;
  188. while (iter != &icmap_ro_access_item_list_head) {
  189. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  190. list_del(&icmap_ro_ai->list);
  191. free(icmap_ro_ai->key_name);
  192. free(icmap_ro_ai);
  193. iter = icmap_ro_access_item_list_head.next;
  194. }
  195. }
  196. static void icmap_del_all_track(void)
  197. {
  198. struct list_head *iter = icmap_track_list_head.next;
  199. struct icmap_track *icmap_track;
  200. while (iter != &icmap_track_list_head) {
  201. icmap_track = list_entry(iter, struct icmap_track, list);
  202. icmap_track_delete(icmap_track);
  203. iter = icmap_track_list_head.next;
  204. }
  205. }
  206. void icmap_fini_r(const icmap_map_t map)
  207. {
  208. qb_map_destroy(map->qb_map);
  209. free(map);
  210. return;
  211. }
  212. void icmap_fini(void)
  213. {
  214. icmap_del_all_track();
  215. /*
  216. * catch 22 warning:
  217. * We need to drop this notify but we can't because it calls icmap_map_free_cb
  218. * while destroying the tree to free icmap_item(s).
  219. * -> qb_map_notify_del_2(icmap_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  220. * and we cannot call it after map_destroy. joy! :)
  221. */
  222. icmap_fini_r(icmap_global_map);
  223. icmap_set_ro_access_free();
  224. return ;
  225. }
  226. icmap_map_t icmap_get_global_map(void)
  227. {
  228. return (icmap_global_map);
  229. }
  230. static int icmap_is_valid_name_char(char c)
  231. {
  232. return ((c >= 'a' && c <= 'z') ||
  233. (c >= 'A' && c <= 'Z') ||
  234. (c >= '0' && c <= '9') ||
  235. c == '.' || c == '_' || c == '-' || c == '/' || c == ':');
  236. }
  237. void icmap_convert_name_to_valid_name(char *key_name)
  238. {
  239. int i;
  240. for (i = 0; i < strlen(key_name); i++) {
  241. if (!icmap_is_valid_name_char(key_name[i])) {
  242. key_name[i] = '_';
  243. }
  244. }
  245. }
  246. static int icmap_check_key_name(const char *key_name)
  247. {
  248. int i;
  249. if ((strlen(key_name) < ICMAP_KEYNAME_MINLEN) || strlen(key_name) > ICMAP_KEYNAME_MAXLEN) {
  250. return (-1);
  251. }
  252. for (i = 0; i < strlen(key_name); i++) {
  253. if (!icmap_is_valid_name_char(key_name[i])) {
  254. return (-1);
  255. }
  256. }
  257. return (0);
  258. }
  259. static size_t icmap_get_valuetype_len(icmap_value_types_t type)
  260. {
  261. size_t res = 0;
  262. switch (type) {
  263. case ICMAP_VALUETYPE_INT8: res = sizeof(int8_t); break;
  264. case ICMAP_VALUETYPE_UINT8: res = sizeof(uint8_t); break;
  265. case ICMAP_VALUETYPE_INT16: res = sizeof(int16_t); break;
  266. case ICMAP_VALUETYPE_UINT16: res = sizeof(uint16_t); break;
  267. case ICMAP_VALUETYPE_INT32: res = sizeof(int32_t); break;
  268. case ICMAP_VALUETYPE_UINT32: res = sizeof(uint32_t); break;
  269. case ICMAP_VALUETYPE_INT64: res = sizeof(int64_t); break;
  270. case ICMAP_VALUETYPE_UINT64: res = sizeof(uint64_t); break;
  271. case ICMAP_VALUETYPE_FLOAT: res = sizeof(float); break;
  272. case ICMAP_VALUETYPE_DOUBLE: res = sizeof(double); break;
  273. case ICMAP_VALUETYPE_STRING:
  274. case ICMAP_VALUETYPE_BINARY:
  275. res = 0;
  276. break;
  277. }
  278. return (res);
  279. }
  280. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type)
  281. {
  282. if (value_len > ICMAP_MAX_VALUE_LEN) {
  283. return (-1);
  284. }
  285. if (type != ICMAP_VALUETYPE_STRING && type != ICMAP_VALUETYPE_BINARY) {
  286. if (icmap_get_valuetype_len(type) == value_len) {
  287. return (0);
  288. } else {
  289. return (-1);
  290. }
  291. }
  292. if (type == ICMAP_VALUETYPE_STRING) {
  293. /*
  294. * value_len can be shorter then real string length, but never
  295. * longer (+ 1 is because of 0 at the end of string)
  296. */
  297. if (value_len > strlen((const char *)value) + 1) {
  298. return (-1);
  299. } else {
  300. return (0);
  301. }
  302. }
  303. return (0);
  304. }
  305. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type)
  306. {
  307. size_t ptr_len;
  308. if (item->type != type) {
  309. return (0);
  310. }
  311. if (item->type == ICMAP_VALUETYPE_STRING) {
  312. ptr_len = strlen((const char *)value);
  313. if (ptr_len > value_len) {
  314. ptr_len = value_len;
  315. }
  316. ptr_len++;
  317. } else {
  318. ptr_len = value_len;
  319. }
  320. if (item->value_len == ptr_len) {
  321. return (memcmp(item->value, value, value_len) == 0);
  322. };
  323. return (0);
  324. }
  325. cs_error_t icmap_set_r(
  326. const icmap_map_t map,
  327. const char *key_name,
  328. const void *value,
  329. size_t value_len,
  330. icmap_value_types_t type)
  331. {
  332. struct icmap_item *item;
  333. struct icmap_item *new_item;
  334. size_t new_value_len;
  335. size_t new_item_size;
  336. if (value == NULL || key_name == NULL) {
  337. return (CS_ERR_INVALID_PARAM);
  338. }
  339. if (icmap_check_value_len(value, value_len, type) != 0) {
  340. return (CS_ERR_INVALID_PARAM);
  341. }
  342. item = qb_map_get(map->qb_map, key_name);
  343. if (item != NULL) {
  344. /*
  345. * Check that key is really changed
  346. */
  347. if (icmap_item_eq(item, value, value_len, type)) {
  348. return (CS_OK);
  349. }
  350. } else {
  351. if (icmap_check_key_name(key_name) != 0) {
  352. return (CS_ERR_NAME_TOO_LONG);
  353. }
  354. }
  355. if (type == ICMAP_VALUETYPE_BINARY || type == ICMAP_VALUETYPE_STRING) {
  356. if (type == ICMAP_VALUETYPE_STRING) {
  357. new_value_len = strlen((const char *)value);
  358. if (new_value_len > value_len) {
  359. new_value_len = value_len;
  360. }
  361. new_value_len++;
  362. } else {
  363. new_value_len = value_len;
  364. }
  365. } else {
  366. new_value_len = icmap_get_valuetype_len(type);
  367. }
  368. new_item_size = sizeof(struct icmap_item) + new_value_len;
  369. new_item = malloc(new_item_size);
  370. if (new_item == NULL) {
  371. return (CS_ERR_NO_MEMORY);
  372. }
  373. memset(new_item, 0, new_item_size);
  374. if (item == NULL) {
  375. new_item->key_name = strdup(key_name);
  376. if (new_item->key_name == NULL) {
  377. free(new_item);
  378. return (CS_ERR_NO_MEMORY);
  379. }
  380. } else {
  381. new_item->key_name = item->key_name;
  382. item->key_name = NULL;
  383. }
  384. new_item->type = type;
  385. new_item->value_len = new_value_len;
  386. memcpy(new_item->value, value, new_value_len);
  387. if (new_item->type == ICMAP_VALUETYPE_STRING) {
  388. ((char *)new_item->value)[new_value_len - 1] = 0;
  389. }
  390. qb_map_put(map->qb_map, new_item->key_name, new_item);
  391. return (CS_OK);
  392. }
  393. cs_error_t icmap_set(
  394. const char *key_name,
  395. const void *value,
  396. size_t value_len,
  397. icmap_value_types_t type)
  398. {
  399. return (icmap_set_r(icmap_global_map, key_name, value, value_len, type));
  400. }
  401. cs_error_t icmap_set_int8_r(const icmap_map_t map, const char *key_name, int8_t value)
  402. {
  403. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT8));
  404. }
  405. cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value)
  406. {
  407. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT8));
  408. }
  409. cs_error_t icmap_set_int16_r(const icmap_map_t map, const char *key_name, int16_t value)
  410. {
  411. return (icmap_set_r(map,key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT16));
  412. }
  413. cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value)
  414. {
  415. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT16));
  416. }
  417. cs_error_t icmap_set_int32_r(const icmap_map_t map, const char *key_name, int32_t value)
  418. {
  419. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT32));
  420. }
  421. cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value)
  422. {
  423. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT32));
  424. }
  425. cs_error_t icmap_set_int64_r(const icmap_map_t map, const char *key_name, int64_t value)
  426. {
  427. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT64));
  428. }
  429. cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value)
  430. {
  431. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT64));
  432. }
  433. cs_error_t icmap_set_float_r(const icmap_map_t map, const char *key_name, float value)
  434. {
  435. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_FLOAT));
  436. }
  437. cs_error_t icmap_set_double_r(const icmap_map_t map, const char *key_name, double value)
  438. {
  439. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_DOUBLE));
  440. }
  441. cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value)
  442. {
  443. if (value == NULL) {
  444. return (CS_ERR_INVALID_PARAM);
  445. }
  446. return (icmap_set_r(map, key_name, value, strlen(value), ICMAP_VALUETYPE_STRING));
  447. }
  448. cs_error_t icmap_set_int8(const char *key_name, int8_t value)
  449. {
  450. return (icmap_set_int8_r(icmap_global_map, key_name, value));
  451. }
  452. cs_error_t icmap_set_uint8(const char *key_name, uint8_t value)
  453. {
  454. return (icmap_set_uint8_r(icmap_global_map, key_name, value));
  455. }
  456. cs_error_t icmap_set_int16(const char *key_name, int16_t value)
  457. {
  458. return (icmap_set_int16_r(icmap_global_map, key_name, value));
  459. }
  460. cs_error_t icmap_set_uint16(const char *key_name, uint16_t value)
  461. {
  462. return (icmap_set_uint16_r(icmap_global_map, key_name, value));
  463. }
  464. cs_error_t icmap_set_int32(const char *key_name, int32_t value)
  465. {
  466. return (icmap_set_int32_r(icmap_global_map, key_name, value));
  467. }
  468. cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
  469. {
  470. return (icmap_set_uint32_r(icmap_global_map, key_name, value));
  471. }
  472. cs_error_t icmap_set_int64(const char *key_name, int64_t value)
  473. {
  474. return (icmap_set_int64_r(icmap_global_map, key_name, value));
  475. }
  476. cs_error_t icmap_set_uint64(const char *key_name, uint64_t value)
  477. {
  478. return (icmap_set_uint64_r(icmap_global_map, key_name, value));
  479. }
  480. cs_error_t icmap_set_float(const char *key_name, float value)
  481. {
  482. return (icmap_set_float_r(icmap_global_map, key_name, value));
  483. }
  484. cs_error_t icmap_set_double(const char *key_name, double value)
  485. {
  486. return (icmap_set_double_r(icmap_global_map, key_name, value));
  487. }
  488. cs_error_t icmap_set_string(const char *key_name, const char *value)
  489. {
  490. return (icmap_set_string_r(icmap_global_map, key_name, value));
  491. }
  492. cs_error_t icmap_delete_r(const icmap_map_t map, const char *key_name)
  493. {
  494. struct icmap_item *item;
  495. if (key_name == NULL) {
  496. return (CS_ERR_INVALID_PARAM);
  497. }
  498. item = qb_map_get(map->qb_map, key_name);
  499. if (item == NULL) {
  500. return (CS_ERR_NOT_EXIST);
  501. }
  502. if (qb_map_rm(map->qb_map, item->key_name) != QB_TRUE) {
  503. return (CS_ERR_NOT_EXIST);
  504. }
  505. return (CS_OK);
  506. }
  507. cs_error_t icmap_delete(const char *key_name)
  508. {
  509. return (icmap_delete_r(icmap_global_map, key_name));
  510. }
  511. static cs_error_t icmap_get_ref_r(
  512. const icmap_map_t map,
  513. const char *key_name,
  514. void **value,
  515. size_t *value_len,
  516. icmap_value_types_t *type)
  517. {
  518. struct icmap_item *item;
  519. if (key_name == NULL) {
  520. return (CS_ERR_INVALID_PARAM);
  521. }
  522. item = qb_map_get(map->qb_map, key_name);
  523. if (item == NULL) {
  524. return (CS_ERR_NOT_EXIST);
  525. }
  526. if (type != NULL) {
  527. *type = item->type;
  528. }
  529. if (value_len != NULL) {
  530. *value_len = item->value_len;
  531. }
  532. if (value != NULL) {
  533. *value = item->value;
  534. }
  535. return (CS_OK);
  536. }
  537. cs_error_t icmap_get_r(
  538. const icmap_map_t map,
  539. const char *key_name,
  540. void *value,
  541. size_t *value_len,
  542. icmap_value_types_t *type)
  543. {
  544. cs_error_t res;
  545. void *tmp_value;
  546. size_t tmp_value_len;
  547. res = icmap_get_ref_r(map, key_name, &tmp_value, &tmp_value_len, type);
  548. if (res != CS_OK) {
  549. return (res);
  550. }
  551. if (value == NULL) {
  552. if (value_len != NULL) {
  553. *value_len = tmp_value_len;
  554. }
  555. } else {
  556. if (value_len == NULL || *value_len < tmp_value_len) {
  557. return (CS_ERR_INVALID_PARAM);
  558. }
  559. *value_len = tmp_value_len;
  560. memcpy(value, tmp_value, tmp_value_len);
  561. }
  562. return (CS_OK);
  563. }
  564. cs_error_t icmap_get(
  565. const char *key_name,
  566. void *value,
  567. size_t *value_len,
  568. icmap_value_types_t *type)
  569. {
  570. return (icmap_get_r(icmap_global_map, key_name, value, value_len, type));
  571. }
  572. static cs_error_t icmap_get_int_r(
  573. const icmap_map_t map,
  574. const char *key_name,
  575. void *value,
  576. icmap_value_types_t type)
  577. {
  578. char key_value[16];
  579. size_t key_size;
  580. cs_error_t err;
  581. icmap_value_types_t key_type;
  582. key_size = sizeof(key_value);
  583. memset(key_value, 0, key_size);
  584. err = icmap_get(key_name, key_value, &key_size, &key_type);
  585. if (err != CS_OK)
  586. return (err);
  587. if (key_type != type) {
  588. return (CS_ERR_INVALID_PARAM);
  589. }
  590. memcpy(value, key_value, icmap_get_valuetype_len(key_type));
  591. return (CS_OK);
  592. }
  593. cs_error_t icmap_get_int8_r(const icmap_map_t map, const char *key_name, int8_t *i8)
  594. {
  595. return (icmap_get_int_r(map, key_name, i8, ICMAP_VALUETYPE_INT8));
  596. }
  597. cs_error_t icmap_get_uint8_r(const icmap_map_t map, const char *key_name, uint8_t *u8)
  598. {
  599. return (icmap_get_int_r(map, key_name, u8, ICMAP_VALUETYPE_UINT8));
  600. }
  601. cs_error_t icmap_get_int16_r(const icmap_map_t map, const char *key_name, int16_t *i16)
  602. {
  603. return (icmap_get_int_r(map, key_name, i16, ICMAP_VALUETYPE_INT16));
  604. }
  605. cs_error_t icmap_get_uint16_r(const icmap_map_t map, const char *key_name, uint16_t *u16)
  606. {
  607. return (icmap_get_int_r(map, key_name, u16, ICMAP_VALUETYPE_UINT16));
  608. }
  609. cs_error_t icmap_get_int32_r(const icmap_map_t map, const char *key_name, int32_t *i32)
  610. {
  611. return (icmap_get_int_r(map, key_name, i32, ICMAP_VALUETYPE_INT32));
  612. }
  613. cs_error_t icmap_get_uint32_r(const icmap_map_t map, const char *key_name, uint32_t *u32)
  614. {
  615. return (icmap_get_int_r(map, key_name, u32, ICMAP_VALUETYPE_UINT32));
  616. }
  617. cs_error_t icmap_get_int64_r(const icmap_map_t map, const char *key_name, int64_t *i64)
  618. {
  619. return(icmap_get_int_r(map, key_name, i64, ICMAP_VALUETYPE_INT64));
  620. }
  621. cs_error_t icmap_get_uint64_r(const icmap_map_t map, const char *key_name, uint64_t *u64)
  622. {
  623. return (icmap_get_int_r(map, key_name, u64, ICMAP_VALUETYPE_UINT64));
  624. }
  625. cs_error_t icmap_get_float_r(const icmap_map_t map, const char *key_name, float *flt)
  626. {
  627. return (icmap_get_int_r(map, key_name, flt, ICMAP_VALUETYPE_FLOAT));
  628. }
  629. cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, double *dbl)
  630. {
  631. return (icmap_get_int_r(map, key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
  632. }
  633. cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
  634. {
  635. return (icmap_get_int8_r(icmap_global_map, key_name, i8));
  636. }
  637. cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8)
  638. {
  639. return (icmap_get_uint8_r(icmap_global_map, key_name, u8));
  640. }
  641. cs_error_t icmap_get_int16(const char *key_name, int16_t *i16)
  642. {
  643. return (icmap_get_int16_r(icmap_global_map, key_name, i16));
  644. }
  645. cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16)
  646. {
  647. return (icmap_get_uint16_r(icmap_global_map, key_name, u16));
  648. }
  649. cs_error_t icmap_get_int32(const char *key_name, int32_t *i32)
  650. {
  651. return (icmap_get_int32_r(icmap_global_map, key_name, i32));
  652. }
  653. cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32)
  654. {
  655. return (icmap_get_uint32_r(icmap_global_map, key_name, u32));
  656. }
  657. cs_error_t icmap_get_int64(const char *key_name, int64_t *i64)
  658. {
  659. return(icmap_get_int64_r(icmap_global_map, key_name, i64));
  660. }
  661. cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64)
  662. {
  663. return (icmap_get_uint64_r(icmap_global_map, key_name, u64));
  664. }
  665. cs_error_t icmap_get_float(const char *key_name, float *flt)
  666. {
  667. return (icmap_get_float_r(icmap_global_map, key_name, flt));
  668. }
  669. cs_error_t icmap_get_double(const char *key_name, double *dbl)
  670. {
  671. return (icmap_get_double_r(icmap_global_map, key_name, dbl));
  672. }
  673. cs_error_t icmap_get_string(const char *key_name, char **str)
  674. {
  675. cs_error_t res;
  676. size_t str_len;
  677. icmap_value_types_t type;
  678. res = icmap_get(key_name, NULL, &str_len, &type);
  679. if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
  680. if (res == CS_OK) {
  681. res = CS_ERR_INVALID_PARAM;
  682. }
  683. goto return_error;
  684. }
  685. *str = malloc(str_len);
  686. if (*str == NULL) {
  687. res = CS_ERR_NO_MEMORY;
  688. goto return_error;
  689. }
  690. res = icmap_get(key_name, *str, &str_len, &type);
  691. if (res != CS_OK) {
  692. free(*str);
  693. goto return_error;
  694. }
  695. return (CS_OK);
  696. return_error:
  697. return (res);
  698. }
  699. cs_error_t icmap_adjust_int_r(
  700. const icmap_map_t map,
  701. const char *key_name,
  702. int32_t step)
  703. {
  704. struct icmap_item *item;
  705. uint8_t u8;
  706. uint16_t u16;
  707. uint32_t u32;
  708. uint64_t u64;
  709. cs_error_t err = CS_OK;
  710. if (key_name == NULL) {
  711. return (CS_ERR_INVALID_PARAM);
  712. }
  713. item = qb_map_get(map->qb_map, key_name);
  714. if (item == NULL) {
  715. return (CS_ERR_NOT_EXIST);
  716. }
  717. switch (item->type) {
  718. case ICMAP_VALUETYPE_INT8:
  719. case ICMAP_VALUETYPE_UINT8:
  720. memcpy(&u8, item->value, sizeof(u8));
  721. u8 += step;
  722. err = icmap_set(key_name, &u8, sizeof(u8), item->type);
  723. break;
  724. case ICMAP_VALUETYPE_INT16:
  725. case ICMAP_VALUETYPE_UINT16:
  726. memcpy(&u16, item->value, sizeof(u16));
  727. u16 += step;
  728. err = icmap_set(key_name, &u16, sizeof(u16), item->type);
  729. break;
  730. case ICMAP_VALUETYPE_INT32:
  731. case ICMAP_VALUETYPE_UINT32:
  732. memcpy(&u32, item->value, sizeof(u32));
  733. u32 += step;
  734. err = icmap_set(key_name, &u32, sizeof(u32), item->type);
  735. break;
  736. case ICMAP_VALUETYPE_INT64:
  737. case ICMAP_VALUETYPE_UINT64:
  738. memcpy(&u64, item->value, sizeof(u64));
  739. u64 += step;
  740. err = icmap_set(key_name, &u64, sizeof(u64), item->type);
  741. break;
  742. case ICMAP_VALUETYPE_FLOAT:
  743. case ICMAP_VALUETYPE_DOUBLE:
  744. case ICMAP_VALUETYPE_STRING:
  745. case ICMAP_VALUETYPE_BINARY:
  746. err = CS_ERR_INVALID_PARAM;
  747. break;
  748. }
  749. return (err);
  750. }
  751. cs_error_t icmap_adjust_int(
  752. const char *key_name,
  753. int32_t step)
  754. {
  755. return (icmap_adjust_int_r(icmap_global_map, key_name, step));
  756. }
  757. cs_error_t icmap_fast_adjust_int_r(
  758. const icmap_map_t map,
  759. const char *key_name,
  760. int32_t step)
  761. {
  762. struct icmap_item *item;
  763. cs_error_t err = CS_OK;
  764. if (key_name == NULL) {
  765. return (CS_ERR_INVALID_PARAM);
  766. }
  767. item = qb_map_get(map->qb_map, key_name);
  768. if (item == NULL) {
  769. return (CS_ERR_NOT_EXIST);
  770. }
  771. switch (item->type) {
  772. case ICMAP_VALUETYPE_INT8:
  773. case ICMAP_VALUETYPE_UINT8:
  774. *(uint8_t *)item->value += step;
  775. break;
  776. case ICMAP_VALUETYPE_INT16:
  777. case ICMAP_VALUETYPE_UINT16:
  778. *(uint16_t *)item->value += step;
  779. break;
  780. case ICMAP_VALUETYPE_INT32:
  781. case ICMAP_VALUETYPE_UINT32:
  782. *(uint32_t *)item->value += step;
  783. break;
  784. case ICMAP_VALUETYPE_INT64:
  785. case ICMAP_VALUETYPE_UINT64:
  786. *(uint64_t *)item->value += step;
  787. break;
  788. case ICMAP_VALUETYPE_FLOAT:
  789. case ICMAP_VALUETYPE_DOUBLE:
  790. case ICMAP_VALUETYPE_STRING:
  791. case ICMAP_VALUETYPE_BINARY:
  792. err = CS_ERR_INVALID_PARAM;
  793. break;
  794. }
  795. if (err == CS_OK) {
  796. qb_map_put(map->qb_map, item->key_name, item);
  797. }
  798. return (err);
  799. }
  800. cs_error_t icmap_fast_adjust_int(
  801. const char *key_name,
  802. int32_t step)
  803. {
  804. return (icmap_fast_adjust_int_r(icmap_global_map, key_name, step));
  805. }
  806. cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name)
  807. {
  808. return (icmap_adjust_int_r(map, key_name, 1));
  809. }
  810. cs_error_t icmap_inc(const char *key_name)
  811. {
  812. return (icmap_inc_r(icmap_global_map, key_name));
  813. }
  814. cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name)
  815. {
  816. return (icmap_adjust_int_r(map, key_name, -1));
  817. }
  818. cs_error_t icmap_dec(const char *key_name)
  819. {
  820. return (icmap_dec_r(icmap_global_map, key_name));
  821. }
  822. cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name)
  823. {
  824. return (icmap_fast_adjust_int_r(map, key_name, 1));
  825. }
  826. cs_error_t icmap_fast_inc(const char *key_name)
  827. {
  828. return (icmap_fast_inc_r(icmap_global_map, key_name));
  829. }
  830. cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name)
  831. {
  832. return (icmap_fast_adjust_int_r(map, key_name, -1));
  833. }
  834. cs_error_t icmap_fast_dec(const char *key_name)
  835. {
  836. return (icmap_fast_dec_r(icmap_global_map, key_name));
  837. }
  838. icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix)
  839. {
  840. return (qb_map_pref_iter_create(map->qb_map, prefix));
  841. }
  842. icmap_iter_t icmap_iter_init(const char *prefix)
  843. {
  844. return (icmap_iter_init_r(icmap_global_map, prefix));
  845. }
  846. const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
  847. {
  848. struct icmap_item *item;
  849. const char *res;
  850. res = qb_map_iter_next(iter, (void **)&item);
  851. if (res == NULL) {
  852. return (res);
  853. }
  854. if (value_len != NULL) {
  855. *value_len = item->value_len;
  856. }
  857. if (type != NULL) {
  858. *type = item->type;
  859. }
  860. return (res);
  861. }
  862. void icmap_iter_finalize(icmap_iter_t iter)
  863. {
  864. qb_map_iter_free(iter);
  865. }
  866. static void icmap_notify_fn(uint32_t event, char *key, void *old_value, void *value, void *user_data)
  867. {
  868. icmap_track_t icmap_track = (icmap_track_t)user_data;
  869. struct icmap_item *new_item = (struct icmap_item *)value;
  870. struct icmap_item *old_item = (struct icmap_item *)old_value;
  871. struct icmap_notify_value new_val;
  872. struct icmap_notify_value old_val;
  873. if (value == NULL && old_value == NULL) {
  874. return ;
  875. }
  876. if (new_item != NULL) {
  877. new_val.type = new_item->type;
  878. new_val.len = new_item->value_len;
  879. new_val.data = new_item->value;
  880. } else {
  881. memset(&new_val, 0, sizeof(new_val));
  882. }
  883. /*
  884. * old_item == new_item if fast functions are used -> don't fill old value
  885. */
  886. if (old_item != NULL && old_item != new_item) {
  887. old_val.type = old_item->type;
  888. old_val.len = old_item->value_len;
  889. old_val.data = old_item->value;
  890. } else {
  891. memset(&old_val, 0, sizeof(old_val));
  892. }
  893. icmap_track->notify_fn(icmap_qbtt_to_tt(event),
  894. key,
  895. new_val,
  896. old_val,
  897. icmap_track->user_data);
  898. }
  899. cs_error_t icmap_track_add(
  900. const char *key_name,
  901. int32_t track_type,
  902. icmap_notify_fn_t notify_fn,
  903. void *user_data,
  904. icmap_track_t *icmap_track)
  905. {
  906. int32_t err;
  907. if (notify_fn == NULL || icmap_track == NULL) {
  908. return (CS_ERR_INVALID_PARAM);
  909. }
  910. if ((track_type & ~(ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX)) != 0) {
  911. return (CS_ERR_INVALID_PARAM);
  912. }
  913. *icmap_track = malloc(sizeof(**icmap_track));
  914. if (*icmap_track == NULL) {
  915. return (CS_ERR_NO_MEMORY);
  916. }
  917. memset(*icmap_track, 0, sizeof(**icmap_track));
  918. if (key_name != NULL) {
  919. (*icmap_track)->key_name = strdup(key_name);
  920. };
  921. (*icmap_track)->track_type = track_type;
  922. (*icmap_track)->notify_fn = notify_fn;
  923. (*icmap_track)->user_data = user_data;
  924. if ((err = qb_map_notify_add(icmap_global_map->qb_map, (*icmap_track)->key_name, icmap_notify_fn,
  925. icmap_tt_to_qbtt(track_type), *icmap_track)) != 0) {
  926. free((*icmap_track)->key_name);
  927. free(*icmap_track);
  928. return (qb_to_cs_error(err));
  929. }
  930. list_init(&(*icmap_track)->list);
  931. list_add (&(*icmap_track)->list, &icmap_track_list_head);
  932. return (CS_OK);
  933. }
  934. cs_error_t icmap_track_delete(icmap_track_t icmap_track)
  935. {
  936. int32_t err;
  937. if ((err = qb_map_notify_del_2(icmap_global_map->qb_map, icmap_track->key_name,
  938. icmap_notify_fn, icmap_tt_to_qbtt(icmap_track->track_type), icmap_track)) != 0) {
  939. return (qb_to_cs_error(err));
  940. }
  941. list_del(&icmap_track->list);
  942. free(icmap_track->key_name);
  943. free(icmap_track);
  944. return (CS_OK);
  945. }
  946. void *icmap_track_get_user_data(icmap_track_t icmap_track)
  947. {
  948. return (icmap_track->user_data);
  949. }
  950. cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
  951. {
  952. struct list_head *iter;
  953. struct icmap_ro_access_item *icmap_ro_ai;
  954. for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
  955. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  956. if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
  957. /*
  958. * We found item
  959. */
  960. if (ro_access) {
  961. return (CS_ERR_EXIST);
  962. } else {
  963. list_del(&icmap_ro_ai->list);
  964. free(icmap_ro_ai->key_name);
  965. free(icmap_ro_ai);
  966. return (CS_OK);
  967. }
  968. }
  969. }
  970. if (!ro_access) {
  971. return (CS_ERR_NOT_EXIST);
  972. }
  973. icmap_ro_ai = malloc(sizeof(*icmap_ro_ai));
  974. if (icmap_ro_ai == NULL) {
  975. return (CS_ERR_NO_MEMORY);
  976. }
  977. memset(icmap_ro_ai, 0, sizeof(*icmap_ro_ai));
  978. icmap_ro_ai->key_name = strdup(key_name);
  979. if (icmap_ro_ai->key_name == NULL) {
  980. free(icmap_ro_ai);
  981. return (CS_ERR_NO_MEMORY);
  982. }
  983. icmap_ro_ai->prefix = prefix;
  984. list_init(&icmap_ro_ai->list);
  985. list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
  986. return (CS_OK);
  987. }
  988. int icmap_is_key_ro(const char *key_name)
  989. {
  990. struct list_head *iter;
  991. struct icmap_ro_access_item *icmap_ro_ai;
  992. for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
  993. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  994. if (icmap_ro_ai->prefix) {
  995. if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
  996. continue;
  997. if (strncmp(icmap_ro_ai->key_name, key_name, strlen(icmap_ro_ai->key_name)) == 0) {
  998. return (CS_TRUE);
  999. }
  1000. } else {
  1001. if (strcmp(icmap_ro_ai->key_name, key_name) == 0) {
  1002. return (CS_TRUE);
  1003. }
  1004. }
  1005. }
  1006. return (CS_FALSE);
  1007. }