1 /*
2 * Copyright (C) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "pool.h"
17
18 #include "securec.h"
19
20 #include "adaptor_algorithm.h"
21 #include "adaptor_log.h"
22 #include "adaptor_memory.h"
23
24 #define MAX_DUPLICATE_CHECK 100
25 #ifdef IAM_TEST_ENABLE
26 #define IAM_STATIC
27 #else
28 #define IAM_STATIC static
29 #endif
30
31 // Resource pool list, which caches registered executor information.
32 IAM_STATIC LinkedList *g_poolList = NULL;
33
DestroyExecutorInfo(void * data)34 IAM_STATIC void DestroyExecutorInfo(void *data)
35 {
36 if (data == NULL) {
37 LOG_ERROR("data is null");
38 return;
39 }
40 Free(data);
41 }
42
IsExecutorIdMatchById(void * data,void * condition)43 IAM_STATIC bool IsExecutorIdMatchById(void *data, void *condition)
44 {
45 if ((condition == NULL) || (data == NULL)) {
46 LOG_ERROR("input para is null");
47 return false;
48 }
49 uint64_t executorIndex = *(uint64_t *)condition;
50 ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)data;
51 return (executorInfo->executorIndex == executorIndex);
52 }
53
IsExecutorNodeMatch(void * data,void * condition)54 IAM_STATIC bool IsExecutorNodeMatch(void *data, void *condition)
55 {
56 if ((condition == NULL) || (data == NULL)) {
57 LOG_ERROR("get null data");
58 return false;
59 }
60 ExecutorInfoHal *executorIndex = (ExecutorInfoHal *)condition;
61 ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)data;
62 return (executorInfo->executorRole == executorIndex->executorRole &&
63 executorInfo->authType == executorIndex->authType &&
64 executorInfo->executorSensorHint == executorIndex->executorSensorHint);
65 }
66
IsInit(void)67 IAM_STATIC bool IsInit(void)
68 {
69 return g_poolList != NULL;
70 }
71
InitResourcePool(void)72 ResultCode InitResourcePool(void)
73 {
74 if (!IsInit()) {
75 g_poolList = CreateLinkedList(DestroyExecutorInfo);
76 }
77 if (g_poolList == NULL) {
78 return RESULT_GENERAL_ERROR;
79 }
80 return RESULT_SUCCESS;
81 }
82
DestroyResourcePool(void)83 void DestroyResourcePool(void)
84 {
85 DestroyLinkedList(g_poolList);
86 g_poolList = NULL;
87 }
88
IsExecutorValid(const ExecutorInfoHal * executorInfo)89 IAM_STATIC bool IsExecutorValid(const ExecutorInfoHal *executorInfo)
90 {
91 if (executorInfo == NULL) {
92 LOG_ERROR("get null data");
93 return false;
94 }
95 return true;
96 }
97
IsExecutorIdDuplicate(uint64_t executorIndex)98 IAM_STATIC bool IsExecutorIdDuplicate(uint64_t executorIndex)
99 {
100 LinkedListNode *temp = g_poolList->head;
101 ExecutorInfoHal *executorInfo = NULL;
102 while (temp != NULL) {
103 executorInfo = (ExecutorInfoHal *)temp->data;
104 if (executorInfo != NULL && executorInfo->executorIndex == executorIndex) {
105 return true;
106 }
107 temp = temp->next;
108 }
109
110 return false;
111 }
112
GenerateValidExecutorId(uint64_t * executorIndex)113 IAM_STATIC ResultCode GenerateValidExecutorId(uint64_t *executorIndex)
114 {
115 if (g_poolList == NULL) {
116 LOG_ERROR("g_poolList is null");
117 return RESULT_BAD_PARAM;
118 }
119
120 for (uint32_t i = 0; i < MAX_DUPLICATE_CHECK; ++i) {
121 uint64_t tempRandom;
122 if (SecureRandom((uint8_t *)&tempRandom, sizeof(uint64_t)) != RESULT_SUCCESS) {
123 LOG_ERROR("get random failed");
124 return RESULT_GENERAL_ERROR;
125 }
126 if (!IsExecutorIdDuplicate(tempRandom)) {
127 *executorIndex = tempRandom;
128 return RESULT_SUCCESS;
129 }
130 }
131
132 LOG_ERROR("a rare failure");
133 return RESULT_GENERAL_ERROR;
134 }
135
QueryRepeatExecutor(const ExecutorInfoHal * executorInfo)136 IAM_STATIC LinkedList *QueryRepeatExecutor(const ExecutorInfoHal *executorInfo)
137 {
138 ExecutorCondition condition = {};
139 SetExecutorConditionAuthType(&condition, executorInfo->authType);
140 SetExecutorConditionSensorHint(&condition, executorInfo->executorSensorHint);
141 SetExecutorConditionExecutorRole(&condition, executorInfo->executorRole);
142 return QueryExecutor(&condition);
143 }
144
RegisterExecutorToPool(ExecutorInfoHal * executorInfo)145 ResultCode RegisterExecutorToPool(ExecutorInfoHal *executorInfo)
146 {
147 if (!IsInit()) {
148 LOG_ERROR("pool not init");
149 return RESULT_NEED_INIT;
150 }
151 if (!IsExecutorValid(executorInfo)) {
152 LOG_ERROR("get invalid executorInfo");
153 return RESULT_BAD_PARAM;
154 }
155 LinkedList *executors = QueryRepeatExecutor(executorInfo);
156 if (executors == NULL) {
157 LOG_ERROR("query executor failed");
158 return RESULT_NO_MEMORY;
159 }
160 ResultCode result = RESULT_UNKNOWN;
161 if (executors->getSize(executors) != 0) {
162 if (executors->head == NULL || executors->head->data == NULL) {
163 LOG_ERROR("list node is invalid");
164 goto EXIT;
165 }
166 executorInfo->executorIndex = ((ExecutorInfoHal *)(executors->head->data))->executorIndex;
167 if (g_poolList->remove(g_poolList, (void *)executorInfo, IsExecutorNodeMatch, true) != RESULT_SUCCESS) {
168 LOG_INFO("remove executor failed");
169 goto EXIT;
170 }
171 } else {
172 result = GenerateValidExecutorId(&executorInfo->executorIndex);
173 if (result != RESULT_SUCCESS) {
174 LOG_ERROR("get executorId failed");
175 goto EXIT;
176 }
177 }
178 ExecutorInfoHal *executorCopy = CopyExecutorInfo(executorInfo);
179 if (executorCopy == NULL) {
180 LOG_ERROR("copy executor failed");
181 result = RESULT_BAD_COPY;
182 goto EXIT;
183 }
184 result = g_poolList->insert(g_poolList, (void *)executorCopy);
185 if (result != RESULT_SUCCESS) {
186 LOG_ERROR("insert failed");
187 DestroyExecutorInfo(executorCopy);
188 }
189
190 EXIT:
191 DestroyLinkedList(executors);
192 return result;
193 }
194
UnregisterExecutorToPool(uint64_t executorIndex)195 ResultCode UnregisterExecutorToPool(uint64_t executorIndex)
196 {
197 if (!IsInit()) {
198 LOG_ERROR("pool not init");
199 return RESULT_NEED_INIT;
200 }
201 return g_poolList->remove(g_poolList, (void *)&executorIndex, IsExecutorIdMatchById, true);
202 }
203
CopyExecutorInfo(ExecutorInfoHal * src)204 ExecutorInfoHal *CopyExecutorInfo(ExecutorInfoHal *src)
205 {
206 if (src == NULL) {
207 LOG_ERROR("get null data");
208 return NULL;
209 }
210 ExecutorInfoHal *dest = (ExecutorInfoHal *)Malloc(sizeof(ExecutorInfoHal));
211 if (dest == NULL) {
212 LOG_ERROR("no memory");
213 return NULL;
214 }
215 if (memcpy_s(dest, sizeof(ExecutorInfoHal), src, sizeof(ExecutorInfoHal)) != EOK) {
216 LOG_ERROR("copy executor info failed");
217 Free(dest);
218 return NULL;
219 }
220 return dest;
221 }
222
IsExecutorMatch(const ExecutorCondition * condition,const ExecutorInfoHal * credentialInfo)223 IAM_STATIC bool IsExecutorMatch(const ExecutorCondition *condition, const ExecutorInfoHal *credentialInfo)
224 {
225 if ((condition->conditonFactor & EXECUTOR_CONDITION_INDEX) != 0 &&
226 condition->executorIndex != credentialInfo->executorIndex) {
227 return false;
228 }
229 if ((condition->conditonFactor & EXECUTOR_CONDITION_AUTH_TYPE) != 0 &&
230 condition->authType != credentialInfo->authType) {
231 return false;
232 }
233 if ((condition->conditonFactor & EXECUTOR_CONDITION_SENSOR_HINT) != 0 &&
234 condition->executorSensorHint != INVALID_SENSOR_HINT &&
235 condition->executorSensorHint != credentialInfo->executorSensorHint) {
236 return false;
237 }
238 if ((condition->conditonFactor & EXECUTOR_CONDITION_ROLE) != 0 &&
239 condition->executorRole != credentialInfo->executorRole) {
240 return false;
241 }
242 if ((condition->conditonFactor & EXECUTOR_CONDITION_MATCHER) != 0 &&
243 condition->executorMatcher != credentialInfo->executorMatcher) {
244 return false;
245 }
246 return true;
247 }
248
QueryExecutor(const ExecutorCondition * condition)249 LinkedList *QueryExecutor(const ExecutorCondition *condition)
250 {
251 if (!IsInit()) {
252 LOG_ERROR("pool not init");
253 return NULL;
254 }
255 LinkedList *result = CreateLinkedList(DestroyExecutorInfo);
256 if (result == NULL) {
257 LOG_ERROR("create result list failed");
258 return NULL;
259 }
260 LinkedListIterator *iterator = g_poolList->createIterator(g_poolList);
261 if (iterator == NULL) {
262 LOG_ERROR("create iterator failed");
263 DestroyLinkedList(result);
264 return NULL;
265 }
266
267 while (iterator->hasNext(iterator)) {
268 ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)iterator->next(iterator);
269 if (!IsExecutorValid(executorInfo)) {
270 LOG_ERROR("get invalid executor info");
271 continue;
272 }
273 if (!IsExecutorMatch(condition, executorInfo)) {
274 continue;
275 }
276 ExecutorInfoHal *copy = CopyExecutorInfo(executorInfo);
277 if (copy == NULL) {
278 LOG_ERROR("copy executor info failed");
279 continue;
280 }
281 if (result->insert(result, copy) != RESULT_SUCCESS) {
282 LOG_ERROR("insert executor info failed");
283 DestroyExecutorInfo(copy);
284 continue;
285 }
286 }
287 g_poolList->destroyIterator(iterator);
288 return result;
289 }
290
QueryCollecterMatcher(uint32_t authType,uint32_t executorSensorHint,uint32_t * matcher)291 ResultCode QueryCollecterMatcher(uint32_t authType, uint32_t executorSensorHint, uint32_t *matcher)
292 {
293 if (!IsInit()) {
294 LOG_ERROR("pool not init");
295 return RESULT_NEED_INIT;
296 }
297 if (matcher == NULL) {
298 LOG_ERROR("matcher is null");
299 return RESULT_BAD_PARAM;
300 }
301 LinkedListIterator *iterator = g_poolList->createIterator(g_poolList);
302 if (iterator == NULL) {
303 LOG_ERROR("create iterator failed");
304 return RESULT_NO_MEMORY;
305 }
306
307 while (iterator->hasNext(iterator)) {
308 ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)(iterator->next(iterator));
309 if (!IsExecutorValid(executorInfo)) {
310 LOG_ERROR("get invalid executor info");
311 continue;
312 }
313 if (executorInfo->authType == authType && executorInfo->executorSensorHint == executorSensorHint &&
314 (executorInfo->executorRole == COLLECTOR || executorInfo->executorRole == ALL_IN_ONE)) {
315 *matcher = executorInfo->executorMatcher;
316 g_poolList->destroyIterator(iterator);
317 return RESULT_SUCCESS;
318 }
319 }
320 LOG_ERROR("can't found executor, sensor hint is %{public}u", executorSensorHint);
321 g_poolList->destroyIterator(iterator);
322 return RESULT_NOT_FOUND;
323 }
324
325
QueryCredentialExecutorIndex(uint32_t authType,uint32_t executorSensorHint)326 uint64_t QueryCredentialExecutorIndex(uint32_t authType, uint32_t executorSensorHint)
327 {
328 if (!IsInit()) {
329 LOG_ERROR("pool not init");
330 return INVALID_EXECUTOR_INDEX;
331 }
332 LinkedListIterator *iterator = g_poolList->createIterator(g_poolList);
333 if (iterator == NULL) {
334 LOG_ERROR("create iterator failed");
335 return INVALID_EXECUTOR_INDEX;
336 }
337
338 while (iterator->hasNext(iterator)) {
339 ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)(iterator->next(iterator));
340 if (!IsExecutorValid(executorInfo)) {
341 LOG_ERROR("get invalid executor info");
342 continue;
343 }
344 if (executorInfo->authType == authType && executorInfo->executorSensorHint == executorSensorHint &&
345 (executorInfo->executorRole == VERIFIER || executorInfo->executorRole == ALL_IN_ONE)) {
346 g_poolList->destroyIterator(iterator);
347 return executorInfo->executorIndex;
348 }
349 }
350 LOG_ERROR("can't found executor, sensor hint is %{public}u", executorSensorHint);
351 g_poolList->destroyIterator(iterator);
352 return INVALID_EXECUTOR_INDEX;
353 }
354
355
SetExecutorConditionExecutorIndex(ExecutorCondition * condition,uint64_t executorIndex)356 void SetExecutorConditionExecutorIndex(ExecutorCondition *condition, uint64_t executorIndex)
357 {
358 if (condition == NULL) {
359 LOG_ERROR("condition is null");
360 return;
361 }
362 condition->executorIndex = executorIndex;
363 condition->conditonFactor |= EXECUTOR_CONDITION_INDEX;
364 }
365
SetExecutorConditionAuthType(ExecutorCondition * condition,uint32_t authType)366 void SetExecutorConditionAuthType(ExecutorCondition *condition, uint32_t authType)
367 {
368 if (condition == NULL) {
369 LOG_ERROR("condition is null");
370 return;
371 }
372 condition->authType = authType;
373 condition->conditonFactor |= EXECUTOR_CONDITION_AUTH_TYPE;
374 }
375
SetExecutorConditionSensorHint(ExecutorCondition * condition,uint32_t executorSensorHint)376 void SetExecutorConditionSensorHint(ExecutorCondition *condition, uint32_t executorSensorHint)
377 {
378 if (condition == NULL) {
379 LOG_ERROR("condition is null");
380 return;
381 }
382 condition->executorSensorHint = executorSensorHint;
383 condition->conditonFactor |= EXECUTOR_CONDITION_SENSOR_HINT;
384 }
385
SetExecutorConditionExecutorRole(ExecutorCondition * condition,uint32_t executorRole)386 void SetExecutorConditionExecutorRole(ExecutorCondition *condition, uint32_t executorRole)
387 {
388 if (condition == NULL) {
389 LOG_ERROR("condition is null");
390 return;
391 }
392 condition->executorRole = executorRole;
393 condition->conditonFactor |= EXECUTOR_CONDITION_ROLE;
394 }
395
SetExecutorConditionExecutorMatcher(ExecutorCondition * condition,uint32_t executorMatcher)396 void SetExecutorConditionExecutorMatcher(ExecutorCondition *condition, uint32_t executorMatcher)
397 {
398 if (condition == NULL) {
399 LOG_ERROR("condition is null");
400 return;
401 }
402 condition->executorMatcher = executorMatcher;
403 condition->conditonFactor |= EXECUTOR_CONDITION_MATCHER;
404 }