1e41f4b71Sopenharmony_ci# Sandbox Management
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci### Function Introduction
5e41f4b71Sopenharmony_ciOpenHarmony supports two types of sandbox, namely, system sandbox and chipset sandbox.
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci### Basic Concepts
8e41f4b71Sopenharmony_ciThe system sandbox and chipset sandbox are created in the init module. Native services choose to enter the system sandbox or chipset sandbox based on their functions. Sandbox components can be isolated through the **mount** attribute if **mount-bind-paths** or **mount-bind-files** is set for them in configuration files such as **system-sandbox.json** and **chipset-sandbox.json**. In addition, a sandbox debugging tool is provided to facilitate sandbox debugging, verification, and optimization. For details about commands, see [Description of begetctl Commands](subsys-boot-init-plugin.md#parameters).
9e41f4b71Sopenharmony_ci
10e41f4b71Sopenharmony_ci### Constraints
11e41f4b71Sopenharmony_ci
12e41f4b71Sopenharmony_ciThe sandbox management module is available only for the standard system.
13e41f4b71Sopenharmony_ci
14e41f4b71Sopenharmony_ci## How to Develop
15e41f4b71Sopenharmony_ci### Parameter Description
16e41f4b71Sopenharmony_ci  **Table 1** Parameters in the sandbox configuration file
17e41f4b71Sopenharmony_ci
18e41f4b71Sopenharmony_ci  | JSON Prefix| Description|
19e41f4b71Sopenharmony_ci  | ---------- | ---------- |
20e41f4b71Sopenharmony_ci  | sandbox-root | Root directory of the sandbox.|
21e41f4b71Sopenharmony_ci  | mount-bind-paths | Directory to mount.|
22e41f4b71Sopenharmony_ci  | mount-bind-files | File to mount.|
23e41f4b71Sopenharmony_ci  | src-path | Source path of the directory or file to mount.|
24e41f4b71Sopenharmony_ci  | sandbox-path | Target path in the sandbox.|
25e41f4b71Sopenharmony_ci  | sandbox-flags | Mount flag. The default value is **bind rec**.|
26e41f4b71Sopenharmony_ci  | ignore | Whether to ignore a mounting failure. If the value is set to **1**, the system ignores the mounting failure and proceeds with the subsequent step.|
27e41f4b71Sopenharmony_ci  | target-name | Directory to link.|
28e41f4b71Sopenharmony_ci  | link-name | Target link in the sandbox.|
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci  **Table 2** Description of sandbox configuration files
31e41f4b71Sopenharmony_ci  | Sandbox Configuration File| Description|
32e41f4b71Sopenharmony_ci  | -------- | -------- |
33e41f4b71Sopenharmony_ci  | chipset-sandbox64.json | Chipset sandbox configuration file for the 64-bit system|
34e41f4b71Sopenharmony_ci  | chipset-sandbox.json | Chipset sandbox configuration file for the 32-bit system|
35e41f4b71Sopenharmony_ci  | system-sandbox64.json  | System sandbox configuration file for the 64-bit system|
36e41f4b71Sopenharmony_ci  | system-sandbox.json  | System sandbox configuration file for the 32-bit system|
37e41f4b71Sopenharmony_ci
38e41f4b71Sopenharmony_ci### Available APIs
39e41f4b71Sopenharmony_ciLogical storage structure of the sandbox:
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci```c++
42e41f4b71Sopenharmony_ci// Main functions
43e41f4b71Sopenharmony_ci// name is "system" or "chipset"
44e41f4b71Sopenharmony_cibool InitSandboxWithName(const char *name); // Parsing to the JSON structure
45e41f4b71Sopenharmony_ci
46e41f4b71Sopenharmony_citypedef struct {
47e41f4b71Sopenharmony_ci    ListNode pathMountsHead;   // sandbox mount_path list head
48e41f4b71Sopenharmony_ci    ListNode fileMountsHead;   // sandbox mount_file list head
49e41f4b71Sopenharmony_ci    ListNode linksHead;        // sandbox symbolic link list head
50e41f4b71Sopenharmony_ci    char *rootPath;            // /mnt/sandbox/system|vendor|xxx
51e41f4b71Sopenharmony_ci    char name[MAX_BUFFER_LEN]; // name of sandbox. i.e system, chipset etc.
52e41f4b71Sopenharmony_ci    bool isCreated;            // sandbox already created or not
53e41f4b71Sopenharmony_ci    int ns;                    // namespace
54e41f4b71Sopenharmony_ci} sandbox_t;
55e41f4b71Sopenharmony_ci```
56e41f4b71Sopenharmony_ci### Procedure
57e41f4b71Sopenharmony_ci1. Create a sandbox.
58e41f4b71Sopenharmony_ci      - Create a system or chipset sandbox and configure the corresponding **system-sandbox.json** or **chipset-sandbox.json** file. For details about how to configure the JSON file, see [Sandbox JSON File Configuration](#sandbox).
59e41f4b71Sopenharmony_ci      - By default, the sandbox function of a service is enabled. If you do not want to move the service to the sandbox, set **sandbox** to **0** in the **.cfg** file. Otherwsie, set **sandbox** to **1**.
60e41f4b71Sopenharmony_ci        ```
61e41f4b71Sopenharmony_ci        "sandbox" : 1
62e41f4b71Sopenharmony_ci        ```
63e41f4b71Sopenharmony_ci
64e41f4b71Sopenharmony_ci2.  Modify the JSON file configuration of the sandbox.
65e41f4b71Sopenharmony_ci    - Go to the **/system/etc/sandbox/** directory, and run **cat system-sandbox.json** and **cat chipset-sandbox.json**. 
66e41f4b71Sopenharmony_ci     If you are using a 64-bit system, run **cat system-sandbox64.json** and **cat chipset-sandbox64.json** instead.
67e41f4b71Sopenharmony_ci    - Modify the sandbox configuration files in the **base/startup/init/interfaces/innerkits/sandbox** directory. After that, restart the system.
68e41f4b71Sopenharmony_ci
69e41f4b71Sopenharmony_ci### Development Example
70e41f4b71Sopenharmony_ciJSON file configuration of the sandbox:
71e41f4b71Sopenharmony_ci
72e41f4b71Sopenharmony_ci```json
73e41f4b71Sopenharmony_ci{
74e41f4b71Sopenharmony_ci    "sandbox-root" : "/mnt/sandbox/system",
75e41f4b71Sopenharmony_ci    "mount-bind-paths" : [{
76e41f4b71Sopenharmony_ci        "src-path" : "/system/lib/ndk",
77e41f4b71Sopenharmony_ci        "sandbox-path" : "/system/lib/ndk",
78e41f4b71Sopenharmony_ci        "sandbox-flags" : [ "bind", "rec", "private" ]
79e41f4b71Sopenharmony_ci    }],
80e41f4b71Sopenharmony_ci    "mount-bind-files" : [{
81e41f4b71Sopenharmony_ci        "src-path" : "/system/lib/ld-musl-aarch64.so.1",
82e41f4b71Sopenharmony_ci        "sandbox-path" : "/system/lib/ld-musl-aarch64.so.1",
83e41f4b71Sopenharmony_ci        "sandbox-flags" : [ "bind", "rec", "private" ]
84e41f4b71Sopenharmony_ci    }],
85e41f4b71Sopenharmony_ci    "symbol-links" : [{
86e41f4b71Sopenharmony_ci        "target-name" : "/vendor/lib",
87e41f4b71Sopenharmony_ci        "link-name" : "/lib"
88e41f4b71Sopenharmony_ci    }]
89e41f4b71Sopenharmony_ci}
90e41f4b71Sopenharmony_ci```
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci## FAQs
93e41f4b71Sopenharmony_ci- **Cause Analysis**
94e41f4b71Sopenharmony_ci     Related services cannot access required resource files such as **.so** file.
95e41f4b71Sopenharmony_ci- **Solution**
96e41f4b71Sopenharmony_ci    Check the hilog information, analyze the failure cause, enter the path of the corresponding **.so** file on the device, and modify the **BUILD.gn** file as appropriate. The operation procedure is as follows:
97e41f4b71Sopenharmony_ci
98e41f4b71Sopenharmony_ci    - Search for keyword **failed** or **.so** in hilog.
99e41f4b71Sopenharmony_ci
100e41f4b71Sopenharmony_ci      ```
101e41f4b71Sopenharmony_ci      08-05 17:27:29.302   488   488 E C02500/driver_loader_full: get driver entry failed, /vendor/lib/libcamera_host_service_1.0.z.so load fail, Error loading shared library libdisplay_buffer_proxy_1.0.z.so: No such file or directory (needed by /system/lib/chipset-pub-sdk/libdisplay_buffer_hdi_impl.z.so)
102e41f4b71Sopenharmony_ci      08-05 17:27:29.303   488   488 E C02500/devhost_service_full: DevHostServiceAddDevice failed and return -207
103e41f4b71Sopenharmony_ci      08-05 17:27:29.305   488   488 E C02500/devhost_service_stub: Dis patch failed, add service failed and ret is -207
104e41f4b71Sopenharmony_ci      08-05 17:27:29.307   488   488 I C02500/devhost_service_stub: add device 0x7000201
105e41f4b71Sopenharmony_ci      08-05 17:27:29.308   488   488 E C02500/driver_loader_full: /vendor/lib/libhdi_media_layer_service.z.so no valid, errno:2
106e41f4b71Sopenharmony_ci      ```
107e41f4b71Sopenharmony_ci
108e41f4b71Sopenharmony_ci    - As shown in the preceding search result, the camera error is caused by the **libdisplay_buffer_proxy_1.0.z.so** loading failure. To rectify the fault, you can mount the file in the sandbox for quick recovery, or modify the corresponding **BUILD.gn** file.
109e41f4b71Sopenharmony_ci
110e41f4b71Sopenharmony_ci      - Method 1: Mount the file in the sandbox for quick recovery. (Only local debugging is supported, and source code modification needs to be reviewed.) 
111e41f4b71Sopenharmony_ci
112e41f4b71Sopenharmony_ci        - System sandbox: Edit the **/system/etc/sandbox/system-sandbox.json** file on the device. By default, only some files in the **/vendor** directory are mounted. If an error is reported, mount the files separately.
113e41f4b71Sopenharmony_ci      
114e41f4b71Sopenharmony_ci        - Chipset sandbox: Edit the **/system/etc/sandbox/chipset-sandbox.json** file on the device. By default, only some files in the **/system** directory are mounted. If an error is reported, mount the files separately.
115e41f4b71Sopenharmony_ci        
116e41f4b71Sopenharmony_ci        - For the preceding case, add the following information to /system/etc/sandbox/chipset-sandbox.json:
117e41f4b71Sopenharmony_ci        
118e41f4b71Sopenharmony_ci        ```
119e41f4b71Sopenharmony_ci        "mount-bind-files" : [
120e41f4b71Sopenharmony_ci        	{
121e41f4b71Sopenharmony_ci                "src-path" : "/system/lib/libdisplay_buffer_proxy_1.0.z.so",
122e41f4b71Sopenharmony_ci                "sandbox-path" : "/system/lib/libdisplay_buffer_proxy_1.0.z.so",
123e41f4b71Sopenharmony_ci                "sandbox-flags" : [ "bind", "rec", "private" ]
124e41f4b71Sopenharmony_ci            },{...}
125e41f4b71Sopenharmony_ci        ],
126e41f4b71Sopenharmony_ci        ```
127e41f4b71Sopenharmony_ci        
128e41f4b71Sopenharmony_ci      - Method 2: Add **innerapi_tags** to the **BUILD.gn** file.
129e41f4b71Sopenharmony_ci      
130e41f4b71Sopenharmony_ci        ```
131e41f4b71Sopenharmony_ci        ohos_shared_library("xxx") {
132e41f4b71Sopenharmony_ci        	...
133e41f4b71Sopenharmony_ci        	innerapi_tags = [
134e41f4b71Sopenharmony_ci            	"chipsetsdk",
135e41f4b71Sopenharmony_ci            ]
136e41f4b71Sopenharmony_ci        }
137e41f4b71Sopenharmony_ci        ```
138e41f4b71Sopenharmony_ci        
139e41f4b71Sopenharmony_ci      - Description of **innerapi_tags**:
140e41f4b71Sopenharmony_ci      
141e41f4b71Sopenharmony_ci        -   Tags related to sandbox permissions include **passthrough**, **chipsetsdk**, **passthrough_indirect**, and **chipsetsdk_indirect**.
142e41f4b71Sopenharmony_ci          
143e41f4b71Sopenharmony_ci          -  You can view the **so** information on the OpenHarmony website. For indirectly dependent modules, use **chipsetsdk_indirect** or **passthrough_indirect**. For other modules, use **chipsetsdk** or **passthrough**.
144e41f4b71Sopenharmony_ci          -  For the **.so** files installed in the system directory, use **chipsetsdk** and **chipsetsdk_indirect** for access by chip components.
145e41f4b71Sopenharmony_ci          -  For the **.so** files installed in the chip directory, use **passthrough** and **passthrough_indirect** for access by system components.
146e41f4b71Sopenharmony_ci        -  You can add **innerapi_tags** to specify the installation path of **.so** files. For example, if **chipsetsdk** is set, **.so** files are installed in the **/lib/chipset-sdk/** directory. The source code is available at **build/templates/cxx/cxx.gni**.
147e41f4b71Sopenharmony_ci        ```gni
148e41f4b71Sopenharmony_ci        # auto set auto_relative_install_dir by innerapi_tags
149e41f4b71Sopenharmony_ci        if (defined(invoker.innerapi_tags)) {
150e41f4b71Sopenharmony_ci      	    is_chipsetsdk = false
151e41f4b71Sopenharmony_ci            is_platformsdk = false
152e41f4b71Sopenharmony_ci            is_passthrough = false
153e41f4b71Sopenharmony_ci            foreach(tag, filter_include(invoker.innerapi_tags, [ "chipsetsdk*" ])) {
154e41f4b71Sopenharmony_ci              is_chipsetsdk = true
155e41f4b71Sopenharmony_ci            }
156e41f4b71Sopenharmony_ci            foreach(tag, filter_include(invoker.innerapi_tags, [ "platformsdk*" ])) {
157e41f4b71Sopenharmony_ci              is_platformsdk = true
158e41f4b71Sopenharmony_ci            }
159e41f4b71Sopenharmony_ci            foreach(tag, filter_include(invoker.innerapi_tags, [ "passthrough*" ])) {
160e41f4b71Sopenharmony_ci              is_passthrough = true
161e41f4b71Sopenharmony_ci            }
162e41f4b71Sopenharmony_ci            if (is_chipsetsdk && is_platformsdk) {
163e41f4b71Sopenharmony_ci              auto_relative_install_dir = "chipset-pub-sdk"
164e41f4b71Sopenharmony_ci            } else if (is_chipsetsdk) {
165e41f4b71Sopenharmony_ci              auto_relative_install_dir = "chipset-sdk"
166e41f4b71Sopenharmony_ci            } else if (is_platformsdk) {
167e41f4b71Sopenharmony_ci              auto_relative_install_dir = "platformsdk"
168e41f4b71Sopenharmony_ci            }
169e41f4b71Sopenharmony_ci            if (is_passthrough) {
170e41f4b71Sopenharmony_ci              auto_relative_install_dir = chipset_passthrough_dir
171e41f4b71Sopenharmony_ci            }
172e41f4b71Sopenharmony_ci            ...
173e41f4b71Sopenharmony_ci        }
174