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 <signal.h>
17#include <stdbool.h>
18#include <stdlib.h>
19
20#include "appspawn_hook.h"
21#include "appspawn_modulemgr.h"
22#include "appspawn_manager.h"
23#include "appspawn_service.h"
24#include "appspawn_kickdog.h"
25#include "parameter.h"
26#include "securec.h"
27
28#define APPSPAWN_PRELOAD "libappspawn_helper.z.so"
29
30#ifndef CJAPP_SPAWN
31static AppSpawnStartArgTemplate g_appSpawnStartArgTemplate[PROCESS_INVALID] = {
32    {APPSPAWN_SERVER_NAME, {MODE_FOR_APP_SPAWN, MODULE_APPSPAWN, APPSPAWN_SOCKET_NAME, APPSPAWN_SERVER_NAME, 1}},
33    {NWEBSPAWN_SERVER_NAME, {MODE_FOR_NWEB_SPAWN, MODULE_NWEBSPAWN, NWEBSPAWN_SOCKET_NAME, NWEBSPAWN_SERVER_NAME, 1}},
34    {"app_cold", {MODE_FOR_APP_COLD_RUN, MODULE_APPSPAWN, APPSPAWN_SOCKET_NAME, APPSPAWN_SERVER_NAME, 0}},
35    {"nweb_cold", {MODE_FOR_NWEB_COLD_RUN, MODULE_NWEBSPAWN, APPSPAWN_SOCKET_NAME, NWEBSPAWN_SERVER_NAME, 0}},
36    {NATIVESPAWN_SERVER_NAME, {MODE_FOR_NATIVE_SPAWN, MODULE_NATIVESPAWN, NATIVESPAWN_SOCKET_NAME,
37        NATIVESPAWN_SERVER_NAME, 1}},
38    {NWEBSPAWN_RESTART, {MODE_FOR_NWEB_SPAWN, MODULE_NWEBSPAWN, NWEBSPAWN_SOCKET_NAME, NWEBSPAWN_SERVER_NAME, 1}},
39};
40#else
41static AppSpawnStartArgTemplate g_appCJSpawnStartArgTemplate[CJPROCESS_INVALID] = {
42    {CJAPPSPAWN_SERVER_NAME, {MODE_FOR_CJAPP_SPAWN, MODULE_APPSPAWN, CJAPPSPAWN_SOCKET_NAME, CJAPPSPAWN_SERVER_NAME,
43        1}},
44    {"app_cold", {MODE_FOR_APP_COLD_RUN, MODULE_APPSPAWN, CJAPPSPAWN_SOCKET_NAME, CJAPPSPAWN_SERVER_NAME, 0}},
45};
46#endif
47
48static void CheckPreload(char *const argv[])
49{
50    char buf[256] = APPSPAWN_PRELOAD;  // 256 is enough in most cases
51    char *preload = getenv("LD_PRELOAD");
52    char *pos = preload ? strstr(preload, APPSPAWN_PRELOAD) : NULL;
53    if (pos) {
54        int len = pos - preload;
55        len = sprintf_s(buf, sizeof(buf), "%.*s%s", len, preload, pos + strlen(APPSPAWN_PRELOAD));
56        APPSPAWN_CHECK(len >= 0, return, "preload too long?: %{public}s", preload);
57        if (len == 0) {
58            int ret = unsetenv("LD_PRELOAD");
59            APPSPAWN_CHECK(ret == 0, return, "unsetenv fail(%{public}d)", errno);
60        } else {
61            int ret = setenv("LD_PRELOAD", buf, true);
62            APPSPAWN_CHECK(ret == 0, return, "setenv fail(%{public}d): %{public}s", errno, buf);
63        }
64        return;
65    }
66    if (preload && preload[0]) {
67        int len = sprintf_s(buf, sizeof(buf), "%s:" APPSPAWN_PRELOAD, preload);
68        APPSPAWN_CHECK(len > 0, return, "preload too long: %{public}s", preload);
69    }
70    int ret = setenv("LD_PRELOAD", buf, true);
71    APPSPAWN_CHECK(ret == 0, return, "setenv fail(%{public}d): %{public}s", errno, buf);
72    ssize_t nread = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
73    APPSPAWN_CHECK(nread != -1, return, "readlink fail: /proc/self/exe: %{public}d", errno);
74    buf[nread] = 0;
75    ret = execv(buf, argv);
76    APPSPAWN_LOGE("execv fail: %{public}s: %{public}d: %{public}d", buf, errno, ret);
77}
78
79#ifndef NATIVE_SPAWN
80static AppSpawnStartArgTemplate *GetAppSpawnStartArg(const char *serverName, AppSpawnStartArgTemplate *argTemplate,
81    int count)
82{
83    for (int i = 0; i < count; i++) {
84        if (strcmp(serverName, argTemplate[i].serverName) == 0) {
85            return &argTemplate[i];
86        }
87    }
88
89    return argTemplate;
90}
91#endif
92
93// appspawn -mode appspawn | cold | nwebspawn -param app_property -fd clientFd
94int main(int argc, char *const argv[])
95{
96    APPSPAWN_LOGE("main argc: %{public}d", argc);
97    if (argc <= 0) {
98        return 0;
99    }
100    uintptr_t start = (uintptr_t)argv[0];
101    uintptr_t end = (uintptr_t)strchr(argv[argc - 1], 0);
102    if (end == 0) {
103        return 0;
104    }
105    InitCommonEnv();
106    CheckPreload(argv);
107    (void)signal(SIGPIPE, SIG_IGN);
108    uint32_t argvSize = end - start;
109    AppSpawnStartArg *arg;
110    AppSpawnStartArgTemplate *argTemp = NULL;
111
112#ifdef CJAPP_SPAWN
113    argTemp = &g_appCJSpawnStartArgTemplate[CJPROCESS_FOR_APP_SPAWN];
114    if (argc > MODE_VALUE_INDEX) {
115        argTemp = GetAppSpawnStartArg(argv[MODE_VALUE_INDEX], g_appCJSpawnStartArgTemplate,
116            ARRAY_LENGTH(g_appCJSpawnStartArgTemplate));
117    }
118#elif NATIVE_SPAWN
119    argTemp = &g_appSpawnStartArgTemplate[PROCESS_FOR_NATIVE_SPAWN];
120#else
121    argTemp = &g_appSpawnStartArgTemplate[PROCESS_FOR_APP_SPAWN];
122    if (argc > MODE_VALUE_INDEX) {
123        argTemp = GetAppSpawnStartArg(argv[MODE_VALUE_INDEX], g_appSpawnStartArgTemplate,
124            ARRAY_LENGTH(g_appSpawnStartArgTemplate));
125    }
126#endif
127    arg = &argTemp->arg;
128    if (arg->initArg == 0) {
129        APPSPAWN_CHECK(argc >= ARG_NULL, return 0, "Invalid arg for cold start %{public}d", argc);
130    } else {
131        if (strcmp(argTemp->serverName, NWEBSPAWN_RESTART) == 0) {  // nweb spawn restart
132            APPSPAWN_CHECK_ONLY_EXPER(argvSize >= APP_LEN_PROC_NAME, argvSize = APP_LEN_PROC_NAME);
133        } else {
134            APPSPAWN_CHECK(argvSize >= APP_LEN_PROC_NAME, return 0, "Invalid arg size for service %{public}s",
135                arg->serviceName);
136        }
137    }
138    AppSpawnContent *content = StartSpawnService(arg, argvSize, argc, argv);
139    if (content != NULL) {
140        if ((arg->moduleType == MODULE_APPSPAWN) && (arg->mode != MODE_FOR_APP_COLD_RUN)) {
141            AppSpawnKickDogStart(content);
142        }
143        content->runAppSpawn(content, argc, argv);
144    }
145    return 0;
146}
147