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 <errno.h>
17 #include <stdio.h>
18 #include <string.h>
19 #include <unistd.h>
20 #include "begetctl.h"
21 #include "init_utils.h"
22 #include "control_fd.h"
23 #ifdef ENABLE_ENTER_APPSPAWN_SANDBOX
24 #include "appspawn.h"
25 #endif
26
27 #define APPSPAWN_CMD_NUMBER 1
28
SendAppspawnTimeMessage(const CmdAgent * agent,uint16_t type,const char * ptyName)29 static int SendAppspawnTimeMessage(const CmdAgent *agent, uint16_t type, const char *ptyName)
30 {
31 #ifdef ENABLE_ENTER_APPSPAWN_SANDBOX
32 if ((agent == NULL) || (ptyName == NULL)) {
33 BEGET_LOGE("Invalid parameter");
34 return -1;
35 }
36
37 AppSpawnClientHandle clientHandle;
38 int ret = AppSpawnClientInit("Appspawn", &clientHandle);
39 BEGET_ERROR_CHECK(ret == 0, return -1, "AppSpawnClientInit error, errno = %d", errno);
40 AppSpawnReqMsgHandle reqHandle;
41 ret = AppSpawnReqMsgCreate(MSG_BEGET_SPAWNTIME, "init", &reqHandle);
42 if (ret != 0) {
43 AppSpawnClientDestroy(clientHandle);
44 return -1;
45 }
46 AppSpawnResult result = {};
47 ret = AppSpawnClientSendMsg(clientHandle, reqHandle, &result);
48 if (ret != 0) {
49 AppSpawnClientDestroy(clientHandle);
50 return -1;
51 }
52 int minAppspawnTime = result.result;
53 int maxAppspawnTime = result.pid;
54 printf("minAppspawnTime: %d, maxAppspawnTime: %d \n", minAppspawnTime, maxAppspawnTime);
55 AppSpawnClientDestroy(clientHandle);
56 return 0;
57 #endif
58 return -1;
59 }
60
main_cmd(BShellHandle shell,int argc,char * argv[])61 static int main_cmd(BShellHandle shell, int argc, char* argv[])
62 {
63 if (argc != APPSPAWN_CMD_NUMBER) {
64 BShellCmdHelp(shell, argc, argv);
65 return 0;
66 }
67 CmdAgent agent;
68 int ret = InitPtyInterface(&agent, ACTION_APP_SPAWNTIME, "init", (CallbackSendMsgProcess)SendAppspawnTimeMessage);
69 if (ret != 0) {
70 BEGET_LOGE("initptyinterface failed");
71 return -1;
72 }
73 return 0;
74 }
75
MODULE_CONSTRUCTOR(void)76 MODULE_CONSTRUCTOR(void)
77 {
78 CmdInfo infos[] = {
79 {"appspawn_time", main_cmd, "get appspawn time", "appspawn_time", ""},
80 };
81 for (size_t i = 0; i < sizeof(infos) / sizeof(infos[0]); i++) {
82 BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
83 }
84 }
85
86