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 <ohos_init.h>
17
18 #include "bus_center_server_stub.h"
19 #include "disc_server_stub.h"
20 #include "ipc_skeleton.h"
21 #include "iproxy_server.h"
22 #include "lnn_bus_center_ipc.h"
23 #include "samgr_lite.h"
24 #include "securec.h"
25 #include "softbus_adapter_mem.h"
26 #include "softbus_client_info_manager.h"
27 #include "softbus_disc_server.h"
28 #include "softbus_def.h"
29 #include "softbus_errcode.h"
30 #include "softbus_server_ipc_interface_code.h"
31 #include "softbus_log.h"
32 #include "softbus_permission.h"
33 #include "softbus_server_frame.h"
34 #include "trans_server_stub.h"
35 #include "trans_session_service.h"
36
37 #define STACK_SIZE 0x800
38 #define QUEUE_SIZE 20
39 #define WAIT_FOR_SERVER 2
40 typedef struct {
41 INHERIT_SERVER_IPROXY;
42 } DefaultFeatureApi;
43
44 typedef struct {
45 INHERIT_SERVICE;
46 INHERIT_IUNKNOWNENTRY(DefaultFeatureApi);
47 Identity identity;
48 } SoftbusSamgrService;
49
GetName(Service * service)50 static const char *GetName(Service *service)
51 {
52 (void)service;
53 return SOFTBUS_SERVICE;
54 }
55
Initialize(Service * service,Identity identity)56 static BOOL Initialize(Service *service, Identity identity)
57 {
58 if (service == NULL) {
59 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
60 return TRUE;
61 }
62
63 SoftbusSamgrService *samgrService = (SoftbusSamgrService *)service;
64 samgrService->identity = identity;
65 return TRUE;
66 }
67
MessageHandle(Service * service,Request * msg)68 static BOOL MessageHandle(Service *service, Request *msg)
69 {
70 if (service == NULL || msg == NULL) {
71 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "invalid param");
72 return TRUE;
73 }
74 return FALSE;
75 }
76
GetTaskConfig(Service * service)77 static TaskConfig GetTaskConfig(Service *service)
78 {
79 (void)service;
80 TaskConfig config = { LEVEL_HIGH, PRI_BELOW_NORMAL, STACK_SIZE, QUEUE_SIZE, SHARED_TASK };
81 return config;
82 }
83
ComponentDeathCallback(const char * pkgName,int32_t pid)84 static void ComponentDeathCallback(const char *pkgName, int32_t pid)
85 {
86 DiscServerDeathCallback(pkgName);
87 TransServerDeathCallback(pkgName, pid);
88 BusCenterServerDeathCallback(pkgName);
89 }
90
91 typedef struct DeathCbArg {
92 char *pkgName;
93 int32_t pid;
94 } DeathCbArg;
95
ClientDeathCb(void * arg)96 static void ClientDeathCb(void *arg)
97 {
98 if (arg == NULL) {
99 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "package name is NULL.");
100 return;
101 }
102 DeathCbArg* argStrcut = (DeathCbArg*)arg;
103 struct CommonScvId svcId = {0};
104 if (SERVER_GetIdentityByPkgName((const char *)argStrcut->pkgName, &svcId) != SOFTBUS_OK) {
105 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not found client by package name.");
106 SoftBusFree(argStrcut->pkgName);
107 SoftBusFree(argStrcut);
108 return;
109 }
110 SERVER_UnregisterService((const char *)argStrcut->pkgName);
111 ComponentDeathCallback((const char *)argStrcut->pkgName, argStrcut->pid);
112 SoftBusFree(argStrcut->pkgName);
113 SoftBusFree(argStrcut);
114 SvcIdentity sid = {0};
115 sid.handle = svcId.handle;
116 sid.token = svcId.token;
117 sid.cookie = svcId.cookie;
118 ReleaseSvc(sid);
119 }
120
ServerRegisterService(IpcIo * req,IpcIo * reply)121 static int32_t ServerRegisterService(IpcIo *req, IpcIo *reply)
122 {
123 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "register service ipc server pop.");
124 size_t len = 0;
125 int ret = SOFTBUS_ERR;
126 struct CommonScvId svcId = {0};
127
128 const char *name = (const char*)ReadString(req, &len);
129 SvcIdentity svc;
130 bool value = ReadRemoteObject(req, &svc);
131 if (!value || name == NULL || len == 0) {
132 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "get data fail");
133 goto EXIT;
134 }
135 int32_t callingUid = GetCallingUid();
136 if (!CheckBusCenterPermission(callingUid, name)) {
137 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "ServerRegisterService no permission.");
138 goto EXIT;
139 }
140 svcId.handle = svc.handle;
141 svcId.token = svc.token;
142 svcId.cookie = svc.cookie;
143
144 char *pkgName = (char *)SoftBusMalloc(len + 1);
145 if (pkgName == NULL) {
146 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus malloc failed!");
147 goto EXIT;
148 }
149 if (strcpy_s(pkgName, len + 1, name) != EOK) {
150 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus strcpy_s failed!");
151 SoftBusFree(pkgName);
152 goto EXIT;
153 }
154
155 DeathCbArg *argStrcut = (DeathCbArg*)SoftBusMalloc(sizeof(DeathCbArg));
156 if (argStrcut == NULL) {
157 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "softbus malloc failed!");
158 SoftBusFree(pkgName);
159 goto EXIT;
160 }
161 argStrcut->pkgName = pkgName;
162 argStrcut->pid = GetCallingPid();
163
164 uint32_t cbId = 0;
165 AddDeathRecipient(svc, ClientDeathCb, argStrcut, &cbId);
166 svcId.cbId = cbId;
167 ret = SERVER_RegisterService(name, &svcId);
168 EXIT:
169 WriteInt32(reply, ret);
170 return SOFTBUS_OK;
171 }
172
173 typedef struct {
174 enum SoftBusFuncId id;
175 int32_t (*func)(IpcIo *req, IpcIo *reply);
176 } ServerInvokeCmd;
177
178 ServerInvokeCmd g_serverInvokeCmdTbl[] = {
179 { MANAGE_REGISTER_SERVICE, ServerRegisterService },
180 { SERVER_PUBLISH_SERVICE, ServerPublishService },
181 { SERVER_UNPUBLISH_SERVICE, ServerUnPublishService },
182 { SERVER_START_DISCOVERY, ServerStartDiscovery },
183 { SERVER_STOP_DISCOVERY, ServerStopDiscovery },
184 { SERVER_JOIN_LNN, ServerJoinLNN },
185 { SERVER_JOIN_METANODE, ServerJoinMetaNode },
186 { SERVER_LEAVE_LNN, ServerLeaveLNN },
187 { SERVER_LEAVE_METANODE, ServerLeaveMetaNode },
188 { SERVER_GET_ALL_ONLINE_NODE_INFO, ServerGetAllOnlineNodeInfo },
189 { SERVER_GET_LOCAL_DEVICE_INFO, ServerGetLocalDeviceInfo },
190 { SERVER_GET_NODE_KEY_INFO, ServerGetNodeKeyInfo },
191 { SERVER_START_TIME_SYNC, ServerStartTimeSync },
192 { SERVER_STOP_TIME_SYNC, ServerStopTimeSync },
193 { SERVER_PUBLISH_LNN, ServerPublishLNN },
194 { SERVER_STOP_PUBLISH_LNN, ServerStopPublishLNN },
195 { SERVER_REFRESH_LNN, ServerRefreshLNN },
196 { SERVER_STOP_REFRESH_LNN, ServerStopRefreshLNN },
197 { SERVER_ACTIVE_META_NODE, ServerActiveMetaNode},
198 { SERVER_DEACTIVE_META_NODE, ServerDeactiveMetaNode },
199 { SERVER_GET_ALL_META_NODE_INFO, ServerGetAllMetaNodeInfo },
200 { SERVER_SHIFT_LNN_GEAR, ServerShiftLnnGear },
201 { SERVER_CREATE_SESSION_SERVER, ServerCreateSessionServer },
202 { SERVER_REMOVE_SESSION_SERVER, ServerRemoveSessionServer },
203 { SERVER_OPEN_SESSION, ServerOpenSession },
204 { SERVER_OPEN_AUTH_SESSION, ServerOpenAuthSession},
205 { SERVER_NOTIFY_AUTH_SUCCESS, ServerNotifyAuthSuccess},
206 { SERVER_CLOSE_CHANNEL, ServerCloseChannel },
207 { SERVER_SESSION_SENDMSG, ServerSendSessionMsg },
208 { SERVER_SET_NODE_DATA_CHANGE_FLAG, ServerSetNodeDataChangeFlag},
209 };
210
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)211 static int32_t Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
212 {
213 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "RECEIVE FUNCID:%d", funcId);
214 if (GetServerIsInit() == false) {
215 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "server not init");
216 WriteInt32(reply, SOFTBUS_SERVER_NOT_INIT);
217 return SOFTBUS_ERR;
218 }
219 int tblSize = sizeof(g_serverInvokeCmdTbl) / sizeof(ServerInvokeCmd);
220 for (int i = 0; i < tblSize; i++) {
221 if (funcId == g_serverInvokeCmdTbl[i].id) {
222 return g_serverInvokeCmdTbl[i].func(req, reply);
223 }
224 }
225 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "not support func[%d]", funcId);
226 return SOFTBUS_ERR;
227 }
228
229 static SoftbusSamgrService g_samgrService = {
230 .GetName = GetName,
231 .Initialize = Initialize,
232 .MessageHandle = MessageHandle,
233 .GetTaskConfig = GetTaskConfig,
234 SERVER_IPROXY_IMPL_BEGIN,
235 .Invoke = Invoke,
236 IPROXY_END,
237 };
238
HOS_SystemInit(void)239 void __attribute__((weak)) HOS_SystemInit(void)
240 {
241 SAMGR_Bootstrap();
242 return;
243 }
244
ServerStubInit(void)245 NO_SANITIZE("cfi") int ServerStubInit(void)
246 {
247 HOS_SystemInit();
248
249 if (LnnIpcInit() != SOFTBUS_OK) {
250 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "Center Ipc init failed.");
251 return SOFTBUS_ERR;
252 }
253
254 if (SERVER_InitClient() != SOFTBUS_OK) {
255 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_ERROR, "client manager init failed.");
256 return SOFTBUS_ERR;
257 }
258 return SOFTBUS_OK;
259 }
260
Init(void)261 static void Init(void)
262 {
263 sleep(WAIT_FOR_SERVER);
264 SAMGR_GetInstance()->RegisterService((Service *)&g_samgrService);
265 SAMGR_GetInstance()->RegisterDefaultFeatureApi(SOFTBUS_SERVICE, GET_IUNKNOWN(g_samgrService));
266 SoftBusLog(SOFTBUS_LOG_COMM, SOFTBUS_LOG_INFO, "Init success %s", SOFTBUS_SERVICE);
267 }
268 SYSEX_SERVICE_INIT(Init);