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