icmap.c 21 KB

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