|
| 1 | +/* |
| 2 | + * |
| 3 | + * Copyright (C) 2025 Intel Corporation |
| 4 | + * |
| 5 | + * Under the Apache License v2.0 with LLVM Exceptions. See LICENSE.TXT. |
| 6 | + * SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | + */ |
| 8 | + |
| 9 | +#include "ctl_defaults.h" |
| 10 | + |
| 11 | +#include <string.h> |
| 12 | + |
| 13 | +#include "base_alloc_global.h" |
| 14 | +#include "utils_concurrency.h" |
| 15 | +#include "utils_log.h" |
| 16 | +#include "utlist.h" |
| 17 | + |
| 18 | +static umf_result_t default_ctl_helper(ctl_ext_ctl_fn fn, void *ctl, |
| 19 | + const char *name, void *arg, size_t size, |
| 20 | + ...) { |
| 21 | + va_list empty_args; |
| 22 | + va_start(empty_args, size); |
| 23 | + umf_result_t ret = fn(ctl, CTL_QUERY_PROGRAMMATIC, name, arg, size, |
| 24 | + CTL_QUERY_WRITE, empty_args); |
| 25 | + va_end(empty_args); |
| 26 | + return ret; |
| 27 | +} |
| 28 | + |
| 29 | +umf_result_t ctl_default_subtree(ctl_default_entry_t **list, utils_mutex_t *mtx, |
| 30 | + umf_ctl_query_source_t source, void *arg, |
| 31 | + size_t size, const char *extra_name, |
| 32 | + umf_ctl_query_type_t queryType) { |
| 33 | + (void)source; |
| 34 | + if (strstr(extra_name, "{}") != NULL) { |
| 35 | + LOG_ERR("%s, default setting do not support wildcard parameters {}", |
| 36 | + extra_name); |
| 37 | + return UMF_RESULT_ERROR_NOT_SUPPORTED; |
| 38 | + } |
| 39 | + |
| 40 | + utils_mutex_lock(mtx); |
| 41 | + |
| 42 | + ctl_default_entry_t *entry = NULL; |
| 43 | + LL_FOREACH(*list, entry) { |
| 44 | + if (strcmp(entry->name, extra_name) == 0) { |
| 45 | + break; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + if (queryType == CTL_QUERY_WRITE) { |
| 50 | + bool is_new_entry = false; |
| 51 | + if (!entry) { |
| 52 | + entry = umf_ba_global_alloc(sizeof(*entry)); |
| 53 | + if (!entry) { |
| 54 | + utils_mutex_unlock(mtx); |
| 55 | + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 56 | + } |
| 57 | + entry->name = NULL; |
| 58 | + entry->value = NULL; |
| 59 | + entry->next = NULL; |
| 60 | + is_new_entry = true; |
| 61 | + } |
| 62 | + |
| 63 | + char *new_name = umf_ba_global_strdup(extra_name); |
| 64 | + if (!new_name) { |
| 65 | + utils_mutex_unlock(mtx); |
| 66 | + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 67 | + } |
| 68 | + |
| 69 | + if (entry->name) { |
| 70 | + umf_ba_global_free(entry->name); |
| 71 | + } |
| 72 | + entry->name = new_name; |
| 73 | + |
| 74 | + void *new_value = NULL; |
| 75 | + if (size > 0) { |
| 76 | + new_value = umf_ba_global_alloc(size); |
| 77 | + if (!new_value) { |
| 78 | + utils_mutex_unlock(mtx); |
| 79 | + return UMF_RESULT_ERROR_OUT_OF_HOST_MEMORY; |
| 80 | + } |
| 81 | + memcpy(new_value, arg, size); |
| 82 | + } |
| 83 | + if (entry->value) { |
| 84 | + umf_ba_global_free(entry->value); |
| 85 | + } |
| 86 | + entry->value = new_value; |
| 87 | + entry->value_size = size; |
| 88 | + entry->source = source; |
| 89 | + |
| 90 | + if (is_new_entry) { |
| 91 | + LL_APPEND(*list, entry); |
| 92 | + } |
| 93 | + } else if (queryType == CTL_QUERY_READ) { |
| 94 | + if (!entry) { |
| 95 | + LOG_WARN("Wrong path name: %s", extra_name); |
| 96 | + utils_mutex_unlock(mtx); |
| 97 | + return UMF_RESULT_ERROR_INVALID_ARGUMENT; |
| 98 | + } |
| 99 | + |
| 100 | + if (entry->value_size > size) { |
| 101 | + LOG_ERR("Provided buffer size %zu is smaller than field size %zu", |
| 102 | + size, entry->value_size); |
| 103 | + utils_mutex_unlock(mtx); |
| 104 | + return UMF_RESULT_ERROR_INVALID_ARGUMENT; |
| 105 | + } |
| 106 | + memcpy(arg, entry->value, entry->value_size); |
| 107 | + } |
| 108 | + |
| 109 | + utils_mutex_unlock(mtx); |
| 110 | + return UMF_RESULT_SUCCESS; |
| 111 | +} |
| 112 | + |
| 113 | +void ctl_default_apply(ctl_default_entry_t *list, const char *pname, |
| 114 | + ctl_ext_ctl_fn ext_ctl, void *priv) { |
| 115 | + if (!pname || !ext_ctl) { |
| 116 | + return; |
| 117 | + } |
| 118 | + |
| 119 | + size_t pname_len = strlen(pname); |
| 120 | + ctl_default_entry_t *it = NULL; |
| 121 | + LL_FOREACH(list, it) { |
| 122 | + if (strlen(it->name) > pname_len + 1 && |
| 123 | + strncmp(it->name, pname, pname_len) == 0 && |
| 124 | + it->name[pname_len] == '.') { |
| 125 | + const char *ctl_name = it->name + pname_len + 1; |
| 126 | + default_ctl_helper(ext_ctl, priv, ctl_name, it->value, |
| 127 | + it->value_size); |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +void ctl_default_destroy(ctl_default_entry_t **list, utils_mutex_t *mtx) { |
| 133 | + utils_mutex_lock(mtx); |
| 134 | + ctl_default_entry_t *entry = NULL, *tmp = NULL; |
| 135 | + LL_FOREACH_SAFE(*list, entry, tmp) { |
| 136 | + LL_DELETE(*list, entry); |
| 137 | + if (entry->name) { |
| 138 | + umf_ba_global_free(entry->name); |
| 139 | + } |
| 140 | + if (entry->value) { |
| 141 | + umf_ba_global_free(entry->value); |
| 142 | + } |
| 143 | + umf_ba_global_free(entry); |
| 144 | + } |
| 145 | + utils_mutex_unlock(mtx); |
| 146 | +} |
0 commit comments