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