icmap.c 30 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333
  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. case ICMAP_VALUETYPE_BOOLEAN:
  264. res = 0;
  265. break;
  266. }
  267. return (res);
  268. }
  269. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type)
  270. {
  271. if (value_len > ICMAP_MAX_VALUE_LEN) {
  272. return (-1);
  273. }
  274. if (type != ICMAP_VALUETYPE_STRING && type != ICMAP_VALUETYPE_BINARY) {
  275. if (icmap_get_valuetype_len(type) == value_len) {
  276. return (0);
  277. } else {
  278. return (-1);
  279. }
  280. }
  281. if (type == ICMAP_VALUETYPE_STRING) {
  282. /*
  283. * value_len can be shorter then real string length, but never
  284. * longer (+ 1 is because of 0 at the end of string)
  285. */
  286. if (value_len > strlen((const char *)value) + 1) {
  287. return (-1);
  288. } else {
  289. return (0);
  290. }
  291. }
  292. return (0);
  293. }
  294. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type)
  295. {
  296. size_t ptr_len;
  297. if (item->type != type) {
  298. return (0);
  299. }
  300. if (item->type == ICMAP_VALUETYPE_STRING) {
  301. ptr_len = strlen((const char *)value);
  302. if (ptr_len > value_len) {
  303. ptr_len = value_len;
  304. }
  305. ptr_len++;
  306. } else {
  307. ptr_len = value_len;
  308. }
  309. if (item->value_len == ptr_len) {
  310. return (memcmp(item->value, value, value_len) == 0);
  311. };
  312. return (0);
  313. }
  314. int icmap_key_value_eq(
  315. const icmap_map_t map1,
  316. const char *key_name1,
  317. const icmap_map_t map2,
  318. const char *key_name2)
  319. {
  320. struct icmap_item *item1, *item2;
  321. if (map1 == NULL || key_name1 == NULL || map2 == NULL || key_name2 == NULL) {
  322. return (0);
  323. }
  324. item1 = qb_map_get(map1->qb_map, key_name1);
  325. item2 = qb_map_get(map2->qb_map, key_name2);
  326. if (item1 == NULL || item2 == NULL) {
  327. return (0);
  328. }
  329. return (icmap_item_eq(item1, item2->value, item2->value_len, item2->type));
  330. }
  331. cs_error_t icmap_set_r(
  332. const icmap_map_t map,
  333. const char *key_name,
  334. const void *value,
  335. size_t value_len,
  336. icmap_value_types_t type)
  337. {
  338. struct icmap_item *item;
  339. struct icmap_item *new_item;
  340. size_t new_value_len;
  341. size_t new_item_size;
  342. if (value == NULL || key_name == NULL) {
  343. return (CS_ERR_INVALID_PARAM);
  344. }
  345. if (icmap_check_value_len(value, value_len, type) != 0) {
  346. return (CS_ERR_INVALID_PARAM);
  347. }
  348. item = qb_map_get(map->qb_map, key_name);
  349. if (item != NULL) {
  350. /*
  351. * Check that key is really changed
  352. */
  353. if (icmap_item_eq(item, value, value_len, type)) {
  354. return (CS_OK);
  355. }
  356. } else {
  357. if (icmap_check_key_name(key_name) != 0) {
  358. return (CS_ERR_NAME_TOO_LONG);
  359. }
  360. }
  361. if (type == ICMAP_VALUETYPE_BINARY || type == ICMAP_VALUETYPE_STRING) {
  362. if (type == ICMAP_VALUETYPE_STRING) {
  363. new_value_len = strlen((const char *)value);
  364. if (new_value_len > value_len) {
  365. new_value_len = value_len;
  366. }
  367. new_value_len++;
  368. } else {
  369. new_value_len = value_len;
  370. }
  371. } else {
  372. new_value_len = icmap_get_valuetype_len(type);
  373. }
  374. new_item_size = sizeof(struct icmap_item) + new_value_len;
  375. new_item = malloc(new_item_size);
  376. if (new_item == NULL) {
  377. return (CS_ERR_NO_MEMORY);
  378. }
  379. memset(new_item, 0, new_item_size);
  380. if (item == NULL) {
  381. new_item->key_name = strdup(key_name);
  382. if (new_item->key_name == NULL) {
  383. free(new_item);
  384. return (CS_ERR_NO_MEMORY);
  385. }
  386. } else {
  387. new_item->key_name = item->key_name;
  388. item->key_name = NULL;
  389. }
  390. new_item->type = type;
  391. new_item->value_len = new_value_len;
  392. memcpy(new_item->value, value, new_value_len);
  393. if (new_item->type == ICMAP_VALUETYPE_STRING) {
  394. ((char *)new_item->value)[new_value_len - 1] = 0;
  395. }
  396. qb_map_put(map->qb_map, new_item->key_name, new_item);
  397. return (CS_OK);
  398. }
  399. cs_error_t icmap_set(
  400. const char *key_name,
  401. const void *value,
  402. size_t value_len,
  403. icmap_value_types_t type)
  404. {
  405. return (icmap_set_r(icmap_global_map, key_name, value, value_len, type));
  406. }
  407. cs_error_t icmap_set_int8_r(const icmap_map_t map, const char *key_name, int8_t value)
  408. {
  409. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT8));
  410. }
  411. cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value)
  412. {
  413. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT8));
  414. }
  415. cs_error_t icmap_set_int16_r(const icmap_map_t map, const char *key_name, int16_t value)
  416. {
  417. return (icmap_set_r(map,key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT16));
  418. }
  419. cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value)
  420. {
  421. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT16));
  422. }
  423. cs_error_t icmap_set_int32_r(const icmap_map_t map, const char *key_name, int32_t value)
  424. {
  425. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT32));
  426. }
  427. cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value)
  428. {
  429. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT32));
  430. }
  431. cs_error_t icmap_set_int64_r(const icmap_map_t map, const char *key_name, int64_t value)
  432. {
  433. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT64));
  434. }
  435. cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value)
  436. {
  437. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT64));
  438. }
  439. cs_error_t icmap_set_float_r(const icmap_map_t map, const char *key_name, float value)
  440. {
  441. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_FLOAT));
  442. }
  443. cs_error_t icmap_set_double_r(const icmap_map_t map, const char *key_name, double value)
  444. {
  445. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_DOUBLE));
  446. }
  447. cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value)
  448. {
  449. if (value == NULL) {
  450. return (CS_ERR_INVALID_PARAM);
  451. }
  452. return (icmap_set_r(map, key_name, value, strlen(value), ICMAP_VALUETYPE_STRING));
  453. }
  454. cs_error_t icmap_set_int8(const char *key_name, int8_t value)
  455. {
  456. return (icmap_set_int8_r(icmap_global_map, key_name, value));
  457. }
  458. cs_error_t icmap_set_uint8(const char *key_name, uint8_t value)
  459. {
  460. return (icmap_set_uint8_r(icmap_global_map, key_name, value));
  461. }
  462. cs_error_t icmap_set_int16(const char *key_name, int16_t value)
  463. {
  464. return (icmap_set_int16_r(icmap_global_map, key_name, value));
  465. }
  466. cs_error_t icmap_set_uint16(const char *key_name, uint16_t value)
  467. {
  468. return (icmap_set_uint16_r(icmap_global_map, key_name, value));
  469. }
  470. cs_error_t icmap_set_int32(const char *key_name, int32_t value)
  471. {
  472. return (icmap_set_int32_r(icmap_global_map, key_name, value));
  473. }
  474. cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
  475. {
  476. return (icmap_set_uint32_r(icmap_global_map, key_name, value));
  477. }
  478. cs_error_t icmap_set_int64(const char *key_name, int64_t value)
  479. {
  480. return (icmap_set_int64_r(icmap_global_map, key_name, value));
  481. }
  482. cs_error_t icmap_set_uint64(const char *key_name, uint64_t value)
  483. {
  484. return (icmap_set_uint64_r(icmap_global_map, key_name, value));
  485. }
  486. cs_error_t icmap_set_float(const char *key_name, float value)
  487. {
  488. return (icmap_set_float_r(icmap_global_map, key_name, value));
  489. }
  490. cs_error_t icmap_set_double(const char *key_name, double value)
  491. {
  492. return (icmap_set_double_r(icmap_global_map, key_name, value));
  493. }
  494. cs_error_t icmap_set_string(const char *key_name, const char *value)
  495. {
  496. return (icmap_set_string_r(icmap_global_map, key_name, value));
  497. }
  498. cs_error_t icmap_delete_r(const icmap_map_t map, const char *key_name)
  499. {
  500. struct icmap_item *item;
  501. if (key_name == NULL) {
  502. return (CS_ERR_INVALID_PARAM);
  503. }
  504. item = qb_map_get(map->qb_map, key_name);
  505. if (item == NULL) {
  506. return (CS_ERR_NOT_EXIST);
  507. }
  508. if (qb_map_rm(map->qb_map, item->key_name) != QB_TRUE) {
  509. return (CS_ERR_NOT_EXIST);
  510. }
  511. return (CS_OK);
  512. }
  513. cs_error_t icmap_delete(const char *key_name)
  514. {
  515. return (icmap_delete_r(icmap_global_map, key_name));
  516. }
  517. static cs_error_t icmap_get_ref_r(
  518. const icmap_map_t map,
  519. const char *key_name,
  520. void **value,
  521. size_t *value_len,
  522. icmap_value_types_t *type)
  523. {
  524. struct icmap_item *item;
  525. if (key_name == NULL) {
  526. return (CS_ERR_INVALID_PARAM);
  527. }
  528. item = qb_map_get(map->qb_map, key_name);
  529. if (item == NULL) {
  530. return (CS_ERR_NOT_EXIST);
  531. }
  532. if (type != NULL) {
  533. *type = item->type;
  534. }
  535. if (value_len != NULL) {
  536. *value_len = item->value_len;
  537. }
  538. if (value != NULL) {
  539. *value = item->value;
  540. }
  541. return (CS_OK);
  542. }
  543. cs_error_t icmap_get_r(
  544. const icmap_map_t map,
  545. const char *key_name,
  546. void *value,
  547. size_t *value_len,
  548. icmap_value_types_t *type)
  549. {
  550. cs_error_t res;
  551. void *tmp_value;
  552. size_t tmp_value_len;
  553. res = icmap_get_ref_r(map, key_name, &tmp_value, &tmp_value_len, type);
  554. if (res != CS_OK) {
  555. return (res);
  556. }
  557. if (value == NULL) {
  558. if (value_len != NULL) {
  559. *value_len = tmp_value_len;
  560. }
  561. } else {
  562. if (value_len == NULL || *value_len < tmp_value_len) {
  563. return (CS_ERR_INVALID_PARAM);
  564. }
  565. *value_len = tmp_value_len;
  566. memcpy(value, tmp_value, tmp_value_len);
  567. }
  568. return (CS_OK);
  569. }
  570. cs_error_t icmap_get(
  571. const char *key_name,
  572. void *value,
  573. size_t *value_len,
  574. icmap_value_types_t *type)
  575. {
  576. return (icmap_get_r(icmap_global_map, key_name, value, value_len, type));
  577. }
  578. cs_error_t icmap_get_string_r(icmap_map_t map, const char *key_name, char **str)
  579. {
  580. cs_error_t res;
  581. size_t str_len;
  582. icmap_value_types_t type;
  583. res = icmap_get_r(map, key_name, NULL, &str_len, &type);
  584. if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
  585. if (res == CS_OK) {
  586. res = CS_ERR_INVALID_PARAM;
  587. }
  588. goto return_error;
  589. }
  590. *str = malloc(str_len);
  591. if (*str == NULL) {
  592. res = CS_ERR_NO_MEMORY;
  593. goto return_error;
  594. }
  595. res = icmap_get_r(map, key_name, *str, &str_len, &type);
  596. if (res != CS_OK) {
  597. free(*str);
  598. goto return_error;
  599. }
  600. return (CS_OK);
  601. return_error:
  602. return (res);
  603. }
  604. static cs_error_t icmap_get_int_r(
  605. const icmap_map_t map,
  606. const char *key_name,
  607. void *value,
  608. icmap_value_types_t type)
  609. {
  610. char key_value[16];
  611. size_t key_size;
  612. cs_error_t err;
  613. icmap_value_types_t key_type;
  614. key_size = sizeof(key_value);
  615. memset(key_value, 0, key_size);
  616. err = icmap_get_r(map, key_name, key_value, &key_size, &key_type);
  617. if (err != CS_OK)
  618. return (err);
  619. if (key_type != type) {
  620. return (CS_ERR_INVALID_PARAM);
  621. }
  622. memcpy(value, key_value, icmap_get_valuetype_len(key_type));
  623. return (CS_OK);
  624. }
  625. cs_error_t icmap_get_int8_r(const icmap_map_t map, const char *key_name, int8_t *i8)
  626. {
  627. return (icmap_get_int_r(map, key_name, i8, ICMAP_VALUETYPE_INT8));
  628. }
  629. cs_error_t icmap_get_uint8_r(const icmap_map_t map, const char *key_name, uint8_t *u8)
  630. {
  631. return (icmap_get_int_r(map, key_name, u8, ICMAP_VALUETYPE_UINT8));
  632. }
  633. cs_error_t icmap_get_int16_r(const icmap_map_t map, const char *key_name, int16_t *i16)
  634. {
  635. return (icmap_get_int_r(map, key_name, i16, ICMAP_VALUETYPE_INT16));
  636. }
  637. cs_error_t icmap_get_uint16_r(const icmap_map_t map, const char *key_name, uint16_t *u16)
  638. {
  639. return (icmap_get_int_r(map, key_name, u16, ICMAP_VALUETYPE_UINT16));
  640. }
  641. cs_error_t icmap_get_int32_r(const icmap_map_t map, const char *key_name, int32_t *i32)
  642. {
  643. return (icmap_get_int_r(map, key_name, i32, ICMAP_VALUETYPE_INT32));
  644. }
  645. cs_error_t icmap_get_uint32_r(const icmap_map_t map, const char *key_name, uint32_t *u32)
  646. {
  647. return (icmap_get_int_r(map, key_name, u32, ICMAP_VALUETYPE_UINT32));
  648. }
  649. cs_error_t icmap_get_int64_r(const icmap_map_t map, const char *key_name, int64_t *i64)
  650. {
  651. return(icmap_get_int_r(map, key_name, i64, ICMAP_VALUETYPE_INT64));
  652. }
  653. cs_error_t icmap_get_uint64_r(const icmap_map_t map, const char *key_name, uint64_t *u64)
  654. {
  655. return (icmap_get_int_r(map, key_name, u64, ICMAP_VALUETYPE_UINT64));
  656. }
  657. cs_error_t icmap_get_float_r(const icmap_map_t map, const char *key_name, float *flt)
  658. {
  659. return (icmap_get_int_r(map, key_name, flt, ICMAP_VALUETYPE_FLOAT));
  660. }
  661. cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, double *dbl)
  662. {
  663. return (icmap_get_int_r(map, key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
  664. }
  665. cs_error_t icmap_get_string(const char *key_name, char **str)
  666. {
  667. return (icmap_get_string_r(icmap_global_map, key_name, str));
  668. }
  669. cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
  670. {
  671. return (icmap_get_int8_r(icmap_global_map, key_name, i8));
  672. }
  673. cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8)
  674. {
  675. return (icmap_get_uint8_r(icmap_global_map, key_name, u8));
  676. }
  677. cs_error_t icmap_get_int16(const char *key_name, int16_t *i16)
  678. {
  679. return (icmap_get_int16_r(icmap_global_map, key_name, i16));
  680. }
  681. cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16)
  682. {
  683. return (icmap_get_uint16_r(icmap_global_map, key_name, u16));
  684. }
  685. cs_error_t icmap_get_int32(const char *key_name, int32_t *i32)
  686. {
  687. return (icmap_get_int32_r(icmap_global_map, key_name, i32));
  688. }
  689. cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32)
  690. {
  691. return (icmap_get_uint32_r(icmap_global_map, key_name, u32));
  692. }
  693. cs_error_t icmap_get_int64(const char *key_name, int64_t *i64)
  694. {
  695. return(icmap_get_int64_r(icmap_global_map, key_name, i64));
  696. }
  697. cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64)
  698. {
  699. return (icmap_get_uint64_r(icmap_global_map, key_name, u64));
  700. }
  701. cs_error_t icmap_get_float(const char *key_name, float *flt)
  702. {
  703. return (icmap_get_float_r(icmap_global_map, key_name, flt));
  704. }
  705. cs_error_t icmap_get_double(const char *key_name, double *dbl)
  706. {
  707. return (icmap_get_double_r(icmap_global_map, key_name, dbl));
  708. }
  709. cs_error_t icmap_adjust_int_r(
  710. const icmap_map_t map,
  711. const char *key_name,
  712. int32_t step)
  713. {
  714. struct icmap_item *item;
  715. uint8_t u8;
  716. uint16_t u16;
  717. uint32_t u32;
  718. uint64_t u64;
  719. cs_error_t err = CS_OK;
  720. if (key_name == NULL) {
  721. return (CS_ERR_INVALID_PARAM);
  722. }
  723. item = qb_map_get(map->qb_map, key_name);
  724. if (item == NULL) {
  725. return (CS_ERR_NOT_EXIST);
  726. }
  727. switch (item->type) {
  728. case ICMAP_VALUETYPE_INT8:
  729. case ICMAP_VALUETYPE_UINT8:
  730. memcpy(&u8, item->value, sizeof(u8));
  731. u8 += step;
  732. err = icmap_set(key_name, &u8, sizeof(u8), item->type);
  733. break;
  734. case ICMAP_VALUETYPE_INT16:
  735. case ICMAP_VALUETYPE_UINT16:
  736. memcpy(&u16, item->value, sizeof(u16));
  737. u16 += step;
  738. err = icmap_set(key_name, &u16, sizeof(u16), item->type);
  739. break;
  740. case ICMAP_VALUETYPE_INT32:
  741. case ICMAP_VALUETYPE_UINT32:
  742. memcpy(&u32, item->value, sizeof(u32));
  743. u32 += step;
  744. err = icmap_set(key_name, &u32, sizeof(u32), item->type);
  745. break;
  746. case ICMAP_VALUETYPE_INT64:
  747. case ICMAP_VALUETYPE_UINT64:
  748. memcpy(&u64, item->value, sizeof(u64));
  749. u64 += step;
  750. err = icmap_set(key_name, &u64, sizeof(u64), item->type);
  751. break;
  752. case ICMAP_VALUETYPE_FLOAT:
  753. case ICMAP_VALUETYPE_DOUBLE:
  754. case ICMAP_VALUETYPE_STRING:
  755. case ICMAP_VALUETYPE_BINARY:
  756. case ICMAP_VALUETYPE_BOOLEAN:
  757. err = CS_ERR_INVALID_PARAM;
  758. break;
  759. }
  760. return (err);
  761. }
  762. cs_error_t icmap_adjust_int(
  763. const char *key_name,
  764. int32_t step)
  765. {
  766. return (icmap_adjust_int_r(icmap_global_map, key_name, step));
  767. }
  768. cs_error_t icmap_fast_adjust_int_r(
  769. const icmap_map_t map,
  770. const char *key_name,
  771. int32_t step)
  772. {
  773. struct icmap_item *item;
  774. cs_error_t err = CS_OK;
  775. if (key_name == NULL) {
  776. return (CS_ERR_INVALID_PARAM);
  777. }
  778. item = qb_map_get(map->qb_map, key_name);
  779. if (item == NULL) {
  780. return (CS_ERR_NOT_EXIST);
  781. }
  782. switch (item->type) {
  783. case ICMAP_VALUETYPE_INT8:
  784. case ICMAP_VALUETYPE_UINT8:
  785. *(uint8_t *)item->value += step;
  786. break;
  787. case ICMAP_VALUETYPE_INT16:
  788. case ICMAP_VALUETYPE_UINT16:
  789. *(uint16_t *)item->value += step;
  790. break;
  791. case ICMAP_VALUETYPE_INT32:
  792. case ICMAP_VALUETYPE_UINT32:
  793. *(uint32_t *)item->value += step;
  794. break;
  795. case ICMAP_VALUETYPE_INT64:
  796. case ICMAP_VALUETYPE_UINT64:
  797. *(uint64_t *)item->value += step;
  798. break;
  799. case ICMAP_VALUETYPE_FLOAT:
  800. case ICMAP_VALUETYPE_DOUBLE:
  801. case ICMAP_VALUETYPE_STRING:
  802. case ICMAP_VALUETYPE_BINARY:
  803. case ICMAP_VALUETYPE_BOOLEAN:
  804. err = CS_ERR_INVALID_PARAM;
  805. break;
  806. }
  807. if (err == CS_OK) {
  808. qb_map_put(map->qb_map, item->key_name, item);
  809. }
  810. return (err);
  811. }
  812. cs_error_t icmap_fast_adjust_int(
  813. const char *key_name,
  814. int32_t step)
  815. {
  816. return (icmap_fast_adjust_int_r(icmap_global_map, key_name, step));
  817. }
  818. cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name)
  819. {
  820. return (icmap_adjust_int_r(map, key_name, 1));
  821. }
  822. cs_error_t icmap_inc(const char *key_name)
  823. {
  824. return (icmap_inc_r(icmap_global_map, key_name));
  825. }
  826. cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name)
  827. {
  828. return (icmap_adjust_int_r(map, key_name, -1));
  829. }
  830. cs_error_t icmap_dec(const char *key_name)
  831. {
  832. return (icmap_dec_r(icmap_global_map, key_name));
  833. }
  834. cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name)
  835. {
  836. return (icmap_fast_adjust_int_r(map, key_name, 1));
  837. }
  838. cs_error_t icmap_fast_inc(const char *key_name)
  839. {
  840. return (icmap_fast_inc_r(icmap_global_map, key_name));
  841. }
  842. cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name)
  843. {
  844. return (icmap_fast_adjust_int_r(map, key_name, -1));
  845. }
  846. cs_error_t icmap_fast_dec(const char *key_name)
  847. {
  848. return (icmap_fast_dec_r(icmap_global_map, key_name));
  849. }
  850. icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix)
  851. {
  852. return (qb_map_pref_iter_create(map->qb_map, prefix));
  853. }
  854. icmap_iter_t icmap_iter_init(const char *prefix)
  855. {
  856. return (icmap_iter_init_r(icmap_global_map, prefix));
  857. }
  858. const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
  859. {
  860. struct icmap_item *item;
  861. const char *res;
  862. res = qb_map_iter_next(iter, (void **)&item);
  863. if (res == NULL) {
  864. return (res);
  865. }
  866. if (value_len != NULL) {
  867. *value_len = item->value_len;
  868. }
  869. if (type != NULL) {
  870. *type = item->type;
  871. }
  872. return (res);
  873. }
  874. void icmap_iter_finalize(icmap_iter_t iter)
  875. {
  876. qb_map_iter_free(iter);
  877. }
  878. static void icmap_notify_fn(uint32_t event, char *key, void *old_value, void *value, void *user_data)
  879. {
  880. icmap_track_t icmap_track = (icmap_track_t)user_data;
  881. struct icmap_item *new_item = (struct icmap_item *)value;
  882. struct icmap_item *old_item = (struct icmap_item *)old_value;
  883. struct icmap_notify_value new_val;
  884. struct icmap_notify_value old_val;
  885. if (value == NULL && old_value == NULL) {
  886. return ;
  887. }
  888. if (new_item != NULL) {
  889. new_val.type = new_item->type;
  890. new_val.len = new_item->value_len;
  891. new_val.data = new_item->value;
  892. } else {
  893. memset(&new_val, 0, sizeof(new_val));
  894. }
  895. /*
  896. * old_item == new_item if fast functions are used -> don't fill old value
  897. */
  898. if (old_item != NULL && old_item != new_item) {
  899. old_val.type = old_item->type;
  900. old_val.len = old_item->value_len;
  901. old_val.data = old_item->value;
  902. } else {
  903. memset(&old_val, 0, sizeof(old_val));
  904. }
  905. icmap_track->notify_fn(icmap_qbtt_to_tt(event),
  906. key,
  907. new_val,
  908. old_val,
  909. icmap_track->user_data);
  910. }
  911. cs_error_t icmap_track_add(
  912. const char *key_name,
  913. int32_t track_type,
  914. icmap_notify_fn_t notify_fn,
  915. void *user_data,
  916. icmap_track_t *icmap_track)
  917. {
  918. int32_t err;
  919. if (notify_fn == NULL || icmap_track == NULL) {
  920. return (CS_ERR_INVALID_PARAM);
  921. }
  922. if ((track_type & ~(ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX)) != 0) {
  923. return (CS_ERR_INVALID_PARAM);
  924. }
  925. *icmap_track = malloc(sizeof(**icmap_track));
  926. if (*icmap_track == NULL) {
  927. return (CS_ERR_NO_MEMORY);
  928. }
  929. memset(*icmap_track, 0, sizeof(**icmap_track));
  930. if (key_name != NULL) {
  931. (*icmap_track)->key_name = strdup(key_name);
  932. };
  933. (*icmap_track)->track_type = track_type;
  934. (*icmap_track)->notify_fn = notify_fn;
  935. (*icmap_track)->user_data = user_data;
  936. if ((err = qb_map_notify_add(icmap_global_map->qb_map, (*icmap_track)->key_name, icmap_notify_fn,
  937. icmap_tt_to_qbtt(track_type), *icmap_track)) != 0) {
  938. free((*icmap_track)->key_name);
  939. free(*icmap_track);
  940. return (qb_to_cs_error(err));
  941. }
  942. qb_list_init(&(*icmap_track)->list);
  943. qb_list_add (&(*icmap_track)->list, &icmap_track_list_head);
  944. return (CS_OK);
  945. }
  946. cs_error_t icmap_track_delete(icmap_track_t icmap_track)
  947. {
  948. int32_t err;
  949. if ((err = qb_map_notify_del_2(icmap_global_map->qb_map, icmap_track->key_name,
  950. icmap_notify_fn, icmap_tt_to_qbtt(icmap_track->track_type), icmap_track)) != 0) {
  951. return (qb_to_cs_error(err));
  952. }
  953. qb_list_del(&icmap_track->list);
  954. free(icmap_track->key_name);
  955. free(icmap_track);
  956. return (CS_OK);
  957. }
  958. void *icmap_track_get_user_data(icmap_track_t icmap_track)
  959. {
  960. return (icmap_track->user_data);
  961. }
  962. cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
  963. {
  964. struct qb_list_head *iter, *tmp_iter;
  965. struct icmap_ro_access_item *icmap_ro_ai;
  966. qb_list_for_each_safe(iter, tmp_iter, &icmap_ro_access_item_list_head) {
  967. icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
  968. if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
  969. /*
  970. * We found item
  971. */
  972. if (ro_access) {
  973. return (CS_ERR_EXIST);
  974. } else {
  975. qb_list_del(&icmap_ro_ai->list);
  976. free(icmap_ro_ai->key_name);
  977. free(icmap_ro_ai);
  978. return (CS_OK);
  979. }
  980. }
  981. }
  982. if (!ro_access) {
  983. return (CS_ERR_NOT_EXIST);
  984. }
  985. icmap_ro_ai = malloc(sizeof(*icmap_ro_ai));
  986. if (icmap_ro_ai == NULL) {
  987. return (CS_ERR_NO_MEMORY);
  988. }
  989. memset(icmap_ro_ai, 0, sizeof(*icmap_ro_ai));
  990. icmap_ro_ai->key_name = strdup(key_name);
  991. if (icmap_ro_ai->key_name == NULL) {
  992. free(icmap_ro_ai);
  993. return (CS_ERR_NO_MEMORY);
  994. }
  995. icmap_ro_ai->prefix = prefix;
  996. qb_list_init(&icmap_ro_ai->list);
  997. qb_list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
  998. return (CS_OK);
  999. }
  1000. int icmap_is_key_ro(const char *key_name)
  1001. {
  1002. struct qb_list_head *iter;
  1003. struct icmap_ro_access_item *icmap_ro_ai;
  1004. qb_list_for_each(iter, &icmap_ro_access_item_list_head) {
  1005. icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
  1006. if (icmap_ro_ai->prefix) {
  1007. if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
  1008. continue;
  1009. if (strncmp(icmap_ro_ai->key_name, key_name, strlen(icmap_ro_ai->key_name)) == 0) {
  1010. return (CS_TRUE);
  1011. }
  1012. } else {
  1013. if (strcmp(icmap_ro_ai->key_name, key_name) == 0) {
  1014. return (CS_TRUE);
  1015. }
  1016. }
  1017. }
  1018. return (CS_FALSE);
  1019. }
  1020. cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map)
  1021. {
  1022. icmap_iter_t iter;
  1023. size_t value_len;
  1024. icmap_value_types_t value_type;
  1025. const char *key_name;
  1026. cs_error_t err;
  1027. void *value;
  1028. iter = icmap_iter_init_r(src_map, NULL);
  1029. if (iter == NULL) {
  1030. return (CS_ERR_NO_MEMORY);
  1031. }
  1032. err = CS_OK;
  1033. while ((key_name = icmap_iter_next(iter, &value_len, &value_type)) != NULL) {
  1034. err = icmap_get_ref_r(src_map, key_name, &value, &value_len, &value_type);
  1035. if (err != CS_OK) {
  1036. goto exit_iter_finalize;
  1037. }
  1038. err = icmap_set_r(dst_map, key_name, value, value_len, value_type);
  1039. if (err != CS_OK) {
  1040. goto exit_iter_finalize;
  1041. }
  1042. }
  1043. exit_iter_finalize:
  1044. icmap_iter_finalize(iter);
  1045. return (err);
  1046. }