1 /*
2 * Copyright (c) 2021 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 "trans_session_manager.h"
17
18 #include "securec.h"
19 #include "softbus_adapter_mem.h"
20 #include "softbus_adapter_thread.h"
21 #include "softbus_def.h"
22 #include "softbus_errcode.h"
23 #include "softbus_log.h"
24 #include "softbus_utils.h"
25 #include "trans_channel_callback.h"
26 #include "softbus_hidumper_trans.h"
27
28 #define MAX_SESSION_SERVER_NUM 32
29
30 static SoftBusList *g_sessionServerList = NULL;
31
TransSessionForEachShowInfo(int fd)32 static void TransSessionForEachShowInfo(int fd)
33 {
34 if (g_sessionServerList == NULL) {
35 return;
36 }
37
38 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
39 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
40 return;
41 }
42
43 SessionServer *pos = NULL;
44 LIST_FOR_EACH_ENTRY(pos, &g_sessionServerList->list, SessionServer, node) {
45 SoftBusTransDumpRegisterSession(fd, pos->pkgName, pos->sessionName, pos->uid, pos->pid);
46 }
47
48 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
49 }
50
TransSessionMgrInit(void)51 int32_t TransSessionMgrInit(void)
52 {
53 if (g_sessionServerList != NULL) {
54 return SOFTBUS_OK;
55 }
56 g_sessionServerList = CreateSoftBusList();
57 if (g_sessionServerList == NULL) {
58 return SOFTBUS_ERR;
59 }
60
61 SetShowRegisterSessionInfosFunc(TransSessionForEachShowInfo);
62 return SOFTBUS_OK;
63 }
64
TransSessionMgrDeinit(void)65 void TransSessionMgrDeinit(void)
66 {
67 if (g_sessionServerList != NULL) {
68 DestroySoftBusList(g_sessionServerList);
69 g_sessionServerList = NULL;
70 }
71 }
72
TransSessionServerIsExist(const char * sessionName)73 bool TransSessionServerIsExist(const char *sessionName)
74 {
75 if (sessionName == NULL) {
76 return false;
77 }
78 if (g_sessionServerList == NULL) {
79 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not init");
80 return false;
81 }
82
83 SessionServer *pos = NULL;
84 SessionServer *tmp = NULL;
85 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
86 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
87 return false;
88 }
89 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
90 if (strcmp(pos->sessionName, sessionName) == 0) {
91 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session server [%s] is exist", sessionName);
92 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
93 return true;
94 }
95 }
96
97 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
98 return false;
99 }
100
TransSessionServerAddItem(SessionServer * newNode)101 int32_t TransSessionServerAddItem(SessionServer *newNode)
102 {
103 if (newNode == NULL) {
104 return SOFTBUS_INVALID_PARAM;
105 }
106 if (g_sessionServerList == NULL) {
107 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "not init");
108 return SOFTBUS_NO_INIT;
109 }
110
111 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
112 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
113 return SOFTBUS_ERR;
114 }
115 if (g_sessionServerList->cnt >= MAX_SESSION_SERVER_NUM) {
116 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
117 return SOFTBUS_INVALID_NUM;
118 }
119 SessionServer *pos = NULL;
120 SessionServer *tmp = NULL;
121 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
122 if (strcmp(pos->sessionName, newNode->sessionName) == 0) {
123 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "session server [%s] is exist", newNode->sessionName);
124 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
125 return SOFTBUS_SERVER_NAME_REPEATED;
126 }
127 }
128
129 ListAdd(&(g_sessionServerList->list), &(newNode->node));
130 g_sessionServerList->cnt++;
131 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
132
133 return SOFTBUS_OK;
134 }
135
TransSessionServerDelItem(const char * sessionName)136 int32_t TransSessionServerDelItem(const char *sessionName)
137 {
138 if (sessionName == NULL) {
139 return SOFTBUS_INVALID_PARAM;
140 }
141 if (g_sessionServerList == NULL) {
142 return SOFTBUS_ERR;
143 }
144
145 bool isFind = false;
146 SessionServer *pos = NULL;
147 SessionServer *tmp = NULL;
148 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
149 return SOFTBUS_ERR;
150 }
151 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
152 if (strcmp(pos->sessionName, sessionName) == 0) {
153 isFind = true;
154 break;
155 }
156 }
157 if (isFind) {
158 ListDelete(&pos->node);
159 g_sessionServerList->cnt--;
160 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "destroy session server [%s]", sessionName);
161 SoftBusFree(pos);
162 }
163 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
164 return SOFTBUS_OK;
165 }
166
TransDelItemByPackageName(const char * pkgName)167 void TransDelItemByPackageName(const char *pkgName)
168 {
169 if (pkgName == NULL || g_sessionServerList == NULL) {
170 return;
171 }
172
173 SessionServer *pos = NULL;
174 SessionServer *tmp = NULL;
175 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
176 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
177 return;
178 }
179 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
180 if (strcmp(pos->pkgName, pkgName) == 0) {
181 ListDelete(&pos->node);
182 g_sessionServerList->cnt--;
183 SoftBusFree(pos);
184 pos = NULL;
185 }
186 }
187 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
188 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "del package name [%s].", pkgName);
189 }
190
TransGetPkgNameBySessionName(const char * sessionName,char * pkgName,uint16_t len)191 int32_t TransGetPkgNameBySessionName(const char *sessionName, char *pkgName, uint16_t len)
192 {
193 if ((sessionName == NULL) || (pkgName == NULL) || (len == 0)) {
194 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransGetPkgNameBySessionName param error.");
195 return SOFTBUS_ERR;
196 }
197 if (g_sessionServerList == NULL) {
198 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "session server list not init");
199 return SOFTBUS_ERR;
200 }
201
202 SessionServer *pos = NULL;
203 SessionServer *tmp = NULL;
204 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
205 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
206 return SOFTBUS_ERR;
207 }
208 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
209 if (strcmp(pos->sessionName, sessionName) == 0) {
210 int32_t ret = strcpy_s(pkgName, len, pos->pkgName);
211 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
212 if (ret != 0) {
213 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "strcpy_s error ret, [%d]", ret);
214 return SOFTBUS_ERR;
215 }
216 return SOFTBUS_OK;
217 }
218 }
219
220 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
221 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not found session name [%s].", sessionName);
222 return SOFTBUS_ERR;
223 }
224
TransGetUidAndPid(const char * sessionName,int32_t * uid,int32_t * pid)225 int32_t TransGetUidAndPid(const char *sessionName, int32_t *uid, int32_t *pid)
226 {
227 if (sessionName == NULL || uid == NULL || pid == NULL) {
228 return SOFTBUS_INVALID_PARAM;
229 }
230 if (g_sessionServerList == NULL) {
231 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "not init");
232 return SOFTBUS_ERR;
233 }
234
235 SessionServer *pos = NULL;
236 SessionServer *tmp = NULL;
237 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
238 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
239 return SOFTBUS_LOCK_ERR;
240 }
241 char *anonyOut = NULL;
242 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
243 if (strcmp(pos->sessionName, sessionName) == 0) {
244 *uid = pos->uid;
245 *pid = pos->pid;
246 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransGetUidAndPid: sessionName=%s, uid=%d, pid=%d",
247 AnonyDevId(&anonyOut, sessionName), pos->uid, pos->pid);
248 SoftBusFree(anonyOut);
249 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
250 return SOFTBUS_OK;
251 }
252 }
253
254 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
255 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransGetUidAndPid err: sessionName=%s",
256 AnonyDevId(&anonyOut, sessionName));
257 SoftBusFree(anonyOut);
258 return SOFTBUS_ERR;
259 }
260
TransListDelete(ListNode * sessionServerList)261 static void TransListDelete(ListNode *sessionServerList)
262 {
263 SessionServer *pos = NULL;
264 SessionServer *tmp = NULL;
265
266 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, sessionServerList, SessionServer, node) {
267 ListDelete(&pos->node);
268 SoftBusFree(pos);
269 }
270 return;
271 }
272
TransListCopy(ListNode * sessionServerList)273 static int32_t TransListCopy(ListNode *sessionServerList)
274 {
275 if (sessionServerList == NULL) {
276 return SOFTBUS_ERR;
277 }
278
279 SessionServer *pos = NULL;
280 SessionServer *tmp = NULL;
281
282 if (SoftBusMutexLock(&g_sessionServerList->lock) != 0) {
283 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "lock mutex fail!");
284 return SOFTBUS_ERR;
285 }
286 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &g_sessionServerList->list, SessionServer, node) {
287 SessionServer *newpos = (SessionServer *)SoftBusMalloc(sizeof(SessionServer));
288 if (newpos == NULL) {
289 TransListDelete(sessionServerList);
290 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "SoftBusMalloc fail!");
291 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
292 return SOFTBUS_MALLOC_ERR;
293 }
294 *newpos = *pos;
295 ListAdd(sessionServerList, &newpos->node);
296 }
297 (void)SoftBusMutexUnlock(&g_sessionServerList->lock);
298 return SOFTBUS_OK;
299 }
300
TransOnLinkDown(const char * networkId,int32_t routeType)301 void TransOnLinkDown(const char *networkId, int32_t routeType)
302 {
303 if (networkId == NULL || g_sessionServerList == NULL) {
304 return;
305 }
306 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnLinkDown: routeType=%d", routeType);
307
308 ListNode sessionServerList = {0};
309 ListInit(&sessionServerList);
310 int32_t ret = TransListCopy(&sessionServerList);
311 if (ret != SOFTBUS_OK) {
312 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_ERROR, "TransOnLinkDown copy list fail!");
313 return;
314 }
315
316 SessionServer *pos = NULL;
317 SessionServer *tmp = NULL;
318
319 LIST_FOR_EACH_ENTRY_SAFE(pos, tmp, &sessionServerList, SessionServer, node) {
320 (void)TransServerOnChannelLinkDown(pos->pkgName, networkId, routeType);
321 }
322
323 TransListDelete(&sessionServerList);
324 SoftBusLog(SOFTBUS_LOG_TRAN, SOFTBUS_LOG_INFO, "TransOnLinkDown end");
325 return;
326 }
327