• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 #include <signal.h>
16 #include <stdbool.h>
17 #include "appspawn_adapter.h"
18 #include "appspawn_msg.h"
19 #include "appspawn_server.h"
20 #include "appspawn_service.h"
21 #include "securec.h"
22 #include "init_param.h"
23 #include "syspara/parameter.h"
24 #include "nwebspawn_lancher.h"
25 #define APPSPAWN_PRELOAD "libappspawn_helper.z.so"
26 
CheckPreload(char * const argv[])27 static void CheckPreload(char *const argv[])
28 {
29     char *preload = getenv("LD_PRELOAD");
30     if (preload && strstr(preload, APPSPAWN_PRELOAD)) {
31         return;
32     }
33     char buf[128] = APPSPAWN_PRELOAD; // 128 is enough in most cases
34     if (preload && preload[0]) {
35         int len = sprintf_s(buf, sizeof(buf), "%s:" APPSPAWN_PRELOAD, preload);
36         APPSPAWN_CHECK(len > 0, return, "preload too long: %{public}s", preload);
37     }
38     int ret = setenv("LD_PRELOAD", buf, true);
39     APPSPAWN_CHECK(ret == 0, return, "setenv fail: %{public}s", buf);
40     ssize_t nread = readlink("/proc/self/exe", buf, sizeof(buf) - 1);
41     APPSPAWN_CHECK(nread != -1, return, "readlink fail: /proc/self/exe: %{public}d", errno);
42     buf[nread] = 0;
43     ret = execv(buf, argv);
44     APPSPAWN_LOGE("execv fail: %{public}s: %{public}d: %{public}d", buf, errno, ret);
45 }
46 
main(int argc,char * const argv[])47 int main(int argc, char *const argv[])
48 {
49     if (argc <= 0) {
50         return 0;
51     }
52 
53     CheckPreload(argv);
54     (void)signal(SIGPIPE, SIG_IGN);
55     uint32_t argvSize = 0;
56     int mode = 0;
57     pid_t pid = 1;
58     if ((argc > PARAM_INDEX) && (strcmp(argv[START_INDEX], "cold-start") == 0)) {
59         argvSize = APP_LEN_PROC_NAME;
60         mode = 1;
61     } else {
62         // calculate child process long name size
63         uintptr_t start = (uintptr_t)argv[0];
64         uintptr_t end = (uintptr_t)strchr(argv[argc - 1], 0);
65         if (end == 0) {
66             return -1;
67         }
68         argvSize = end - start;
69         if (argvSize > APP_MSG_MAX_SIZE) {
70             return -1;
71         }
72         pid = NwebSpawnLanch();
73         int isRet = memset_s(argv[0], argvSize, 0, (size_t)argvSize) != EOK;
74         APPSPAWN_CHECK(!isRet, return -EINVAL, "Failed to memset argv[0]");
75         if (pid == 0) {
76             isRet = strncpy_s(argv[0], argvSize, NWEBSPAWN_SERVER_NAME, strlen(NWEBSPAWN_SERVER_NAME)) != EOK;
77         } else {
78             isRet = strncpy_s(argv[0], argvSize, APPSPAWN_SERVER_NAME, strlen(APPSPAWN_SERVER_NAME)) != EOK;
79         }
80         APPSPAWN_CHECK(!isRet, return -EINVAL, "strncpy_s appspawn server name error: %{public}d", errno);
81     }
82 
83     if (pid == 0) {
84         APPSPAWN_LOGI("NwebSpawnCreateContent argc %{public}d mode %{public}d %{public}u", argc, mode, argvSize);
85         AppSpawnContent *content = AppSpawnCreateContent(NWEBSPAWN_SOCKET_NAME, argv[0], argvSize, mode);
86         APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for nwebspawn");
87         APPSPAWN_CHECK(content->initAppSpawn != NULL, return -1, "Invalid content for nwebspawn");
88         APPSPAWN_CHECK(content->runAppSpawn != NULL, return -1, "Invalid content for nwebspawn");
89 
90         // set common operation
91         content->loadExtendLib = LoadExtendLibNweb;
92         content->runChildProcessor = RunChildProcessorNweb;
93         content->initAppSpawn(content);
94         content->runAppSpawn(content, argc, argv);
95     } else {
96         APPSPAWN_LOGI("AppSpawnCreateContent argc %{public}d mode %{public}d %{public}u", argc, mode, argvSize);
97         AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, argv[0], argvSize, mode);
98         APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn");
99         APPSPAWN_CHECK(content->initAppSpawn != NULL, return -1, "Invalid content for appspawn");
100         APPSPAWN_CHECK(content->runAppSpawn != NULL, return -1, "Invalid content for appspawn");
101 
102         // set common operation
103         content->loadExtendLib = LoadExtendLib;
104         content->runChildProcessor = RunChildProcessor;
105         content->initAppSpawn(content);
106         if (mode == 0) {
107             AddNwebInfo(pid, NWEBSPAWN_SERVER_NAME);
108             SystemSetParameter("bootevent.appspawn.started", "true");
109         }
110         content->runAppSpawn(content, argc, argv);
111     }
112 
113     return 0;
114 }
115