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 "appspawn_adapter.h"
17 #include "appspawn_msg.h"
18 #include "appspawn_server.h"
19 #include "appspawn_service.h"
20 #include "securec.h"
21 #include "init_param.h"
22 #include "syspara/parameter.h"
23
main(int argc,char * const argv[])24 int main(int argc, char *const argv[])
25 {
26 if (argc <= 0) {
27 return 0;
28 }
29
30 (void)signal(SIGPIPE, SIG_IGN);
31 uint32_t argvSize = 0;
32 int mode = 0;
33 int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
34 SetInitLogLevel(loglevel);
35 if ((argc > PARAM_INDEX) && (strcmp(argv[START_INDEX], "cold-start") == 0)) {
36 argvSize = APP_LEN_PROC_NAME;
37 mode = 1;
38 } else {
39 // calculate child process long name size
40 uintptr_t start = (uintptr_t)argv[0];
41 uintptr_t end = (uintptr_t)strchr(argv[argc - 1], 0);
42 if (end == 0) {
43 return -1;
44 }
45 argvSize = end - start;
46 if (argvSize > APP_MSG_MAX_SIZE) {
47 return -1;
48 }
49 int isRet = memset_s(argv[0], argvSize, 0, (size_t)argvSize) != EOK;
50 APPSPAWN_CHECK(!isRet, return -EINVAL, "Failed to memset argv[0]");
51 isRet = strncpy_s(argv[0], argvSize, APPSPAWN_SERVER_NAME, strlen(APPSPAWN_SERVER_NAME)) != EOK;
52 APPSPAWN_CHECK(!isRet, return -EINVAL, "strncpy_s appspawn server name error: %d", errno);
53 }
54
55 APPSPAWN_LOGI("AppSpawnCreateContent argc %d mode %d %u", argc, mode, argvSize);
56 AppSpawnContent *content = AppSpawnCreateContent(APPSPAWN_SOCKET_NAME, argv[0], argvSize, mode);
57 APPSPAWN_CHECK(content != NULL, return -1, "Invalid content for appspawn");
58 APPSPAWN_CHECK(content->initAppSpawn != NULL, return -1, "Invalid content for appspawn");
59 APPSPAWN_CHECK(content->runAppSpawn != NULL, return -1, "Invalid content for appspawn");
60
61 // set common operation
62 content->loadExtendLib = LoadExtendLib;
63 content->runChildProcessor = RunChildProcessor;
64 content->setUidGidFilter = SetUidGidFilter;
65 content->initAppSpawn(content);
66 if (mode == 0) {
67 SystemSetParameter("bootevent.appspawn.started", "true");
68 }
69 content->runAppSpawn(content, argc, argv);
70
71 _Exit(0);
72 }
73