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
AppSpawnChild(AppSpawnContent * content,AppSpawnClient * client)67 int AppSpawnChild(AppSpawnContent *content, AppSpawnClient *client)
68 {
69 APPSPAWN_CHECK(content != NULL && client != NULL, return -1, "Invalid arg for appspawn child");
70 APPSPAWN_LOGI("AppSpawnChild id %{public}u flags: 0x%{public}x", client->id, client->flags);
71 StartAppspawnTrace("AppSpawnExecuteClearEnvHook");
72 int ret = AppSpawnExecuteClearEnvHook(content, client);
73 FinishAppspawnTrace();
74 APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
75 NotifyResToParent(content, client, ret);
76 AppSpawnEnvClear(content, client);
77 return 0);
78
79 if (client->flags & APP_COLD_START) {
80 // cold start fail, to start normal
81 if (content->coldStartApp != NULL && content->coldStartApp(content, client) == 0) {
82 return 0;
83 }
84 APPSPAWN_LOGW("AppSpawnChild cold start fail %{public}u", client->id);
85 }
86 StartAppspawnTrace("AppSpawnExecuteSpawningHook");
87 ret = AppSpawnExecuteSpawningHook(content, client);
88 FinishAppspawnTrace();
89 APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
90 NotifyResToParent(content, client, ret);
91 AppSpawnEnvClear(content, client);
92 return 0);
93 StartAppspawnTrace("AppSpawnExecutePreReplyHook");
94 ret = AppSpawnExecutePreReplyHook(content, client);
95 FinishAppspawnTrace();
96 APPSPAWN_CHECK_ONLY_EXPER(ret == 0,
97 NotifyResToParent(content, client, ret);
98 AppSpawnEnvClear(content, client);
99 return 0);
100
101 // notify success to father process and start app process
102 StartAppspawnTrace("NotifyResToParent");
103 NotifyResToParent(content, client, 0);
104 FinishAppspawnTrace();
105
106 StartAppspawnTrace("AppSpawnExecutePostReplyHook");
107 (void)AppSpawnExecutePostReplyHook(content, client);
108 FinishAppspawnTrace();
109
110 if (content->runChildProcessor != NULL) {
111 ret = content->runChildProcessor(content, client);
112 }
113 if (ret != 0) {
114 AppSpawnEnvClear(content, client);
115 }
116 return 0;
117 }
118
CloneAppSpawn(void * arg)119 static int CloneAppSpawn(void *arg)
120 {
121 APPSPAWN_CHECK(arg != NULL, return -1, "Invalid content for appspawn");
122 AppSpawnForkArg *forkArg = (AppSpawnForkArg *)arg;
123 ProcessExit(AppSpawnChild(forkArg->content, forkArg->client));
124 return 0;
125 }
126
127 #ifndef OHOS_LITE
NwebSpawnCloneChildProcess(AppSpawnContent * content,AppSpawnClient * client,pid_t * pid)128 static void NwebSpawnCloneChildProcess(AppSpawnContent *content, AppSpawnClient *client, pid_t *pid)
129 {
130 AppSpawnForkArg arg;
131 arg.client = client;
132 arg.content = content;
133 #ifndef APPSPAWN_TEST
134 AppSpawningCtx *property = (AppSpawningCtx *)client;
135 uint32_t len = 0;
136 char *processType = (char *)(GetAppSpawnMsgExtInfo(property->message, MSG_EXT_NAME_PROCESS_TYPE, &len));
137 APPSPAWN_CHECK(processType != NULL, return, "Invalid processType data");
138
139 if (strcmp(processType, "gpu") == 0) {
140 *pid = clone(CloneAppSpawn, NULL, CLONE_NEWNET | SIGCHLD, (void *)&arg);
141 } else {
142 *pid = clone(CloneAppSpawn, NULL, content->sandboxNsFlags | SIGCHLD, (void *)&arg);
143 }
144 #else
145 *pid = clone(CloneAppSpawn, NULL, content->sandboxNsFlags | SIGCHLD, (void *)&arg);
146 #endif
147 }
148 #endif
149
AppSpawnForkChildProcess(AppSpawnContent * content,AppSpawnClient * client,pid_t * pid)150 static void AppSpawnForkChildProcess(AppSpawnContent *content, AppSpawnClient *client, pid_t *pid)
151 {
152 struct timespec forkStart = {0};
153 clock_gettime(CLOCK_MONOTONIC, &forkStart);
154 StartAppspawnTrace("AppspawnForkStart");
155 *pid = fork();
156 if (*pid == 0) {
157 struct timespec forkEnd = {0};
158 clock_gettime(CLOCK_MONOTONIC, &forkEnd);
159 uint64_t diff = DiffTime(&forkStart, &forkEnd);
160 APPSPAWN_CHECK_ONLY_LOG(diff < MAX_FORK_TIME, "fork time %{public}" PRId64 " us", diff);
161 ProcessExit(AppSpawnChild(content, client));
162 } else {
163 FinishAppspawnTrace();
164 }
165 }
166
AppSpawnProcessMsg(AppSpawnContent * content,AppSpawnClient * client,pid_t * childPid)167 int AppSpawnProcessMsg(AppSpawnContent *content, AppSpawnClient *client, pid_t *childPid)
168 {
169 APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn");
170 APPSPAWN_CHECK(client != NULL && childPid != NULL, return -1, "Invalid client for appspawn");
171 APPSPAWN_LOGI("AppSpawnProcessMsg id: %{public}d mode: %{public}d sandboxNsFlags: 0x%{public}x",
172 client->id, content->mode, content->sandboxNsFlags);
173
174 pid_t pid = 0;
175 #ifndef OHOS_LITE
176 if (content->mode == MODE_FOR_NWEB_SPAWN) {
177 NwebSpawnCloneChildProcess(content, client, &pid);
178 } else {
179 #else
180 {
181 #endif
182 AppSpawnForkChildProcess(content, client, &pid);
183 }
184 APPSPAWN_CHECK(pid >= 0, return APPSPAWN_FORK_FAIL, "fork child process error: %{public}d", errno);
185 *childPid = pid;
186 return 0;
187 }
188