4
0

icmap.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892
  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/engine/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. if (item != NULL) {
  143. free(item->key_name);
  144. free(item);
  145. }
  146. }
  147. cs_error_t icmap_init(void)
  148. {
  149. int32_t err;
  150. icmap_map = qb_trie_create();
  151. if (icmap_map == NULL)
  152. return (CS_ERR_INIT);
  153. err = qb_map_notify_add(icmap_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
  154. return (qb_to_cs_error(err));
  155. }
  156. static int icmap_is_valid_name_char(char c)
  157. {
  158. return ((c >= 'a' && c <= 'z') ||
  159. (c >= 'A' && c <= 'Z') ||
  160. (c >= '0' && c <= '9') ||
  161. c == '.' || c == '_' || c == '-' || c == '/' || c == ':');
  162. }
  163. void icmap_convert_name_to_valid_name(char *key_name)
  164. {
  165. int i;
  166. for (i = 0; i < strlen(key_name); i++) {
  167. if (!icmap_is_valid_name_char(key_name[i])) {
  168. key_name[i] = '_';
  169. }
  170. }
  171. }
  172. static int icmap_check_key_name(const char *key_name)
  173. {
  174. int i;
  175. if ((strlen(key_name) < ICMAP_KEYNAME_MINLEN) || strlen(key_name) > ICMAP_KEYNAME_MAXLEN) {
  176. return (-1);
  177. }
  178. for (i = 0; i < strlen(key_name); i++) {
  179. if (!icmap_is_valid_name_char(key_name[i])) {
  180. return (-1);
  181. }
  182. }
  183. return (0);
  184. }
  185. static size_t icmap_get_valuetype_len(icmap_value_types_t type)
  186. {
  187. size_t res;
  188. switch (type) {
  189. case ICMAP_VALUETYPE_INT8: res = sizeof(int8_t); break;
  190. case ICMAP_VALUETYPE_UINT8: res = sizeof(uint8_t); break;
  191. case ICMAP_VALUETYPE_INT16: res = sizeof(int16_t); break;
  192. case ICMAP_VALUETYPE_UINT16: res = sizeof(uint16_t); break;
  193. case ICMAP_VALUETYPE_INT32: res = sizeof(int32_t); break;
  194. case ICMAP_VALUETYPE_UINT32: res = sizeof(uint32_t); break;
  195. case ICMAP_VALUETYPE_INT64: res = sizeof(int64_t); break;
  196. case ICMAP_VALUETYPE_UINT64: res = sizeof(uint64_t); break;
  197. case ICMAP_VALUETYPE_FLOAT: res = sizeof(float); break;
  198. case ICMAP_VALUETYPE_DOUBLE: res = sizeof(double); break;
  199. case ICMAP_VALUETYPE_STRING:
  200. case ICMAP_VALUETYPE_BINARY:
  201. res = 0;
  202. break;
  203. }
  204. return (res);
  205. }
  206. static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type)
  207. {
  208. if (value_len > ICMAP_MAX_VALUE_LEN) {
  209. return (-1);
  210. }
  211. if (type != ICMAP_VALUETYPE_STRING && type != ICMAP_VALUETYPE_BINARY) {
  212. if (icmap_get_valuetype_len(type) == value_len) {
  213. return (0);
  214. } else {
  215. return (-1);
  216. }
  217. }
  218. if (type == ICMAP_VALUETYPE_STRING) {
  219. if (value_len > strlen((const char *)value)) {
  220. return (-1);
  221. } else {
  222. return (0);
  223. }
  224. }
  225. return (0);
  226. }
  227. static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type)
  228. {
  229. size_t ptr_len;
  230. if (item->type != type) {
  231. return (0);
  232. }
  233. if (item->type == ICMAP_VALUETYPE_STRING) {
  234. ptr_len = strlen((const char *)value);
  235. if (ptr_len > value_len) {
  236. ptr_len = value_len;
  237. }
  238. ptr_len++;
  239. } else {
  240. ptr_len = value_len;
  241. }
  242. if (item->value_len == ptr_len) {
  243. return (memcmp(item->value, value, value_len) == 0);
  244. };
  245. return (0);
  246. }
  247. cs_error_t icmap_set(
  248. const char *key_name,
  249. const void *value,
  250. size_t value_len,
  251. icmap_value_types_t type)
  252. {
  253. struct icmap_item *item;
  254. struct icmap_item *new_item;
  255. size_t new_value_len;
  256. size_t new_item_size;
  257. if (value == NULL || key_name == NULL) {
  258. return (CS_ERR_INVALID_PARAM);
  259. }
  260. if (icmap_check_value_len(value, value_len, type) != 0) {
  261. return (CS_ERR_INVALID_PARAM);
  262. }
  263. item = qb_map_get(icmap_map, key_name);
  264. if (item != NULL) {
  265. /*
  266. * Check that key is really changed
  267. */
  268. if (icmap_item_eq(item, value, value_len, type)) {
  269. return (CS_OK);
  270. }
  271. } else {
  272. if (icmap_check_key_name(key_name) != 0) {
  273. return (CS_ERR_NAME_TOO_LONG);
  274. }
  275. }
  276. if (type == ICMAP_VALUETYPE_BINARY || type == ICMAP_VALUETYPE_STRING) {
  277. if (type == ICMAP_VALUETYPE_STRING) {
  278. new_value_len = strlen((const char *)value);
  279. if (new_value_len > value_len) {
  280. new_value_len = value_len;
  281. }
  282. new_value_len++;
  283. } else {
  284. new_value_len = value_len;
  285. }
  286. } else {
  287. new_value_len = icmap_get_valuetype_len(type);
  288. }
  289. new_item_size = sizeof(struct icmap_item) + new_value_len;
  290. new_item = malloc(new_item_size);
  291. if (new_item == NULL) {
  292. return (CS_ERR_NO_MEMORY);
  293. }
  294. memset(new_item, 0, new_item_size);
  295. if (item == NULL) {
  296. new_item->key_name = strdup(key_name);
  297. if (new_item->key_name == NULL) {
  298. free(new_item);
  299. return (CS_ERR_NO_MEMORY);
  300. }
  301. } else {
  302. new_item->key_name = item->key_name;
  303. item->key_name = NULL;
  304. }
  305. new_item->type = type;
  306. new_item->value_len = new_value_len;
  307. memcpy(new_item->value, value, new_value_len);
  308. if (new_item->type == ICMAP_VALUETYPE_STRING) {
  309. ((char *)new_item->value)[new_value_len - 1] = 0;
  310. }
  311. qb_map_put(icmap_map, new_item->key_name, new_item);
  312. return (CS_OK);
  313. }
  314. cs_error_t icmap_set_int8(const char *key_name, int8_t value)
  315. {
  316. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT8));
  317. }
  318. cs_error_t icmap_set_uint8(const char *key_name, uint8_t value)
  319. {
  320. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT8));
  321. }
  322. cs_error_t icmap_set_int16(const char *key_name, int16_t value)
  323. {
  324. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT16));
  325. }
  326. cs_error_t icmap_set_uint16(const char *key_name, uint16_t value)
  327. {
  328. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT16));
  329. }
  330. cs_error_t icmap_set_int32(const char *key_name, int32_t value)
  331. {
  332. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT32));
  333. }
  334. cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
  335. {
  336. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT32));
  337. }
  338. cs_error_t icmap_set_int64(const char *key_name, int64_t value)
  339. {
  340. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT64));
  341. }
  342. cs_error_t icmap_set_uint64(const char *key_name, uint64_t value)
  343. {
  344. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT64));
  345. }
  346. cs_error_t icmap_set_float(const char *key_name, float value)
  347. {
  348. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_FLOAT));
  349. }
  350. cs_error_t icmap_set_double(const char *key_name, double value)
  351. {
  352. return (icmap_set(key_name, &value, sizeof(value), ICMAP_VALUETYPE_DOUBLE));
  353. }
  354. cs_error_t icmap_set_string(const char *key_name, const char *value)
  355. {
  356. return (icmap_set(key_name, value, strlen(value), ICMAP_VALUETYPE_STRING));
  357. }
  358. cs_error_t icmap_delete(const char *key_name)
  359. {
  360. struct icmap_item *item;
  361. if (key_name == NULL) {
  362. return (CS_ERR_INVALID_PARAM);
  363. }
  364. item = qb_map_get(icmap_map, key_name);
  365. if (item == NULL) {
  366. return (CS_ERR_NOT_EXIST);
  367. }
  368. if (qb_map_rm(icmap_map, item->key_name) != QB_TRUE) {
  369. return (CS_ERR_NOT_EXIST);
  370. }
  371. return (CS_OK);
  372. }
  373. cs_error_t icmap_get(
  374. const char *key_name,
  375. void *value,
  376. size_t *value_len,
  377. icmap_value_types_t *type)
  378. {
  379. struct icmap_item *item;
  380. if (key_name == NULL) {
  381. return (CS_ERR_INVALID_PARAM);
  382. }
  383. item = qb_map_get(icmap_map, key_name);
  384. if (item == NULL) {
  385. return (CS_ERR_NOT_EXIST);
  386. }
  387. if (type != NULL) {
  388. *type = item->type;
  389. }
  390. if (value == NULL) {
  391. if (value_len != NULL) {
  392. *value_len = item->value_len;
  393. }
  394. } else {
  395. if (value_len == NULL || *value_len < item->value_len) {
  396. return (CS_ERR_INVALID_PARAM);
  397. }
  398. *value_len = item->value_len;
  399. memcpy(value, item->value, item->value_len);
  400. }
  401. return (CS_OK);
  402. }
  403. static cs_error_t icmap_get_int(
  404. const char *key_name,
  405. void *value,
  406. icmap_value_types_t type)
  407. {
  408. char key_value[16];
  409. size_t key_size;
  410. cs_error_t err;
  411. icmap_value_types_t key_type;
  412. key_size = sizeof(key_value);
  413. memset(key_value, 0, key_size);
  414. err = icmap_get(key_name, key_value, &key_size, &key_type);
  415. if (err != CS_OK)
  416. return (err);
  417. if (key_type != type) {
  418. return (CS_ERR_INVALID_PARAM);
  419. }
  420. memcpy(value, key_value, icmap_get_valuetype_len(key_type));
  421. return (CS_OK);
  422. }
  423. cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
  424. {
  425. return (icmap_get_int(key_name, i8, ICMAP_VALUETYPE_INT8));
  426. }
  427. cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8)
  428. {
  429. return (icmap_get_int(key_name, u8, ICMAP_VALUETYPE_UINT8));
  430. }
  431. cs_error_t icmap_get_int16(const char *key_name, int16_t *i16)
  432. {
  433. return (icmap_get_int(key_name, i16, ICMAP_VALUETYPE_INT16));
  434. }
  435. cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16)
  436. {
  437. return (icmap_get_int(key_name, u16, ICMAP_VALUETYPE_UINT16));
  438. }
  439. cs_error_t icmap_get_int32(const char *key_name, int32_t *i32)
  440. {
  441. return (icmap_get_int(key_name, i32, ICMAP_VALUETYPE_INT32));
  442. }
  443. cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32)
  444. {
  445. return (icmap_get_int(key_name, u32, ICMAP_VALUETYPE_UINT32));
  446. }
  447. cs_error_t icmap_get_int64(const char *key_name, int64_t *i64)
  448. {
  449. return(icmap_get_int(key_name, i64, ICMAP_VALUETYPE_INT64));
  450. }
  451. cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64)
  452. {
  453. return (icmap_get_int(key_name, u64, ICMAP_VALUETYPE_UINT64));
  454. }
  455. cs_error_t icmap_get_float(const char *key_name, float *flt)
  456. {
  457. return (icmap_get_int(key_name, flt, ICMAP_VALUETYPE_FLOAT));
  458. }
  459. cs_error_t icmap_get_double(const char *key_name, double *dbl)
  460. {
  461. return (icmap_get_int(key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
  462. }
  463. cs_error_t icmap_get_string(const char *key_name, char **str)
  464. {
  465. cs_error_t res;
  466. size_t str_len;
  467. icmap_value_types_t type;
  468. res = icmap_get(key_name, NULL, &str_len, &type);
  469. if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
  470. if (res == CS_OK) {
  471. res = CS_ERR_INVALID_PARAM;
  472. }
  473. goto return_error;
  474. }
  475. *str = malloc(str_len);
  476. if (*str == NULL) {
  477. res = CS_ERR_NO_MEMORY;
  478. goto return_error;
  479. }
  480. res = icmap_get(key_name, *str, &str_len, &type);
  481. if (res != CS_OK) {
  482. free(*str);
  483. goto return_error;
  484. }
  485. return (CS_OK);
  486. return_error:
  487. return (res);
  488. }
  489. cs_error_t icmap_adjust_int(
  490. const char *key_name,
  491. int32_t step)
  492. {
  493. struct icmap_item *item;
  494. uint8_t u8;
  495. uint16_t u16;
  496. uint32_t u32;
  497. uint64_t u64;
  498. cs_error_t err = CS_OK;
  499. if (key_name == NULL) {
  500. return (CS_ERR_INVALID_PARAM);
  501. }
  502. item = qb_map_get(icmap_map, key_name);
  503. if (item == NULL) {
  504. return (CS_ERR_NOT_EXIST);
  505. }
  506. switch (item->type) {
  507. case ICMAP_VALUETYPE_INT8:
  508. case ICMAP_VALUETYPE_UINT8:
  509. memcpy(&u8, item->value, sizeof(u8));
  510. u8 += step;
  511. err = icmap_set(key_name, &u8, sizeof(u8), item->type);
  512. break;
  513. case ICMAP_VALUETYPE_INT16:
  514. case ICMAP_VALUETYPE_UINT16:
  515. memcpy(&u16, item->value, sizeof(u16));
  516. u16 += step;
  517. err = icmap_set(key_name, &u16, sizeof(u16), item->type);
  518. break;
  519. case ICMAP_VALUETYPE_INT32:
  520. case ICMAP_VALUETYPE_UINT32:
  521. memcpy(&u32, item->value, sizeof(u32));
  522. u32 += step;
  523. err = icmap_set(key_name, &u32, sizeof(u32), item->type);
  524. break;
  525. case ICMAP_VALUETYPE_INT64:
  526. case ICMAP_VALUETYPE_UINT64:
  527. memcpy(&u64, item->value, sizeof(u64));
  528. u64 += step;
  529. err = icmap_set(key_name, &u64, sizeof(u64), item->type);
  530. break;
  531. case ICMAP_VALUETYPE_FLOAT:
  532. case ICMAP_VALUETYPE_DOUBLE:
  533. case ICMAP_VALUETYPE_STRING:
  534. case ICMAP_VALUETYPE_BINARY:
  535. err = CS_ERR_INVALID_PARAM;
  536. break;
  537. }
  538. return (err);
  539. }
  540. cs_error_t icmap_inc(const char *key_name)
  541. {
  542. return (icmap_adjust_int(key_name, 1));
  543. }
  544. cs_error_t icmap_dec(const char *key_name)
  545. {
  546. return (icmap_adjust_int(key_name, -1));
  547. }
  548. icmap_iter_t icmap_iter_init(const char *prefix)
  549. {
  550. return (qb_map_pref_iter_create(icmap_map, prefix));
  551. }
  552. const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
  553. {
  554. struct icmap_item *item;
  555. const char *res;
  556. res = qb_map_iter_next(iter, (void **)&item);
  557. if (res == NULL) {
  558. return (res);
  559. }
  560. if (value_len != NULL) {
  561. *value_len = item->value_len;
  562. }
  563. if (type != NULL) {
  564. *type = item->type;
  565. }
  566. return (res);
  567. }
  568. void icmap_iter_finalize(icmap_iter_t iter)
  569. {
  570. qb_map_iter_free(iter);
  571. }
  572. static void icmap_notify_fn(uint32_t event, char *key, void *old_value, void *value, void *user_data)
  573. {
  574. icmap_track_t icmap_track = (icmap_track_t)user_data;
  575. struct icmap_item *new_item = (struct icmap_item *)value;
  576. struct icmap_item *old_item = (struct icmap_item *)old_value;
  577. struct icmap_notify_value new_val;
  578. struct icmap_notify_value old_val;
  579. if (value == NULL && old_value == NULL) {
  580. return ;
  581. }
  582. if (new_item != NULL) {
  583. new_val.type = new_item->type;
  584. new_val.len = new_item->value_len;
  585. new_val.data = new_item->value;
  586. } else {
  587. memset(&new_val, 0, sizeof(new_val));
  588. }
  589. if (old_item != NULL) {
  590. old_val.type = old_item->type;
  591. old_val.len = old_item->value_len;
  592. old_val.data = old_item->value;
  593. } else {
  594. memset(&old_val, 0, sizeof(old_val));
  595. }
  596. icmap_track->notify_fn(icmap_qbtt_to_tt(event),
  597. key,
  598. new_val,
  599. old_val,
  600. icmap_track->user_data);
  601. }
  602. cs_error_t icmap_track_add(
  603. const char *key_name,
  604. int32_t track_type,
  605. icmap_notify_fn_t notify_fn,
  606. void *user_data,
  607. icmap_track_t *icmap_track)
  608. {
  609. int32_t err;
  610. if (notify_fn == NULL || icmap_track == NULL) {
  611. return (CS_ERR_INVALID_PARAM);
  612. }
  613. if ((track_type & ~(ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX)) != 0) {
  614. return (CS_ERR_INVALID_PARAM);
  615. }
  616. *icmap_track = malloc(sizeof(**icmap_track));
  617. if (*icmap_track == NULL) {
  618. return (CS_ERR_NO_MEMORY);
  619. }
  620. memset(*icmap_track, 0, sizeof(**icmap_track));
  621. if (key_name != NULL) {
  622. (*icmap_track)->key_name = strdup(key_name);
  623. };
  624. (*icmap_track)->track_type = track_type;
  625. (*icmap_track)->notify_fn = notify_fn;
  626. (*icmap_track)->user_data = user_data;
  627. if ((err = qb_map_notify_add(icmap_map, (*icmap_track)->key_name, icmap_notify_fn,
  628. icmap_tt_to_qbtt(track_type), *icmap_track)) != 0) {
  629. free((*icmap_track)->key_name);
  630. free(*icmap_track);
  631. return (qb_to_cs_error(err));
  632. }
  633. return (CS_OK);
  634. }
  635. cs_error_t icmap_track_delete(icmap_track_t icmap_track)
  636. {
  637. int32_t err;
  638. if ((err = qb_map_notify_del_2(icmap_map, icmap_track->key_name,
  639. icmap_notify_fn, icmap_tt_to_qbtt(icmap_track->track_type), icmap_track)) != 0) {
  640. return (qb_to_cs_error(err));
  641. }
  642. free(icmap_track->key_name);
  643. free(icmap_track);
  644. return (CS_OK);
  645. }
  646. void *icmap_track_get_user_data(icmap_track_t icmap_track)
  647. {
  648. return (icmap_track->user_data);
  649. }
  650. cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
  651. {
  652. struct list_head *iter;
  653. struct icmap_ro_access_item *icmap_ro_ai;
  654. for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
  655. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  656. if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
  657. /*
  658. * We found item
  659. */
  660. if (ro_access) {
  661. return (CS_ERR_EXIST);
  662. } else {
  663. list_del(&icmap_ro_ai->list);
  664. free(icmap_ro_ai);
  665. return (CS_OK);
  666. }
  667. }
  668. }
  669. if (!ro_access) {
  670. return (CS_ERR_NOT_EXIST);
  671. }
  672. icmap_ro_ai = malloc(sizeof(*icmap_ro_ai));
  673. if (icmap_ro_ai == NULL) {
  674. return (CS_ERR_NO_MEMORY);
  675. }
  676. memset(icmap_ro_ai, 0, sizeof(*icmap_ro_ai));
  677. icmap_ro_ai->key_name = strdup(key_name);
  678. if (icmap_ro_ai->key_name == NULL) {
  679. free(icmap_ro_ai);
  680. return (CS_ERR_NO_MEMORY);
  681. }
  682. icmap_ro_ai->prefix = prefix;
  683. list_init(&icmap_ro_ai->list);
  684. list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
  685. return (CS_OK);
  686. }
  687. int icmap_is_key_ro(const char *key_name)
  688. {
  689. struct list_head *iter;
  690. struct icmap_ro_access_item *icmap_ro_ai;
  691. for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
  692. icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
  693. if (icmap_ro_ai->prefix) {
  694. if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
  695. continue;
  696. if (strncmp(icmap_ro_ai->key_name, key_name, strlen(icmap_ro_ai->key_name)) == 0) {
  697. return (CS_TRUE);
  698. }
  699. } else {
  700. if (strcmp(icmap_ro_ai->key_name, key_name) == 0) {
  701. return (CS_TRUE);
  702. }
  703. }
  704. }
  705. return (CS_FALSE);
  706. }