icmap.c 30 KB

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