1 /*
2  * Copyright (c) 2020 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 #include "remote_register.h"
16 #include <ohos_errno.h>
17 #include <ohos_init.h>
18 #include <log.h>
19 #ifdef MINI_SAMGR_LITE_RPC
20 #include "dbinder_service.h"
21 #include "samgr_server.h"
22 #endif
23 #include "default_client.h"
24 #include "iproxy_client.h"
25 #include "memory_adapter.h"
26 #include "policy_define.h"
27 #include "pthread.h"
28 #include "samgr_lite.h"
29 #include "thread_adapter.h"
30 #include "unistd.h"
31 
32 #undef LOG_TAG
33 #undef LOG_DOMAIN
34 #define LOG_TAG "Samgr"
35 #define LOG_DOMAIN 0xD001800
36 
37 #define RETRY_INTERVAL 2
38 #define MAX_RETRY_TIMES 10
39 #define ABILITY_UID_START 100
40 #define SA_NAME_NUM 2
41 static void ClientInitializeRegistry(void);
42 RemoteRegister g_remoteRegister;
43 static BOOL g_isAbilityInited = FALSE;
44 
SAMGR_RegisterServiceApi(const char * service,const char * feature,const Identity * identity,IUnknown * iUnknown)45 int SAMGR_RegisterServiceApi(const char *service, const char *feature, const Identity *identity, IUnknown *iUnknown)
46 {
47     if (service == NULL) {
48         return EC_INVALID;
49     }
50     ClientInitializeRegistry();
51     MUTEX_Lock(g_remoteRegister.mtx);
52     SaName saName = {service, feature};
53     int32 token = SAMGR_AddRouter(g_remoteRegister.endpoint, &saName, identity, iUnknown);
54 #ifdef MINI_SAMGR_LITE_RPC
55     char saNameStr[SA_NAME_NUM * MAX_NAME_LEN + SA_NAME_NUM];
56     (void)sprintf_s(saNameStr, SA_NAME_NUM * MAX_NAME_LEN + SA_NAME_NUM, "%s#%s", service, feature?feature:"");
57     HILOG_INFO(HILOG_MODULE_SAMGR, "register saname: %s index: %d\n", saNameStr, token);
58     SaNode *saNode = GetSaNodeBySaName(service, feature);
59     if (saNode != NULL) {
60         RegisterRemoteProxy(saNameStr, strlen(saNameStr), saNode->saId);
61     }
62 #endif
63     MUTEX_Unlock(g_remoteRegister.mtx);
64     if (token < 0 || !g_remoteRegister.endpoint->running) {
65         return token;
66     }
67 #ifndef MINI_SAMGR_LITE_RPC
68     SAMGR_ProcPolicy(g_remoteRegister.endpoint, &saName, token);
69 #endif
70     return EC_SUCCESS;
71 }
72 
SAMGR_FindServiceApi(const char * service,const char * feature)73 IUnknown *SAMGR_FindServiceApi(const char *service, const char *feature)
74 {
75     if (service == NULL) {
76         return NULL;
77     }
78     ClientInitializeRegistry();
79     SaName key = {service, feature};
80     int index = VECTOR_FindByKey(&g_remoteRegister.clients, &key);
81     if (index != INVALID_INDEX) {
82         return VECTOR_At(&g_remoteRegister.clients, index);
83     }
84     IUnknown *proxy = SAMGR_CreateIProxy(service, feature);
85     if (proxy == NULL) {
86         return NULL;
87     }
88     MUTEX_Lock(g_remoteRegister.mtx);
89     index = VECTOR_FindByKey(&g_remoteRegister.clients, &key);
90     if (index != INVALID_INDEX) {
91         MUTEX_Unlock(g_remoteRegister.mtx);
92         proxy->Release(proxy);
93         return VECTOR_At(&g_remoteRegister.clients, index);
94     }
95     VECTOR_Add(&g_remoteRegister.clients, proxy);
96     MUTEX_Unlock(g_remoteRegister.mtx);
97     HILOG_INFO(HILOG_MODULE_SAMGR, "Create remote sa proxy<%s, %s>!", service, feature);
98     return proxy;
99 }
100 
SAMGR_RegisterSystemCapabilityApi(const char * sysCap,BOOL isReg)101 int32 SAMGR_RegisterSystemCapabilityApi(const char *sysCap, BOOL isReg)
102 {
103     ClientInitializeRegistry();
104     return SAMGR_AddSysCap(g_remoteRegister.endpoint, sysCap, isReg);
105 }
106 
SAMGR_QuerySystemCapabilityApi(const char * sysCap)107 BOOL SAMGR_QuerySystemCapabilityApi(const char *sysCap)
108 {
109     ClientInitializeRegistry();
110     BOOL isReg = FALSE;
111     if (SAMGR_GetSysCap(g_remoteRegister.endpoint, sysCap, &isReg) != EC_SUCCESS) {
112         return FALSE;
113     }
114     return isReg;
115 }
116 
SAMGR_GetSystemCapabilitiesApi(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN],int32 * size)117 int32 SAMGR_GetSystemCapabilitiesApi(char sysCaps[MAX_SYSCAP_NUM][MAX_SYSCAP_NAME_LEN], int32 *size)
118 {
119     ClientInitializeRegistry();
120     return SAMGR_GetSystemCapabilities(g_remoteRegister.endpoint, sysCaps, size);
121 }
122 
ClearRegistry(void)123 static void ClearRegistry(void)
124 {
125     if (g_remoteRegister.endpoint == NULL) {
126         return;
127     }
128     HILOG_INFO(HILOG_MODULE_SAMGR, "Clear Client Registry!");
129     SAMGR_Free(g_remoteRegister.mtx);
130     g_remoteRegister.mtx = NULL;
131     VECTOR_Clear(&(g_remoteRegister.clients));
132     VECTOR_Clear(&(g_remoteRegister.endpoint->routers));
133     SAMGR_Free(g_remoteRegister.endpoint);
134     g_remoteRegister.endpoint = NULL;
135 }
136 
ClientInitializeRegistry(void)137 static void ClientInitializeRegistry(void)
138 {
139 #ifndef MINI_SAMGR_LITE_RPC
140     if (getuid() >= ABILITY_UID_START && !g_isAbilityInited) {
141         ClearRegistry();
142         g_isAbilityInited = TRUE;
143     }
144 #endif
145 
146     if (g_remoteRegister.endpoint != NULL) {
147         return;
148     }
149     HILOG_INFO(HILOG_MODULE_SAMGR, "Initialize Client Registry!");
150     MUTEX_GlobalLock();
151     if (g_remoteRegister.endpoint == NULL) {
152         g_remoteRegister.mtx = MUTEX_InitValue();
153         g_remoteRegister.clients = VECTOR_Make((VECTOR_Key)SAMGR_GetSAName, (VECTOR_Compare)SAMGR_CompareSAName);
154         g_remoteRegister.endpoint = SAMGR_CreateEndpoint("ipc client", NULL);
155     }
156     MUTEX_GlobalUnlock();
157 }
158