• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 
16 #include <cerrno>
17 #include <cstdlib>
18 #include <cstdio>
19 #include <cstdint>
20 #include <getopt.h>
21 #include <iostream>
22 #include <map>
23 #include <string>
24 #include <unistd.h>
25 #include <vector>
26 
27 #include "begetctl.h"
28 #include "control_fd.h"
29 #include "init_utils.h"
30 #include "sandbox.h"
31 #include "sandbox_namespace.h"
32 #include "string_ex.h"
33 
34 using namespace OHOS;
35 struct option g_options[] = {
36     { "service_name", required_argument, nullptr, 's' },
37     { "namespace_name", required_argument, nullptr, 'n' },
38     { "process_name", required_argument, nullptr, 'p' },
39     { "help", no_argument, nullptr, 'h' },
40     { nullptr, 0, nullptr, 0 },
41 };
42 
Usage()43 static void Usage()
44 {
45     std::cout << "sandbox -s | -n [-p] | -p | -h" << std::endl;
46     std::cout << "sandbox -s, --service_name=sandbox service \"enter service sandbox\"" << std::endl;
47     std::cout << "sandbox -n, --namespace_name=namespace name \"namespace name, system, chipset etc.\"" << std::endl;
48     std::cout << "sandbox -p, --process=process name \"sh, hdcd, hdf_devhost, etc.\"" << std::endl;
49     std::cout << "sandbox -h, --help \"Show help\"" << std::endl;
50 #ifndef STARTUP_INIT_TEST
51     exit(0);
52 #endif
53 }
54 
RunSandbox(const std::string & sandboxName)55 static void RunSandbox(const std::string &sandboxName)
56 {
57     InitDefaultNamespace();
58     if (!InitSandboxWithName(sandboxName.c_str())) {
59         std::cout << "Init sandbox failed." << std::endl;
60         return;
61     }
62 
63     DumpSandboxByName(sandboxName.c_str());
64     if (PrepareSandbox(sandboxName.c_str()) != 0) {
65         std::cout << "Prepare sandbox failed." << std::endl;
66         return;
67     }
68     EnterDefaultNamespace();
69     CloseDefaultNamespace();
70     EnterSandbox(sandboxName.c_str());
71     return;
72 }
73 
EnterShell()74 static void EnterShell()
75 {
76     char *argv[] = { const_cast<char *>("sh"), nullptr };
77     char *envp[] = { nullptr };
78     if (execve("/system/bin/sh", argv, envp) != 0) {
79         std::cout << "execve sh failed! err = "<< errno << std::endl;
80     }
81     return;
82 }
83 
84 static const int MAX_PROCESS_ARGC = 8;
EnterExec(const std::string & processName)85 static void EnterExec(const std::string &processName)
86 {
87     std::string tmpName = processName;
88     std::vector<std::string> vtr;
89     const std::string sep = " ";
90     OHOS::SplitStr(tmpName, sep, vtr, true, false);
91 
92     if ((vtr.size() > MAX_PROCESS_ARGC) || (vtr.size() == 0)) {
93         std::cout << "Service parameters is error." << std::endl;
94         return;
95     }
96     char *argv[MAX_PROCESS_ARGC] = {};
97     std::vector<std::string>::iterator it;
98     int i = 0;
99     for (it = vtr.begin(); it != vtr.end(); ++it) {
100         argv[i] = (char *)(*it).c_str();
101         std::cout << std::string(argv[i]) << std::endl;
102         i++;
103     }
104     argv[i] = NULL;
105     char *envp[] = { NULL };
106     if (execve(argv[0], argv, envp) != 0) {
107         std::cout << "execve:" << argv[0] << "failed! err = "<< errno << std::endl;
108     }
109     return;
110 }
111 
RunCmd(const std::string & serviceName,const std::string & namespaceName,const std::string & processName)112 static void RunCmd(const std::string &serviceName, const std::string &namespaceName, const std::string &processName)
113 {
114     if (!namespaceName.empty() && processName.empty() && serviceName.empty()) {
115         RunSandbox(namespaceName);
116         EnterShell();
117     } else if (!namespaceName.empty() && !processName.empty() && serviceName.empty()) {
118         RunSandbox(namespaceName);
119         EnterExec(processName);
120     } else if (namespaceName.empty() && !processName.empty() && serviceName.empty()) {
121         std::cout << "process name:" << processName << std::endl;
122         RunSandbox(std::string("system"));
123         EnterExec(processName);
124     } else if (namespaceName.empty() && processName.empty() && !serviceName.empty()) {
125         std::cout << "enter sandbox service name " << serviceName << std::endl;
126         CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_SANDBOX, serviceName.c_str());
127     } else {
128         Usage();
129     }
130 }
131 
main_cmd(BShellHandle shell,int argc,char ** argv)132 static int main_cmd(BShellHandle shell, int argc, char **argv)
133 {
134     int rc = -1;
135     int optIndex = -1;
136     std::string serviceName {};
137     std::string namespaceName {};
138     std::string processName {};
139     while ((rc = getopt_long(argc, argv, "s:n:p:h",  g_options, &optIndex)) != -1) {
140         switch (rc) {
141             case 0: {
142                 std::string optionName = g_options[optIndex].name;
143                 if (optionName == "service_name") {
144                     serviceName = optarg;
145                 } else if (optionName == "help") {
146                     Usage();
147                 } else if (optionName == "namespace_name") {
148                     namespaceName = optarg;
149                 } else if (optionName == "process_name") {
150                     processName = optarg;
151                 }
152                 break;
153             }
154             case 's':
155                 serviceName = optarg;
156                 break;
157             case 'h':
158                 Usage();
159                 break;
160             case 'n':
161                 namespaceName = optarg;
162                 break;
163             case 'p':
164                 std::cout << "process name:" << optarg << std::endl;
165                 processName = optarg;
166                 break;
167             case '?':
168                 std::cout << "Invalid argument\n";
169                 break;
170             default:
171                 std::cout << "Invalid argument\n";
172                 break;
173         }
174     }
175     RunCmd(serviceName, namespaceName, processName);
176     return 0;
177 }
178 
MODULE_CONSTRUCTOR(void)179 MODULE_CONSTRUCTOR(void)
180 {
181     const CmdInfo infos[] = {
182         {
183             const_cast<char *>("sandbox"), main_cmd, const_cast<char *>("enter service sandbox"),
184             const_cast<char *>("sandbox -s service_name"),
185             NULL
186         },
187         {
188             const_cast<char *>("sandbox"), main_cmd, const_cast<char *>("enter namespace, system, chipset etc."),
189             const_cast<char *>("sandbox -n namespace_name [-p]"),
190             NULL
191         },
192         {
193             const_cast<char *>("sandbox"), main_cmd, const_cast<char *>("enter namespace and exec process"),
194             const_cast<char *>("sandbox -p process_name"),
195             NULL
196         }
197     };
198     for (size_t i = 0; i < ARRAY_LENGTH(infos); i++) {
199         BShellEnvRegisterCmd(GetShellHandle(), &infos[i]);
200     }
201 }
202