• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 // Resource pool list, which caches registered executor information.
27 static LinkedList *g_poolList = NULL;
28 
DestroyExecutorInfo(void * data)29 static void DestroyExecutorInfo(void *data)
30 {
31     if (data  == NULL) {
32         LOG_ERROR("data is null");
33         return;
34     }
35     Free(data);
36 }
37 
IsExecutorIdMatchById(void * data,void * condition)38 static bool IsExecutorIdMatchById(void *data, void *condition)
39 {
40     if ((condition == NULL) || (data == NULL)) {
41         LOG_ERROR("input para is null");
42         return false;
43     }
44     uint64_t executorId = *(uint64_t *)condition;
45     ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)data;
46     return (executorInfo->executorId == executorId);
47 }
48 
IsExecutorIdMatchByType(void * data,void * condition)49 static bool IsExecutorIdMatchByType(void *data, void *condition)
50 {
51     if ((condition == NULL) || (data == NULL)) {
52         LOG_ERROR("get null data");
53         return false;
54     }
55     ExecutorInfoHal *executorIndex = (ExecutorInfoHal *)condition;
56     ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)data;
57     return (executorInfo->executorType == executorIndex->executorType &&
58         executorInfo->authType == executorIndex->authType);
59 }
60 
IsInit()61 static bool IsInit()
62 {
63     return g_poolList != NULL;
64 }
65 
InitResourcePool(void)66 ResultCode InitResourcePool(void)
67 {
68     if (!IsInit()) {
69         g_poolList = CreateLinkedList(DestroyExecutorInfo);
70     }
71     if (g_poolList == NULL) {
72         return RESULT_GENERAL_ERROR;
73     }
74     return RESULT_SUCCESS;
75 }
76 
DestroyResourcePool(void)77 void DestroyResourcePool(void)
78 {
79     DestroyLinkedList(g_poolList);
80     g_poolList = NULL;
81 }
82 
IsExecutorValid(ExecutorInfoHal * executorInfo)83 static bool IsExecutorValid(ExecutorInfoHal *executorInfo)
84 {
85     if (executorInfo == NULL) {
86         LOG_ERROR("get null data");
87         return false;
88     }
89     return true;
90 }
91 
IsExecutorIdDuplicate(uint64_t executorId)92 static bool IsExecutorIdDuplicate(uint64_t executorId)
93 {
94     LinkedListNode *temp = g_poolList->head;
95     ExecutorInfoHal *executorInfo = NULL;
96     while (temp != NULL) {
97         executorInfo = (ExecutorInfoHal *)temp->data;
98         if (executorInfo != NULL && executorInfo->executorId == executorId) {
99             return true;
100         }
101         temp = temp->next;
102     }
103 
104     return false;
105 }
106 
GenerateValidExecutorId(uint64_t * executorId)107 static ResultCode GenerateValidExecutorId(uint64_t *executorId)
108 {
109     if (g_poolList == NULL) {
110         LOG_ERROR("g_poolList is null");
111         return RESULT_BAD_PARAM;
112     }
113 
114     for (uint32_t i = 0; i < MAX_DUPLICATE_CHECK; i++) {
115         uint64_t tempRandom;
116         if (SecureRandom((uint8_t *)&tempRandom, sizeof(uint64_t)) != RESULT_SUCCESS) {
117             LOG_ERROR("get random failed");
118             return RESULT_GENERAL_ERROR;
119         }
120         if (!IsExecutorIdDuplicate(tempRandom)) {
121             *executorId = tempRandom;
122             return RESULT_SUCCESS;
123         }
124     }
125 
126     LOG_ERROR("a rare failure");
127     return RESULT_GENERAL_ERROR;
128 }
129 
RegisterExecutorToPool(ExecutorInfoHal * executorInfo)130 ResultCode RegisterExecutorToPool(ExecutorInfoHal *executorInfo)
131 {
132     if (!IsInit()) {
133         LOG_ERROR("pool not init");
134         return RESULT_NEED_INIT;
135     }
136     if (!IsExecutorValid(executorInfo)) {
137         LOG_ERROR("get invalid executorInfo");
138         return RESULT_BAD_PARAM;
139     }
140     if (g_poolList->remove(g_poolList, (void *)executorInfo, IsExecutorIdMatchByType, true) != RESULT_SUCCESS) {
141         LOG_INFO("current executor isn't registered");
142     }
143     ResultCode result = GenerateValidExecutorId(&executorInfo->executorId);
144     if (result != RESULT_SUCCESS) {
145         LOG_ERROR("get executorId failed");
146         return result;
147     }
148     ExecutorInfoHal *executorCopy = CopyExecutorInfo(executorInfo);
149     if (executorCopy == NULL) {
150         LOG_ERROR("copy executor failed");
151         return RESULT_NO_MEMORY;
152     }
153     result = g_poolList->insert(g_poolList, (void *)executorCopy);
154     if (result != RESULT_SUCCESS) {
155         LOG_ERROR("insert failed");
156         DestroyExecutorInfo(executorCopy);
157         return result;
158     }
159     return result;
160 }
161 
UnregisterExecutorToPool(uint64_t executorId)162 ResultCode UnregisterExecutorToPool(uint64_t executorId)
163 {
164     if (!IsInit()) {
165         LOG_ERROR("pool not init");
166         return RESULT_NEED_INIT;
167     }
168     return g_poolList->remove(g_poolList, (void *)&executorId, IsExecutorIdMatchById, true);
169 }
170 
CopyExecutorInfo(ExecutorInfoHal * src)171 ExecutorInfoHal *CopyExecutorInfo(ExecutorInfoHal *src)
172 {
173     if (src == NULL) {
174         LOG_ERROR("get null data");
175         return NULL;
176     }
177     ExecutorInfoHal *dest = (ExecutorInfoHal *)Malloc(sizeof(ExecutorInfoHal));
178     if (dest == NULL) {
179         LOG_ERROR("no memory");
180         return NULL;
181     }
182     if (memcpy_s(dest, sizeof(ExecutorInfoHal), src, sizeof(ExecutorInfoHal)) != EOK) {
183         LOG_ERROR("copy executor info failed");
184         Free(dest);
185         return NULL;
186     }
187     return dest;
188 }
189 
QueryExecutor(uint32_t authType,LinkedList ** result)190 ResultCode QueryExecutor(uint32_t authType, LinkedList **result)
191 {
192     if (!IsInit()) {
193         LOG_ERROR("pool not init");
194         return RESULT_NEED_INIT;
195     }
196     if (result == NULL) {
197         LOG_ERROR("get null data");
198         return RESULT_BAD_PARAM;
199     }
200     *result = CreateLinkedList(DestroyExecutorInfo);
201     if (*result == NULL) {
202         LOG_ERROR("create result list failed");
203         return RESULT_NO_MEMORY;
204     }
205     LinkedListIterator *iterator = g_poolList->createIterator(g_poolList);
206     if (iterator == NULL) {
207         LOG_ERROR("create iterator failed");
208         DestroyLinkedList(*result);
209         return RESULT_NO_MEMORY;
210     }
211 
212     while (iterator->hasNext(iterator)) {
213         ExecutorInfoHal *executorInfo = (ExecutorInfoHal *)iterator->next(iterator);
214         if (!IsExecutorValid(executorInfo)) {
215             LOG_ERROR("get invalid executor info");
216             continue;
217         }
218         if (executorInfo->authType != authType) {
219             continue;
220         }
221         ExecutorInfoHal *copy = CopyExecutorInfo(executorInfo);
222         if (copy == NULL) {
223             LOG_ERROR("copy executor info failed");
224             continue;
225         }
226         if ((*result)->insert(*result, copy) != RESULT_SUCCESS) {
227             LOG_ERROR("insert executor info failed");
228             DestroyExecutorInfo(copy);
229             continue;
230         }
231     }
232     g_poolList->destroyIterator(iterator);
233     return RESULT_SUCCESS;
234 }