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