1e41f4b71Sopenharmony_ci# Service Management 2e41f4b71Sopenharmony_ci## Overview 3e41f4b71Sopenharmony_ci 4e41f4b71Sopenharmony_ciService management is implemented by using the init process to parse the service process configuration files (**xxx.cfg** files), which are used to configure services based on service scenarios. Currently, the supported value-added services include startup control, on-demand startup, command execution, scheduled startup, FD proxy, and sandbox management. 5e41f4b71Sopenharmony_ci 6e41f4b71Sopenharmony_ci## Description of Service Process Configuration Files 7e41f4b71Sopenharmony_ci 8e41f4b71Sopenharmony_ci- Naming: 9e41f4b71Sopenharmony_ci 10e41f4b71Sopenharmony_ci Generally, the configuration file is named in the format of **serviceName.cfg**, where **serviceName** is the service process name, for example, **appspawn.cfg**. 11e41f4b71Sopenharmony_ci 12e41f4b71Sopenharmony_ci- Format: 13e41f4b71Sopenharmony_ci 14e41f4b71Sopenharmony_ci The configuration file is based on the JSON format and complies with the basic rules of the JSON file. 15e41f4b71Sopenharmony_ci 16e41f4b71Sopenharmony_ci- Content: 17e41f4b71Sopenharmony_ci 18e41f4b71Sopenharmony_ci The configuration file contains the following fields. Except mandatory fields, other fields can be configured based on service features. 19e41f4b71Sopenharmony_ci 20e41f4b71Sopenharmony_ci Table 1 Service configuration fields 21e41f4b71Sopenharmony_ci 22e41f4b71Sopenharmony_ci | Field| Configuration|Supported System Type| Description| Remarks| 23e41f4b71Sopenharmony_ci | ---------- | ------- | -------- | --------| --------| 24e41f4b71Sopenharmony_ci | name | Mandatory| Small and standard systems | Name of the current service. (Mandatory)| Type: string. The value cannot be empty and can contain a maximum of 32 bytes.| 25e41f4b71Sopenharmony_ci | path | Mandatory| Small and standard systems| 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.<br> Each element is a string that contains a maximum of 64 bytes.| 26e41f4b71Sopenharmony_ci | uid | Mandatory| Small and standard systems| User ID (UID) of the current service process.| Type: int or string.| 27e41f4b71Sopenharmony_ci | gid | Mandatory| Small and standard systems| Group ID (GID) of the current service process.| Type: int, int[], string, or string array.| 28e41f4b71Sopenharmony_ci | once | Optional| Small and standard systems| 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.<br>**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.| 29e41f4b71Sopenharmony_ci | importance | Optional| Small and standard systems| <br>Standard system: service priority<br>Small system: service importance| <br>Standard system: The service priority ranges from **-20** to **19**. A smaller value indicates a higher priority. A value beyond the range is invalid.<br>Small system: The value **0** indicates an unimportant process and a value greater than **0** indicates an important process.| 30e41f4b71Sopenharmony_ci | caps | Optional| Small and standard systems| 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.| 31e41f4b71Sopenharmony_ci | critical | Optional| Small and standard systems| 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.| <br> Type: int[], for example, "critical": [M, N, T].<br>- **M**: enable flag (**0**: disable; **1**: enable).<br>- **N**: number of times the service is started.<br>- **T**: period of time, in seconds.<br> By default, **N** is **4** and **T** is **240**.| 32e41f4b71Sopenharmony_ci | cpucore | Optional| Standard system| 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**.| 33e41f4b71Sopenharmony_ci | d-caps | Optional| Standard system| Distributed service capability.| Type: string array, for example, **"d-caps": ["OHOS_DMS"]**.| 34e41f4b71Sopenharmony_ci | apl | Optional| Standard system| Ability privilege level.| Type: string, for example, **"apl": "system_core"**.<br> The value can be **system_core** (default), **normal**, or **system_basic**.| 35e41f4b71Sopenharmony_ci | start-mode | Optional| Standard system| Service startup mode.| Type: string, for example, **start-mode: condition**.<br>The value can be **boot**, **normal**, or **condition**. For details, see init Service Startup Control.| 36e41f4b71Sopenharmony_ci | ondemand | Optional| Small and standard systems| Whether on-demand startup is enabled.| Type: bool, for example, **ondemand: true**. For small systems, this feature is available only on the Linux kernel.<br>For details, see [init Service On-Demand Startup](#section56901555920).| 37e41f4b71Sopenharmony_ci | disable | Optional| Small and standard systems| Reserved.| None.| 38e41f4b71Sopenharmony_ci | sandbox | Optional| Standard system| Whether the sandbox function is enabled.| **1** (default): Enable the sandbox function.<br>**0**: Disable the sandbox function.| 39e41f4b71Sopenharmony_ci | socket | Optional| Standard system| Socket attribute configuration.| This field is required for services that uses a socket connection.| 40e41f4b71Sopenharmony_ci | env | Optional| Standard system| Environment variable configuration.| Type: key-value pair array.<br>Multiple environment variables can be configured. For example:<br>"env" : [{<br> "name" : "SERVICE_NALE", <br>"value" : "ueventd"},{<br> "name" : "TEST",<br> "value" : "test_value" <br>}]| 41e41f4b71Sopenharmony_ci | period | Optional| Standard system| Scheduled startup upon service exit.| Type: int, for example, 60. This field specifies the interval for starting a scheduled task, in seconds.<br>If this function is enabled, a timer is started to periodically start the service upon service exit. For example, **"period": 60** indicates that the service is started 60 seconds upon service exit.| 42e41f4b71Sopenharmony_ci 43e41f4b71Sopenharmony_ci **Table 2** Description of socket fields 44e41f4b71Sopenharmony_ci | Field| Description| 45e41f4b71Sopenharmony_ci | -------- | -------- | 46e41f4b71Sopenharmony_ci | name | Name of the socket. It does not need to be the same as the service name. The value must not be empty and can contain a maximum of 32 bytes.| 47e41f4b71Sopenharmony_ci | family | Address family to which the socket belongs. Currently, the AF_UNIX and AF_NETLINK families are supported.| 48e41f4b71Sopenharmony_ci | type | Socket type. Currently, connection-based sockets, SOCK_SEQPACKET and SOCK_STREAM, and UDP-based connectionless socket, SOCK_DGRAM, are supported.| 49e41f4b71Sopenharmony_ci | protocol | Protocol used for socket communication. Unless otherwise required, set the value to **default** so that the socket automatically selects a proper protocol based on the socket address family and socket type. In addition to the default protocol, the NETLINK_KOBJECT_UEVENT protocol is also supported.| 50e41f4b71Sopenharmony_ci | permissions | Permissions of the socket node file. This field is valid only for sockets that have entity node files, such as the AF_UNIX address family.| 51e41f4b71Sopenharmony_ci | uid | User ID of the socket node file. This field is valid only for sockets that have entity node files, such as the AF_UNIX address family.| 52e41f4b71Sopenharmony_ci | gid | Group ID of the socket node file. This field is valid only for sockets that have entity node files, such as the AF_UNIX address family.| 53e41f4b71Sopenharmony_ci | option | Socket option. This field is passed when **setsockopt** is called. Currently, the available options include **SOCKET_OPTION_PASSCRED**, **SOCKET_OPTION_RCVBUFFORCE**, **SOCK_CLOEXEC**, and **SOCK_NONBLOCK**.| 54e41f4b71Sopenharmony_ci 55e41f4b71Sopenharmony_ci - init service startup control (for standard system or higher)<a name = "section56901555918"> 56e41f4b71Sopenharmony_ci 57e41f4b71Sopenharmony_ci The init process classifies services into three types based on service configurations and starts the services in different phases. 58e41f4b71Sopenharmony_ci 59e41f4b71Sopenharmony_ci - **boot**: services that need to be preferentially started in the system. This type of services are started in the init phase. 60e41f4b71Sopenharmony_ci - **normal**: common services in the system. This type of services are started in the post-init phase. This is the default service type. 61e41f4b71Sopenharmony_ci - **condition**: services that are started by running the startup command. You can add the **start xxxx** command to **jobs** to start the corresponding service. 62e41f4b71Sopenharmony_ci 63e41f4b71Sopenharmony_ci - init service command execution (for standard system or higher)<a name="section56901555919"></a> 64e41f4b71Sopenharmony_ci 65e41f4b71Sopenharmony_ci The init module provides the service command execution function, which allows services to execute different commands in different phases. 66e41f4b71Sopenharmony_ci 67e41f4b71Sopenharmony_ci - **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. 68e41f4b71Sopenharmony_ci - **on-stop**: execution of commands in the init process when the service is stopped. 69e41f4b71Sopenharmony_ci - **on-restart**: execution of commands in the init process when the service is restarted. 70e41f4b71Sopenharmony_ci 71e41f4b71Sopenharmony_ci Configure the parameters as follows: 72e41f4b71Sopenharmony_ci ``` 73e41f4b71Sopenharmony_ci "services" : [{ 74e41f4b71Sopenharmony_ci "name" : "serviceName", 75e41f4b71Sopenharmony_ci "path" : ["/system/bin/serviceName"] 76e41f4b71Sopenharmony_ci "jobs" : { 77e41f4b71Sopenharmony_ci "on-start" : "services:serviceName_start", 78e41f4b71Sopenharmony_ci "on-stop" : "services:serviceName_stop", 79e41f4b71Sopenharmony_ci "on-restart" : "services:serviceName_restart" 80e41f4b71Sopenharmony_ci } 81e41f4b71Sopenharmony_ci }, 82e41f4b71Sopenharmony_ci ``` 83e41f4b71Sopenharmony_ci 84e41f4b71Sopenharmony_ci With the parallel startup and command execution capabilities, processes can be started concurrently. 85e41f4b71Sopenharmony_ci 86e41f4b71Sopenharmony_ci - init service on-demand startup<a name="section56901555920"> 87e41f4b71Sopenharmony_ci 88e41f4b71Sopenharmony_ci 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. 89e41f4b71Sopenharmony_ci 90e41f4b71Sopenharmony_ci The following is an example configuration of the **ondemand** attribute: 91e41f4b71Sopenharmony_ci ``` 92e41f4b71Sopenharmony_ci "services" : [{ 93e41f4b71Sopenharmony_ci "name" : "serviceName", 94e41f4b71Sopenharmony_ci "path" : ["/system/bin/serviceName"] 95e41f4b71Sopenharmony_ci "ondemand" : true 96e41f4b71Sopenharmony_ci }] 97e41f4b71Sopenharmony_ci ``` 98e41f4b71Sopenharmony_ci - SA process on-demand startup 99e41f4b71Sopenharmony_ci 100e41f4b71Sopenharmony_ci For details, see [samgr Usage](https://gitee.com/openharmony/systemabilitymgr_samgr/blob/master/README.md). 101e41f4b71Sopenharmony_ci 102e41f4b71Sopenharmony_ci - Socket process on-demand startup 103e41f4b71Sopenharmony_ci 1. The init process creates a socket for socket processes in the pre-fork phase and listens to read/write events on this socket. 104e41f4b71Sopenharmony_ci 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. 105e41f4b71Sopenharmony_ci 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. 106e41f4b71Sopenharmony_ci 107e41f4b71Sopenharmony_ci - Hot-swap service process on-demand startup 108e41f4b71Sopenharmony_ci 109e41f4b71Sopenharmony_ci The hot-swap service process can be started to process hot swap events based on system parameter changes. 110e41f4b71Sopenharmony_ci 111e41f4b71Sopenharmony_ci - Enhanced init process startup and recycling 112e41f4b71Sopenharmony_ci 113e41f4b71Sopenharmony_ci The CPU core binding, priority, MAC address, and AccessToken information of the service process can be configured in the configuration file during process startup. 114e41f4b71Sopenharmony_ci 115e41f4b71Sopenharmony_ci - Support of CPU core binding for service processes (through modification of the **.cfg** file) 116e41f4b71Sopenharmony_ci - Support of priority setting for service processes (through modification of the **.cfg** file) 117e41f4b71Sopenharmony_ci - Support of MAC address setting (that is, SELinux tag setting) for service processes (through modification of the **.cfg** file) 118e41f4b71Sopenharmony_ci - Support of AccessToken setting for service processes and distributed capability setting for system service processes (through modification of the **.cfg** file) 119e41f4b71Sopenharmony_ci - Support of the suppression mechanism for service processes (through modification of the **.cfg** file) 120e41f4b71Sopenharmony_ci 121e41f4b71Sopenharmony_ci The following is the example configuration for enhanced init process startup and recycling: 122e41f4b71Sopenharmony_ci ``` 123e41f4b71Sopenharmony_ci "services" : [{ 124e41f4b71Sopenharmony_ci "name" : "serviceName", 125e41f4b71Sopenharmony_ci "path" : ["/system/bin/serviceName"] 126e41f4b71Sopenharmony_ci "importance" : 1, // Priority for service processes 127e41f4b71Sopenharmony_ci "cpucore" : [0], // CPU binding for service processes 128e41f4b71Sopenharmony_ci "critical" : [1, 5, 10], // Suppression for service processes 129e41f4b71Sopenharmony_ci "apl" : "normal", // Ability privilege level for service processes 130e41f4b71Sopenharmony_ci "d-caps" : ["OHOS_DMS"], // Distributed capabilities for service processes 131e41f4b71Sopenharmony_ci "secon" : "u:r:distributedsche:s0" // SELinux tag for service processes. In this example, the SELinux tag is u:r:distributedsche:s0. 132e41f4b71Sopenharmony_ci } 133e41f4b71Sopenharmony_ci ``` 134e41f4b71Sopenharmony_ci 135e41f4b71Sopenharmony_ci - Scheduled startup 136e41f4b71Sopenharmony_ci 137e41f4b71Sopenharmony_ci 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). 138e41f4b71Sopenharmony_ci ``` 139e41f4b71Sopenharmony_ci timer_start servicename timeout 140e41f4b71Sopenharmony_ci ``` 141e41f4b71Sopenharmony_ci## Basic Process of Creating a Service Process 142e41f4b71Sopenharmony_ci 143e41f4b71Sopenharmony_ci### Configuring the Service Process Configuration File 144e41f4b71Sopenharmony_ci 145e41f4b71Sopenharmony_ci After finishing the service process code, create the **serviceName.cfg** file (**serviceName** indicates the service process name) and configure the file content based on service features. 146e41f4b71Sopenharmony_ci 147e41f4b71Sopenharmony_ci Install the configuration file in the corresponding directory. Configure the **/base/startup/init/services/etc/BUILD.gn** file of the init module as follows: 148e41f4b71Sopenharmony_ci 149e41f4b71Sopenharmony_ci ``` 150e41f4b71Sopenharmony_ci ohos_prebuilt_etc("watchdog.cfg") { 151e41f4b71Sopenharmony_ci source = "//base/startup/init/services/etc/watchdog.cfg" 152e41f4b71Sopenharmony_ci relative_install_dir = "init" 153e41f4b71Sopenharmony_ci part_name = "init" 154e41f4b71Sopenharmony_ci subsystem_name = "startup" 155e41f4b71Sopenharmony_ci } 156e41f4b71Sopenharmony_ci 157e41f4b71Sopenharmony_ci ohos_prebuilt_etc("console.cfg") { 158e41f4b71Sopenharmony_ci source = "//base/startup/init/services/etc/console.cfg" 159e41f4b71Sopenharmony_ci relative_install_dir = "init" 160e41f4b71Sopenharmony_ci part_name = "init" 161e41f4b71Sopenharmony_ci subsystem_name = "startup" 162e41f4b71Sopenharmony_ci } 163e41f4b71Sopenharmony_ci 164e41f4b71Sopenharmony_ci ``` 165e41f4b71Sopenharmony_ci 166e41f4b71Sopenharmony_ci Add the build path of the configuration file to the **bundle.json** file of the corresponding module. For example: 167e41f4b71Sopenharmony_ci 168e41f4b71Sopenharmony_ci ``` 169e41f4b71Sopenharmony_ci "build": { 170e41f4b71Sopenharmony_ci "group_type": { 171e41f4b71Sopenharmony_ci "base_group": [], 172e41f4b71Sopenharmony_ci "fwk_group": [], 173e41f4b71Sopenharmony_ci "service_group": [ 174e41f4b71Sopenharmony_ci "//base/startup/init/ueventd:startup_ueventd", 175e41f4b71Sopenharmony_ci "//base/startup/init/services/etc:ueventd.cfg" 176e41f4b71Sopenharmony_ci ] 177e41f4b71Sopenharmony_ci }, 178e41f4b71Sopenharmony_ci } 179e41f4b71Sopenharmony_ci ``` 180e41f4b71Sopenharmony_ci### Configuring the DAC Permission for a Service Process 181e41f4b71Sopenharmony_ci 182e41f4b71Sopenharmony_ci Configure the DAC permission for the desired service process based on the GID and UID in the service process configuration file. 183e41f4b71Sopenharmony_ci 184e41f4b71Sopenharmony_ci If the corresponding GID and UID do not exist, add them to the **/init/services/etc/passwd** and **/init/services/etc/group** files of the init module. 185e41f4b71Sopenharmony_ci 186e41f4b71Sopenharmony_ci - Description of the **passwd** file: 187e41f4b71Sopenharmony_ci 188e41f4b71Sopenharmony_ci - Each entry corresponds to a user. User information is separated by colons (:) and divided into seven fields, as shown below: 189e41f4b71Sopenharmony_ci 190e41f4b71Sopenharmony_ci ```js 191e41f4b71Sopenharmony_ci root:x:0:0:::/bin/false 192e41f4b71Sopenharmony_ci bin:x:2:2:::/bin/false 193e41f4b71Sopenharmony_ci system:x:1000:1000:::/bin/false 194e41f4b71Sopenharmony_ci ``` 195e41f4b71Sopenharmony_ci 196e41f4b71Sopenharmony_ci Fields in the **passwd** file 197e41f4b71Sopenharmony_ci | SN | Field | Description | 198e41f4b71Sopenharmony_ci | ---------- | --------- | ------------------------------------------------------------ | 199e41f4b71Sopenharmony_ci | 1| User name | Use a name that is easy to remember, for example, **root**. Based on the mapping between user names and user ID (UIDs), the system identifies a user and grants the required user permissions.| 200e41f4b71Sopenharmony_ci | 2| Password flag | **x** indicates that a password has bee set, but not the real password. The real password is stored in the **/etc/shadow** file.| 201e41f4b71Sopenharmony_ci | 3| UID | Each user has a unique UID. The system identifies a user based on the UID.<br>A valid UID ranges from 0 to 65535. Numbers in different ranges represent different UIDs.<br>1. 0: administrator<br>2. 1 to 499: system users<br>3. 500 to 65535: common users| 202e41f4b71Sopenharmony_ci | 4| GID | A GID indicates an initial group. A user has the permissions of the initial group once logging in to the system. Each user can have only one initial group. Generally, the initial group has the same name as the user name.| 203e41f4b71Sopenharmony_ci | 5| Description | This field provides a simple description of the user. It is usually left blank. | 204e41f4b71Sopenharmony_ci | 6| Home directory | This field specifies the home directory of a user. For example, the home directory of the **root** user is **/root**. This field is usually left blank. | 205e41f4b71Sopenharmony_ci | 7| Default shell| Shell is a Linux command interpreter and serves as a bridge between users and the Linux kernel. It is used to convert commands entered by users into machine languages that can be identified by the system. By default, the command interpreter used by the Linux system is usually **/bin/bash**.| 206e41f4b71Sopenharmony_ci 207e41f4b71Sopenharmony_ci - Description of the **group** file 208e41f4b71Sopenharmony_ci 209e41f4b71Sopenharmony_ci - Each entry corresponds to a user group. User group information is separated by colons (:) and divided into four fields, as shown below: 210e41f4b71Sopenharmony_ci 211e41f4b71Sopenharmony_ci ```js 212e41f4b71Sopenharmony_ci root:x:0: 213e41f4b71Sopenharmony_ci bin:x:2: 214e41f4b71Sopenharmony_ci system:x:1000: 215e41f4b71Sopenharmony_ci servicectrl:x:1050:root,shell,system,samgr,hdf_devmgr,foundation,update 216e41f4b71Sopenharmony_ci ``` 217e41f4b71Sopenharmony_ci 218e41f4b71Sopenharmony_ci Fields in the **group** file 219e41f4b71Sopenharmony_ci | SN | Field | Description | 220e41f4b71Sopenharmony_ci | ---------- | --------- | -------------------------------------------------- | 221e41f4b71Sopenharmony_ci | 1| Group name| This field indicates the user group name. For example, **servicectrl** indicates that the user group name is **servicectrl**.| 222e41f4b71Sopenharmony_ci | 2| Password flag| Similar to the **/etc/passwd** file, **x** is only the password flag. The encrypted group password is stored in the **/etc/gshdow** file.| 223e41f4b71Sopenharmony_ci | 3| GID | GIDs are used to identify different user groups.| 224e41f4b71Sopenharmony_ci | 4| Group member| This field indicates the members of a user group. For example, the members of the **servicectrl** group include **root**, **shell**, **system**, **samgr**, **hdf_devmgr**, **foundation**, and **update**.| 225e41f4b71Sopenharmony_ci 226e41f4b71Sopenharmony_ci - If the GID and UID are set to **root** or **system**, add a trustlist to the **security_config/high_privilege_process_list.json** file in the corresponding product directory. For example: 227e41f4b71Sopenharmony_ci 228e41f4b71Sopenharmony_ci ``` 229e41f4b71Sopenharmony_ci { 230e41f4b71Sopenharmony_ci "high_privilege_process_list": [ 231e41f4b71Sopenharmony_ci { 232e41f4b71Sopenharmony_ci "name": "appspawn", 233e41f4b71Sopenharmony_ci "uid": "root", 234e41f4b71Sopenharmony_ci "gid": "root" 235e41f4b71Sopenharmony_ci }, 236e41f4b71Sopenharmony_ci { 237e41f4b71Sopenharmony_ci "name": "console", 238e41f4b71Sopenharmony_ci "uid": "root" 239e41f4b71Sopenharmony_ci }, 240e41f4b71Sopenharmony_ci { 241e41f4b71Sopenharmony_ci "name": "testService", 242e41f4b71Sopenharmony_ci "uid": "root", 243e41f4b71Sopenharmony_ci "gid": "root" 244e41f4b71Sopenharmony_ci }, 245e41f4b71Sopenharmony_ci { 246e41f4b71Sopenharmony_ci "name": "media_service", 247e41f4b71Sopenharmony_ci "gid": ["root", "system"] 248e41f4b71Sopenharmony_ci }, 249e41f4b71Sopenharmony_ci { 250e41f4b71Sopenharmony_ci "name": "misc", 251e41f4b71Sopenharmony_ci "uid": "root", 252e41f4b71Sopenharmony_ci "gid": "root" 253e41f4b71Sopenharmony_ci } ... ... 254e41f4b71Sopenharmony_ci ] 255e41f4b71Sopenharmony_ci } 256e41f4b71Sopenharmony_ci 257e41f4b71Sopenharmony_ci ``` 258e41f4b71Sopenharmony_ci### Configuring the SELinux Permission for a Service Process 259e41f4b71Sopenharmony_ci 260e41f4b71Sopenharmony_ci- Configuring the SELinux permission 261e41f4b71Sopenharmony_ci 262e41f4b71Sopenharmony_ci 1. Check whether the service process can be started in permissive mode. If yes, you do not need to configure the **selinux** tag. 263e41f4b71Sopenharmony_ci 264e41f4b71Sopenharmony_ci 2. To configure SELinux rules, you need to set the SELinux tag in the **secon** field in the configuration file of the service process. 265e41f4b71Sopenharmony_ci 266e41f4b71Sopenharmony_ci 3. Define the service process tag in the SELinux module. The procedure is as follows: 267e41f4b71Sopenharmony_ci 268e41f4b71Sopenharmony_ci Define the tag for program execution in the **base/security/selinux_adapter/sepolicy/base/system/file_contexts** file. For example: 269e41f4b71Sopenharmony_ci 270e41f4b71Sopenharmony_ci ``` 271e41f4b71Sopenharmony_ci /system/bin/watchdog_service u:object_r:watchdog_service_exec:s0 272e41f4b71Sopenharmony_ci /system/bin/hdcd u:object_r:hdcd_exec:s0 273e41f4b71Sopenharmony_ci ``` 274e41f4b71Sopenharmony_ci 275e41f4b71Sopenharmony_ci Define the applicable scope of the tag in the **base/security/selinux_adapter/sepolicy/base/public/type.te** file. For example: 276e41f4b71Sopenharmony_ci 277e41f4b71Sopenharmony_ci ``` 278e41f4b71Sopenharmony_ci type watchdog_service, sadomain, domain; 279e41f4b71Sopenharmony_ci type watchdog_service_exec, exec_attr, file_attr, system_file_attr; 280e41f4b71Sopenharmony_ci ``` 281e41f4b71Sopenharmony_ci 282e41f4b71Sopenharmony_ci## Error Codes 283e41f4b71Sopenharmony_ci 284e41f4b71Sopenharmony_ciDescription of error codes 285e41f4b71Sopenharmony_ci 286e41f4b71Sopenharmony_ci| Enum | Value| Error Message | Description | 287e41f4b71Sopenharmony_ci| ------------------ | ------ | ------------------------ | ----------------------- | 288e41f4b71Sopenharmony_ci| INIT_OK | 0 | | Operation success. | 289e41f4b71Sopenharmony_ci| INIT_EPARAMETER | 1 | parameter | Invalid parameter. | 290e41f4b71Sopenharmony_ci| INIT_EFORMAT | 2 | Format string fail | String format error. | 291e41f4b71Sopenharmony_ci| INIT_ECFG | 3 | cfg error | Configuration parsing error. | 292e41f4b71Sopenharmony_ci| INIT_EPATH | 4 | Invalid path | Incorrect service path. | 293e41f4b71Sopenharmony_ci| INIT_EFORK | 5 | Fork fail | Failed to fork a subprocess. | 294e41f4b71Sopenharmony_ci| INIT_ESANDBOX | 6 | Create sandbox fail | Failed to add a service to the sandbox. | 295e41f4b71Sopenharmony_ci| INIT_EACCESSTOKEN | 7 | Set access token fail | Failed to set the access token. | 296e41f4b71Sopenharmony_ci| INIT_ESOCKET | 8 | Create socket fail | Failed to create a socket. | 297e41f4b71Sopenharmony_ci| INIT_EFILE | 9 | Create file fail | Failed to create a file. | 298e41f4b71Sopenharmony_ci| INIT_ECONSOLE | 10 | Open console fail | Failed to open the console. | 299e41f4b71Sopenharmony_ci| INIT_EHOLDER | 11 | Publish holder fail | Failed to publish the holder. | 300e41f4b71Sopenharmony_ci| INIT_EBINDCORE | 12 | Bind core fail | Failed to bind cores. | 301e41f4b71Sopenharmony_ci| INIT_EKEEPCAP | 13 | Set keep capability fail | Failed to set **keep capability**.| 302e41f4b71Sopenharmony_ci| INIT_EGIDSET | 14 | Set gid fail | Failed to set the service GID. | 303e41f4b71Sopenharmony_ci| INIT_ESECCOMP | 15 | Set SECCOMP fail | Failed to set the service security policy. | 304e41f4b71Sopenharmony_ci| INIT_EUIDSET | 16 | Set uid fail | Failed to set the service UID. | 305e41f4b71Sopenharmony_ci| INIT_ECAP | 17 | Set capability fail | Failed to set the service capability. | 306e41f4b71Sopenharmony_ci| INIT_EWRITEPID | 18 | Write pid fail | Failed to write the PID. | 307e41f4b71Sopenharmony_ci| INIT_ECONTENT | 19 | Set sub content fail | Failed to set the service security context. | 308e41f4b71Sopenharmony_ci| INIT_EPRIORITY | 20 | Set priority fail | Failed to set the service priority. | 309e41f4b71Sopenharmony_ci| INIT_EEXEC_CONTENT | 21 | Set exec content fail | Failed to set the SELinux tag. | 310e41f4b71Sopenharmony_ci| INIT_EEXEC | 22 | Exec fail | Failed to run exec. | 311e41f4b71Sopenharmony_ci 312e41f4b71Sopenharmony_ci## Service Process Configuration Example 313e41f4b71Sopenharmony_ci 314e41f4b71Sopenharmony_ci### Example Code 315e41f4b71Sopenharmony_ci 316e41f4b71Sopenharmony_ci### Example Configuration 317e41f4b71Sopenharmony_ci 318e41f4b71Sopenharmony_ci## Available APIs 319e41f4b71Sopenharmony_ci 320e41f4b71Sopenharmony_ci **Table 3** Service control APIs 321e41f4b71Sopenharmony_ci| Function| Description| Parameter Description| 322e41f4b71Sopenharmony_ci| :---------- | :---------- |:--------| 323e41f4b71Sopenharmony_ci| int ServiceControlWithExtra(const char *serviceName, int action, const char *extArgv[], int extArgc) | Configures service parameters.| Return value: **0** if the operation is successful; **-1** otherwise.<br> Parameters:<br> **serviceName**: service name.<br> **action**: service action, which can be **start**, **stop**, or **restart**.<br> **extArgv**: parameter array.<br> **extArgc**: number of parameters.| 324e41f4b71Sopenharmony_ci| int ServiceControl(const char *serviceName, int action) | Controls the service behavior.| Return value: **0** if the operation is successful; **-1** otherwise.<br> Parameters:<br> **serviceName**: service name.<br> **action**: service action, which can be **start**, **stop**, or **restart**.| 325e41f4b71Sopenharmony_ci| int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int waitTimeout) | Sets the service waiting status.| Return value: **0** if the operation is successful; **-1** otherwise.<br> Parameters:<br>**serviceName**: service name.<br> **status**: service status.<br> waitTimeout: timeout interval.| 326e41f4b71Sopenharmony_ci| int ServiceSetReady(const char *serviceName) | Sets a service as being ready.| Return value: **0** if the operation is successful; **-1** otherwise.<br> Parameters:<br> **serviceName**: service name.| 327e41f4b71Sopenharmony_ci| int StartServiceByTimer(const char *serviceName, uint64_t timeout) | Starts a service by timer.| Return value: **0** if the operation is successful; **-1** otherwise.<br> Parameters:<br> **serviceName**: service name.<br> timeout: timeout interval.| 328e41f4b71Sopenharmony_ci| int StopServiceTimer(const char *serviceName) | Stops a service timer.| Return value: **0** if the operation is successful; **-1** otherwise.<br> Parameters:<br> **serviceName**: service name.| 329e41f4b71Sopenharmony_ci 330e41f4b71Sopenharmony_ci - The service management APIs are privileged APIs. Therefore, you need to configure the DAC and SELinux permissions as follows: 331e41f4b71Sopenharmony_ci 332e41f4b71Sopenharmony_ci - Go to **/base/startup/init/services/etc/group** and add the correct user ID to **servicectrl**. For example: 333e41f4b71Sopenharmony_ci 334e41f4b71Sopenharmony_ci ``` 335e41f4b71Sopenharmony_ci servicectrl:x:1050:root,shell,system,samgr,hdf_devmgr 336e41f4b71Sopenharmony_ci ``` 337e41f4b71Sopenharmony_ci 338e41f4b71Sopenharmony_ci - SELinux configuration for service control APIs 339e41f4b71Sopenharmony_ci 340e41f4b71Sopenharmony_ci Add the required SELinux permissions to the **init.te** file. For example, grant the system parameter write permission for services such as **init**, **samgr**, and **hdf_devmgr**. 341e41f4b71Sopenharmony_ci 342e41f4b71Sopenharmony_ci ```java 343e41f4b71Sopenharmony_ci allow { init samgr hdf_devmgr } servicectrl_param:parameter_service { set }; 344e41f4b71Sopenharmony_ci ``` 345e41f4b71Sopenharmony_ci## Constraints 346e41f4b71Sopenharmony_ci 347e41f4b71Sopenharmony_ciThe service management module is available only for the mini system and standard system. 348e41f4b71Sopenharmony_ci## FAQs 349e41f4b71Sopenharmony_ci 350e41f4b71Sopenharmony_ciFor details, see [init Module FAQs](./subsys-boot-init-faqs.md). 351