icmap.c 30 KB

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