icmap.c 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335
  1. /*
  2. * Copyright (c) 2011 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Jan Friesse (jfriesse@redhat.com)
  7. *
  8. * This software licensed under BSD license, the text of which follows:
  9. *
  10. * Redistribution and use in source and binary forms, with or without
  11. * modification, are permitted provided that the following conditions are met:
  12. *
  13. * - Redistributions of source code must retain the above copyright notice,
  14. * this list of conditions and the following disclaimer.
  15. * - Redistributions in binary form must reproduce the above copyright notice,
  16. * this list of conditions and the following disclaimer in the documentation
  17. * and/or other materials provided with the distribution.
  18. * - Neither the name of the Red Hat, Inc. nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  32. * THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include <config.h>
  35. #include <string.h>
  36. #include <stdio.h>
  37. #include <corosync/corotypes.h>
  38. #include <qb/qbdefs.h>
  39. #include <qb/qblist.h>
  40. #include <corosync/icmap.h>
  41. #define ICMAP_MAX_VALUE_LEN (16*1024)
  42. struct icmap_item {
  43. char *key_name;
  44. icmap_value_types_t type;
  45. size_t value_len;
  46. char value[];
  47. };
  48. struct icmap_map {
  49. qb_map_t *qb_map;
  50. };
  51. static icmap_map_t icmap_global_map;
  52. struct icmap_track {
  53. char *key_name;
  54. int32_t track_type;
  55. icmap_notify_fn_t notify_fn;
  56. void *user_data;
  57. struct qb_list_head list;
  58. };
  59. struct icmap_ro_access_item {
  60. char *key_name;
  61. int prefix;
  62. struct qb_list_head list;
  63. };
  64. QB_LIST_DECLARE (icmap_ro_access_item_list_head);
  65. QB_LIST_DECLARE (icmap_track_list_head);
  66. /*
  67. * Static functions declarations
  68. */
  69. /*
  70. * Check if key_name is valid icmap key name. Returns 0 on success, and -1 on fail
  71. */
  72. static int icmap_check_key_name(const char *key_name);
  73. /*
  74. * Check that value with given type has correct length value_len. Returns 0 on success,
  75. * and -1 on fail
  76. */
  77. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type);
  78. /*
  79. * Returns length of value of given type, or 0 for string and binary data type
  80. */
  81. static size_t icmap_get_valuetype_len(icmap_value_types_t type);
  82. /*
  83. * Converts track type of icmap to qb
  84. */
  85. static int32_t icmap_tt_to_qbtt(int32_t track_type);
  86. /*
  87. * Convert track type of qb to icmap
  88. */
  89. static int32_t icmap_qbtt_to_tt(int32_t track_type);
  90. /*
  91. * Checks if item has same value as value with value_len and given type. Returns 0 if not, otherwise !0.
  92. */
  93. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type);
  94. /*
  95. * Checks if given character is valid in key name. Returns 0 if not, otherwise !0.
  96. */
  97. static int icmap_is_valid_name_char(char c);
  98. /*
  99. * Helper for getting integer and float value with given type for key key_name and store it in value.
  100. */
  101. static cs_error_t icmap_get_int_r(
  102. const icmap_map_t map,
  103. const char *key_name,
  104. void *value,
  105. icmap_value_types_t type);
  106. /*
  107. * Return raw item value data. Internal function used by icmap_get_r which does most
  108. * of arguments validity checks but doesn't copy data (it returns raw item data
  109. * pointer). It's not very safe tho it's static.
  110. */
  111. static cs_error_t icmap_get_ref_r(
  112. const icmap_map_t map,
  113. const char *key_name,
  114. void **value,
  115. size_t *value_len,
  116. icmap_value_types_t *type);
  117. /*
  118. * Function implementation
  119. */
  120. static int32_t icmap_tt_to_qbtt(int32_t track_type)
  121. {
  122. int32_t res = 0;
  123. if (track_type & ICMAP_TRACK_DELETE) {
  124. res |= QB_MAP_NOTIFY_DELETED;
  125. }
  126. if (track_type & ICMAP_TRACK_MODIFY) {
  127. res |= QB_MAP_NOTIFY_REPLACED;
  128. }
  129. if (track_type & ICMAP_TRACK_ADD) {
  130. res |= QB_MAP_NOTIFY_INSERTED;
  131. }
  132. if (track_type & ICMAP_TRACK_PREFIX) {
  133. res |= QB_MAP_NOTIFY_RECURSIVE;
  134. }
  135. return (res);
  136. }
  137. static int32_t icmap_qbtt_to_tt(int32_t track_type)
  138. {
  139. int32_t res = 0;
  140. if (track_type & QB_MAP_NOTIFY_DELETED) {
  141. res |= ICMAP_TRACK_DELETE;
  142. }
  143. if (track_type & QB_MAP_NOTIFY_REPLACED) {
  144. res |= ICMAP_TRACK_MODIFY;
  145. }
  146. if (track_type & QB_MAP_NOTIFY_INSERTED) {
  147. res |= ICMAP_TRACK_ADD;
  148. }
  149. if (track_type & QB_MAP_NOTIFY_RECURSIVE) {
  150. res |= ICMAP_TRACK_PREFIX;
  151. }
  152. return (res);
  153. }
  154. static void icmap_map_free_cb(uint32_t event,
  155. char* key, void* old_value,
  156. void* value, void* user_data)
  157. {
  158. struct icmap_item *item = (struct icmap_item *)old_value;
  159. /*
  160. * value == old_value -> fast_adjust_int was used, don't free data
  161. */
  162. if (item != NULL && value != old_value) {
  163. free(item->key_name);
  164. free(item);
  165. }
  166. }
  167. cs_error_t icmap_init_r(icmap_map_t *result)
  168. {
  169. int32_t err;
  170. *result = malloc(sizeof(struct icmap_map));
  171. if (*result == NULL) {
  172. return (CS_ERR_NO_MEMORY);
  173. }
  174. (*result)->qb_map = qb_trie_create();
  175. if ((*result)->qb_map == NULL)
  176. return (CS_ERR_INIT);
  177. err = qb_map_notify_add((*result)->qb_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  178. return (qb_to_cs_error(err));
  179. }
  180. cs_error_t icmap_init(void)
  181. {
  182. return (icmap_init_r(&icmap_global_map));
  183. }
  184. static void icmap_set_ro_access_free(void)
  185. {
  186. struct qb_list_head *iter;
  187. struct icmap_ro_access_item *icmap_ro_ai;
  188. qb_list_for_each(iter, &icmap_ro_access_item_list_head) {
  189. icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
  190. qb_list_del(&icmap_ro_ai->list);
  191. free(icmap_ro_ai->key_name);
  192. free(icmap_ro_ai);
  193. }
  194. }
  195. static void icmap_del_all_track(void)
  196. {
  197. struct qb_list_head *iter;
  198. struct icmap_track *icmap_track;
  199. qb_list_for_each(iter, &icmap_track_list_head) {
  200. icmap_track = qb_list_entry(iter, struct icmap_track, list);
  201. icmap_track_delete(icmap_track);
  202. }
  203. }
  204. void icmap_fini_r(const icmap_map_t map)
  205. {
  206. qb_map_destroy(map->qb_map);
  207. free(map);
  208. return;
  209. }
  210. void icmap_fini(void)
  211. {
  212. icmap_del_all_track();
  213. /*
  214. * catch 22 warning:
  215. * We need to drop this notify but we can't because it calls icmap_map_free_cb
  216. * while destroying the tree to free icmap_item(s).
  217. * -> qb_map_notify_del_2(icmap_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  218. * and we cannot call it after map_destroy. joy! :)
  219. */
  220. icmap_fini_r(icmap_global_map);
  221. icmap_set_ro_access_free();
  222. return ;
  223. }
  224. icmap_map_t icmap_get_global_map(void)
  225. {
  226. return (icmap_global_map);
  227. }
  228. static int icmap_is_valid_name_char(char c)
  229. {
  230. return ((c >= 'a' && c <= 'z') ||
  231. (c >= 'A' && c <= 'Z') ||
  232. (c >= '0' && c <= '9') ||
  233. c == '.' || c == '_' || c == '-' || c == '/' || c == ':');
  234. }
  235. void icmap_convert_name_to_valid_name(char *key_name)
  236. {
  237. int i;
  238. for (i = 0; i < strlen(key_name); i++) {
  239. if (!icmap_is_valid_name_char(key_name[i])) {
  240. key_name[i] = '_';
  241. }
  242. }
  243. }
  244. static int icmap_check_key_name(const char *key_name)
  245. {
  246. int i;
  247. if ((strlen(key_name) < ICMAP_KEYNAME_MINLEN) || strlen(key_name) > ICMAP_KEYNAME_MAXLEN) {
  248. return (-1);
  249. }
  250. for (i = 0; i < strlen(key_name); i++) {
  251. if (!icmap_is_valid_name_char(key_name[i])) {
  252. return (-1);
  253. }
  254. }
  255. return (0);
  256. }
  257. static size_t icmap_get_valuetype_len(icmap_value_types_t type)
  258. {
  259. size_t res = 0;
  260. switch (type) {
  261. case ICMAP_VALUETYPE_INT8: res = sizeof(int8_t); break;
  262. case ICMAP_VALUETYPE_UINT8: res = sizeof(uint8_t); break;
  263. case ICMAP_VALUETYPE_INT16: res = sizeof(int16_t); break;
  264. case ICMAP_VALUETYPE_UINT16: res = sizeof(uint16_t); break;
  265. case ICMAP_VALUETYPE_INT32: res = sizeof(int32_t); break;
  266. case ICMAP_VALUETYPE_UINT32: res = sizeof(uint32_t); break;
  267. case ICMAP_VALUETYPE_INT64: res = sizeof(int64_t); break;
  268. case ICMAP_VALUETYPE_UINT64: res = sizeof(uint64_t); break;
  269. case ICMAP_VALUETYPE_FLOAT: res = sizeof(float); break;
  270. case ICMAP_VALUETYPE_DOUBLE: res = sizeof(double); break;
  271. case ICMAP_VALUETYPE_STRING:
  272. case ICMAP_VALUETYPE_BINARY:
  273. res = 0;
  274. break;
  275. }
  276. return (res);
  277. }
  278. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type)
  279. {
  280. if (value_len > ICMAP_MAX_VALUE_LEN) {
  281. return (-1);
  282. }
  283. if (type != ICMAP_VALUETYPE_STRING && type != ICMAP_VALUETYPE_BINARY) {
  284. if (icmap_get_valuetype_len(type) == value_len) {
  285. return (0);
  286. } else {
  287. return (-1);
  288. }
  289. }
  290. if (type == ICMAP_VALUETYPE_STRING) {
  291. /*
  292. * value_len can be shorter then real string length, but never
  293. * longer (+ 1 is because of 0 at the end of string)
  294. */
  295. if (value_len > strlen((const char *)value) + 1) {
  296. return (-1);
  297. } else {
  298. return (0);
  299. }
  300. }
  301. return (0);
  302. }
  303. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type)
  304. {
  305. size_t ptr_len;
  306. if (item->type != type) {
  307. return (0);
  308. }
  309. if (item->type == ICMAP_VALUETYPE_STRING) {
  310. ptr_len = strlen((const char *)value);
  311. if (ptr_len > value_len) {
  312. ptr_len = value_len;
  313. }
  314. ptr_len++;
  315. } else {
  316. ptr_len = value_len;
  317. }
  318. if (item->value_len == ptr_len) {
  319. return (memcmp(item->value, value, value_len) == 0);
  320. };
  321. return (0);
  322. }
  323. int icmap_key_value_eq(
  324. const icmap_map_t map1,
  325. const char *key_name1,
  326. const icmap_map_t map2,
  327. const char *key_name2)
  328. {
  329. struct icmap_item *item1, *item2;
  330. if (map1 == NULL || key_name1 == NULL || map2 == NULL || key_name2 == NULL) {
  331. return (0);
  332. }
  333. item1 = qb_map_get(map1->qb_map, key_name1);
  334. item2 = qb_map_get(map2->qb_map, key_name2);
  335. if (item1 == NULL || item2 == NULL) {
  336. return (0);
  337. }
  338. return (icmap_item_eq(item1, item2->value, item2->value_len, item2->type));
  339. }
  340. cs_error_t icmap_set_r(
  341. const icmap_map_t map,
  342. const char *key_name,
  343. const void *value,
  344. size_t value_len,
  345. icmap_value_types_t type)
  346. {
  347. struct icmap_item *item;
  348. struct icmap_item *new_item;
  349. size_t new_value_len;
  350. size_t new_item_size;
  351. if (value == NULL || key_name == NULL) {
  352. return (CS_ERR_INVALID_PARAM);
  353. }
  354. if (icmap_check_value_len(value, value_len, type) != 0) {
  355. return (CS_ERR_INVALID_PARAM);
  356. }
  357. item = qb_map_get(map->qb_map, key_name);
  358. if (item != NULL) {
  359. /*
  360. * Check that key is really changed
  361. */
  362. if (icmap_item_eq(item, value, value_len, type)) {
  363. return (CS_OK);
  364. }
  365. } else {
  366. if (icmap_check_key_name(key_name) != 0) {
  367. return (CS_ERR_NAME_TOO_LONG);
  368. }
  369. }
  370. if (type == ICMAP_VALUETYPE_BINARY || type == ICMAP_VALUETYPE_STRING) {
  371. if (type == ICMAP_VALUETYPE_STRING) {
  372. new_value_len = strlen((const char *)value);
  373. if (new_value_len > value_len) {
  374. new_value_len = value_len;
  375. }
  376. new_value_len++;
  377. } else {
  378. new_value_len = value_len;
  379. }
  380. } else {
  381. new_value_len = icmap_get_valuetype_len(type);
  382. }
  383. new_item_size = sizeof(struct icmap_item) + new_value_len;
  384. new_item = malloc(new_item_size);
  385. if (new_item == NULL) {
  386. return (CS_ERR_NO_MEMORY);
  387. }
  388. memset(new_item, 0, new_item_size);
  389. if (item == NULL) {
  390. new_item->key_name = strdup(key_name);
  391. if (new_item->key_name == NULL) {
  392. free(new_item);
  393. return (CS_ERR_NO_MEMORY);
  394. }
  395. } else {
  396. new_item->key_name = item->key_name;
  397. item->key_name = NULL;
  398. }
  399. new_item->type = type;
  400. new_item->value_len = new_value_len;
  401. memcpy(new_item->value, value, new_value_len);
  402. if (new_item->type == ICMAP_VALUETYPE_STRING) {
  403. ((char *)new_item->value)[new_value_len - 1] = 0;
  404. }
  405. qb_map_put(map->qb_map, new_item->key_name, new_item);
  406. return (CS_OK);
  407. }
  408. cs_error_t icmap_set(
  409. const char *key_name,
  410. const void *value,
  411. size_t value_len,
  412. icmap_value_types_t type)
  413. {
  414. return (icmap_set_r(icmap_global_map, key_name, value, value_len, type));
  415. }
  416. cs_error_t icmap_set_int8_r(const icmap_map_t map, const char *key_name, int8_t value)
  417. {
  418. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT8));
  419. }
  420. cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value)
  421. {
  422. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT8));
  423. }
  424. cs_error_t icmap_set_int16_r(const icmap_map_t map, const char *key_name, int16_t value)
  425. {
  426. return (icmap_set_r(map,key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT16));
  427. }
  428. cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value)
  429. {
  430. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT16));
  431. }
  432. cs_error_t icmap_set_int32_r(const icmap_map_t map, const char *key_name, int32_t value)
  433. {
  434. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT32));
  435. }
  436. cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value)
  437. {
  438. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT32));
  439. }
  440. cs_error_t icmap_set_int64_r(const icmap_map_t map, const char *key_name, int64_t value)
  441. {
  442. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT64));
  443. }
  444. cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value)
  445. {
  446. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT64));
  447. }
  448. cs_error_t icmap_set_float_r(const icmap_map_t map, const char *key_name, float value)
  449. {
  450. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_FLOAT));
  451. }
  452. cs_error_t icmap_set_double_r(const icmap_map_t map, const char *key_name, double value)
  453. {
  454. return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_DOUBLE));
  455. }
  456. cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value)
  457. {
  458. if (value == NULL) {
  459. return (CS_ERR_INVALID_PARAM);
  460. }
  461. return (icmap_set_r(map, key_name, value, strlen(value), ICMAP_VALUETYPE_STRING));
  462. }
  463. cs_error_t icmap_set_int8(const char *key_name, int8_t value)
  464. {
  465. return (icmap_set_int8_r(icmap_global_map, key_name, value));
  466. }
  467. cs_error_t icmap_set_uint8(const char *key_name, uint8_t value)
  468. {
  469. return (icmap_set_uint8_r(icmap_global_map, key_name, value));
  470. }
  471. cs_error_t icmap_set_int16(const char *key_name, int16_t value)
  472. {
  473. return (icmap_set_int16_r(icmap_global_map, key_name, value));
  474. }
  475. cs_error_t icmap_set_uint16(const char *key_name, uint16_t value)
  476. {
  477. return (icmap_set_uint16_r(icmap_global_map, key_name, value));
  478. }
  479. cs_error_t icmap_set_int32(const char *key_name, int32_t value)
  480. {
  481. return (icmap_set_int32_r(icmap_global_map, key_name, value));
  482. }
  483. cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
  484. {
  485. return (icmap_set_uint32_r(icmap_global_map, key_name, value));
  486. }
  487. cs_error_t icmap_set_int64(const char *key_name, int64_t value)
  488. {
  489. return (icmap_set_int64_r(icmap_global_map, key_name, value));
  490. }
  491. cs_error_t icmap_set_uint64(const char *key_name, uint64_t value)
  492. {
  493. return (icmap_set_uint64_r(icmap_global_map, key_name, value));
  494. }
  495. cs_error_t icmap_set_float(const char *key_name, float value)
  496. {
  497. return (icmap_set_float_r(icmap_global_map, key_name, value));
  498. }
  499. cs_error_t icmap_set_double(const char *key_name, double value)
  500. {
  501. return (icmap_set_double_r(icmap_global_map, key_name, value));
  502. }
  503. cs_error_t icmap_set_string(const char *key_name, const char *value)
  504. {
  505. return (icmap_set_string_r(icmap_global_map, key_name, value));
  506. }
  507. cs_error_t icmap_delete_r(const icmap_map_t map, const char *key_name)
  508. {
  509. struct icmap_item *item;
  510. if (key_name == NULL) {
  511. return (CS_ERR_INVALID_PARAM);
  512. }
  513. item = qb_map_get(map->qb_map, key_name);
  514. if (item == NULL) {
  515. return (CS_ERR_NOT_EXIST);
  516. }
  517. if (qb_map_rm(map->qb_map, item->key_name) != QB_TRUE) {
  518. return (CS_ERR_NOT_EXIST);
  519. }
  520. return (CS_OK);
  521. }
  522. cs_error_t icmap_delete(const char *key_name)
  523. {
  524. return (icmap_delete_r(icmap_global_map, key_name));
  525. }
  526. static cs_error_t icmap_get_ref_r(
  527. const icmap_map_t map,
  528. const char *key_name,
  529. void **value,
  530. size_t *value_len,
  531. icmap_value_types_t *type)
  532. {
  533. struct icmap_item *item;
  534. if (key_name == NULL) {
  535. return (CS_ERR_INVALID_PARAM);
  536. }
  537. item = qb_map_get(map->qb_map, key_name);
  538. if (item == NULL) {
  539. return (CS_ERR_NOT_EXIST);
  540. }
  541. if (type != NULL) {
  542. *type = item->type;
  543. }
  544. if (value_len != NULL) {
  545. *value_len = item->value_len;
  546. }
  547. if (value != NULL) {
  548. *value = item->value;
  549. }
  550. return (CS_OK);
  551. }
  552. cs_error_t icmap_get_r(
  553. const icmap_map_t map,
  554. const char *key_name,
  555. void *value,
  556. size_t *value_len,
  557. icmap_value_types_t *type)
  558. {
  559. cs_error_t res;
  560. void *tmp_value;
  561. size_t tmp_value_len;
  562. res = icmap_get_ref_r(map, key_name, &tmp_value, &tmp_value_len, type);
  563. if (res != CS_OK) {
  564. return (res);
  565. }
  566. if (value == NULL) {
  567. if (value_len != NULL) {
  568. *value_len = tmp_value_len;
  569. }
  570. } else {
  571. if (value_len == NULL || *value_len < tmp_value_len) {
  572. return (CS_ERR_INVALID_PARAM);
  573. }
  574. *value_len = tmp_value_len;
  575. memcpy(value, tmp_value, tmp_value_len);
  576. }
  577. return (CS_OK);
  578. }
  579. cs_error_t icmap_get(
  580. const char *key_name,
  581. void *value,
  582. size_t *value_len,
  583. icmap_value_types_t *type)
  584. {
  585. return (icmap_get_r(icmap_global_map, key_name, value, value_len, type));
  586. }
  587. static cs_error_t icmap_get_int_r(
  588. const icmap_map_t map,
  589. const char *key_name,
  590. void *value,
  591. icmap_value_types_t type)
  592. {
  593. char key_value[16];
  594. size_t key_size;
  595. cs_error_t err;
  596. icmap_value_types_t key_type;
  597. key_size = sizeof(key_value);
  598. memset(key_value, 0, key_size);
  599. err = icmap_get(key_name, key_value, &key_size, &key_type);
  600. if (err != CS_OK)
  601. return (err);
  602. if (key_type != type) {
  603. return (CS_ERR_INVALID_PARAM);
  604. }
  605. memcpy(value, key_value, icmap_get_valuetype_len(key_type));
  606. return (CS_OK);
  607. }
  608. cs_error_t icmap_get_int8_r(const icmap_map_t map, const char *key_name, int8_t *i8)
  609. {
  610. return (icmap_get_int_r(map, key_name, i8, ICMAP_VALUETYPE_INT8));
  611. }
  612. cs_error_t icmap_get_uint8_r(const icmap_map_t map, const char *key_name, uint8_t *u8)
  613. {
  614. return (icmap_get_int_r(map, key_name, u8, ICMAP_VALUETYPE_UINT8));
  615. }
  616. cs_error_t icmap_get_int16_r(const icmap_map_t map, const char *key_name, int16_t *i16)
  617. {
  618. return (icmap_get_int_r(map, key_name, i16, ICMAP_VALUETYPE_INT16));
  619. }
  620. cs_error_t icmap_get_uint16_r(const icmap_map_t map, const char *key_name, uint16_t *u16)
  621. {
  622. return (icmap_get_int_r(map, key_name, u16, ICMAP_VALUETYPE_UINT16));
  623. }
  624. cs_error_t icmap_get_int32_r(const icmap_map_t map, const char *key_name, int32_t *i32)
  625. {
  626. return (icmap_get_int_r(map, key_name, i32, ICMAP_VALUETYPE_INT32));
  627. }
  628. cs_error_t icmap_get_uint32_r(const icmap_map_t map, const char *key_name, uint32_t *u32)
  629. {
  630. return (icmap_get_int_r(map, key_name, u32, ICMAP_VALUETYPE_UINT32));
  631. }
  632. cs_error_t icmap_get_int64_r(const icmap_map_t map, const char *key_name, int64_t *i64)
  633. {
  634. return(icmap_get_int_r(map, key_name, i64, ICMAP_VALUETYPE_INT64));
  635. }
  636. cs_error_t icmap_get_uint64_r(const icmap_map_t map, const char *key_name, uint64_t *u64)
  637. {
  638. return (icmap_get_int_r(map, key_name, u64, ICMAP_VALUETYPE_UINT64));
  639. }
  640. cs_error_t icmap_get_float_r(const icmap_map_t map, const char *key_name, float *flt)
  641. {
  642. return (icmap_get_int_r(map, key_name, flt, ICMAP_VALUETYPE_FLOAT));
  643. }
  644. cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, double *dbl)
  645. {
  646. return (icmap_get_int_r(map, key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
  647. }
  648. cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
  649. {
  650. return (icmap_get_int8_r(icmap_global_map, key_name, i8));
  651. }
  652. cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8)
  653. {
  654. return (icmap_get_uint8_r(icmap_global_map, key_name, u8));
  655. }
  656. cs_error_t icmap_get_int16(const char *key_name, int16_t *i16)
  657. {
  658. return (icmap_get_int16_r(icmap_global_map, key_name, i16));
  659. }
  660. cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16)
  661. {
  662. return (icmap_get_uint16_r(icmap_global_map, key_name, u16));
  663. }
  664. cs_error_t icmap_get_int32(const char *key_name, int32_t *i32)
  665. {
  666. return (icmap_get_int32_r(icmap_global_map, key_name, i32));
  667. }
  668. cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32)
  669. {
  670. return (icmap_get_uint32_r(icmap_global_map, key_name, u32));
  671. }
  672. cs_error_t icmap_get_int64(const char *key_name, int64_t *i64)
  673. {
  674. return(icmap_get_int64_r(icmap_global_map, key_name, i64));
  675. }
  676. cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64)
  677. {
  678. return (icmap_get_uint64_r(icmap_global_map, key_name, u64));
  679. }
  680. cs_error_t icmap_get_float(const char *key_name, float *flt)
  681. {
  682. return (icmap_get_float_r(icmap_global_map, key_name, flt));
  683. }
  684. cs_error_t icmap_get_double(const char *key_name, double *dbl)
  685. {
  686. return (icmap_get_double_r(icmap_global_map, key_name, dbl));
  687. }
  688. cs_error_t icmap_get_string(const char *key_name, char **str)
  689. {
  690. cs_error_t res;
  691. size_t str_len;
  692. icmap_value_types_t type;
  693. res = icmap_get(key_name, NULL, &str_len, &type);
  694. if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
  695. if (res == CS_OK) {
  696. res = CS_ERR_INVALID_PARAM;
  697. }
  698. goto return_error;
  699. }
  700. *str = malloc(str_len);
  701. if (*str == NULL) {
  702. res = CS_ERR_NO_MEMORY;
  703. goto return_error;
  704. }
  705. res = icmap_get(key_name, *str, &str_len, &type);
  706. if (res != CS_OK) {
  707. free(*str);
  708. goto return_error;
  709. }
  710. return (CS_OK);
  711. return_error:
  712. return (res);
  713. }
  714. cs_error_t icmap_adjust_int_r(
  715. const icmap_map_t map,
  716. const char *key_name,
  717. int32_t step)
  718. {
  719. struct icmap_item *item;
  720. uint8_t u8;
  721. uint16_t u16;
  722. uint32_t u32;
  723. uint64_t u64;
  724. cs_error_t err = CS_OK;
  725. if (key_name == NULL) {
  726. return (CS_ERR_INVALID_PARAM);
  727. }
  728. item = qb_map_get(map->qb_map, key_name);
  729. if (item == NULL) {
  730. return (CS_ERR_NOT_EXIST);
  731. }
  732. switch (item->type) {
  733. case ICMAP_VALUETYPE_INT8:
  734. case ICMAP_VALUETYPE_UINT8:
  735. memcpy(&u8, item->value, sizeof(u8));
  736. u8 += step;
  737. err = icmap_set(key_name, &u8, sizeof(u8), item->type);
  738. break;
  739. case ICMAP_VALUETYPE_INT16:
  740. case ICMAP_VALUETYPE_UINT16:
  741. memcpy(&u16, item->value, sizeof(u16));
  742. u16 += step;
  743. err = icmap_set(key_name, &u16, sizeof(u16), item->type);
  744. break;
  745. case ICMAP_VALUETYPE_INT32:
  746. case ICMAP_VALUETYPE_UINT32:
  747. memcpy(&u32, item->value, sizeof(u32));
  748. u32 += step;
  749. err = icmap_set(key_name, &u32, sizeof(u32), item->type);
  750. break;
  751. case ICMAP_VALUETYPE_INT64:
  752. case ICMAP_VALUETYPE_UINT64:
  753. memcpy(&u64, item->value, sizeof(u64));
  754. u64 += step;
  755. err = icmap_set(key_name, &u64, sizeof(u64), item->type);
  756. break;
  757. case ICMAP_VALUETYPE_FLOAT:
  758. case ICMAP_VALUETYPE_DOUBLE:
  759. case ICMAP_VALUETYPE_STRING:
  760. case ICMAP_VALUETYPE_BINARY:
  761. err = CS_ERR_INVALID_PARAM;
  762. break;
  763. }
  764. return (err);
  765. }
  766. cs_error_t icmap_adjust_int(
  767. const char *key_name,
  768. int32_t step)
  769. {
  770. return (icmap_adjust_int_r(icmap_global_map, key_name, step));
  771. }
  772. cs_error_t icmap_fast_adjust_int_r(
  773. const icmap_map_t map,
  774. const char *key_name,
  775. int32_t step)
  776. {
  777. struct icmap_item *item;
  778. cs_error_t err = CS_OK;
  779. if (key_name == NULL) {
  780. return (CS_ERR_INVALID_PARAM);
  781. }
  782. item = qb_map_get(map->qb_map, key_name);
  783. if (item == NULL) {
  784. return (CS_ERR_NOT_EXIST);
  785. }
  786. switch (item->type) {
  787. case ICMAP_VALUETYPE_INT8:
  788. case ICMAP_VALUETYPE_UINT8:
  789. *(uint8_t *)item->value += step;
  790. break;
  791. case ICMAP_VALUETYPE_INT16:
  792. case ICMAP_VALUETYPE_UINT16:
  793. *(uint16_t *)item->value += step;
  794. break;
  795. case ICMAP_VALUETYPE_INT32:
  796. case ICMAP_VALUETYPE_UINT32:
  797. *(uint32_t *)item->value += step;
  798. break;
  799. case ICMAP_VALUETYPE_INT64:
  800. case ICMAP_VALUETYPE_UINT64:
  801. *(uint64_t *)item->value += step;
  802. break;
  803. case ICMAP_VALUETYPE_FLOAT:
  804. case ICMAP_VALUETYPE_DOUBLE:
  805. case ICMAP_VALUETYPE_STRING:
  806. case ICMAP_VALUETYPE_BINARY:
  807. err = CS_ERR_INVALID_PARAM;
  808. break;
  809. }
  810. if (err == CS_OK) {
  811. qb_map_put(map->qb_map, item->key_name, item);
  812. }
  813. return (err);
  814. }
  815. cs_error_t icmap_fast_adjust_int(
  816. const char *key_name,
  817. int32_t step)
  818. {
  819. return (icmap_fast_adjust_int_r(icmap_global_map, key_name, step));
  820. }
  821. cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name)
  822. {
  823. return (icmap_adjust_int_r(map, key_name, 1));
  824. }
  825. cs_error_t icmap_inc(const char *key_name)
  826. {
  827. return (icmap_inc_r(icmap_global_map, key_name));
  828. }
  829. cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name)
  830. {
  831. return (icmap_adjust_int_r(map, key_name, -1));
  832. }
  833. cs_error_t icmap_dec(const char *key_name)
  834. {
  835. return (icmap_dec_r(icmap_global_map, key_name));
  836. }
  837. cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name)
  838. {
  839. return (icmap_fast_adjust_int_r(map, key_name, 1));
  840. }
  841. cs_error_t icmap_fast_inc(const char *key_name)
  842. {
  843. return (icmap_fast_inc_r(icmap_global_map, key_name));
  844. }
  845. cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name)
  846. {
  847. return (icmap_fast_adjust_int_r(map, key_name, -1));
  848. }
  849. cs_error_t icmap_fast_dec(const char *key_name)
  850. {
  851. return (icmap_fast_dec_r(icmap_global_map, key_name));
  852. }
  853. icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix)
  854. {
  855. return (qb_map_pref_iter_create(map->qb_map, prefix));
  856. }
  857. icmap_iter_t icmap_iter_init(const char *prefix)
  858. {
  859. return (icmap_iter_init_r(icmap_global_map, prefix));
  860. }
  861. const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
  862. {
  863. struct icmap_item *item;
  864. const char *res;
  865. res = qb_map_iter_next(iter, (void **)&item);
  866. if (res == NULL) {
  867. return (res);
  868. }
  869. if (value_len != NULL) {
  870. *value_len = item->value_len;
  871. }
  872. if (type != NULL) {
  873. *type = item->type;
  874. }
  875. return (res);
  876. }
  877. void icmap_iter_finalize(icmap_iter_t iter)
  878. {
  879. qb_map_iter_free(iter);
  880. }
  881. static void icmap_notify_fn(uint32_t event, char *key, void *old_value, void *value, void *user_data)
  882. {
  883. icmap_track_t icmap_track = (icmap_track_t)user_data;
  884. struct icmap_item *new_item = (struct icmap_item *)value;
  885. struct icmap_item *old_item = (struct icmap_item *)old_value;
  886. struct icmap_notify_value new_val;
  887. struct icmap_notify_value old_val;
  888. if (value == NULL && old_value == NULL) {
  889. return ;
  890. }
  891. if (new_item != NULL) {
  892. new_val.type = new_item->type;
  893. new_val.len = new_item->value_len;
  894. new_val.data = new_item->value;
  895. } else {
  896. memset(&new_val, 0, sizeof(new_val));
  897. }
  898. /*
  899. * old_item == new_item if fast functions are used -> don't fill old value
  900. */
  901. if (old_item != NULL && old_item != new_item) {
  902. old_val.type = old_item->type;
  903. old_val.len = old_item->value_len;
  904. old_val.data = old_item->value;
  905. } else {
  906. memset(&old_val, 0, sizeof(old_val));
  907. }
  908. icmap_track->notify_fn(icmap_qbtt_to_tt(event),
  909. key,
  910. new_val,
  911. old_val,
  912. icmap_track->user_data);
  913. }
  914. cs_error_t icmap_track_add(
  915. const char *key_name,
  916. int32_t track_type,
  917. icmap_notify_fn_t notify_fn,
  918. void *user_data,
  919. icmap_track_t *icmap_track)
  920. {
  921. int32_t err;
  922. if (notify_fn == NULL || icmap_track == NULL) {
  923. return (CS_ERR_INVALID_PARAM);
  924. }
  925. if ((track_type & ~(ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX)) != 0) {
  926. return (CS_ERR_INVALID_PARAM);
  927. }
  928. *icmap_track = malloc(sizeof(**icmap_track));
  929. if (*icmap_track == NULL) {
  930. return (CS_ERR_NO_MEMORY);
  931. }
  932. memset(*icmap_track, 0, sizeof(**icmap_track));
  933. if (key_name != NULL) {
  934. (*icmap_track)->key_name = strdup(key_name);
  935. };
  936. (*icmap_track)->track_type = track_type;
  937. (*icmap_track)->notify_fn = notify_fn;
  938. (*icmap_track)->user_data = user_data;
  939. if ((err = qb_map_notify_add(icmap_global_map->qb_map, (*icmap_track)->key_name, icmap_notify_fn,
  940. icmap_tt_to_qbtt(track_type), *icmap_track)) != 0) {
  941. free((*icmap_track)->key_name);
  942. free(*icmap_track);
  943. return (qb_to_cs_error(err));
  944. }
  945. qb_list_init(&(*icmap_track)->list);
  946. qb_list_add (&(*icmap_track)->list, &icmap_track_list_head);
  947. return (CS_OK);
  948. }
  949. cs_error_t icmap_track_delete(icmap_track_t icmap_track)
  950. {
  951. int32_t err;
  952. if ((err = qb_map_notify_del_2(icmap_global_map->qb_map, icmap_track->key_name,
  953. icmap_notify_fn, icmap_tt_to_qbtt(icmap_track->track_type), icmap_track)) != 0) {
  954. return (qb_to_cs_error(err));
  955. }
  956. qb_list_del(&icmap_track->list);
  957. free(icmap_track->key_name);
  958. free(icmap_track);
  959. return (CS_OK);
  960. }
  961. void *icmap_track_get_user_data(icmap_track_t icmap_track)
  962. {
  963. return (icmap_track->user_data);
  964. }
  965. cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
  966. {
  967. struct qb_list_head *iter;
  968. struct icmap_ro_access_item *icmap_ro_ai;
  969. qb_list_for_each(iter, &icmap_ro_access_item_list_head) {
  970. icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
  971. if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
  972. /*
  973. * We found item
  974. */
  975. if (ro_access) {
  976. return (CS_ERR_EXIST);
  977. } else {
  978. qb_list_del(&icmap_ro_ai->list);
  979. free(icmap_ro_ai->key_name);
  980. free(icmap_ro_ai);
  981. return (CS_OK);
  982. }
  983. }
  984. }
  985. if (!ro_access) {
  986. return (CS_ERR_NOT_EXIST);
  987. }
  988. icmap_ro_ai = malloc(sizeof(*icmap_ro_ai));
  989. if (icmap_ro_ai == NULL) {
  990. return (CS_ERR_NO_MEMORY);
  991. }
  992. memset(icmap_ro_ai, 0, sizeof(*icmap_ro_ai));
  993. icmap_ro_ai->key_name = strdup(key_name);
  994. if (icmap_ro_ai->key_name == NULL) {
  995. free(icmap_ro_ai);
  996. return (CS_ERR_NO_MEMORY);
  997. }
  998. icmap_ro_ai->prefix = prefix;
  999. qb_list_init(&icmap_ro_ai->list);
  1000. qb_list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
  1001. return (CS_OK);
  1002. }
  1003. int icmap_is_key_ro(const char *key_name)
  1004. {
  1005. struct qb_list_head *iter;
  1006. struct icmap_ro_access_item *icmap_ro_ai;
  1007. qb_list_for_each(iter, &icmap_ro_access_item_list_head) {
  1008. icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
  1009. if (icmap_ro_ai->prefix) {
  1010. if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
  1011. continue;
  1012. if (strncmp(icmap_ro_ai->key_name, key_name, strlen(icmap_ro_ai->key_name)) == 0) {
  1013. return (CS_TRUE);
  1014. }
  1015. } else {
  1016. if (strcmp(icmap_ro_ai->key_name, key_name) == 0) {
  1017. return (CS_TRUE);
  1018. }
  1019. }
  1020. }
  1021. return (CS_FALSE);
  1022. }
  1023. cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map)
  1024. {
  1025. icmap_iter_t iter;
  1026. size_t value_len;
  1027. icmap_value_types_t value_type;
  1028. const char *key_name;
  1029. cs_error_t err;
  1030. void *value;
  1031. iter = icmap_iter_init_r(src_map, NULL);
  1032. if (iter == NULL) {
  1033. return (CS_ERR_NO_MEMORY);
  1034. }
  1035. err = CS_OK;
  1036. while ((key_name = icmap_iter_next(iter, &value_len, &value_type)) != NULL) {
  1037. err = icmap_get_ref_r(src_map, key_name, &value, &value_len, &value_type);
  1038. if (err != CS_OK) {
  1039. goto exit_iter_finalize;
  1040. }
  1041. err = icmap_set_r(dst_map, key_name, value, value_len, value_type);
  1042. if (err != CS_OK) {
  1043. goto exit_iter_finalize;
  1044. }
  1045. }
  1046. exit_iter_finalize:
  1047. icmap_iter_finalize(iter);
  1048. return (err);
  1049. }