]> git.proxmox.com Git - mirror_corosync.git/blob - exec/icmap.c
icmap: Add func to test equality of two key values
[mirror_corosync.git] / exec / icmap.c
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
35 #include <config.h>
36
37 #include <string.h>
38 #include <stdio.h>
39
40 #include <corosync/corotypes.h>
41
42 #include <qb/qbdefs.h>
43 #include <corosync/list.h>
44 #include <corosync/icmap.h>
45
46 #define ICMAP_MAX_VALUE_LEN (16*1024)
47
48 struct icmap_item {
49 char *key_name;
50 icmap_value_types_t type;
51 size_t value_len;
52 char value[];
53 };
54
55 struct icmap_map {
56 qb_map_t *qb_map;
57 };
58
59 static icmap_map_t icmap_global_map;
60
61 struct icmap_track {
62 char *key_name;
63 int32_t track_type;
64 icmap_notify_fn_t notify_fn;
65 void *user_data;
66 struct list_head list;
67 };
68
69 struct icmap_ro_access_item {
70 char *key_name;
71 int prefix;
72 struct list_head list;
73 };
74
75 DECLARE_LIST_INIT(icmap_ro_access_item_list_head);
76 DECLARE_LIST_INIT(icmap_track_list_head);
77
78 /*
79 * Static functions declarations
80 */
81
82 /*
83 * Check if key_name is valid icmap key name. Returns 0 on success, and -1 on fail
84 */
85 static int icmap_check_key_name(const char *key_name);
86
87 /*
88 * Check that value with given type has correct length value_len. Returns 0 on success,
89 * and -1 on fail
90 */
91 static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type);
92
93 /*
94 * Returns length of value of given type, or 0 for string and binary data type
95 */
96 static size_t icmap_get_valuetype_len(icmap_value_types_t type);
97
98 /*
99 * Converts track type of icmap to qb
100 */
101 static int32_t icmap_tt_to_qbtt(int32_t track_type);
102
103 /*
104 * Convert track type of qb to icmap
105 */
106 static int32_t icmap_qbtt_to_tt(int32_t track_type);
107
108 /*
109 * Checks if item has same value as value with value_len and given type. Returns 0 if not, otherwise !0.
110 */
111 static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type);
112
113 /*
114 * Checks if given character is valid in key name. Returns 0 if not, otherwise !0.
115 */
116 static int icmap_is_valid_name_char(char c);
117
118 /*
119 * Helper for getting integer and float value with given type for key key_name and store it in value.
120 */
121 static cs_error_t icmap_get_int_r(
122 const icmap_map_t map,
123 const char *key_name,
124 void *value,
125 icmap_value_types_t type);
126
127 /*
128 * Return raw item value data. Internal function used by icmap_get_r which does most
129 * of arguments validity checks but doesn't copy data (it returns raw item data
130 * pointer). It's not very safe tho it's static.
131 */
132 static cs_error_t icmap_get_ref_r(
133 const icmap_map_t map,
134 const char *key_name,
135 void **value,
136 size_t *value_len,
137 icmap_value_types_t *type);
138
139 /*
140 * Function implementation
141 */
142 static int32_t icmap_tt_to_qbtt(int32_t track_type)
143 {
144 int32_t res = 0;
145
146 if (track_type & ICMAP_TRACK_DELETE) {
147 res |= QB_MAP_NOTIFY_DELETED;
148 }
149
150 if (track_type & ICMAP_TRACK_MODIFY) {
151 res |= QB_MAP_NOTIFY_REPLACED;
152 }
153
154 if (track_type & ICMAP_TRACK_ADD) {
155 res |= QB_MAP_NOTIFY_INSERTED;
156 }
157
158 if (track_type & ICMAP_TRACK_PREFIX) {
159 res |= QB_MAP_NOTIFY_RECURSIVE;
160 }
161
162 return (res);
163 }
164
165 static int32_t icmap_qbtt_to_tt(int32_t track_type)
166 {
167 int32_t res = 0;
168
169 if (track_type & QB_MAP_NOTIFY_DELETED) {
170 res |= ICMAP_TRACK_DELETE;
171 }
172
173 if (track_type & QB_MAP_NOTIFY_REPLACED) {
174 res |= ICMAP_TRACK_MODIFY;
175 }
176
177 if (track_type & QB_MAP_NOTIFY_INSERTED) {
178 res |= ICMAP_TRACK_ADD;
179 }
180
181 if (track_type & QB_MAP_NOTIFY_RECURSIVE) {
182 res |= ICMAP_TRACK_PREFIX;
183 }
184
185 return (res);
186 }
187
188 static void icmap_map_free_cb(uint32_t event,
189 char* key, void* old_value,
190 void* value, void* user_data)
191 {
192 struct icmap_item *item = (struct icmap_item *)old_value;
193
194 /*
195 * value == old_value -> fast_adjust_int was used, don't free data
196 */
197 if (item != NULL && value != old_value) {
198 free(item->key_name);
199 free(item);
200 }
201 }
202
203 cs_error_t icmap_init_r(icmap_map_t *result)
204 {
205 int32_t err;
206
207 *result = malloc(sizeof(struct icmap_map));
208 if (*result == NULL) {
209 return (CS_ERR_NO_MEMORY);
210 }
211
212 (*result)->qb_map = qb_trie_create();
213 if ((*result)->qb_map == NULL)
214 return (CS_ERR_INIT);
215
216 err = qb_map_notify_add((*result)->qb_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
217
218 return (qb_to_cs_error(err));
219 }
220
221 cs_error_t icmap_init(void)
222 {
223 return (icmap_init_r(&icmap_global_map));
224 }
225
226 static void icmap_set_ro_access_free(void)
227 {
228 struct list_head *iter = icmap_ro_access_item_list_head.next;
229 struct icmap_ro_access_item *icmap_ro_ai;
230
231 while (iter != &icmap_ro_access_item_list_head) {
232 icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
233 list_del(&icmap_ro_ai->list);
234 free(icmap_ro_ai->key_name);
235 free(icmap_ro_ai);
236 iter = icmap_ro_access_item_list_head.next;
237 }
238 }
239
240 static void icmap_del_all_track(void)
241 {
242 struct list_head *iter = icmap_track_list_head.next;
243 struct icmap_track *icmap_track;
244
245 while (iter != &icmap_track_list_head) {
246 icmap_track = list_entry(iter, struct icmap_track, list);
247 icmap_track_delete(icmap_track);
248 iter = icmap_track_list_head.next;
249 }
250 }
251
252 void icmap_fini_r(const icmap_map_t map)
253 {
254
255 qb_map_destroy(map->qb_map);
256 free(map);
257
258 return;
259 }
260
261 void icmap_fini(void)
262 {
263
264 icmap_del_all_track();
265 /*
266 * catch 22 warning:
267 * We need to drop this notify but we can't because it calls icmap_map_free_cb
268 * while destroying the tree to free icmap_item(s).
269 * -> qb_map_notify_del_2(icmap_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
270 * and we cannot call it after map_destroy. joy! :)
271 */
272 icmap_fini_r(icmap_global_map);
273 icmap_set_ro_access_free();
274
275 return ;
276 }
277
278 icmap_map_t icmap_get_global_map(void)
279 {
280
281 return (icmap_global_map);
282 }
283
284 static int icmap_is_valid_name_char(char c)
285 {
286 return ((c >= 'a' && c <= 'z') ||
287 (c >= 'A' && c <= 'Z') ||
288 (c >= '0' && c <= '9') ||
289 c == '.' || c == '_' || c == '-' || c == '/' || c == ':');
290 }
291
292 void icmap_convert_name_to_valid_name(char *key_name)
293 {
294 int i;
295
296 for (i = 0; i < strlen(key_name); i++) {
297 if (!icmap_is_valid_name_char(key_name[i])) {
298 key_name[i] = '_';
299 }
300 }
301 }
302
303 static int icmap_check_key_name(const char *key_name)
304 {
305 int i;
306
307 if ((strlen(key_name) < ICMAP_KEYNAME_MINLEN) || strlen(key_name) > ICMAP_KEYNAME_MAXLEN) {
308 return (-1);
309 }
310
311 for (i = 0; i < strlen(key_name); i++) {
312 if (!icmap_is_valid_name_char(key_name[i])) {
313 return (-1);
314 }
315 }
316
317 return (0);
318 }
319
320 static size_t icmap_get_valuetype_len(icmap_value_types_t type)
321 {
322 size_t res = 0;
323
324 switch (type) {
325 case ICMAP_VALUETYPE_INT8: res = sizeof(int8_t); break;
326 case ICMAP_VALUETYPE_UINT8: res = sizeof(uint8_t); break;
327 case ICMAP_VALUETYPE_INT16: res = sizeof(int16_t); break;
328 case ICMAP_VALUETYPE_UINT16: res = sizeof(uint16_t); break;
329 case ICMAP_VALUETYPE_INT32: res = sizeof(int32_t); break;
330 case ICMAP_VALUETYPE_UINT32: res = sizeof(uint32_t); break;
331 case ICMAP_VALUETYPE_INT64: res = sizeof(int64_t); break;
332 case ICMAP_VALUETYPE_UINT64: res = sizeof(uint64_t); break;
333 case ICMAP_VALUETYPE_FLOAT: res = sizeof(float); break;
334 case ICMAP_VALUETYPE_DOUBLE: res = sizeof(double); break;
335 case ICMAP_VALUETYPE_STRING:
336 case ICMAP_VALUETYPE_BINARY:
337 res = 0;
338 break;
339 }
340
341 return (res);
342 }
343
344 static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type)
345 {
346
347 if (value_len > ICMAP_MAX_VALUE_LEN) {
348 return (-1);
349 }
350
351 if (type != ICMAP_VALUETYPE_STRING && type != ICMAP_VALUETYPE_BINARY) {
352 if (icmap_get_valuetype_len(type) == value_len) {
353 return (0);
354 } else {
355 return (-1);
356 }
357 }
358
359 if (type == ICMAP_VALUETYPE_STRING) {
360 /*
361 * value_len can be shorter then real string length, but never
362 * longer (+ 1 is because of 0 at the end of string)
363 */
364 if (value_len > strlen((const char *)value) + 1) {
365 return (-1);
366 } else {
367 return (0);
368 }
369 }
370
371 return (0);
372 }
373
374 static int icmap_item_eq(const struct icmap_item *item, const void *value, size_t value_len, icmap_value_types_t type)
375 {
376 size_t ptr_len;
377
378 if (item->type != type) {
379 return (0);
380 }
381
382 if (item->type == ICMAP_VALUETYPE_STRING) {
383 ptr_len = strlen((const char *)value);
384 if (ptr_len > value_len) {
385 ptr_len = value_len;
386 }
387 ptr_len++;
388 } else {
389 ptr_len = value_len;
390 }
391
392 if (item->value_len == ptr_len) {
393 return (memcmp(item->value, value, value_len) == 0);
394 };
395
396 return (0);
397 }
398
399 int icmap_key_value_eq(
400 const icmap_map_t map1,
401 const char *key_name1,
402 const icmap_map_t map2,
403 const char *key_name2)
404 {
405 struct icmap_item *item1, *item2;
406
407 if (map1 == NULL || key_name1 == NULL || map2 == NULL || key_name2 == NULL) {
408 return (0);
409 }
410
411 item1 = qb_map_get(map1->qb_map, key_name1);
412 item2 = qb_map_get(map2->qb_map, key_name2);
413
414 if (item1 == NULL || item2 == NULL) {
415 return (0);
416 }
417
418 return (icmap_item_eq(item1, item2->value, item2->value_len, item2->type));
419 }
420
421 cs_error_t icmap_set_r(
422 const icmap_map_t map,
423 const char *key_name,
424 const void *value,
425 size_t value_len,
426 icmap_value_types_t type)
427 {
428 struct icmap_item *item;
429 struct icmap_item *new_item;
430 size_t new_value_len;
431 size_t new_item_size;
432
433 if (value == NULL || key_name == NULL) {
434 return (CS_ERR_INVALID_PARAM);
435 }
436
437 if (icmap_check_value_len(value, value_len, type) != 0) {
438 return (CS_ERR_INVALID_PARAM);
439 }
440
441 item = qb_map_get(map->qb_map, key_name);
442 if (item != NULL) {
443 /*
444 * Check that key is really changed
445 */
446 if (icmap_item_eq(item, value, value_len, type)) {
447 return (CS_OK);
448 }
449 } else {
450 if (icmap_check_key_name(key_name) != 0) {
451 return (CS_ERR_NAME_TOO_LONG);
452 }
453 }
454
455 if (type == ICMAP_VALUETYPE_BINARY || type == ICMAP_VALUETYPE_STRING) {
456 if (type == ICMAP_VALUETYPE_STRING) {
457 new_value_len = strlen((const char *)value);
458 if (new_value_len > value_len) {
459 new_value_len = value_len;
460 }
461 new_value_len++;
462 } else {
463 new_value_len = value_len;
464 }
465 } else {
466 new_value_len = icmap_get_valuetype_len(type);
467 }
468
469 new_item_size = sizeof(struct icmap_item) + new_value_len;
470 new_item = malloc(new_item_size);
471 if (new_item == NULL) {
472 return (CS_ERR_NO_MEMORY);
473 }
474 memset(new_item, 0, new_item_size);
475
476 if (item == NULL) {
477 new_item->key_name = strdup(key_name);
478 if (new_item->key_name == NULL) {
479 free(new_item);
480 return (CS_ERR_NO_MEMORY);
481 }
482 } else {
483 new_item->key_name = item->key_name;
484 item->key_name = NULL;
485 }
486
487 new_item->type = type;
488 new_item->value_len = new_value_len;
489
490 memcpy(new_item->value, value, new_value_len);
491
492 if (new_item->type == ICMAP_VALUETYPE_STRING) {
493 ((char *)new_item->value)[new_value_len - 1] = 0;
494 }
495
496 qb_map_put(map->qb_map, new_item->key_name, new_item);
497
498 return (CS_OK);
499 }
500
501 cs_error_t icmap_set(
502 const char *key_name,
503 const void *value,
504 size_t value_len,
505 icmap_value_types_t type)
506 {
507
508 return (icmap_set_r(icmap_global_map, key_name, value, value_len, type));
509 }
510
511 cs_error_t icmap_set_int8_r(const icmap_map_t map, const char *key_name, int8_t value)
512 {
513
514 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT8));
515 }
516
517 cs_error_t icmap_set_uint8_r(const icmap_map_t map, const char *key_name, uint8_t value)
518 {
519
520 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT8));
521 }
522
523 cs_error_t icmap_set_int16_r(const icmap_map_t map, const char *key_name, int16_t value)
524 {
525
526 return (icmap_set_r(map,key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT16));
527 }
528
529 cs_error_t icmap_set_uint16_r(const icmap_map_t map, const char *key_name, uint16_t value)
530 {
531
532 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT16));
533 }
534
535 cs_error_t icmap_set_int32_r(const icmap_map_t map, const char *key_name, int32_t value)
536 {
537
538 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT32));
539 }
540
541 cs_error_t icmap_set_uint32_r(const icmap_map_t map, const char *key_name, uint32_t value)
542 {
543
544 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT32));
545 }
546
547 cs_error_t icmap_set_int64_r(const icmap_map_t map, const char *key_name, int64_t value)
548 {
549
550 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_INT64));
551 }
552
553 cs_error_t icmap_set_uint64_r(const icmap_map_t map, const char *key_name, uint64_t value)
554 {
555
556 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_UINT64));
557 }
558
559 cs_error_t icmap_set_float_r(const icmap_map_t map, const char *key_name, float value)
560 {
561
562 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_FLOAT));
563 }
564
565 cs_error_t icmap_set_double_r(const icmap_map_t map, const char *key_name, double value)
566 {
567
568 return (icmap_set_r(map, key_name, &value, sizeof(value), ICMAP_VALUETYPE_DOUBLE));
569 }
570
571 cs_error_t icmap_set_string_r(const icmap_map_t map, const char *key_name, const char *value)
572 {
573
574 if (value == NULL) {
575 return (CS_ERR_INVALID_PARAM);
576 }
577
578 return (icmap_set_r(map, key_name, value, strlen(value), ICMAP_VALUETYPE_STRING));
579 }
580
581 cs_error_t icmap_set_int8(const char *key_name, int8_t value)
582 {
583
584 return (icmap_set_int8_r(icmap_global_map, key_name, value));
585 }
586
587 cs_error_t icmap_set_uint8(const char *key_name, uint8_t value)
588 {
589
590 return (icmap_set_uint8_r(icmap_global_map, key_name, value));
591 }
592
593 cs_error_t icmap_set_int16(const char *key_name, int16_t value)
594 {
595
596 return (icmap_set_int16_r(icmap_global_map, key_name, value));
597 }
598
599 cs_error_t icmap_set_uint16(const char *key_name, uint16_t value)
600 {
601
602 return (icmap_set_uint16_r(icmap_global_map, key_name, value));
603 }
604
605 cs_error_t icmap_set_int32(const char *key_name, int32_t value)
606 {
607
608 return (icmap_set_int32_r(icmap_global_map, key_name, value));
609 }
610
611 cs_error_t icmap_set_uint32(const char *key_name, uint32_t value)
612 {
613
614 return (icmap_set_uint32_r(icmap_global_map, key_name, value));
615 }
616
617 cs_error_t icmap_set_int64(const char *key_name, int64_t value)
618 {
619
620 return (icmap_set_int64_r(icmap_global_map, key_name, value));
621 }
622
623 cs_error_t icmap_set_uint64(const char *key_name, uint64_t value)
624 {
625
626 return (icmap_set_uint64_r(icmap_global_map, key_name, value));
627 }
628
629 cs_error_t icmap_set_float(const char *key_name, float value)
630 {
631
632 return (icmap_set_float_r(icmap_global_map, key_name, value));
633 }
634
635 cs_error_t icmap_set_double(const char *key_name, double value)
636 {
637
638 return (icmap_set_double_r(icmap_global_map, key_name, value));
639 }
640
641 cs_error_t icmap_set_string(const char *key_name, const char *value)
642 {
643
644 return (icmap_set_string_r(icmap_global_map, key_name, value));
645 }
646
647 cs_error_t icmap_delete_r(const icmap_map_t map, const char *key_name)
648 {
649 struct icmap_item *item;
650
651 if (key_name == NULL) {
652 return (CS_ERR_INVALID_PARAM);
653 }
654
655 item = qb_map_get(map->qb_map, key_name);
656 if (item == NULL) {
657 return (CS_ERR_NOT_EXIST);
658 }
659
660 if (qb_map_rm(map->qb_map, item->key_name) != QB_TRUE) {
661 return (CS_ERR_NOT_EXIST);
662 }
663
664 return (CS_OK);
665 }
666
667 cs_error_t icmap_delete(const char *key_name)
668 {
669
670 return (icmap_delete_r(icmap_global_map, key_name));
671 }
672
673 static cs_error_t icmap_get_ref_r(
674 const icmap_map_t map,
675 const char *key_name,
676 void **value,
677 size_t *value_len,
678 icmap_value_types_t *type)
679 {
680 struct icmap_item *item;
681
682 if (key_name == NULL) {
683 return (CS_ERR_INVALID_PARAM);
684 }
685
686 item = qb_map_get(map->qb_map, key_name);
687 if (item == NULL) {
688 return (CS_ERR_NOT_EXIST);
689 }
690
691 if (type != NULL) {
692 *type = item->type;
693 }
694
695 if (value_len != NULL) {
696 *value_len = item->value_len;
697 }
698
699 if (value != NULL) {
700 *value = item->value;
701 }
702
703 return (CS_OK);
704 }
705
706 cs_error_t icmap_get_r(
707 const icmap_map_t map,
708 const char *key_name,
709 void *value,
710 size_t *value_len,
711 icmap_value_types_t *type)
712 {
713 cs_error_t res;
714 void *tmp_value;
715 size_t tmp_value_len;
716
717 res = icmap_get_ref_r(map, key_name, &tmp_value, &tmp_value_len, type);
718 if (res != CS_OK) {
719 return (res);
720 }
721
722 if (value == NULL) {
723 if (value_len != NULL) {
724 *value_len = tmp_value_len;
725 }
726 } else {
727 if (value_len == NULL || *value_len < tmp_value_len) {
728 return (CS_ERR_INVALID_PARAM);
729 }
730
731 *value_len = tmp_value_len;
732
733 memcpy(value, tmp_value, tmp_value_len);
734 }
735
736 return (CS_OK);
737 }
738
739 cs_error_t icmap_get(
740 const char *key_name,
741 void *value,
742 size_t *value_len,
743 icmap_value_types_t *type)
744 {
745
746 return (icmap_get_r(icmap_global_map, key_name, value, value_len, type));
747 }
748
749 static cs_error_t icmap_get_int_r(
750 const icmap_map_t map,
751 const char *key_name,
752 void *value,
753 icmap_value_types_t type)
754 {
755 char key_value[16];
756 size_t key_size;
757 cs_error_t err;
758 icmap_value_types_t key_type;
759
760 key_size = sizeof(key_value);
761 memset(key_value, 0, key_size);
762
763 err = icmap_get(key_name, key_value, &key_size, &key_type);
764 if (err != CS_OK)
765 return (err);
766
767 if (key_type != type) {
768 return (CS_ERR_INVALID_PARAM);
769 }
770
771 memcpy(value, key_value, icmap_get_valuetype_len(key_type));
772
773 return (CS_OK);
774 }
775
776 cs_error_t icmap_get_int8_r(const icmap_map_t map, const char *key_name, int8_t *i8)
777 {
778
779 return (icmap_get_int_r(map, key_name, i8, ICMAP_VALUETYPE_INT8));
780 }
781
782 cs_error_t icmap_get_uint8_r(const icmap_map_t map, const char *key_name, uint8_t *u8)
783 {
784
785 return (icmap_get_int_r(map, key_name, u8, ICMAP_VALUETYPE_UINT8));
786 }
787
788 cs_error_t icmap_get_int16_r(const icmap_map_t map, const char *key_name, int16_t *i16)
789 {
790
791 return (icmap_get_int_r(map, key_name, i16, ICMAP_VALUETYPE_INT16));
792 }
793
794 cs_error_t icmap_get_uint16_r(const icmap_map_t map, const char *key_name, uint16_t *u16)
795 {
796
797 return (icmap_get_int_r(map, key_name, u16, ICMAP_VALUETYPE_UINT16));
798 }
799
800 cs_error_t icmap_get_int32_r(const icmap_map_t map, const char *key_name, int32_t *i32)
801 {
802
803 return (icmap_get_int_r(map, key_name, i32, ICMAP_VALUETYPE_INT32));
804 }
805
806 cs_error_t icmap_get_uint32_r(const icmap_map_t map, const char *key_name, uint32_t *u32)
807 {
808
809 return (icmap_get_int_r(map, key_name, u32, ICMAP_VALUETYPE_UINT32));
810 }
811
812 cs_error_t icmap_get_int64_r(const icmap_map_t map, const char *key_name, int64_t *i64)
813 {
814
815 return(icmap_get_int_r(map, key_name, i64, ICMAP_VALUETYPE_INT64));
816 }
817
818 cs_error_t icmap_get_uint64_r(const icmap_map_t map, const char *key_name, uint64_t *u64)
819 {
820
821 return (icmap_get_int_r(map, key_name, u64, ICMAP_VALUETYPE_UINT64));
822 }
823
824 cs_error_t icmap_get_float_r(const icmap_map_t map, const char *key_name, float *flt)
825 {
826
827 return (icmap_get_int_r(map, key_name, flt, ICMAP_VALUETYPE_FLOAT));
828 }
829
830 cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, double *dbl)
831 {
832
833 return (icmap_get_int_r(map, key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
834 }
835
836 cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
837 {
838
839 return (icmap_get_int8_r(icmap_global_map, key_name, i8));
840 }
841
842 cs_error_t icmap_get_uint8(const char *key_name, uint8_t *u8)
843 {
844
845 return (icmap_get_uint8_r(icmap_global_map, key_name, u8));
846 }
847
848 cs_error_t icmap_get_int16(const char *key_name, int16_t *i16)
849 {
850
851 return (icmap_get_int16_r(icmap_global_map, key_name, i16));
852 }
853
854 cs_error_t icmap_get_uint16(const char *key_name, uint16_t *u16)
855 {
856
857 return (icmap_get_uint16_r(icmap_global_map, key_name, u16));
858 }
859
860 cs_error_t icmap_get_int32(const char *key_name, int32_t *i32)
861 {
862
863 return (icmap_get_int32_r(icmap_global_map, key_name, i32));
864 }
865
866 cs_error_t icmap_get_uint32(const char *key_name, uint32_t *u32)
867 {
868
869 return (icmap_get_uint32_r(icmap_global_map, key_name, u32));
870 }
871
872 cs_error_t icmap_get_int64(const char *key_name, int64_t *i64)
873 {
874
875 return(icmap_get_int64_r(icmap_global_map, key_name, i64));
876 }
877
878 cs_error_t icmap_get_uint64(const char *key_name, uint64_t *u64)
879 {
880
881 return (icmap_get_uint64_r(icmap_global_map, key_name, u64));
882 }
883
884 cs_error_t icmap_get_float(const char *key_name, float *flt)
885 {
886
887 return (icmap_get_float_r(icmap_global_map, key_name, flt));
888 }
889
890 cs_error_t icmap_get_double(const char *key_name, double *dbl)
891 {
892
893 return (icmap_get_double_r(icmap_global_map, key_name, dbl));
894 }
895
896 cs_error_t icmap_get_string(const char *key_name, char **str)
897 {
898 cs_error_t res;
899 size_t str_len;
900 icmap_value_types_t type;
901
902 res = icmap_get(key_name, NULL, &str_len, &type);
903 if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
904 if (res == CS_OK) {
905 res = CS_ERR_INVALID_PARAM;
906 }
907
908 goto return_error;
909 }
910
911 *str = malloc(str_len);
912 if (*str == NULL) {
913 res = CS_ERR_NO_MEMORY;
914
915 goto return_error;
916 }
917
918 res = icmap_get(key_name, *str, &str_len, &type);
919 if (res != CS_OK) {
920 free(*str);
921 goto return_error;
922 }
923
924 return (CS_OK);
925
926 return_error:
927 return (res);
928 }
929
930 cs_error_t icmap_adjust_int_r(
931 const icmap_map_t map,
932 const char *key_name,
933 int32_t step)
934 {
935 struct icmap_item *item;
936 uint8_t u8;
937 uint16_t u16;
938 uint32_t u32;
939 uint64_t u64;
940 cs_error_t err = CS_OK;
941
942 if (key_name == NULL) {
943 return (CS_ERR_INVALID_PARAM);
944 }
945
946 item = qb_map_get(map->qb_map, key_name);
947 if (item == NULL) {
948 return (CS_ERR_NOT_EXIST);
949 }
950
951 switch (item->type) {
952 case ICMAP_VALUETYPE_INT8:
953 case ICMAP_VALUETYPE_UINT8:
954 memcpy(&u8, item->value, sizeof(u8));
955 u8 += step;
956 err = icmap_set(key_name, &u8, sizeof(u8), item->type);
957 break;
958 case ICMAP_VALUETYPE_INT16:
959 case ICMAP_VALUETYPE_UINT16:
960 memcpy(&u16, item->value, sizeof(u16));
961 u16 += step;
962 err = icmap_set(key_name, &u16, sizeof(u16), item->type);
963 break;
964 case ICMAP_VALUETYPE_INT32:
965 case ICMAP_VALUETYPE_UINT32:
966 memcpy(&u32, item->value, sizeof(u32));
967 u32 += step;
968 err = icmap_set(key_name, &u32, sizeof(u32), item->type);
969 break;
970 case ICMAP_VALUETYPE_INT64:
971 case ICMAP_VALUETYPE_UINT64:
972 memcpy(&u64, item->value, sizeof(u64));
973 u64 += step;
974 err = icmap_set(key_name, &u64, sizeof(u64), item->type);
975 break;
976 case ICMAP_VALUETYPE_FLOAT:
977 case ICMAP_VALUETYPE_DOUBLE:
978 case ICMAP_VALUETYPE_STRING:
979 case ICMAP_VALUETYPE_BINARY:
980 err = CS_ERR_INVALID_PARAM;
981 break;
982 }
983
984 return (err);
985 }
986
987 cs_error_t icmap_adjust_int(
988 const char *key_name,
989 int32_t step)
990 {
991
992 return (icmap_adjust_int_r(icmap_global_map, key_name, step));
993 }
994
995 cs_error_t icmap_fast_adjust_int_r(
996 const icmap_map_t map,
997 const char *key_name,
998 int32_t step)
999 {
1000 struct icmap_item *item;
1001 cs_error_t err = CS_OK;
1002
1003 if (key_name == NULL) {
1004 return (CS_ERR_INVALID_PARAM);
1005 }
1006
1007 item = qb_map_get(map->qb_map, key_name);
1008 if (item == NULL) {
1009 return (CS_ERR_NOT_EXIST);
1010 }
1011
1012 switch (item->type) {
1013 case ICMAP_VALUETYPE_INT8:
1014 case ICMAP_VALUETYPE_UINT8:
1015 *(uint8_t *)item->value += step;
1016 break;
1017 case ICMAP_VALUETYPE_INT16:
1018 case ICMAP_VALUETYPE_UINT16:
1019 *(uint16_t *)item->value += step;
1020 break;
1021 case ICMAP_VALUETYPE_INT32:
1022 case ICMAP_VALUETYPE_UINT32:
1023 *(uint32_t *)item->value += step;
1024 break;
1025 case ICMAP_VALUETYPE_INT64:
1026 case ICMAP_VALUETYPE_UINT64:
1027 *(uint64_t *)item->value += step;
1028 break;
1029 case ICMAP_VALUETYPE_FLOAT:
1030 case ICMAP_VALUETYPE_DOUBLE:
1031 case ICMAP_VALUETYPE_STRING:
1032 case ICMAP_VALUETYPE_BINARY:
1033 err = CS_ERR_INVALID_PARAM;
1034 break;
1035 }
1036
1037 if (err == CS_OK) {
1038 qb_map_put(map->qb_map, item->key_name, item);
1039 }
1040
1041 return (err);
1042 }
1043
1044 cs_error_t icmap_fast_adjust_int(
1045 const char *key_name,
1046 int32_t step)
1047 {
1048
1049 return (icmap_fast_adjust_int_r(icmap_global_map, key_name, step));
1050 }
1051
1052 cs_error_t icmap_inc_r(const icmap_map_t map, const char *key_name)
1053 {
1054 return (icmap_adjust_int_r(map, key_name, 1));
1055 }
1056
1057 cs_error_t icmap_inc(const char *key_name)
1058 {
1059 return (icmap_inc_r(icmap_global_map, key_name));
1060 }
1061
1062 cs_error_t icmap_dec_r(const icmap_map_t map, const char *key_name)
1063 {
1064 return (icmap_adjust_int_r(map, key_name, -1));
1065 }
1066
1067 cs_error_t icmap_dec(const char *key_name)
1068 {
1069 return (icmap_dec_r(icmap_global_map, key_name));
1070 }
1071
1072 cs_error_t icmap_fast_inc_r(const icmap_map_t map, const char *key_name)
1073 {
1074 return (icmap_fast_adjust_int_r(map, key_name, 1));
1075 }
1076
1077 cs_error_t icmap_fast_inc(const char *key_name)
1078 {
1079 return (icmap_fast_inc_r(icmap_global_map, key_name));
1080 }
1081
1082 cs_error_t icmap_fast_dec_r(const icmap_map_t map, const char *key_name)
1083 {
1084 return (icmap_fast_adjust_int_r(map, key_name, -1));
1085 }
1086
1087 cs_error_t icmap_fast_dec(const char *key_name)
1088 {
1089 return (icmap_fast_dec_r(icmap_global_map, key_name));
1090 }
1091
1092 icmap_iter_t icmap_iter_init_r(const icmap_map_t map, const char *prefix)
1093 {
1094 return (qb_map_pref_iter_create(map->qb_map, prefix));
1095 }
1096
1097 icmap_iter_t icmap_iter_init(const char *prefix)
1098 {
1099 return (icmap_iter_init_r(icmap_global_map, prefix));
1100 }
1101
1102
1103 const char *icmap_iter_next(icmap_iter_t iter, size_t *value_len, icmap_value_types_t *type)
1104 {
1105 struct icmap_item *item;
1106 const char *res;
1107
1108 res = qb_map_iter_next(iter, (void **)&item);
1109 if (res == NULL) {
1110 return (res);
1111 }
1112
1113 if (value_len != NULL) {
1114 *value_len = item->value_len;
1115 }
1116
1117 if (type != NULL) {
1118 *type = item->type;
1119 }
1120
1121 return (res);
1122 }
1123
1124 void icmap_iter_finalize(icmap_iter_t iter)
1125 {
1126 qb_map_iter_free(iter);
1127 }
1128
1129 static void icmap_notify_fn(uint32_t event, char *key, void *old_value, void *value, void *user_data)
1130 {
1131 icmap_track_t icmap_track = (icmap_track_t)user_data;
1132 struct icmap_item *new_item = (struct icmap_item *)value;
1133 struct icmap_item *old_item = (struct icmap_item *)old_value;
1134 struct icmap_notify_value new_val;
1135 struct icmap_notify_value old_val;
1136
1137 if (value == NULL && old_value == NULL) {
1138 return ;
1139 }
1140
1141 if (new_item != NULL) {
1142 new_val.type = new_item->type;
1143 new_val.len = new_item->value_len;
1144 new_val.data = new_item->value;
1145 } else {
1146 memset(&new_val, 0, sizeof(new_val));
1147 }
1148
1149 /*
1150 * old_item == new_item if fast functions are used -> don't fill old value
1151 */
1152 if (old_item != NULL && old_item != new_item) {
1153 old_val.type = old_item->type;
1154 old_val.len = old_item->value_len;
1155 old_val.data = old_item->value;
1156 } else {
1157 memset(&old_val, 0, sizeof(old_val));
1158 }
1159
1160 icmap_track->notify_fn(icmap_qbtt_to_tt(event),
1161 key,
1162 new_val,
1163 old_val,
1164 icmap_track->user_data);
1165 }
1166
1167 cs_error_t icmap_track_add(
1168 const char *key_name,
1169 int32_t track_type,
1170 icmap_notify_fn_t notify_fn,
1171 void *user_data,
1172 icmap_track_t *icmap_track)
1173 {
1174 int32_t err;
1175
1176 if (notify_fn == NULL || icmap_track == NULL) {
1177 return (CS_ERR_INVALID_PARAM);
1178 }
1179
1180 if ((track_type & ~(ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX)) != 0) {
1181 return (CS_ERR_INVALID_PARAM);
1182 }
1183
1184 *icmap_track = malloc(sizeof(**icmap_track));
1185 if (*icmap_track == NULL) {
1186 return (CS_ERR_NO_MEMORY);
1187 }
1188 memset(*icmap_track, 0, sizeof(**icmap_track));
1189
1190 if (key_name != NULL) {
1191 (*icmap_track)->key_name = strdup(key_name);
1192 };
1193
1194 (*icmap_track)->track_type = track_type;
1195 (*icmap_track)->notify_fn = notify_fn;
1196 (*icmap_track)->user_data = user_data;
1197
1198 if ((err = qb_map_notify_add(icmap_global_map->qb_map, (*icmap_track)->key_name, icmap_notify_fn,
1199 icmap_tt_to_qbtt(track_type), *icmap_track)) != 0) {
1200 free((*icmap_track)->key_name);
1201 free(*icmap_track);
1202
1203 return (qb_to_cs_error(err));
1204 }
1205
1206 list_init(&(*icmap_track)->list);
1207 list_add (&(*icmap_track)->list, &icmap_track_list_head);
1208
1209 return (CS_OK);
1210 }
1211
1212 cs_error_t icmap_track_delete(icmap_track_t icmap_track)
1213 {
1214 int32_t err;
1215
1216 if ((err = qb_map_notify_del_2(icmap_global_map->qb_map, icmap_track->key_name,
1217 icmap_notify_fn, icmap_tt_to_qbtt(icmap_track->track_type), icmap_track)) != 0) {
1218 return (qb_to_cs_error(err));
1219 }
1220
1221 list_del(&icmap_track->list);
1222 free(icmap_track->key_name);
1223 free(icmap_track);
1224
1225 return (CS_OK);
1226 }
1227
1228 void *icmap_track_get_user_data(icmap_track_t icmap_track)
1229 {
1230 return (icmap_track->user_data);
1231 }
1232
1233 cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
1234 {
1235 struct list_head *iter;
1236 struct icmap_ro_access_item *icmap_ro_ai;
1237
1238 for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
1239 icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
1240
1241 if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
1242 /*
1243 * We found item
1244 */
1245 if (ro_access) {
1246 return (CS_ERR_EXIST);
1247 } else {
1248 list_del(&icmap_ro_ai->list);
1249 free(icmap_ro_ai->key_name);
1250 free(icmap_ro_ai);
1251
1252 return (CS_OK);
1253 }
1254 }
1255 }
1256
1257 if (!ro_access) {
1258 return (CS_ERR_NOT_EXIST);
1259 }
1260
1261 icmap_ro_ai = malloc(sizeof(*icmap_ro_ai));
1262 if (icmap_ro_ai == NULL) {
1263 return (CS_ERR_NO_MEMORY);
1264 }
1265
1266 memset(icmap_ro_ai, 0, sizeof(*icmap_ro_ai));
1267 icmap_ro_ai->key_name = strdup(key_name);
1268 if (icmap_ro_ai->key_name == NULL) {
1269 free(icmap_ro_ai);
1270 return (CS_ERR_NO_MEMORY);
1271 }
1272
1273 icmap_ro_ai->prefix = prefix;
1274 list_init(&icmap_ro_ai->list);
1275 list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
1276
1277 return (CS_OK);
1278 }
1279
1280 int icmap_is_key_ro(const char *key_name)
1281 {
1282 struct list_head *iter;
1283 struct icmap_ro_access_item *icmap_ro_ai;
1284
1285 for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
1286 icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
1287
1288 if (icmap_ro_ai->prefix) {
1289 if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
1290 continue;
1291
1292 if (strncmp(icmap_ro_ai->key_name, key_name, strlen(icmap_ro_ai->key_name)) == 0) {
1293 return (CS_TRUE);
1294 }
1295 } else {
1296 if (strcmp(icmap_ro_ai->key_name, key_name) == 0) {
1297 return (CS_TRUE);
1298 }
1299 }
1300 }
1301
1302 return (CS_FALSE);
1303
1304 }
1305
1306 cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map)
1307 {
1308 icmap_iter_t iter;
1309 size_t value_len;
1310 icmap_value_types_t value_type;
1311 const char *key_name;
1312 cs_error_t err;
1313 void *value;
1314
1315 iter = icmap_iter_init_r(src_map, NULL);
1316 if (iter == NULL) {
1317 return (CS_ERR_NO_MEMORY);
1318 }
1319
1320 err = CS_OK;
1321
1322 while ((key_name = icmap_iter_next(iter, &value_len, &value_type)) != NULL) {
1323 err = icmap_get_ref_r(src_map, key_name, &value, &value_len, &value_type);
1324 if (err != CS_OK) {
1325 goto exit_iter_finalize;
1326 }
1327
1328 err = icmap_set_r(dst_map, key_name, value, value_len, value_type);
1329 if (err != CS_OK) {
1330 goto exit_iter_finalize;
1331 }
1332 }
1333
1334 exit_iter_finalize:
1335 icmap_iter_finalize(iter);
1336
1337 return (err);
1338 }