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