1 /*
2 * Copyright (c) 2021-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 "appspawn_message.h"
17 #include "appspawn_server.h"
18 #include <errno.h>
19 #include <time.h>
20
21 #include "iproxy_server.h"
22 #include "iunknown.h"
23 #include "ipc_skeleton.h"
24 #include "message.h"
25 #include "ohos_errno.h"
26 #include "ohos_init.h"
27 #include "samgr_lite.h"
28 #include "service.h"
29 #include "securec.h"
30
31 static const int INVALID_PID = -1;
32 static const int CLIENT_ID = 100;
33
34 typedef struct AppSpawnFeatureApi {
35 INHERIT_SERVER_IPROXY;
36 } AppSpawnFeatureApi;
37
38 typedef struct AppSpawnService {
39 INHERIT_SERVICE;
40 INHERIT_IUNKNOWNENTRY(AppSpawnFeatureApi);
41 Identity identity;
42 } AppSpawnService;
43
GetName(Service * service)44 static const char *GetName(Service *service)
45 {
46 UNUSED(service);
47 APPSPAWN_LOGI("[appspawn] get service name %s.", APPSPAWN_SERVICE_NAME);
48 return APPSPAWN_SERVICE_NAME;
49 }
50
Initialize(Service * service,Identity identity)51 static BOOL Initialize(Service *service, Identity identity)
52 {
53 if (service == NULL) {
54 APPSPAWN_LOGE("[appspawn] initialize, service NULL!");
55 return FALSE;
56 }
57
58 AppSpawnService *spawnService = (AppSpawnService *)service;
59 spawnService->identity = identity;
60
61 APPSPAWN_LOGI("[appspawn] initialize, identity<%d, %d, %p>", \
62 identity.serviceId, identity.featureId, identity.queueId);
63 return TRUE;
64 }
65
MessageHandle(Service * service,Request * msg)66 static BOOL MessageHandle(Service *service, Request *msg)
67 {
68 UNUSED(service);
69 UNUSED(msg);
70 APPSPAWN_LOGE("[appspawn] message handle not support yet!");
71 return FALSE;
72 }
73
GetTaskConfig(Service * service)74 static TaskConfig GetTaskConfig(Service *service)
75 {
76 UNUSED(service);
77 TaskConfig config = {LEVEL_HIGH, PRI_BELOW_NORMAL, 0x800, 20, SHARED_TASK};
78 return config;
79 }
80
GetMessageSt(MessageSt * msgSt,IpcIo * req)81 static int GetMessageSt(MessageSt *msgSt, IpcIo *req)
82 {
83 if (msgSt == NULL || req == NULL) {
84 return EC_FAILURE;
85 }
86
87 size_t len = 0;
88 char* str = ReadString(req, &len);
89 if (str == NULL || len == 0) {
90 APPSPAWN_LOGE("[appspawn] invoke, get data failed.");
91 return EC_FAILURE;
92 }
93
94 int ret = SplitMessage(str, len, msgSt); // after split message, str no need to free(linux version)
95 return ret;
96 }
97
98 static AppSpawnContentLite *g_appSpawnContentLite = NULL;
AppSpawnCreateContent(const char * socketName,char * longProcName,uint32_t longProcNameLen,int cold)99 AppSpawnContent *AppSpawnCreateContent(const char *socketName, char *longProcName, uint32_t longProcNameLen, int cold)
100 {
101 UNUSED(longProcName);
102 UNUSED(longProcNameLen);
103 APPSPAWN_LOGI("AppSpawnCreateContent %s", socketName);
104 AppSpawnContentLite *appSpawnContent = (AppSpawnContentLite *)malloc(sizeof(AppSpawnContentLite));
105 APPSPAWN_CHECK(appSpawnContent != NULL, return NULL, "Failed to alloc memory for appspawn");
106 int ret = memset_s(appSpawnContent, sizeof(AppSpawnContentLite), 0, sizeof(AppSpawnContentLite));
107 APPSPAWN_CHECK(ret == 0, free(appSpawnContent);
108 return NULL, "Failed to memset content");
109 appSpawnContent->content.longProcName = NULL;
110 appSpawnContent->content.longProcNameLen = 0;
111 g_appSpawnContentLite = appSpawnContent;
112 return appSpawnContent;
113 }
114
Invoke(IServerProxy * iProxy,int funcId,void * origin,IpcIo * req,IpcIo * reply)115 static int Invoke(IServerProxy *iProxy, int funcId, void *origin, IpcIo *req, IpcIo *reply)
116 {
117 #ifdef OHOS_DEBUG
118 struct timespec tmStart = {0};
119 clock_gettime(CLOCK_REALTIME, &tmStart);
120 #endif
121
122 UNUSED(iProxy);
123 UNUSED(origin);
124
125 if (reply == NULL || funcId != ID_CALL_CREATE_SERVICE || req == NULL) {
126 APPSPAWN_LOGE("[appspawn] invoke, funcId %d invalid, reply %d.", funcId, INVALID_PID);
127 WriteInt64(reply, INVALID_PID);
128 return EC_BADPTR;
129 }
130 APPSPAWN_LOGI("[appspawn] invoke.");
131 AppSpawnClientLite client = {};
132 client.client.id = CLIENT_ID;
133 client.client.flags = 0;
134 client.client.cloneFlags = 0;
135 if (GetMessageSt(&client.message, req) != EC_SUCCESS) {
136 APPSPAWN_LOGE("[appspawn] invoke, parse failed! reply %d.", INVALID_PID);
137 WriteInt64(reply, INVALID_PID);
138 return EC_FAILURE;
139 }
140
141 APPSPAWN_LOGI("[appspawn] invoke, msg<%s,%s,%d,%d %d>", client.message.bundleName, client.message.identityID,
142 client.message.uID, client.message.gID, client.message.capsCnt);
143 /* Clone support only one parameter, so need to package application parameters */
144 AppSandboxArg *sandboxArg = (AppSandboxArg *)malloc(sizeof(AppSandboxArg));
145 if (sandboxArg == NULL) {
146 WriteInt64(reply, INVALID_PID);
147 return EC_FAILURE;
148 }
149 (void)memset_s(sandboxArg, sizeof(AppSandboxArg), 0, sizeof(AppSandboxArg));
150
151 pid_t newPid = 0;
152 sandboxArg->content = &g_appSpawnContentLite->content;
153 sandboxArg->client = &client.client;
154 int ret = AppSpawnProcessMsg(sandboxArg, &newPid);
155 if (ret != 0) {
156 newPid = -1;
157 }
158 FreeMessageSt(&client.message);
159 WriteInt64(reply, newPid);
160 free(sandboxArg);
161
162 #ifdef OHOS_DEBUG
163 long long diff = DiffTime(&tmStart);
164 APPSPAWN_LOGI("[appspawn] invoke, reply pid %d, timeused %lld ns.", newPid, diff);
165 #else
166 APPSPAWN_LOGI("[appspawn] invoke, reply pid %d.", newPid);
167 #endif // OHOS_DEBUG
168
169 return ((newPid > 0) ? EC_SUCCESS : EC_FAILURE);
170 }
171
172 static AppSpawnService g_appSpawnService = {
173 .GetName = GetName,
174 .Initialize = Initialize,
175 .MessageHandle = MessageHandle,
176 .GetTaskConfig = GetTaskConfig,
177 SERVER_IPROXY_IMPL_BEGIN,
178 .Invoke = Invoke,
179 IPROXY_END,
180 };
181
AppSpawnInit(void)182 void AppSpawnInit(void)
183 {
184 if (SAMGR_GetInstance()->RegisterService((Service *)&g_appSpawnService) != TRUE) {
185 APPSPAWN_LOGE("[appspawn] register service failed!");
186 return;
187 }
188
189 APPSPAWN_LOGI("[appspawn] register service succeed. %p.", &g_appSpawnService);
190
191 if (SAMGR_GetInstance()->RegisterDefaultFeatureApi(APPSPAWN_SERVICE_NAME, \
192 GET_IUNKNOWN(g_appSpawnService)) != TRUE) {
193 (void)SAMGR_GetInstance()->UnregisterService(APPSPAWN_SERVICE_NAME);
194 APPSPAWN_LOGE("[appspawn] register featureapi failed!");
195 return;
196 }
197
198 APPSPAWN_LOGI("[appspawn] register featureapi succeed.");
199 }
200
201 SYSEX_SERVICE_INIT(AppSpawnInit);
202