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