# appspawn Module for the Standard System<a name="EN-US_TOPIC_0000001063680582"></a>
## Overview<a name="section56901555910"></a>
After being started by the init process, the appspawn process waits for inter-process communication (IPC) messages. Upon receiving a message, the appspawn process starts an application service based on the message content, and grants the corresponding permission to the application service.
### Introduction<a name="section56901555911"></a>
- Security control
<br>Support for setting of SELinux tags for applications
- Application process control
- Support for setting of AccessToken for applications
- Support for simultaneous stopping of all spawn application processes (after stopping of the appspawn process and before a restart)
- Cold start
<br>Support for cold start of applications by using the **aa** command
```
param set appspawn.cold.boot true // Enable cold start.
aa start -d 12345 -a $name -b $package -C
Example:
aa start -d 12345 -a ohos.acts.startup.sysparam.function.MainAbility -b ohos.acts.startup.sysparam.function -C
**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 **AppProperty** structure. It is defined in **base/startup/appspawn_standard/interfaces/innerkits/include/sclient_socket.h**.
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p2650329183715"><a name="p2650329183715"></a><a name="p2650329183715"></a>Name of the service process to be started. The value contains a maximum of 256 bytes.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p2650329183715"><a name="p2650329183715"></a><a name="p2650329183715"></a>Bundle name of the application to be started. The value contains a maximum of 256 bytes.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p13650192963715"><a name="p13650192963715"></a><a name="p13650192963715"></a>Path of the dynamic library specified by the application. The value contains a maximum of 256 bytes.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p186503291371"><a name="p186503291371"></a><a name="p186503291371"></a>UID of the application process to be started. The value must be a positive number.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p187716587310"><a name="p187716587310"></a><a name="p187716587310"></a>GID of the application process to be started. The value must be a positive number.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p187716587310"><a name="p187716587310"></a><a name="p187716587310"></a>Information about the application process group to be started. Its length is specified by <strong>gidCount</strong>. A maximum of 64 process groups are supported. The value must be a positive number.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p187716587310"><a name="p187716587310"></a><a name="p187716587310"></a>Number of application process groups to be started.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p11650182953717"><a name="p11650182953717"></a><a name="p11650182953717"></a>Token ID for application process permission control.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p11650182953717"><a name="p11650182953717"></a><a name="p11650182953717"></a> APL for application process permission control. The value contains a maximum of 32 bytes.</p>
</td>
</tr>
</tbody>
</table>
## Development Guidelines<a name="section56901555913"></a>
The API definitions are provided in **base/startup/appspawn_standard/interfaces/innerkits/include/client_socket.h**. Table 2 is a list of available APIs.
### Available APIs<a name="section56901555914"></a>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p186503291371"><a name="p186503291371"></a><a name="p186503291371"></a>Sends a message to the appspawn service.</p>
<td class="cellrowborder" valign="top" width="60.51%" headers="mcps1.2.3.1.2 "><p id="p187716587310"><a name="p187716587310"></a><a name="p187716587310"></a>Receives a message from the appspawn service.</p>
</td>
</tr>
</tbody>
</table>
### Development Example<a name="section56901555915"></a>
<br>The following is an example of using related APIs:
After appspawn is started by init, its service name \(appspawn\) is registered with the IPC framework. Upon receiving inter-process messages, appspawn starts the application service based on the message parsing result, and grants the corresponding permission to the application service.
You can obtain the macro definition **APPSPAWN\_SERVICE\_NAME** corresponding to **appspawn** by including the header file **base\\startup\\appspawn\_lite\\services\\include\\appspawn\_service.h**. Due to some constraints on the security subsystem, only Ability Manager Service \(AMS\) has the permission to send inter-process messages to appspawn.
The messages are in JSON format, as shown in the following:
<tdclass="cellrowborder"valign="top"width="60.51%"headers="mcps1.2.3.1.2 "><pid="p2650329183715"><aname="p2650329183715"></a><aname="p2650329183715"></a>Name of the application service process to be started. The length ranges from 7 bytes to 127 bytes.</p>
<tdclass="cellrowborder"valign="top"width="60.51%"headers="mcps1.2.3.1.2 "><pid="p13650192963715"><aname="p13650192963715"></a><aname="p13650192963715"></a>Identifier generated by the AMS for the new process. The length ranges from 1 byte to 24 bytes. The appspawn process passes the value to the new process.</p>
<tdclass="cellrowborder"valign="top"width="60.51%"headers="mcps1.2.3.1.2 "><pid="p186503291371"><aname="p186503291371"></a><aname="p186503291371"></a>UID of the application service process to be started. The value must be a positive number.</p>
<tdclass="cellrowborder"valign="top"width="60.51%"headers="mcps1.2.3.1.2 "><pid="p187716587310"><aname="p187716587310"></a><aname="p187716587310"></a>GID of the application service process to be started. The value must be a positive number.</p>
<tdclass="cellrowborder"valign="top"width="60.51%"headers="mcps1.2.3.1.2 "><pid="p11650182953717"><aname="p11650182953717"></a><aname="p11650182953717"></a>Capability permissions required by the application service process to be started. A maximum of 10 capability permissions are allowed.</p>
</td>
</tr>
</tbody>
</table>
## Overview
### Introduction
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.
### Basic Concepts
**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**.
**Table 1** Field description
| Field| Description|
| -------- | -------- |
| processName | Name of the service process to be started. The value contains a maximum of 256 bytes.|
| bundleName | Bundle name of the application to be started. The value contains a maximum of 256 bytes.|
| soPath | Path of the dynamic library specified by the application. The value contains a maximum of 256 bytes.|
| uid | UID of the application process to be started.|
| gid | GID of the application process to be started.|
| 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.|
| gidCount | Number of application process groups to be started.|
| accessTokenId | Token ID for application process permission control.|
| apl | APL for application process permission control. The value contains a maximum of 32 bytes.|
| renderCmd | Image rendering command. The value contains a maximum of 1024 bytes.|
| flags | Cold start flag.|
| pid | PID of the rendering process, which is used to query the process exit status.|
| AppOperateType | Application operation type. The value **0** means to obtain the default status, and the value **1** means to obtain the rendering termination status.|
### Constraints
The appspawn module is used only for the standard system.
## Development Guidelines
### Use Cases
- Application security control based on SELinux tags
int32_t ret = SetSelfTokenID(appProperty->property.accessTokenId);
APPSPAWN_LOGI("AppSpawnServer::set access token id = %d, ret = %d %d", appProperty->property.accessTokenId, ret, getuid());
```
- Support for cold start of applications by using the aa command
```
param set appspawn.cold.boot true // Enable cold start.
aa start -d 12345 -a $name -b $package -C
Reference command:
aa start -d 12345 -a ohos.acts.startup.sysparam.function.MainAbility -b ohos.acts.startup.sysparam.function -C
```
- Application sandbox
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.
### Available APIs
The API definitions are provided in **/interfaces/innerkits/include/client_socket.h**. Table 2 is a list of available APIs.
**Table 2** API description
| API| Description|
| -------- | -------- |
| CreateClient | Creates a client.|
| CloseClient | Closes a client.|
| ConnectSocket | Sends a connection request to the appspawn service.|
| WriteSocketMessage | Sends a message to the appspawn service.|
| ReadSocketMessage | Receives a message from the appspawn service.|
### How to Develop
Sandbox configuration description:
```
{
"common" : [{ // Common mount options of the application sandbox
"top-sandbox-switch": "ON", // Application sandbox switch. The value ON means to enable the applicable sandbox, and the value OFF means the opposite.
"app-base" : [{
"sandbox-root" : "/mnt/sandbox/<PackageName>", // Root path of the application sandbox
"mount-paths" : [{
"src-path" : "/config", // Source mount path
"sandbox-path" : "/config", // Sandbox mount path
"sandbox-flags" : [ "bind", "rec" ], // Mount mode
"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.
}
],
"symbol-links" : [{ // Link path
"target-name" : "/system/bin", // Source link path
"link-name" : "/bin", // Link name
"check-action-status": "false"
}
]
}],
// Reference application-specific configuration
"individual" : [{ // Independent mount options of an application
"com.ohos.medialibrary.MediaLibraryDataA" : [{ // Application name
"sandbox-switch": "ON", // Application sandbox switch. The value ON means to enable the applicable sandbox, and the value OFF means the opposite.
"sandbox-root" : "/mnt/sandbox/<PackageName>", // Root path of the application sandbox
"mount-paths" : [{
"src-path" : "/storage/media/<currentUserId>",
"sandbox-path" : "/storage/media",
"sandbox-flags" : [ "bind", "rec" ],
"check-action-status": "false"
}
],
"symbol-links" : []
}]
}]
}
```
Modify configuration files by referring to the sandbox configuration description.
- On the device, go to **/system/etc/sandbox/**, modify the sandbox configuration files, and restart the device.
- In the code path, go to **base/startup/appspawn_standard**, and modify the sandbox configuration files.
**Table 3** Description of sandbox configuration files
| Sandbox Configuration File| Description|
| -------- | -------- |
| appdata-sandbox64.json | Sandbox configuration for the 64-bit OS|
| appdata-sandbox.json | Sandbox configuration for the 32-bit OS|
| product-sandbox.json | Product-specific configuration for the application sandbox|
### Development Example
The following is the sample code for adding product-specific configuration for the launcher application:
```c++
"com.ohos.launcher":[{
"sandbox-switch":"ON",
"sandbox-root":"/mnt/sandbox/<PackageName>",
"mount-paths":[{
"src-path":"/data/app/el1/bundle/public/",
"sandbox-path":"/data/bundles/",
"sandbox-flags":["bind","rec"],
"check-action-status":"true"
}
],
"symbol-links":[]
}],
```
## FAQ
### Cold Start of Applications Failed
**Symptom**
<br>Applications fail to be started by running the cold start command.
**Solution**
<br>1. Enable cold start by setting **param set appspawn.cold.boot true**.
<br>2. Make sure that the cold start command is correct.