icmap.c 30 KB

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