icmap.c 30 KB

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