# Service Management ## Overview ### Function Service configuration allows you to configure services on demand to create different value-added services. Currently, the following value-added services are supported: startup control, on-demand startup, command execution, scheduled startup, FD proxy, and [sandbox](subsys-boot-init-sandbox.md). ### Basic Concepts - init service parameter options For details, see [Description of service Fields](#table14737791471). - init service startup control (for standard system or higher) The init process classifies services into three types based on service configurations and starts the services in different phases. - **boot**: services that need to be preferentially started in the system. This type of services are started in the init phase. - **normal**: common services in the system. This type of services are started in the post-init phase. This is the default service type. - **condition**: services that are started by running the startup command. You can add the **start xxxx** command to **jobs** to start the corresponding service. - init service command execution (for standard system or higher) The init module provides the service command execution function, which allows services to execute different commands in different phases. - **on-start**: execution of commands after the service process is forked. The on-start job is executed in the subprocess of the service and affects only the subprocess. - **on-stop**: execution of commands in the init process when the service is stopped. - **on-restart**: execution of commands in the init process when the service is restarted. The following is an example of service configuration: ``` "services" : [{ "name" : "serviceName", "path" : ["/system/bin/serviceName"] "jobs" : { "on-boot" : "boot", "on-start" : "services:serviceName_start", "on-stop" : "services:serviceName_stop", "on-restart" : "services:serviceName_restart" } }, ``` With the parallel startup and command execution capabilities, processes can be started concurrently. - init service on-demand startup If on-demand startup is enabled, the init process starts a service only when it is required. The **ondemand** attribute is used to determine whether to enable on-demand startup for a service. The following is an example configuration of the **ondemand** attribute: ``` "services" : [{ "name" : "serviceName", "path" : ["/system/bin/serviceName"] "ondemand" : true }] ``` - SA process on-demand startup For details, see [samgr Usage](https://gitee.com/openharmony/distributedschedule_samgr/blob/master/README.md). - Socket process on-demand startup 1. The init process creates a socket for socket processes in the pre-fork phase and listens to read/write events on this socket. 2. If a read/write event is received over the socket, the init process starts the native service corresponding to the socket process, cancels event listening on the socket, and transfers the socket to the corresponding native service for management. 3. The native service automatically exits if no more packets need to be processed. When reclaiming subprocesses, the init process starts event listening over the socket again based on the service configuration. - Hot-swap service process on-demand startup The hot-swap service process can be started to process hot swap events based on system parameter changes. - Enhanced init process startup and recycling The CPU core binding, priority, MAC address, and AccessToken information of the service process can be configured in the configuration file during process startup. - Support of CPU core binding for service processes (through modification of the **\*.cfg** file) - Support of priority setting for service processes (through modification of the **\*.cfg** file) - Support of MAC address setting (that is, SELinux tag setting) for service processes (through modification of the **\*.cfg** file) - Support of AccessToken setting for service processes and distributed capability setting for system service processes (through modification of the **\*.cfg** file) - Support of the suppression mechanism for service processes (through modification of the **\*.cfg** file) The following is the example configuration for enhanced init process startup and recycling: ``` "services" : [{ "name" : "serviceName", "path" : ["/system/bin/serviceName"] "importance" : 1, // Priority setting for service processes "cpucore" : [0], // CPU binding for service processes "critical" : [1, 5, 10], // Suppression for service processes "apl" : "normal", // Ability privilege level setting for service processes "d-caps" : ["OHOS_DMS"], // Distributed capability setting for service processes "secon" : "u:r:distributedsche:s0" // SELinux tag setting for service processes. In this example, u:r:distributedsche:s0 is the SELinux tag. } ``` - init service FD proxy (for standard system or higher) FD proxy is an extended mechanism for on-demand startup. It can ensure that the FD state handle is not lost before the service process exits. Specifically, a service process sends the FD to the init process before it exits, and then reclaims the FD from the init process when it is started again. This mechanism is implemented using the API provided by the init process. Before a service process exits, it can call the related API to send the FD to the init process over the socket that supports IPC communication. After the service process is restarted, the init process returns the corresponding FD handle to it in the same way. For details about the APIs, see [FD Proxy APIs](#table14737791479). - Scheduled startup Scheduled startup means to start a service at the specified time. If the service has been started, it will not be started again. For details about the command, see [Description of begetctl Commands](subsys-boot-init-plugin.md#parameters). ``` timer_start servicename timeout ``` - Sandbox For details, see [Sandbox Management](subsys-boot-init-sandbox.md). ### Constraints The service management module is available only for the mini system and standard system. ## How to Develop ### Use Cases By parsing the ***.cfg** file, you can obtain **service** fields, and then set and start the service. ### Parameters **Table 1** Description of service fields
Name | Description | Remarks | Supported System Type |
---|---|---|---|
name | Name of the current service. (Mandatory) | Type: string. The value cannot be empty and can contain a maximum of 32 bytes. | Small and standard systems |
path | Full path (including parameters) of the executable file for the current service. This is an array. (Mandatory) |
The first element is the path of the executable file, and the maximum number of elements is 20. Each element is a string that contains a maximum of 64 bytes. |
Small and standard systems |
uid | User ID (UID) of the current service process. | Type: int or string. | Small and standard systems |
gid | Group ID (GID) of the current service process. | Type: int, int[], string, or string array. | Small and standard systems |
once | Whether the current service process is a one-off process. |
**1**: The current service process is a one-off process. If the process exits, the init process does not restart it. **0**: The current service process is not a one-off process. If the process exits, the init process restarts it upon receiving the SIGCHLD signal. |
Small and standard systems |
importance | Current service priority. |
Standard system: The service priority ranges from -20 to 19. A value beyond the range is invalid. Small system: The value **0** indicates an unimportant process and a value greater than **0** indicates an important process. |
Small and standard systems |
caps | Capabilities required by the current service. They are evaluated based on the capabilities supported by the security subsystem and configured in accordance with the principle of least permission. | Type: number or string array. If you set the value to a number, use the standard Linux capability. If you set the value to a string array, use the standard macro name. | Small and standard systems |
critical | Suppression mechanism for services. If the number of times a service is restarted exceeds the value N within the specified period T, the system will be restarted. |
Standard system: Type: int array, for example, **"critical": [M, N, T]**. - **M**: enable flag (**0**: disable; **1**: enable). - **N**: number of times the service is started. - **T**: period of time, in seconds. Both **M** and **N** are greater than **0**. Small and standard systems: Type: int, for example, **"critical": M**. M: enable flag (**0**: disable; **1**: enable). By default, **N** is **4** and **T** is **20**. |
Standard system |
cpucore | Number of CPU cores bound to the service. | Type: int array, for example, **"cpucore": [N1, N2, ...]**. **N1** and **N2** indicate the indices of the CPU cores to be bound. For a single-core device, **cpucore** is **0**. | Standard system |
d-caps | Distributed service capability (for standard system or higher) | Type: string array, for example, **"d-caps": ["OHOS_DMS"]**. | Standard system |
apl | Ability privilege level (for standard system or higher). | Type: string, for example, **"apl": "system_core"**. The value can be **system_core** (default), **normal**, or **system_basic**. | Standard system |
start-mode | Service startup mode (for standard system or higher). | Type: string, for example, **"start-mode": "condition"**. The value can be **boot**, **normal**, or **condition**. For details, see init Service Startup Control. | Standard system |
ondemand | Whether on-demand startup is enabled. | Type: bool, for example, **"ondemand": true**. This feature is available only for the small system running the Linux kernel. For details, see init Service On-Demand Startup. | Small and standard systems |
disable | Reserved. | None. | Small and standard systems |
sandbox | Whether the sandbox function is enabled. | **1** (default): Enable the sandbox function. **0**: Disable the sandbox function. | Standard system |