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 #include "samgr_ipc_adapter.h"
16 #include "samgr_server.h"
17 #include "dbinder_service.h"
18 static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option);
19 static void HandleIpc(const Request *request, const Response *response);
20 static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum);
ClientRegisterRemoteEndpoint(SvcIdentity * identity,int token,const char * service,const char * feature)21 int ClientRegisterRemoteEndpoint(SvcIdentity *identity, int token, const char *service, const char *feature)
22 {
23 IpcObjectStub *objectStubOne = (IpcObjectStub *)calloc(1, sizeof(IpcObjectStub));
24 if (objectStubOne == NULL) {
25 return -1;
26 }
27 objectStubOne->func = Dispatch;
28 objectStubOne->isRemote = true;
29 // handle is used by rpc, should be bigger than 0
30 identity->handle = token + 1;
31 identity->cookie = objectStubOne;
32 // token is used by router index, should be itself, and save in SaNode
33 identity->token = token;
34 extern int AddEndpoint(SvcIdentity identity, const char *service, const char *feature);
35 return AddEndpoint(*identity, service, feature);
36 }
37
Listen(Endpoint * endpoint,int token,const char * service,const char * feature)38 void Listen(Endpoint *endpoint, int token, const char *service, const char *feature)
39 {
40 endpoint->registerEP(&endpoint->identity, token, service, feature);
41 }
42
Dispatch(uint32_t code,IpcIo * data,IpcIo * reply,MessageOption option)43 static int Dispatch(uint32_t code, IpcIo *data, IpcIo *reply, MessageOption option)
44 {
45 extern RemoteRegister g_remoteRegister;
46 Endpoint *endpoint = g_remoteRegister.endpoint;
47 int token = GetRemoteToken(data);
48 if (token == EC_INVALID) {
49 goto ERROR;
50 }
51 if (TB_CheckMessage(&endpoint->bucket) == BUCKET_BUSY) {
52 HILOG_WARN(HILOG_MODULE_SAMGR, "Flow Control <%u> is NULL", token);
53 goto ERROR;
54 }
55
56 Router *router = VECTOR_At(&endpoint->routers, token);
57 if (router == NULL) {
58 HILOG_ERROR(HILOG_MODULE_SAMGR, "Router <%s, %u> is NULL", endpoint->name, token);
59 goto ERROR;
60 }
61 Response resp = {0};
62 resp.data = endpoint;
63 Request request = {0};
64 request.msgId = token;
65 request.data = data;
66 resp.reply = reply;
67 request.msgValue = code;
68 uint32 *ref = NULL;
69 int ret = SAMGR_SendSharedDirectRequest(&router->identity, &request, &resp, &ref, HandleIpc);
70 if (ret != EC_SUCCESS) {
71 HILOG_ERROR(HILOG_MODULE_SAMGR, "Router[%u] Service<%d, %d> is busy",
72 token, router->identity.serviceId, router->identity.featureId);
73 goto ERROR;
74 }
75
76 return EC_SUCCESS;
77 ERROR:
78 return EC_INVALID;
79 }
80
HandleIpc(const Request * request,const Response * response)81 static void HandleIpc(const Request *request, const Response *response)
82 {
83 Endpoint *endpoint = (Endpoint *)response->data;
84 Router *router = VECTOR_At(&endpoint->routers, request->msgId);
85
86 router->proxy->Invoke(router->proxy, request->msgValue, NULL, request->data, response->reply);
87 }
88
RegisterIdentity(const SaName * saName,SvcIdentity * saInfo,PolicyTrans ** policy,uint32 * policyNum)89 int RegisterIdentity(const SaName *saName, SvcIdentity *saInfo, PolicyTrans **policy, uint32 *policyNum)
90 {
91 IpcIo req;
92 uint8 data[MAX_DATA_LEN];
93 IpcIoInit(&req, data, MAX_DATA_LEN, 0);
94 WriteUint32(&req, RES_FEATURE);
95 WriteUint32(&req, OP_PUT);
96 WriteString(&req, saName->service);
97 WriteBool(&req, saName->feature == NULL);
98 if (saName->feature != NULL) {
99 WriteBool(&req, saName->feature);
100 }
101 WriteUint32(&req, saInfo->token);
102 IpcIo reply;
103 void *replyBuf = NULL;
104 SvcIdentity *samgr = GetContextObject();
105 MessageOption option;
106 MessageOptionInit(&option);
107 int ret = SendRequest(*samgr, INVALID_INDEX, &req, &reply, option,
108 (uintptr_t *)&replyBuf);
109 ret = -ret;
110 int32_t ipcRet = EC_FAILURE;
111 if (ret == EC_SUCCESS) {
112 ret = ReadInt32(&reply, &ipcRet);
113 }
114 if (ipcRet == EC_SUCCESS) {
115 SvcIdentity target;
116 (void)ReadRemoteObject(&reply, &target);
117 GetRemotePolicy(&reply, policy, policyNum);
118 }
119 if (replyBuf != NULL) {
120 FreeBuffer(replyBuf);
121 }
122 return ret;
123 }
124
GetRemoteToken(IpcIo * data)125 int GetRemoteToken(IpcIo *data)
126 {
127 uintptr_t saId = 0;
128 ReadInt32(data, &saId);
129 SaNode *saNode = GetSaNodeBySaId(saId);
130 if (saNode == NULL) {
131 HILOG_WARN(HILOG_MODULE_SAMGR, "get sa node by sa id %d is NULL", saId);
132 return EC_INVALID;
133 }
134 return saNode->token;
135 }
136
GetRemotePolicy(IpcIo * reply,PolicyTrans ** policy,uint32 * policyNum)137 static void GetRemotePolicy(IpcIo *reply, PolicyTrans **policy, uint32 *policyNum)
138 {
139 if (reply == NULL) {
140 return;
141 }
142 ReadUint32(reply, policyNum);
143 if (*policyNum > MAX_POLICY_NUM) {
144 *policyNum = MAX_POLICY_NUM;
145 }
146 SAMGR_Free(*policy);
147 if (*policyNum == 0) {
148 *policy = NULL;
149 return;
150 }
151 *policy = (PolicyTrans *)SAMGR_Malloc(sizeof(PolicyTrans) * (*policyNum));
152 if (*policy == NULL) {
153 return;
154 }
155 for (uint32 i = 0; i < *policyNum; i++) {
156 if (ReadInt32(reply, &(*policy)[i].type)) {
157 switch ((*policy)[i].type) {
158 case RANGE:
159 ReadInt32(reply, &((*policy)[i].uidMin));
160 ReadInt32(reply, &((*policy)[i].uidMax));
161 break;
162 case FIXED:
163 for (uint32 j = 0; j < UID_SIZE; j++) {
164 ReadInt32(reply, &((*policy)[i].fixedUid[j]));
165 }
166 break;
167 case BUNDLENAME:
168 ReadInt32(reply, &((*policy)[i].fixedUid[0]));
169 break;
170 default:
171 break;
172 }
173 }
174 }
175 }