1e41f4b71Sopenharmony_ci# appspawn Module<a name="EN-US_TOPIC_0000001063680582"></a>
2e41f4b71Sopenharmony_ci
3e41f4b71Sopenharmony_ci## Overview
4e41f4b71Sopenharmony_ci
5e41f4b71Sopenharmony_ci### Introduction
6e41f4b71Sopenharmony_ci
7e41f4b71Sopenharmony_ci  The appspawn module spawns application processes upon receiving commands from the application framework, configures permissions for new processes, and calls the entry function of the application framework.
8e41f4b71Sopenharmony_ci
9e41f4b71Sopenharmony_ci### Basic Concepts
10e41f4b71Sopenharmony_ci
11e41f4b71Sopenharmony_ci  **appspawn** is a registered service name. The appspawn process receives requests from the client by listening to messages over the local socket. The message type is an **AppParameter** structure. It is defined in **interfaces/innerkits/include/appspawn_msg.h**.
12e41f4b71Sopenharmony_ci
13e41f4b71Sopenharmony_ci  **Table 1**  Field description
14e41f4b71Sopenharmony_ci  | Field| Description|
15e41f4b71Sopenharmony_ci  | -------- | -------- |
16e41f4b71Sopenharmony_ci  | processName | Name of the service process to be started. The value contains a maximum of 256 bytes.|
17e41f4b71Sopenharmony_ci  | bundleName | Bundle name of the application to be started. The value contains a maximum of 256 bytes.|
18e41f4b71Sopenharmony_ci  | soPath | Path of the dynamic library specified by the application. The value contains a maximum of 256 bytes.|
19e41f4b71Sopenharmony_ci  | uid | UID of the application process to be started.|
20e41f4b71Sopenharmony_ci  | gid | GID of the application process to be started.|
21e41f4b71Sopenharmony_ci  | gidTable | Information about the application process group to be started. Its length is specified by **gidCount**. A maximum of 64 process groups are supported. The value must be a positive number.|
22e41f4b71Sopenharmony_ci  | gidCount | Number of application process groups to be started.|
23e41f4b71Sopenharmony_ci  | accessTokenId | Token ID for application process permission control.|
24e41f4b71Sopenharmony_ci  | apl | APL for application process permission control. The value contains a maximum of 32 bytes.|
25e41f4b71Sopenharmony_ci  | renderCmd | Image rendering command. The value contains a maximum of 1024 bytes.|
26e41f4b71Sopenharmony_ci  | flags | Cold start flag.|
27e41f4b71Sopenharmony_ci  | pid | PID of the rendering process, which is used to query the process exit status.|
28e41f4b71Sopenharmony_ci  | AppOperateType | Application operation type. The value **0** means to obtain the default status, and the value **1** means to obtain the rendering termination status.|
29e41f4b71Sopenharmony_ci
30e41f4b71Sopenharmony_ci### Constraints
31e41f4b71Sopenharmony_ciThe appspawn module is used only for the standard system.
32e41f4b71Sopenharmony_ci
33e41f4b71Sopenharmony_ci## Development Guidelines
34e41f4b71Sopenharmony_ci
35e41f4b71Sopenharmony_ci### Use Cases
36e41f4b71Sopenharmony_ci
37e41f4b71Sopenharmony_ci- Application security control based on SELinux tags
38e41f4b71Sopenharmony_ci
39e41f4b71Sopenharmony_ci   
40e41f4b71Sopenharmony_ci
41e41f4b71Sopenharmony_ci  Example code:
42e41f4b71Sopenharmony_ci    ```c++
43e41f4b71Sopenharmony_ci    AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client;
44e41f4b71Sopenharmony_ci    HapContext hapContext;
45e41f4b71Sopenharmony_ci    ret = hapContext.HapDomainSetcontext(appProperty->property.apl, appProperty->property.processName);
46e41f4b71Sopenharmony_ci    if (ret != 0) {
47e41f4b71Sopenharmony_ci        APPSPAWN_LOGE("AppSpawnServer::Failed to hap domain set context, errno = %d %s",
48e41f4b71Sopenharmony_ci            errno, appProperty->property.apl);
49e41f4b71Sopenharmony_ci    } else {
50e41f4b71Sopenharmony_ci        APPSPAWN_LOGI("AppSpawnServer::Success to hap domain set context, ret = %d", ret);
51e41f4b71Sopenharmony_ci    }
52e41f4b71Sopenharmony_ci    ```
53e41f4b71Sopenharmony_ci- Application process control
54e41f4b71Sopenharmony_ci
55e41f4b71Sopenharmony_ci  - Support for setting of AccessToken for applications
56e41f4b71Sopenharmony_ci  - Support for simultaneous stopping of all spawn application processes (after stopping of the appspawn process and before a restart)
57e41f4b71Sopenharmony_ci
58e41f4b71Sopenharmony_ci  Example code:
59e41f4b71Sopenharmony_ci    ```
60e41f4b71Sopenharmony_ci    AppSpawnClientExt *appProperty = (AppSpawnClientExt *)client;
61e41f4b71Sopenharmony_ci    int32_t ret = SetSelfTokenID(appProperty->property.accessTokenId);
62e41f4b71Sopenharmony_ci    APPSPAWN_LOGI("AppSpawnServer::set access token id = %d, ret = %d %d", appProperty->property.accessTokenId, ret, getuid());
63e41f4b71Sopenharmony_ci    ```
64e41f4b71Sopenharmony_ci
65e41f4b71Sopenharmony_ci- Support for cold start of applications by using the aa command
66e41f4b71Sopenharmony_ci
67e41f4b71Sopenharmony_ci   
68e41f4b71Sopenharmony_ci    ```
69e41f4b71Sopenharmony_ci    param set startup.appspawn.cold.boot 1 // Enable the cold start function.
70e41f4b71Sopenharmony_ci    aa start -d 12345 -a $name -b $package -C
71e41f4b71Sopenharmony_ci    Reference command:
72e41f4b71Sopenharmony_ci    aa start -d 12345 -a ohos.acts.startup.sysparam.function.MainAbility -b ohos.acts.startup.sysparam.function -C
73e41f4b71Sopenharmony_ci    ```
74e41f4b71Sopenharmony_ci
75e41f4b71Sopenharmony_ci- Application sandbox
76e41f4b71Sopenharmony_ci
77e41f4b71Sopenharmony_ci  Applications run independently in their own sandbox environments. In an application sandbox, only necessary libraries or files of applications are retained and data of different applications is isolated.
78e41f4b71Sopenharmony_ci
79e41f4b71Sopenharmony_ci### Available APIs
80e41f4b71Sopenharmony_ci
81e41f4b71Sopenharmony_ci  The API definitions are provided in **/base/startup/appspawn/interfaces/innerkits/include/client_socket.h**. Table 2 is a list of available APIs.
82e41f4b71Sopenharmony_ci
83e41f4b71Sopenharmony_ci  **Table 2**  API description
84e41f4b71Sopenharmony_ci  | API| Description|
85e41f4b71Sopenharmony_ci  | -------- | -------- |
86e41f4b71Sopenharmony_ci  | CreateClient | Creates a client.|
87e41f4b71Sopenharmony_ci  | CloseClient | Closes a client.|
88e41f4b71Sopenharmony_ci  | ConnectSocket | Sends a connection request to the appspawn service.|
89e41f4b71Sopenharmony_ci  | WriteSocketMessage | Sends a message to the appspawn service.|
90e41f4b71Sopenharmony_ci  | ReadSocketMessage | Receives a message from the appspawn service.|
91e41f4b71Sopenharmony_ci
92e41f4b71Sopenharmony_ci### How to Develop
93e41f4b71Sopenharmony_ci
94e41f4b71Sopenharmony_ci  Sandbox configuration description:
95e41f4b71Sopenharmony_ci
96e41f4b71Sopenharmony_ci  ```
97e41f4b71Sopenharmony_ci    {
98e41f4b71Sopenharmony_ci        "common" : [{                                           // Common mount options of the application sandbox
99e41f4b71Sopenharmony_ci            "top-sandbox-switch": "ON",                         // Application sandbox switch. The value ON means to enable the applicable sandbox, and the value OFF means the opposite.
100e41f4b71Sopenharmony_ci            "app-base" : [{
101e41f4b71Sopenharmony_ci                "sandbox-root" : "/mnt/sandbox/<PackageName>",  // Root path of the application sandbox
102e41f4b71Sopenharmony_ci                "mount-paths" : [{
103e41f4b71Sopenharmony_ci                        "src-path" : "/config",                 // Source mount path
104e41f4b71Sopenharmony_ci                        "sandbox-path" : "/config",             // Sandbox mount path
105e41f4b71Sopenharmony_ci                        "sandbox-flags" : [ "bind", "rec" ],    // Mount mode
106e41f4b71Sopenharmony_ci                        "check-action-status": "false"          // Whether to check the mount result. The value true means to check the mount result, and the value false means the opposite.
107e41f4b71Sopenharmony_ci                    }
108e41f4b71Sopenharmony_ci                ],
109e41f4b71Sopenharmony_ci                "symbol-links" : [{                             // Link path
110e41f4b71Sopenharmony_ci                        "target-name" : "/system/bin",          // Source link path
111e41f4b71Sopenharmony_ci                        "link-name" : "/bin",                   // Link name
112e41f4b71Sopenharmony_ci                        "check-action-status": "false"
113e41f4b71Sopenharmony_ci                    }
114e41f4b71Sopenharmony_ci                ]
115e41f4b71Sopenharmony_ci            }],
116e41f4b71Sopenharmony_ci        // Reference application-specific configuration
117e41f4b71Sopenharmony_ci        "individual" : [{                                        // Independent mount options of an application
118e41f4b71Sopenharmony_ci            "com.ohos.medialibrary.MediaLibraryDataA" : [{       // Application name
119e41f4b71Sopenharmony_ci                "sandbox-switch": "ON",                          // Application sandbox switch. The value ON means to enable the applicable sandbox, and the value OFF means the opposite.
120e41f4b71Sopenharmony_ci                "sandbox-root" : "/mnt/sandbox/<PackageName>",   // Root path of the application sandbox
121e41f4b71Sopenharmony_ci                "mount-paths" : [{
122e41f4b71Sopenharmony_ci                        "src-path" : "/storage/media/<currentUserId>",
123e41f4b71Sopenharmony_ci                        "sandbox-path" : "/storage/media",
124e41f4b71Sopenharmony_ci                        "sandbox-flags" : [ "bind", "rec" ],
125e41f4b71Sopenharmony_ci                        "check-action-status": "false"
126e41f4b71Sopenharmony_ci                    }
127e41f4b71Sopenharmony_ci                ],
128e41f4b71Sopenharmony_ci                "symbol-links" : []
129e41f4b71Sopenharmony_ci            }]
130e41f4b71Sopenharmony_ci        }]
131e41f4b71Sopenharmony_ci    }
132e41f4b71Sopenharmony_ci  ```
133e41f4b71Sopenharmony_ci
134e41f4b71Sopenharmony_ci   Modify configuration files by referring to the sandbox configuration description.
135e41f4b71Sopenharmony_ci
136e41f4b71Sopenharmony_ci   - On the device, go to **/system/etc/sandbox/**, modify the sandbox configuration files, and restart the device.
137e41f4b71Sopenharmony_ci   - In the code path, go to **base/startup/appspawn_standard**, and modify the sandbox configuration files.
138e41f4b71Sopenharmony_ci
139e41f4b71Sopenharmony_ci  **Table 3**  Description of sandbox configuration files
140e41f4b71Sopenharmony_ci
141e41f4b71Sopenharmony_ci  | Sandbox Configuration File| Description|
142e41f4b71Sopenharmony_ci  | -------- | -------- |
143e41f4b71Sopenharmony_ci  | appdata-sandbox64.json | Sandbox configuration for the 64-bit OS|
144e41f4b71Sopenharmony_ci  | appdata-sandbox.json | Sandbox configuration for the 32-bit OS|
145e41f4b71Sopenharmony_ci  | product-sandbox.json  | Product-specific configuration for the application sandbox|
146e41f4b71Sopenharmony_ci
147e41f4b71Sopenharmony_ci### Development Example
148e41f4b71Sopenharmony_ciThe following is the sample code for adding product-specific configuration for the launcher application:
149e41f4b71Sopenharmony_ci  ```c++
150e41f4b71Sopenharmony_ci  "com.ohos.launcher" : [{
151e41f4b71Sopenharmony_ci      "sandbox-switch": "ON",
152e41f4b71Sopenharmony_ci      "sandbox-root" : "/mnt/sandbox/<PackageName>",
153e41f4b71Sopenharmony_ci      "mount-paths" : [{
154e41f4b71Sopenharmony_ci              "src-path" : "/data/app/el1/bundle/public/",
155e41f4b71Sopenharmony_ci              "sandbox-path" : "/data/bundles/",
156e41f4b71Sopenharmony_ci              "sandbox-flags" : [ "bind", "rec" ],
157e41f4b71Sopenharmony_ci              "check-action-status": "true"
158e41f4b71Sopenharmony_ci          }
159e41f4b71Sopenharmony_ci      ],
160e41f4b71Sopenharmony_ci      "symbol-links" : []
161e41f4b71Sopenharmony_ci  }],
162e41f4b71Sopenharmony_ci  ```
163e41f4b71Sopenharmony_ci
164e41f4b71Sopenharmony_ci## FAQ
165e41f4b71Sopenharmony_ci
166e41f4b71Sopenharmony_ci### Cold Start of Applications Failed
167e41f4b71Sopenharmony_ci
168e41f4b71Sopenharmony_ci   **Symptom**
169e41f4b71Sopenharmony_ci   <br>Applications fail to be started by running the cold start command.
170e41f4b71Sopenharmony_ci
171e41f4b71Sopenharmony_ci   **Solution**
172e41f4b71Sopenharmony_ci    <br>&emsp;&emsp;1. Set **param set startup.appspawn.cold.boot 1** for cold start to take effect.
173e41f4b71Sopenharmony_ci    <br>&emsp;&emsp;2. Make sure that the cold start command is correct.
174