]> git.proxmox.com Git - mirror_corosync.git/blobdiff - exec/icmap.c
icmap: icmap_init_r() leaks if trie_create() fails
[mirror_corosync.git] / exec / icmap.c
index 83cdfb432fcd62f8bce108e8daaa700a33cb89bf..4aeababe1da7bbec03f60ef4215ff6c07f62710a 100644 (file)
@@ -40,7 +40,7 @@
 #include <corosync/corotypes.h>
 
 #include <qb/qbdefs.h>
-#include <corosync/list.h>
+#include <qb/qblist.h>
 #include <corosync/icmap.h>
 
 #define ICMAP_MAX_VALUE_LEN    (16*1024)
@@ -63,17 +63,17 @@ struct icmap_track {
        int32_t track_type;
        icmap_notify_fn_t notify_fn;
        void *user_data;
-       struct list_head list;
+       struct qb_list_head list;
 };
 
 struct icmap_ro_access_item {
        char *key_name;
        int prefix;
-       struct list_head list;
+       struct qb_list_head list;
 };
 
-DECLARE_LIST_INIT(icmap_ro_access_item_list_head);
-DECLARE_LIST_INIT(icmap_track_list_head);
+QB_LIST_DECLARE (icmap_ro_access_item_list_head);
+QB_LIST_DECLARE (icmap_track_list_head);
 
 /*
  * Static functions declarations
@@ -90,21 +90,6 @@ static int icmap_check_key_name(const char *key_name);
  */
 static int icmap_check_value_len(const void *value, size_t value_len, icmap_value_types_t type);
 
-/*
- * Returns length of value of given type, or 0 for string and binary data type
- */
-static size_t icmap_get_valuetype_len(icmap_value_types_t type);
-
-/*
- * Converts track type of icmap to qb
- */
-static int32_t icmap_tt_to_qbtt(int32_t track_type);
-
-/*
- * Convert track type of qb to icmap
- */
-static int32_t icmap_qbtt_to_tt(int32_t track_type);
-
 /*
  * Checks if item has same value as value with value_len and given type. Returns 0 if not, otherwise !0.
  */
@@ -139,7 +124,7 @@ static cs_error_t icmap_get_ref_r(
 /*
  * Function implementation
  */
-static int32_t icmap_tt_to_qbtt(int32_t track_type)
+int32_t icmap_tt_to_qbtt(int32_t track_type)
 {
        int32_t res = 0;
 
@@ -162,7 +147,7 @@ static int32_t icmap_tt_to_qbtt(int32_t track_type)
        return (res);
 }
 
-static int32_t icmap_qbtt_to_tt(int32_t track_type)
+int32_t icmap_qbtt_to_tt(int32_t track_type)
 {
        int32_t res = 0;
 
@@ -210,8 +195,10 @@ cs_error_t icmap_init_r(icmap_map_t *result)
        }
 
         (*result)->qb_map = qb_trie_create();
-       if ((*result)->qb_map == NULL)
+       if ((*result)->qb_map == NULL) {
+               free(*result);
                return (CS_ERR_INIT);
+       }
 
        err = qb_map_notify_add((*result)->qb_map, NULL, icmap_map_free_cb, QB_MAP_NOTIFY_FREE, NULL);
 
@@ -225,27 +212,26 @@ cs_error_t icmap_init(void)
 
 static void icmap_set_ro_access_free(void)
 {
-       struct list_head *iter = icmap_ro_access_item_list_head.next;
+       struct qb_list_head *iter, *tmp_iter;
        struct icmap_ro_access_item *icmap_ro_ai;
 
-       while (iter != &icmap_ro_access_item_list_head) {
-               icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
-               list_del(&icmap_ro_ai->list);
+       qb_list_for_each_safe(iter, tmp_iter, &icmap_ro_access_item_list_head) {
+               icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
+               qb_list_del(&icmap_ro_ai->list);
                free(icmap_ro_ai->key_name);
                free(icmap_ro_ai);
-               iter = icmap_ro_access_item_list_head.next;
        }
 }
 
 static void icmap_del_all_track(void)
 {
-       struct list_head *iter = icmap_track_list_head.next;
+       struct qb_list_head *iter, *tmp_iter;
        struct icmap_track *icmap_track;
 
-       while (iter != &icmap_track_list_head) {
-               icmap_track = list_entry(iter, struct icmap_track, list);
+       qb_list_for_each_safe(iter, tmp_iter, &icmap_track_list_head) {
+               icmap_track = qb_list_entry(iter, struct icmap_track, list);
+
                icmap_track_delete(icmap_track);
-               iter = icmap_track_list_head.next;
        }
 }
 
@@ -317,7 +303,7 @@ static int icmap_check_key_name(const char *key_name)
        return (0);
 }
 
-static size_t icmap_get_valuetype_len(icmap_value_types_t type)
+size_t icmap_get_valuetype_len(icmap_value_types_t type)
 {
        size_t res = 0;
 
@@ -396,6 +382,28 @@ static int icmap_item_eq(const struct icmap_item *item, const void *value, size_
        return (0);
 }
 
+int icmap_key_value_eq(
+       const icmap_map_t map1,
+       const char *key_name1,
+       const icmap_map_t map2,
+       const char *key_name2)
+{
+       struct icmap_item *item1, *item2;
+
+       if (map1 == NULL || key_name1 == NULL || map2 == NULL || key_name2 == NULL) {
+               return (0);
+       }
+
+       item1 = qb_map_get(map1->qb_map, key_name1);
+       item2 = qb_map_get(map2->qb_map, key_name2);
+
+       if (item1 == NULL || item2 == NULL) {
+               return (0);
+       }
+
+       return (icmap_item_eq(item1, item2->value, item2->value_len, item2->type));
+}
+
 cs_error_t icmap_set_r(
        const icmap_map_t map,
        const char *key_name,
@@ -724,6 +732,40 @@ cs_error_t icmap_get(
        return (icmap_get_r(icmap_global_map, key_name, value, value_len, type));
 }
 
+cs_error_t icmap_get_string_r(icmap_map_t map, const char *key_name, char **str)
+{
+       cs_error_t res;
+       size_t str_len;
+       icmap_value_types_t type;
+
+       res = icmap_get_r(map, key_name, NULL, &str_len, &type);
+       if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
+               if (res == CS_OK) {
+                       res = CS_ERR_INVALID_PARAM;
+               }
+
+               goto return_error;
+       }
+
+       *str = malloc(str_len);
+       if (*str == NULL) {
+               res = CS_ERR_NO_MEMORY;
+
+               goto return_error;
+       }
+
+       res = icmap_get_r(map, key_name, *str, &str_len, &type);
+       if (res != CS_OK) {
+               free(*str);
+               goto return_error;
+       }
+
+       return (CS_OK);
+
+return_error:
+       return (res);
+}
+
 static cs_error_t icmap_get_int_r(
        const icmap_map_t map,
        const char *key_name,
@@ -738,7 +780,7 @@ static cs_error_t icmap_get_int_r(
        key_size = sizeof(key_value);
        memset(key_value, 0, key_size);
 
-       err = icmap_get(key_name, key_value, &key_size, &key_type);
+       err = icmap_get_r(map, key_name, key_value, &key_size, &key_type);
        if (err != CS_OK)
                return (err);
 
@@ -811,6 +853,12 @@ cs_error_t icmap_get_double_r(const icmap_map_t map, const char *key_name, doubl
        return (icmap_get_int_r(map, key_name, dbl, ICMAP_VALUETYPE_DOUBLE));
 }
 
+cs_error_t icmap_get_string(const char *key_name, char **str)
+{
+
+       return (icmap_get_string_r(icmap_global_map, key_name, str));
+}
+
 cs_error_t icmap_get_int8(const char *key_name, int8_t *i8)
 {
 
@@ -871,40 +919,6 @@ cs_error_t icmap_get_double(const char *key_name, double *dbl)
        return (icmap_get_double_r(icmap_global_map, key_name, dbl));
 }
 
-cs_error_t icmap_get_string(const char *key_name, char **str)
-{
-       cs_error_t res;
-       size_t str_len;
-       icmap_value_types_t type;
-
-       res = icmap_get(key_name, NULL, &str_len, &type);
-       if (res != CS_OK || type != ICMAP_VALUETYPE_STRING) {
-               if (res == CS_OK) {
-                       res = CS_ERR_INVALID_PARAM;
-               }
-
-               goto return_error;
-       }
-
-       *str = malloc(str_len);
-       if (*str == NULL) {
-               res = CS_ERR_NO_MEMORY;
-
-               goto return_error;
-       }
-
-       res = icmap_get(key_name, *str, &str_len, &type);
-       if (res != CS_OK) {
-               free(*str);
-               goto return_error;
-       }
-
-       return (CS_OK);
-
-return_error:
-       return (res);
-}
-
 cs_error_t icmap_adjust_int_r(
        const icmap_map_t map,
        const char *key_name,
@@ -1181,8 +1195,8 @@ cs_error_t icmap_track_add(
                return (qb_to_cs_error(err));
        }
 
-       list_init(&(*icmap_track)->list);
-       list_add (&(*icmap_track)->list, &icmap_track_list_head);
+       qb_list_init(&(*icmap_track)->list);
+       qb_list_add (&(*icmap_track)->list, &icmap_track_list_head);
 
        return (CS_OK);
 }
@@ -1196,7 +1210,7 @@ cs_error_t icmap_track_delete(icmap_track_t icmap_track)
                return (qb_to_cs_error(err));
        }
 
-       list_del(&icmap_track->list);
+       qb_list_del(&icmap_track->list);
        free(icmap_track->key_name);
        free(icmap_track);
 
@@ -1210,11 +1224,11 @@ void *icmap_track_get_user_data(icmap_track_t icmap_track)
 
 cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
 {
-       struct list_head *iter;
+       struct qb_list_head *iter, *tmp_iter;
        struct icmap_ro_access_item *icmap_ro_ai;
 
-       for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
-               icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
+       qb_list_for_each_safe(iter, tmp_iter, &icmap_ro_access_item_list_head) {
+               icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
 
                if (icmap_ro_ai->prefix == prefix && strcmp(key_name, icmap_ro_ai->key_name) == 0) {
                        /*
@@ -1223,7 +1237,7 @@ cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
                        if (ro_access) {
                                return (CS_ERR_EXIST);
                        } else {
-                               list_del(&icmap_ro_ai->list);
+                               qb_list_del(&icmap_ro_ai->list);
                                free(icmap_ro_ai->key_name);
                                free(icmap_ro_ai);
 
@@ -1249,19 +1263,19 @@ cs_error_t icmap_set_ro_access(const char *key_name, int prefix, int ro_access)
        }
 
        icmap_ro_ai->prefix = prefix;
-       list_init(&icmap_ro_ai->list);
-       list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
+       qb_list_init(&icmap_ro_ai->list);
+       qb_list_add (&icmap_ro_ai->list, &icmap_ro_access_item_list_head);
 
        return (CS_OK);
 }
 
 int icmap_is_key_ro(const char *key_name)
 {
-       struct list_head *iter;
+       struct qb_list_head *iter;
        struct icmap_ro_access_item *icmap_ro_ai;
 
-       for (iter = icmap_ro_access_item_list_head.next; iter != &icmap_ro_access_item_list_head; iter = iter->next) {
-               icmap_ro_ai = list_entry(iter, struct icmap_ro_access_item, list);
+       qb_list_for_each(iter, &icmap_ro_access_item_list_head) {
+               icmap_ro_ai = qb_list_entry(iter, struct icmap_ro_access_item, list);
 
                if (icmap_ro_ai->prefix) {
                        if (strlen(icmap_ro_ai->key_name) > strlen(key_name))
@@ -1280,3 +1294,37 @@ int icmap_is_key_ro(const char *key_name)
        return (CS_FALSE);
 
 }
+
+cs_error_t icmap_copy_map(icmap_map_t dst_map, const icmap_map_t src_map)
+{
+       icmap_iter_t iter;
+       size_t value_len;
+       icmap_value_types_t value_type;
+       const char *key_name;
+       cs_error_t err;
+       void *value;
+
+       iter = icmap_iter_init_r(src_map, NULL);
+       if (iter == NULL) {
+               return (CS_ERR_NO_MEMORY);
+       }
+
+       err = CS_OK;
+
+       while ((key_name = icmap_iter_next(iter, &value_len, &value_type)) != NULL) {
+               err = icmap_get_ref_r(src_map, key_name, &value, &value_len, &value_type);
+               if (err != CS_OK) {
+                       goto exit_iter_finalize;
+               }
+
+               err = icmap_set_r(dst_map, key_name, value, value_len, value_type);
+               if (err != CS_OK) {
+                       goto exit_iter_finalize;
+               }
+       }
+
+exit_iter_finalize:
+       icmap_iter_finalize(iter);
+
+       return (err);
+}