1 /*
2  * Copyright (c) 2024 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_server.h"
17 
18 #undef _GNU_SOURCE
19 #define _GNU_SOURCE
20 #include <sched.h>
21 #include <signal.h>
22 #include <time.h>
23 
24 #include "appspawn_trace.h"
25 #include "appspawn_utils.h"
26 #ifndef OHOS_LITE
27 #include "appspawn_manager.h"
28 #endif
29 
30 #define MAX_FORK_TIME (30 * 1000)   // 30ms
31 
NotifyResToParent(struct AppSpawnContent * content,AppSpawnClient * client,int result)32 static void NotifyResToParent(struct AppSpawnContent *content, AppSpawnClient *client, int result)
33 {
34     APPSPAWN_LOGI("NotifyResToParent: %{public}d", result);
35     if (content->notifyResToParent != NULL) {
36         content->notifyResToParent(content, client, result);
37     }
38 }
39 
ProcessExit(int code)40 void ProcessExit(int code)
41 {
42     APPSPAWN_LOGI("App exit code: %{public}d", code);
43 #ifdef OHOS_LITE
44     _exit(0x7f); // 0x7f user exit
45 #else
46     quick_exit(0);
47 #endif
48 }
49 
50 #ifdef APPSPAWN_HELPER
51 __attribute__((visibility("default")))
52 _Noreturn
exit(int code)53 void exit(int code)
54 {
55     char *checkExit = getenv(APPSPAWN_CHECK_EXIT);
56     if (checkExit && atoi(checkExit) == getpid()) {
57         APPSPAWN_LOGF("Unexpected call: exit(%{public}d)", code);
58         abort();
59     }
60     // hook `exit` to `ProcessExit` to ensure app exit in a clean way
61     ProcessExit(code);
62     // should not come here
63     abort();
64 }
65 #endif
66 
67 #ifdef USE_ENCAPS
68 #include <sys/ioctl.h>
69 
70 #define OH_ENCAPS_PROC_TYPE_BASE 0x18
71 #define OH_ENCAPS_MAGIC 'E'
72 #define OH_PROC_APP 4
73 #define SET_PROC_TYPE_CMD _IOW(OH_ENCAPS_MAGIC, OH_ENCAPS_PROC_TYPE_BASE, uint32_t)
74 
SetEncapsFlag()75 static void SetEncapsFlag()
76 {
77     uint32_t flag = OH_PROC_APP;
78     int fdEncaps = open("/dev/encaps", O_RDWR);
79     if (fdEncaps < 0) {
80         APPSPAWN_LOGE("AppSpawnChild open encaps failed");
81         return;
82     }
83     int ret = ioctl(fdEncaps, SET_PROC_TYPE_CMD, &flag);
84     if (ret != 0) {
85         APPSPAWN_LOGE("AppSpawnChild SetEncapsFlag failed");
86     }
87     close(fdEncaps);
88 }
89 #endif
90 
AppSpawnChild(AppSpawnContent * content,AppSpawnClient * client)91 int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client)
92 {
93     APPSPAWN_CHECK(content != NULL && client != NULL, return -1, "Invalid arg for appspawn child");
94     APPSPAWN_LOGI("AppSpawnChild id %{public}u flags: 0x%{public}x", client->id, client->flags);
95     StartAppspawnTrace("AppSpawnExecuteClearEnvHook");
96     int ret = AppSpawnExecuteClearEnvHook(content, client);
97     FinishAppspawnTrace();
98     APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
99         NotifyResToParent(content, client, ret);
100         AppSpawnEnvClear(content, client);
101         return 0);
102 
103     if (client->flags & APP_COLD_START) {
104         // cold start fail, to start normal
105         if (content->coldStartApp != NULL && content->coldStartApp(content, client) == 0) {
106             return 0;
107         }
108         APPSPAWN_LOGW("AppSpawnChild cold start fail %{public}u", client->id);
109     }
110 #ifdef USE_ENCAPS
111     SetEncapsFlag();
112 #endif
113     StartAppspawnTrace("AppSpawnExecuteSpawningHook");
114     ret = AppSpawnExecuteSpawningHook(content, client);
115     FinishAppspawnTrace();
116     APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
117         NotifyResToParent(content, client, ret);
118         AppSpawnEnvClear(content, client);
119         return 0);
120     StartAppspawnTrace("AppSpawnExecutePreReplyHook");
121     ret = AppSpawnExecutePreReplyHook(content, client);
122     FinishAppspawnTrace();
123     APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
124         NotifyResToParent(content, client, ret);
125         AppSpawnEnvClear(content, client);
126         return 0);
127 
128     // notify success to father process and start app process
129     StartAppspawnTrace("NotifyResToParent");
130     NotifyResToParent(content, client, 0);
131     FinishAppspawnTrace();
132 
133     StartAppspawnTrace("AppSpawnExecutePostReplyHook");
134     (void)AppSpawnExecutePostReplyHook(content, client);
135     FinishAppspawnTrace();
136 
137     if (content->runChildProcessor != NULL) {
138         ret = content->runChildProcessor(content, client);
139     }
140     if (ret != 0) {
141         AppSpawnEnvClear(content, client);
142     }
143     return 0;
144 }
145 
CloneAppSpawn(void * arg)146 static int CloneAppSpawn(void *arg)
147 {
148     APPSPAWN_CHECK(arg != NULL, return -1, "Invalid content for appspawn");
149     AppSpawnForkArg *forkArg = (AppSpawnForkArg *)arg;
150     ProcessExit(AppSpawnChild(forkArg->content, forkArg->client));
151     return 0;
152 }
153 
154 #ifndef OHOS_LITE
NwebSpawnCloneChildProcess(AppSpawnContent * content,AppSpawnClient * client,pid_t * pid)155 static void NwebSpawnCloneChildProcess(AppSpawnContent *content, AppSpawnClient *client, pid_t *pid)
156 {
157     AppSpawnForkArg arg;
158     arg.client = client;
159     arg.content = content;
160 #ifndef APPSPAWN_TEST
161     AppSpawningCtx *property = (AppSpawningCtx *)client;
162     uint32_t len = 0;
163     char *processType = (char *)(GetAppSpawnMsgExtInfo(property->message, MSG_EXT_NAME_PROCESS_TYPE, &len));
164     APPSPAWN_CHECK(processType != NULL, return, "Invalid processType data");
165 
166     if (strcmp(processType, "gpu") == 0) {
167         *pid = clone(CloneAppSpawn, NULL, CLONE_NEWNET | SIGCHLD, (void *)&arg);
168     } else {
169         *pid = clone(CloneAppSpawn, NULL, content->sandboxNsFlags | SIGCHLD, (void *)&arg);
170     }
171 #else
172     *pid = clone(CloneAppSpawn, NULL, content->sandboxNsFlags | SIGCHLD, (void *)&arg);
173 #endif
174 }
175 #endif
176 
AppSpawnForkChildProcess(AppSpawnContent * content,AppSpawnClient * client,pid_t * pid)177 static void AppSpawnForkChildProcess(AppSpawnContent *content, AppSpawnClient *client, pid_t *pid)
178 {
179     struct timespec forkStart = {0};
180     clock_gettime(CLOCK_MONOTONIC, &forkStart);
181     StartAppspawnTrace("AppspawnForkStart");
182     *pid = fork();
183     if (*pid == 0) {
184         struct timespec forkEnd = {0};
185         clock_gettime(CLOCK_MONOTONIC, &forkEnd);
186         uint64_t diff = DiffTime(&forkStart, &forkEnd);
187         APPSPAWN_CHECK_ONLY_LOG(diff < MAX_FORK_TIME, "fork time %{public}" PRId64 " us", diff);
188         ProcessExit(AppSpawnChild(content, client));
189     } else {
190         FinishAppspawnTrace();
191     }
192 }
193 
AppSpawnProcessMsg(AppSpawnContent * content,AppSpawnClient * client,pid_t * childPid)194 int AppSpawnProcessMsg(AppSpawnContent *content, AppSpawnClient *client, pid_t *childPid)
195 {
196     APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn");
197     APPSPAWN_CHECK(client != NULL && childPid != NULL, return -1, "Invalid client for appspawn");
198     APPSPAWN_LOGI("AppSpawnProcessMsg id: %{public}d mode: %{public}d sandboxNsFlags: 0x%{public}x",
199         client->id, content->mode, content->sandboxNsFlags);
200 
201     pid_t pid = 0;
202 #ifndef OHOS_LITE
203     if (content->mode == MODE_FOR_NWEB_SPAWN) {
204         NwebSpawnCloneChildProcess(content, client, &pid);
205     } else {
206 #else
207     {
208 #endif
209         AppSpawnForkChildProcess(content, client, &pid);
210     }
211     APPSPAWN_CHECK(pid >= 0, return APPSPAWN_FORK_FAIL, "fork child process error: %{public}d", errno);
212     *childPid = pid;
213     return 0;
214 }
215