icmap.c 30 KB

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