4
0

icmap.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008
  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. static qb_map_t *icmap_map;
  49. struct icmap_track {
  50. char *key_name;
  51. int32_t track_type;
  52. icmap_notify_fn_t notify_fn;
  53. void *user_data;
  54. struct list_head list;
  55. };
  56. struct icmap_ro_access_item {
  57. char *key_name;
  58. int prefix;
  59. struct list_head list;
  60. };
  61. DECLARE_LIST_INIT(icmap_ro_access_item_list_head);
  62. DECLARE_LIST_INIT(icmap_track_list_head);
  63. /*
  64. * Static functions declarations
  65. */
  66. /*
  67. * Check if key_name is valid icmap key name. Returns 0 on success, and -1 on fail
  68. */
  69. static int icmap_check_key_name(const char *key_name);
  70. /*
  71. * Check that value with given type has correct length value_len. Returns 0 on success,
  72. * and -1 on fail
  73. */
  74. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type);
  75. /*
  76. * Returns length of value of given type, or 0 for string and binary data type
  77. */
  78. static size_t icmap_get_valuetype_len(icmap_value_types_t type);
  79. /*
  80. * Converts track type of icmap to qb
  81. */
  82. static int32_t icmap_tt_to_qbtt(int32_t track_type);
  83. /*
  84. * Convert track type of qb to icmap
  85. */
  86. static int32_t icmap_qbtt_to_tt(int32_t track_type);
  87. /*
  88. * Checks if item has same value as value with value_len and given type. Returns 0 if not, otherwise !0.
  89. */
  90. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type);
  91. /*
  92. * Checks if given character is valid in key name. Returns 0 if not, otherwise !0.
  93. */
  94. static int icmap_is_valid_name_char(char c);
  95. /*
  96. * Helper for getting integer and float value with given type for key key_name and store it in value.
  97. */
  98. static cs_error_t icmap_get_int(
  99. const char *key_name,
  100. void *value,
  101. icmap_value_types_t type);
  102. /*
  103. * Function implementation
  104. */
  105. static int32_t icmap_tt_to_qbtt(int32_t track_type)
  106. {
  107. int32_t res = 0;
  108. if (track_type & ICMAP_TRACK_DELETE) {
  109. res |= QB_MAP_NOTIFY_DELETED;
  110. }
  111. if (track_type & ICMAP_TRACK_MODIFY) {
  112. res |= QB_MAP_NOTIFY_REPLACED;
  113. }
  114. if (track_type & ICMAP_TRACK_ADD) {
  115. res |= QB_MAP_NOTIFY_INSERTED;
  116. }
  117. if (track_type & ICMAP_TRACK_PREFIX) {
  118. res |= QB_MAP_NOTIFY_RECURSIVE;
  119. }
  120. return (res);
  121. }
  122. static int32_t icmap_qbtt_to_tt(int32_t track_type)
  123. {
  124. int32_t res = 0;
  125. if (track_type & QB_MAP_NOTIFY_DELETED) {
  126. res |= ICMAP_TRACK_DELETE;
  127. }
  128. if (track_type & QB_MAP_NOTIFY_REPLACED) {
  129. res |= ICMAP_TRACK_MODIFY;
  130. }
  131. if (track_type & QB_MAP_NOTIFY_INSERTED) {
  132. res |= ICMAP_TRACK_ADD;
  133. }
  134. if (track_type & QB_MAP_NOTIFY_RECURSIVE) {
  135. res |= ICMAP_TRACK_PREFIX;
  136. }
  137. return (res);
  138. }
  139. static void icmap_map_free_cb(uint32_t event,
  140. char* key, void* old_value,
  141. void* value, void* user_data)
  142. {
  143. struct icmap_item *item = (struct icmap_item *)old_value;
  144. /*
  145. * value == old_value -> fast_adjust_int was used, don't free data
  146. */
  147. if (item != NULL && value != old_value) {
  148. free(item->key_name);
  149. free(item);
  150. }
  151. }
  152. cs_error_t icmap_init(void)
  153. {
  154. int32_t err;
  155. icmap_map = qb_trie_create();
  156. if (icmap_map == NULL)
  157. return (CS_ERR_INIT);
  158. err = qb_map_notify_add(icmap_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  159. return (qb_to_cs_error(err));
  160. }
  161. static void icmap_set_ro_access_free(void)
  162. {
  163. struct list_head *iter = icmap_ro_access_item_list_head.next;
  164. struct icmap_ro_access_item *icmap_ro_ai;
  165. while (iter != &icmap_ro_access_item_list_head) {
  166. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  167. list_del(&icmap_ro_ai->list);
  168. free(icmap_ro_ai->key_name);
  169. free(icmap_ro_ai);
  170. iter = icmap_ro_access_item_list_head.next;
  171. }
  172. }
  173. static void icmap_del_all_track(void)
  174. {
  175. struct list_head *iter = icmap_track_list_head.next;
  176. struct icmap_track *icmap_track;
  177. while (iter != &icmap_track_list_head) {
  178. icmap_track = list_entry(iter, struct icmap_track, list);
  179. icmap_track_delete(icmap_track);
  180. iter = icmap_track_list_head.next;
  181. }
  182. }
  183. void icmap_fini(void)
  184. {
  185. icmap_del_all_track();
  186. /*
  187. * catch 22 warning:
  188. * We need to drop this notify but we can't because it calls icmap_map_free_cb
  189. * while destroying the tree to free icmap_item(s).
  190. * -> qb_map_notify_del_2(icmap_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  191. * and we cannot call it after map_destroy. joy! :)
  192. */
  193. qb_map_destroy(icmap_map);
  194. icmap_set_ro_access_free();
  195. return;
  196. }
  197. static int icmap_is_valid_name_char(char c)
  198. {
  199. return ((c >= 'a' && c <= 'z') ||
  200. (c >= 'A' && c <= 'Z') ||
  201. (c >= '0' && c <= '9') ||
  202. c == '.' || c == '_' || c == '-' || c == '/' || c == ':');
  203. }
  204. void icmap_convert_name_to_valid_name(char *key_name)
  205. {
  206. int i;
  207. for (i = 0; i < strlen(key_name); i++) {
  208. if (!icmap_is_valid_name_char(key_name[i])) {
  209. key_name[i] = '_';
  210. }
  211. }
  212. }
  213. static int icmap_check_key_name(const char *key_name)
  214. {
  215. int i;
  216. if ((strlen(key_name) < ICMAP_KEYNAME_MINLEN) || strlen(key_name) > ICMAP_KEYNAME_MAXLEN) {
  217. return (-1);
  218. }
  219. for (i = 0; i < strlen(key_name); i++) {
  220. if (!icmap_is_valid_name_char(key_name[i])) {
  221. return (-1);
  222. }
  223. }
  224. return (0);
  225. }
  226. static size_t icmap_get_valuetype_len(icmap_value_types_t type)
  227. {
  228. size_t res = 0;
  229. switch (type) {
  230. case ICMAP_VALUETYPE_INT8: res = sizeof(int8_t); break;
  231. case ICMAP_VALUETYPE_UINT8: res = sizeof(uint8_t); break;
  232. case ICMAP_VALUETYPE_INT16: res = sizeof(int16_t); break;
  233. case ICMAP_VALUETYPE_UINT16: res = sizeof(uint16_t); break;
  234. case ICMAP_VALUETYPE_INT32: res = sizeof(int32_t); break;
  235. case ICMAP_VALUETYPE_UINT32: res = sizeof(uint32_t); break;
  236. case ICMAP_VALUETYPE_INT64: res = sizeof(int64_t); break;
  237. case ICMAP_VALUETYPE_UINT64: res = sizeof(uint64_t); break;
  238. case ICMAP_VALUETYPE_FLOAT: res = sizeof(float); break;
  239. case ICMAP_VALUETYPE_DOUBLE: res = sizeof(double); break;
  240. case ICMAP_VALUETYPE_STRING:
  241. case ICMAP_VALUETYPE_BINARY:
  242. res = 0;
  243. break;
  244. }
  245. return (res);
  246. }
  247. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type)
  248. {
  249. if (value_len > ICMAP_MAX_VALUE_LEN) {
  250. return (-1);
  251. }
  252. if (type != ICMAP_VALUETYPE_STRING && type != ICMAP_VALUETYPE_BINARY) {
  253. if (icmap_get_valuetype_len(type) == value_len) {
  254. return (0);
  255. } else {
  256. return (-1);
  257. }
  258. }
  259. if (type == ICMAP_VALUETYPE_STRING) {
  260. if (value_len > strlen((const char *)value)) {
  261. return (-1);
  262. } else {
  263. return (0);
  264. }
  265. }
  266. return (0);
  267. }
  268. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type)
  269. {
  270. size_t ptr_len;
  271. if (item->type != type) {
  272. return (0);
  273. }
  274. if (item->type == ICMAP_VALUETYPE_STRING) {
  275. ptr_len = strlen((const char *)value);
  276. if (ptr_len > value_len) {
  277. ptr_len = value_len;
  278. }
  279. ptr_len++;
  280. } else {
  281. ptr_len = value_len;
  282. }
  283. if (item->value_len == ptr_len) {
  284. return (memcmp(item->value, value, value_len) == 0);
  285. };
  286. return (0);
  287. }
  288. cs_error_t icmap_set(
  289. const char *key_name,
  290. const void *value,
  291. size_t value_len,
  292. icmap_value_types_t type)
  293. {
  294. struct icmap_item *item;
  295. struct icmap_item *new_item;
  296. size_t new_value_len;
  297. size_t new_item_size;
  298. if (value == NULL || key_name == NULL) {
  299. return (CS_ERR_INVALID_PARAM);
  300. }
  301. if (icmap_check_value_len(value, value_len, type) != 0) {
  302. return (CS_ERR_INVALID_PARAM);
  303. }
  304. item = qb_map_get(icmap_map, key_name);
  305. if (item != NULL) {
  306. /*
  307. * Check that key is really changed
  308. */
  309. if (icmap_item_eq(item, value, value_len, type)) {
  310. return (CS_OK);
  311. }
  312. } else {
  313. if (icmap_check_key_name(key_name) != 0) {
  314. return (CS_ERR_NAME_TOO_LONG);
  315. }
  316. }
  317. if (type == ICMAP_VALUETYPE_BINARY || type == ICMAP_VALUETYPE_STRING) {
  318. if (type == ICMAP_VALUETYPE_STRING) {
  319. new_value_len = strlen((const char *)value);
  320. if (new_value_len > value_len) {
  321. new_value_len = value_len;
  322. }
  323. new_value_len++;
  324. } else {
  325. new_value_len = value_len;
  326. }
  327. } else {
  328. new_value_len = icmap_get_valuetype_len(type);
  329. }
  330. new_item_size = sizeof(struct icmap_item) + new_value_len;
  331. new_item = malloc(new_item_size);
  332. if (new_item == NULL) {
  333. return (CS_ERR_NO_MEMORY);
  334. }
  335. memset(new_item, 0, new_item_size);
  336. if (item == NULL) {
  337. new_item->key_name = strdup(key_name);
  338. if (new_item->key_name == NULL) {
  339. free(new_item);
  340. return (CS_ERR_NO_MEMORY);
  341. }
  342. } else {
  343. new_item->key_name = item->key_name;
  344. item->key_name = NULL;
  345. }
  346. new_item->type = type;
  347. new_item->value_len = new_value_len;
  348. memcpy(new_item->value, value, new_value_len);
  349. if (new_item->type == ICMAP_VALUETYPE_STRING) {
  350. ((char *)new_item->value)[new_value_len - 1] = 0;
  351. }
  352. qb_map_put(icmap_map, new_item->key_name, new_item);
  353. return (CS_OK);
  354. }
  355. cs_error_t icmap_set_int8(const char *key_name, int8_t value)
  356. {
  357. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT8));
  358. }
  359. cs_error_t icmap_set_uint8(const char *key_name, uint8_t value)
  360. {
  361. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT8));
  362. }
  363. cs_error_t icmap_set_int16(const char *key_name, int16_t value)
  364. {
  365. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT16));
  366. }
  367. cs_error_t icmap_set_uint16(const char *key_name, uint16_t value)
  368. {
  369. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT16));
  370. }
  371. cs_error_t icmap_set_int32(const char *key_name, int32_t value)
  372. {
  373. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT32));
  374. }
  375. cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
  376. {
  377. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT32));
  378. }
  379. cs_error_t icmap_set_int64(const char *key_name, int64_t value)
  380. {
  381. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT64));
  382. }
  383. cs_error_t icmap_set_uint64(const char *key_name, uint64_t value)
  384. {
  385. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT64));
  386. }
  387. cs_error_t icmap_set_float(const char *key_name, float value)
  388. {
  389. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_FLOAT));
  390. }
  391. cs_error_t icmap_set_double(const char *key_name, double value)
  392. {
  393. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_DOUBLE));
  394. }
  395. cs_error_t icmap_set_string(const char *key_name, const char *value)
  396. {
  397. if (value == NULL) {
  398. return (CS_ERR_INVALID_PARAM);
  399. }
  400. return (icmap_set(key_name, value, strlen(value), ICMAP_VALUETYPE_STRING));
  401. }
  402. cs_error_t icmap_delete(const char *key_name)
  403. {
  404. struct icmap_item *item;
  405. if (key_name == NULL) {
  406. return (CS_ERR_INVALID_PARAM);
  407. }
  408. item = qb_map_get(icmap_map, key_name);
  409. if (item == NULL) {
  410. return (CS_ERR_NOT_EXIST);
  411. }
  412. if (qb_map_rm(icmap_map, item->key_name) != QB_TRUE) {
  413. return (CS_ERR_NOT_EXIST);
  414. }
  415. return (CS_OK);
  416. }
  417. cs_error_t icmap_get(
  418. const char *key_name,
  419. void *value,
  420. size_t *value_len,
  421. icmap_value_types_t *type)
  422. {
  423. struct icmap_item *item;
  424. if (key_name == NULL) {
  425. return (CS_ERR_INVALID_PARAM);
  426. }
  427. item = qb_map_get(icmap_map, key_name);
  428. if (item == NULL) {
  429. return (CS_ERR_NOT_EXIST);
  430. }
  431. if (type != NULL) {
  432. *type = item->type;
  433. }
  434. if (value == NULL) {
  435. if (value_len != NULL) {
  436. *value_len = item->value_len;
  437. }
  438. } else {
  439. if (value_len == NULL || *value_len < item->value_len) {
  440. return (CS_ERR_INVALID_PARAM);
  441. }
  442. *value_len = item->value_len;
  443. memcpy(value, item->value, item->value_len);
  444. }
  445. return (CS_OK);
  446. }
  447. static cs_error_t icmap_get_int(
  448. const char *key_name,
  449. void *value,
  450. icmap_value_types_t type)
  451. {
  452. char key_value[16];
  453. size_t key_size;
  454. cs_error_t err;
  455. icmap_value_types_t key_type;
  456. key_size = sizeof(key_value);
  457. memset(key_value, 0, key_size);
  458. err = icmap_get(key_name, key_value, &key_size, &key_type);
  459. if (err != CS_OK)
  460. return (err);
  461. if (key_type != type) {
  462. return (CS_ERR_INVALID_PARAM);
  463. }
  464. memcpy(value, key_value, icmap_get_valuetype_len(key_type));
  465. return (CS_OK);
  466. }
  467. cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
  468. {
  469. return (icmap_get_int(key_name, i8, ICMAP_VALUETYPE_INT8));
  470. }
  471. cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8)
  472. {
  473. return (icmap_get_int(key_name, u8, ICMAP_VALUETYPE_UINT8));
  474. }
  475. cs_error_t icmap_get_int16(const char *key_name, int16_t *i16)
  476. {
  477. return (icmap_get_int(key_name, i16, ICMAP_VALUETYPE_INT16));
  478. }
  479. cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16)
  480. {
  481. return (icmap_get_int(key_name, u16, ICMAP_VALUETYPE_UINT16));
  482. }
  483. cs_error_t icmap_get_int32(const char *key_name, int32_t *i32)
  484. {
  485. return (icmap_get_int(key_name, i32, ICMAP_VALUETYPE_INT32));
  486. }
  487. cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32)
  488. {
  489. return (icmap_get_int(key_name, u32, ICMAP_VALUETYPE_UINT32));
  490. }
  491. cs_error_t icmap_get_int64(const char *key_name, int64_t *i64)
  492. {
  493. return(icmap_get_int(key_name, i64, ICMAP_VALUETYPE_INT64));
  494. }
  495. cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64)
  496. {
  497. return (icmap_get_int(key_name, u64, ICMAP_VALUETYPE_UINT64));
  498. }
  499. cs_error_t icmap_get_float(const char *key_name, float *flt)
  500. {
  501. return (icmap_get_int(key_name, flt, ICMAP_VALUETYPE_FLOAT));
  502. }
  503. cs_error_t icmap_get_double(const char *key_name, double *dbl)
  504. {
  505. return (icmap_get_int(key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
  506. }
  507. cs_error_t icmap_get_string(const char *key_name, char **str)
  508. {
  509. cs_error_t res;
  510. size_t str_len;
  511. icmap_value_types_t type;
  512. res = icmap_get(key_name, NULL, &str_len, &type);
  513. if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
  514. if (res == CS_OK) {
  515. res = CS_ERR_INVALID_PARAM;
  516. }
  517. goto return_error;
  518. }
  519. *str = malloc(str_len);
  520. if (*str == NULL) {
  521. res = CS_ERR_NO_MEMORY;
  522. goto return_error;
  523. }
  524. res = icmap_get(key_name, *str, &str_len, &type);
  525. if (res != CS_OK) {
  526. free(*str);
  527. goto return_error;
  528. }
  529. return (CS_OK);
  530. return_error:
  531. return (res);
  532. }
  533. cs_error_t icmap_adjust_int(
  534. const char *key_name,
  535. int32_t step)
  536. {
  537. struct icmap_item *item;
  538. uint8_t u8;
  539. uint16_t u16;
  540. uint32_t u32;
  541. uint64_t u64;
  542. cs_error_t err = CS_OK;
  543. if (key_name == NULL) {
  544. return (CS_ERR_INVALID_PARAM);
  545. }
  546. item = qb_map_get(icmap_map, key_name);
  547. if (item == NULL) {
  548. return (CS_ERR_NOT_EXIST);
  549. }
  550. switch (item->type) {
  551. case ICMAP_VALUETYPE_INT8:
  552. case ICMAP_VALUETYPE_UINT8:
  553. memcpy(&u8, item->value, sizeof(u8));
  554. u8 += step;
  555. err = icmap_set(key_name, &u8, sizeof(u8), item->type);
  556. break;
  557. case ICMAP_VALUETYPE_INT16:
  558. case ICMAP_VALUETYPE_UINT16:
  559. memcpy(&u16, item->value, sizeof(u16));
  560. u16 += step;
  561. err = icmap_set(key_name, &u16, sizeof(u16), item->type);
  562. break;
  563. case ICMAP_VALUETYPE_INT32:
  564. case ICMAP_VALUETYPE_UINT32:
  565. memcpy(&u32, item->value, sizeof(u32));
  566. u32 += step;
  567. err = icmap_set(key_name, &u32, sizeof(u32), item->type);
  568. break;
  569. case ICMAP_VALUETYPE_INT64:
  570. case ICMAP_VALUETYPE_UINT64:
  571. memcpy(&u64, item->value, sizeof(u64));
  572. u64 += step;
  573. err = icmap_set(key_name, &u64, sizeof(u64), item->type);
  574. break;
  575. case ICMAP_VALUETYPE_FLOAT:
  576. case ICMAP_VALUETYPE_DOUBLE:
  577. case ICMAP_VALUETYPE_STRING:
  578. case ICMAP_VALUETYPE_BINARY:
  579. err = CS_ERR_INVALID_PARAM;
  580. break;
  581. }
  582. return (err);
  583. }
  584. cs_error_t icmap_fast_adjust_int(
  585. const char *key_name,
  586. int32_t step)
  587. {
  588. struct icmap_item *item;
  589. cs_error_t err = CS_OK;
  590. if (key_name == NULL) {
  591. return (CS_ERR_INVALID_PARAM);
  592. }
  593. item = qb_map_get(icmap_map, key_name);
  594. if (item == NULL) {
  595. return (CS_ERR_NOT_EXIST);
  596. }
  597. switch (item->type) {
  598. case ICMAP_VALUETYPE_INT8:
  599. case ICMAP_VALUETYPE_UINT8:
  600. *(uint8_t *)item->value += step;
  601. break;
  602. case ICMAP_VALUETYPE_INT16:
  603. case ICMAP_VALUETYPE_UINT16:
  604. *(uint16_t *)item->value += step;
  605. break;
  606. case ICMAP_VALUETYPE_INT32:
  607. case ICMAP_VALUETYPE_UINT32:
  608. *(uint32_t *)item->value += step;
  609. break;
  610. case ICMAP_VALUETYPE_INT64:
  611. case ICMAP_VALUETYPE_UINT64:
  612. *(uint64_t *)item->value += step;
  613. break;
  614. case ICMAP_VALUETYPE_FLOAT:
  615. case ICMAP_VALUETYPE_DOUBLE:
  616. case ICMAP_VALUETYPE_STRING:
  617. case ICMAP_VALUETYPE_BINARY:
  618. err = CS_ERR_INVALID_PARAM;
  619. break;
  620. }
  621. if (err == CS_OK) {
  622. qb_map_put(icmap_map, item->key_name, item);
  623. }
  624. return (err);
  625. }
  626. cs_error_t icmap_inc(const char *key_name)
  627. {
  628. return (icmap_adjust_int(key_name, 1));
  629. }
  630. cs_error_t icmap_dec(const char *key_name)
  631. {
  632. return (icmap_adjust_int(key_name, -1));
  633. }
  634. cs_error_t icmap_fast_inc(const char *key_name)
  635. {
  636. return (icmap_fast_adjust_int(key_name, 1));
  637. }
  638. cs_error_t icmap_fast_dec(const char *key_name)
  639. {
  640. return (icmap_fast_adjust_int(key_name, -1));
  641. }
  642. icmap_iter_t icmap_iter_init(const char *prefix)
  643. {
  644. return (qb_map_pref_iter_create(icmap_map, prefix));
  645. }
  646. const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
  647. {
  648. struct icmap_item *item;
  649. const char *res;
  650. res = qb_map_iter_next(iter, (void **)&item);
  651. if (res == NULL) {
  652. return (res);
  653. }
  654. if (value_len != NULL) {
  655. *value_len = item->value_len;
  656. }
  657. if (type != NULL) {
  658. *type = item->type;
  659. }
  660. return (res);
  661. }
  662. void icmap_iter_finalize(icmap_iter_t iter)
  663. {
  664. qb_map_iter_free(iter);
  665. }
  666. static void icmap_notify_fn(uint32_t event, char *key, void *old_value, void *value, void *user_data)
  667. {
  668. icmap_track_t icmap_track = (icmap_track_t)user_data;
  669. struct icmap_item *new_item = (struct icmap_item *)value;
  670. struct icmap_item *old_item = (struct icmap_item *)old_value;
  671. struct icmap_notify_value new_val;
  672. struct icmap_notify_value old_val;
  673. if (value == NULL && old_value == NULL) {
  674. return ;
  675. }
  676. if (new_item != NULL) {
  677. new_val.type = new_item->type;
  678. new_val.len = new_item->value_len;
  679. new_val.data = new_item->value;
  680. } else {
  681. memset(&new_val, 0, sizeof(new_val));
  682. }
  683. /*
  684. * old_item == new_item if fast functions are used -> don't fill old value
  685. */
  686. if (old_item != NULL && old_item != new_item) {
  687. old_val.type = old_item->type;
  688. old_val.len = old_item->value_len;
  689. old_val.data = old_item->value;
  690. } else {
  691. memset(&old_val, 0, sizeof(old_val));
  692. }
  693. icmap_track->notify_fn(icmap_qbtt_to_tt(event),
  694. key,
  695. new_val,
  696. old_val,
  697. icmap_track->user_data);
  698. }
  699. cs_error_t icmap_track_add(
  700. const char *key_name,
  701. int32_t track_type,
  702. icmap_notify_fn_t notify_fn,
  703. void *user_data,
  704. icmap_track_t *icmap_track)
  705. {
  706. int32_t err;
  707. if (notify_fn == NULL || icmap_track == NULL) {
  708. return (CS_ERR_INVALID_PARAM);
  709. }
  710. if ((track_type & ~(ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX)) != 0) {
  711. return (CS_ERR_INVALID_PARAM);
  712. }
  713. *icmap_track = malloc(sizeof(**icmap_track));
  714. if (*icmap_track == NULL) {
  715. return (CS_ERR_NO_MEMORY);
  716. }
  717. memset(*icmap_track, 0, sizeof(**icmap_track));
  718. if (key_name != NULL) {
  719. (*icmap_track)->key_name = strdup(key_name);
  720. };
  721. (*icmap_track)->track_type = track_type;
  722. (*icmap_track)->notify_fn = notify_fn;
  723. (*icmap_track)->user_data = user_data;
  724. if ((err = qb_map_notify_add(icmap_map, (*icmap_track)->key_name, icmap_notify_fn,
  725. icmap_tt_to_qbtt(track_type), *icmap_track)) != 0) {
  726. free((*icmap_track)->key_name);
  727. free(*icmap_track);
  728. return (qb_to_cs_error(err));
  729. }
  730. list_init(&(*icmap_track)->list);
  731. list_add (&(*icmap_track)->list, &icmap_track_list_head);
  732. return (CS_OK);
  733. }
  734. cs_error_t icmap_track_delete(icmap_track_t icmap_track)
  735. {
  736. int32_t err;
  737. if ((err = qb_map_notify_del_2(icmap_map, icmap_track->key_name,
  738. icmap_notify_fn, icmap_tt_to_qbtt(icmap_track->track_type), icmap_track)) != 0) {
  739. return (qb_to_cs_error(err));
  740. }
  741. list_del(&icmap_track->list);
  742. free(icmap_track->key_name);
  743. free(icmap_track);
  744. return (CS_OK);
  745. }
  746. void *icmap_track_get_user_data(icmap_track_t icmap_track)
  747. {
  748. return (icmap_track->user_data);
  749. }
  750. cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
  751. {
  752. struct list_head *iter;
  753. struct icmap_ro_access_item *icmap_ro_ai;
  754. for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
  755. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  756. if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
  757. /*
  758. * We found item
  759. */
  760. if (ro_access) {
  761. return (CS_ERR_EXIST);
  762. } else {
  763. list_del(&icmap_ro_ai->list);
  764. free(icmap_ro_ai->key_name);
  765. free(icmap_ro_ai);
  766. return (CS_OK);
  767. }
  768. }
  769. }
  770. if (!ro_access) {
  771. return (CS_ERR_NOT_EXIST);
  772. }
  773. icmap_ro_ai = malloc(sizeof(*icmap_ro_ai));
  774. if (icmap_ro_ai == NULL) {
  775. return (CS_ERR_NO_MEMORY);
  776. }
  777. memset(icmap_ro_ai, 0, sizeof(*icmap_ro_ai));
  778. icmap_ro_ai->key_name = strdup(key_name);
  779. if (icmap_ro_ai->key_name == NULL) {
  780. free(icmap_ro_ai);
  781. return (CS_ERR_NO_MEMORY);
  782. }
  783. icmap_ro_ai->prefix = prefix;
  784. list_init(&icmap_ro_ai->list);
  785. list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
  786. return (CS_OK);
  787. }
  788. int icmap_is_key_ro(const char *key_name)
  789. {
  790. struct list_head *iter;
  791. struct icmap_ro_access_item *icmap_ro_ai;
  792. for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
  793. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  794. if (icmap_ro_ai->prefix) {
  795. if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
  796. continue;
  797. if (strncmp(icmap_ro_ai->key_name, key_name, strlen(icmap_ro_ai->key_name)) == 0) {
  798. return (CS_TRUE);
  799. }
  800. } else {
  801. if (strcmp(icmap_ro_ai->key_name, key_name) == 0) {
  802. return (CS_TRUE);
  803. }
  804. }
  805. }
  806. return (CS_FALSE);
  807. }