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
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 int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
58 SetInitLogLevel(loglevel);
59 if ((argc > PARAM_INDEX) && (strcmp(argv[START_INDEX], "cold-start") == 0)) {
60 argvSize = APP_LEN_PROC_NAME;
61 mode = 1;
62 } else {
63 // calculate child process long name size
64 uintptr_t start = (uintptr_t)argv[0];
65 uintptr_t end = (uintptr_t)strchr(argv[argc - 1], 0);
66 if (end == 0) {
67 return -1;
68 }
69 argvSize = end - start;
70 if (argvSize > APP_MSG_MAX_SIZE) {
71 return -1;
72 }
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 isRet = strncpy_s(argv[0], argvSize, APPSPAWN_SERVER_NAME, strlen(APPSPAWN_SERVER_NAME)) != EOK;
76 APPSPAWN_CHECK(!isRet, return -EINVAL, "strncpy_s appspawn server name error: %d", errno);
77 }
78
79 APPSPAWN_LOGI("AppSpawnCreateContent argc %d mode %d %u", argc, mode, argvSize);
80 AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, argv[0], argvSize, mode);
81 APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn");
82 APPSPAWN_CHECK(content->initAppSpawn != NULL, return -1, "Invalid content for appspawn");
83 APPSPAWN_CHECK(content->runAppSpawn != NULL, return -1, "Invalid content for appspawn");
84
85 // set common operation
86 content->loadExtendLib = LoadExtendLib;
87 content->runChildProcessor = RunChildProcessor;
88 content->setUidGidFilter = SetUidGidFilter;
89 content->initAppSpawn(content);
90 if (mode == 0) {
91 SystemSetParameter("bootevent.appspawn.started", "true");
92 }
93 content->runAppSpawn(content, argc, argv);
94
95 return 0;
96 }
97