• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "appspawn_test_client.h"
17 
18 #include <signal.h>
19 #include <unistd.h>
20 
21 OHOS::AppSpawn::AppSpawnTestClient g_testClient;
22 
DoRun(const char * server,const AppParameter * request)23 static int DoRun(const char *server, const AppParameter *request)
24 {
25     int ret = g_testClient.ClientCreateSocket(server);
26     if (ret != 0) {
27         printf("Failed to connect server \n");
28         return 1;
29     }
30     ret = g_testClient.ClientSendMsg(reinterpret_cast<const uint8_t *>(request), sizeof(AppParameter));
31     if (ret != 0) {
32         printf("Failed to send msg to server \n");
33         g_testClient.ClientClose();
34         return 1;
35     }
36     pid_t pid = 0;
37     ret = g_testClient.ClientRecvMsg(pid);
38     if (ret != 0) {
39         printf("Failed spawn new app result %d \n", ret);
40         g_testClient.ClientClose();
41         quick_exit(0);
42         return 1;
43     }
44     if (pid > 0) {
45         printf("Success spawn new app pid %d \n", pid);
46     }
47     int index = 0;
48     while (index < 5) { // wait 5s
49         sleep(1);
50         index++;
51     }
52     if (pid > 0) {
53         APPSPAWN_LOGI("Success spawn new app pid %{public}d \n", pid);
54         kill(pid, SIGKILL);
55     }
56     // close client
57     g_testClient.ClientClose();
58     quick_exit(0);
59     return 0;
60 }
61 
main(int argc,char * const argv[])62 int main(int argc, char *const argv[])
63 {
64     int coldStart = 0;
65     int withSandbox = 0;
66     const char *cmd = "ls -l /data > /data/test.log";
67     const char *bundleName = "ohos.samples.test";
68     const char *server = "/dev/unix/socket/AppSpawn";
69     for (int32_t i = 0; i < argc; i++) {
70         if (strcmp(argv[i], "-c") == 0) {
71             coldStart = 1;
72         } else if (strcmp(argv[i], "-s") == 0) {
73             withSandbox = 1;
74         } else if (strcmp(argv[i], "--nwebspawn") == 0) {
75             server = "/dev/unix/socket/NWebSpawn";
76         } else if (strcmp(argv[i], "-b") == 0 && ((i + 1) < argc)) {
77             i++;
78             bundleName = argv[i];
79         } else if (strcmp(argv[i], "-C") == 0 && ((i + 1) < argc)) {
80             i++;
81             cmd = argv[i];
82         }
83     }
84 
85     if (coldStart) {
86         SetParameter("startup.appspawn.cold.boot", "1");
87         SetParameter("persist.appspawn.client.timeout", "10");
88     }
89 
90     AppParameter request = {};
91     memset_s((void *)(&request), sizeof(request), 0, sizeof(request));
92     g_testClient.ClientFillMsg(&request, bundleName, cmd);
93 
94     request.flags = coldStart ? APP_COLD_BOOT : 0;
95     request.flags |= !withSandbox ? APP_NO_SANDBOX : 0;
96     request.code = SPAWN_NATIVE_PROCESS;
97     return DoRun(server, &request);
98 }
99