subsys-boot-init-sysparam.md 17.2 KB
Newer Older
S
shawn_he 已提交
1
# Parameter Management
S
shawn_he 已提交
2

S
shawn_he 已提交
3
## Overview
S
shawn_he 已提交
4

S
shawn_he 已提交
5 6
### Function

S
shawn_he 已提交
7
The parameter management subsystem, namely, sysparam, provides easy-to-use key-value pair access interfaces for system services to customize functions based on their own system parameters.
S
shawn_he 已提交
8 9 10

### Basic Concepts

S
shawn_he 已提交
11 12
#### Operation Primitives

S
shawn_he 已提交
13
Figure 1 shows the primitives used to operate system parameters. For details about how they work, see Table 1.
S
shawn_he 已提交
14 15

Figure 1 Overview of system parameter operation primitives
S
shawn_he 已提交
16

17
![System parameter operation primitives](figures/system-parameter-operation-primitives.png)
S
shawn_he 已提交
18 19

**Table 1** Description of system parameter operation primitives
S
shawn_he 已提交
20 21

| Operation Primitive | Description |
S
shawn_he 已提交
22 23 24 25
| -------- | -------- |
| get      | Obtains the value of a system parameter.       |
| set      | Sets the value of a system parameter.       |
| wait     | Waits for value change of a system parameter synchronously.|
S
shawn_he 已提交
26
| watch    | Watches for the value change of a system parameter asynchronously.|
S
shawn_he 已提交
27

S
shawn_he 已提交
28
#### Parameter Definition
S
shawn_he 已提交
29 30 31

- Naming format

S
shawn_he 已提交
32 33 34
  A system parameter name consists of multiple segments in dotted notation. Each segment can be a string that consists of letters, digits, and underscores (_). The total length cannot exceed 96 bytes. 

  Two naming formats are supported for system parameters, as described in Table 2.
S
shawn_he 已提交
35

S
shawn_he 已提交
36
  **Table 2** Description of system parameter naming formats
S
shawn_he 已提交
37

S
shawn_he 已提交
38 39 40 41
  | Type | Example | Description |
  | -------- | -------- | -------- |
  | Parameter name | const.product.**name** | Complete system parameter name. It does not end with a period (.).          |
  | Parameter directory | const.product **.**     | Name of the directory storing system parameters with the same prefix. It ends with a period (.).|
S
shawn_he 已提交
42 43 44

- Type

S
shawn_he 已提交
45
  System parameters are categorized into three types, as described in Table 3.
S
shawn_he 已提交
46

S
shawn_he 已提交
47
  **Table 3** Description of system parameter types
S
shawn_he 已提交
48 49 50

  | Type| Prefix| Description|
  | -------- | -------- | -------- |
S
shawn_he 已提交
51 52 53
  | Constant| const. | Constant parameter, which will not change once a value is assigned. The value can contain a maximum of 4,096 bytes (including the end-of-text character).|
  | Writable| Others| Writable parameter, which will be discarded after a system restart. The value can contain a maximum of 96 bytes (including the end-of-text character).|
  | Persistent| persist. | Writable and persistent parameter, which will not be discarded after a system restart. The value can contain a maximum of 96 bytes (including the end-of-text character).|
S
shawn_he 已提交
54

S
shawn_he 已提交
55
  Given below is the general naming format for system parameters:
S
shawn_he 已提交
56 57 58
  ```
  [ const | persist ].$sub_system.$desc
  ```
S
shawn_he 已提交
59 60 61
  wherein,
  
  - **$sub_system** is the subsystem or module name.
S
shawn_he 已提交
62

S
shawn_he 已提交
63
  - **$desc** is the parameter description, which can contain multiple segments in dotted notation.
S
shawn_he 已提交
64

S
shawn_he 已提交
65
#### Parameter Definition Rule
S
shawn_he 已提交
66

S
shawn_he 已提交
67
System parameters are defined per module on each subsystem. The parameter definition includes the system parameter name, default value, and access permission information.
S
shawn_he 已提交
68

S
shawn_he 已提交
69
- Value definition file
S
shawn_he 已提交
70

S
shawn_he 已提交
71
  - A value definition file of system parameters ends with the **.para** extension. The following is an example format of file content:
S
shawn_he 已提交
72 73 74 75 76 77 78 79

    ```shell
    # This is comment
    const.product.name=OHOS-PRODUCT
    const.os.version.api=26
    const.telephony.enable=false|true

    const.test.withblank=My Value
S
shawn_he 已提交
80
    const.test.withcomment=MyValue # This should be omitted
S
shawn_he 已提交
81 82 83 84 85
    const.test.multiline="This is a multiline parameter.
    Line2 value.
    Last line."
    ```

S
shawn_he 已提交
86
  - A complete system parameter command is required to assign a value for a system parameter. See Table 4 for the value assignment modes.
S
shawn_he 已提交
87

S
shawn_he 已提交
88
    **Table 4** Description of value assignment modes
S
shawn_he 已提交
89 90 91

    | Type| Example| Description|
    | -------- | -------- | -------- |
S
shawn_he 已提交
92 93
    | String  | const.product.name=OHOS-PRODUCT | A string that spans multiple lines must be enclosed in double quotation marks ("").|
    | Number    | const.os.version.api=26         | A number that spans multiple lines does not need to be enclosed in double quotation marks ("").|
S
shawn_he 已提交
94 95 96 97
    | Boolean    | const.telephony.enable=false    | A Boolean value can be **0**, **1**, **false**, or **true**.|

- DAC definition file

S
shawn_he 已提交
98
  Access permissions of system parameters are managed in discretionary access control (DAC) mode. A DAC definition file ends with the **.para.dac** extension. The following is an example format of DAC information:
S
shawn_he 已提交
99 100 101 102 103

  ```java
  const.product.="root:root:660"
  ```

S
shawn_he 已提交
104
  As shown in this example, a **parameter directory** can be used to define the same access permission for system parameters with the same prefix. A piece of DAC information is divided into three segments, user, group, and Ugo rule, which are separated using a semicolon (:).
S
shawn_he 已提交
105 106 107 108

  > **NOTE**
  > 
  > Ugo is the abbreviation for user access, group access, ad other system user's access, respectively. These permissions are set to allow or deny access to members of their own group, or any other groups.
S
shawn_he 已提交
109

S
shawn_he 已提交
110
  Figure 2 shows the Ugo rule structure.
S
shawn_he 已提交
111

S
shawn_he 已提交
112
  **Figure 2** Overview of the Ugo rule structure
S
shawn_he 已提交
113

S
shawn_he 已提交
114
  ![Ugo rule](figures/dac-definition.png)
S
shawn_he 已提交
115

S
shawn_he 已提交
116
- SELinux policy
S
shawn_he 已提交
117

S
shawn_he 已提交
118
  An SELinux policy defines the access permissions for all users, programs, processes, and files.
S
shawn_he 已提交
119

S
shawn_he 已提交
120 121 122
  - Adding an SELinux tag

    To add an SELinux tag to system parameters, you first need to define the tag in the **/base/security/selinux/sepolicy/base/public/parameter.te** file. For example:
S
shawn_he 已提交
123 124 125 126 127 128 129 130 131 132 133

    ```java
    type servicectrl_param, parameter_attr
    ```

    After the tag is defined, add the system parameter prefix associated with the tag to **/base/security/selinux/sepolicy/base/public/parameter_contexts**. The following uses the prefix **ohos.servicectrl** as an example:

    ```java
    ohos.servicectrl.           u:object_r:servicectrl_param:s0
    ```

S
shawn_he 已提交
134 135 136
  - Granting access permissions for a process

    For example, to grant access permissions such as map for the init process, add the following code to the **/base/security/selinux/sepolicy/ohos_policy/startup/init/public/init.te** file:
S
shawn_he 已提交
137 138 139 140 141 142

    ```java
    allow servicectrl_param tmpfs:filesystem associate;
    allow init servicectrl_param:file { map open read relabelto relabelfrom };
    ```

S
shawn_he 已提交
143 144 145
  - Granting the write permission

    For example, to grant the write permission for services such as **init**, **samgr**, and **hdf_devmgr**, use the following code:
S
shawn_he 已提交
146 147 148 149 150

    ```java
    allow { init samgr hdf_devmgr } servicectrl_param:parameter_service { set };
    ```

S
shawn_he 已提交
151 152 153
  - Granting the read permission

    If you want to grant the read permission only for certain services, replace **xxx** with these services in the following code:
S
shawn_he 已提交
154 155 156 157 158

    ```java
    allow { xxx } servicectrl_param:file { map open read };
    ```

S
shawn_he 已提交
159 160 161
  - Granting access permissions for all services

    If you want to grant access permissions for all services, use the following code:
S
shawn_he 已提交
162 163 164 165 166

    ```java
    allow { domain -limit_domain } servicectrl_param:file { map open read };
    ```

S
shawn_he 已提交
167
-  System parameter tag
S
shawn_he 已提交
168

S
shawn_he 已提交
169
   You are advised to keep only two system parameter tags for each subsystem:
S
shawn_he 已提交
170 171 172

   - A private tag to control system parameter settings.

S
shawn_he 已提交
173
   - A public tag to grant access permissions from all services.
S
shawn_he 已提交
174

S
shawn_he 已提交
175 176
-  Loading sequence

S
shawn_he 已提交
177 178 179
   System parameters are loaded by priority in the specified sequence, as described in Table 5.

    **Table 5** Description of the system parameter loading sequence
S
shawn_he 已提交
180 181 182 183 184 185

    | Type| Path| Description|
    | -------- | -------- | -------- |
    | Kernel parameters   | /proc/cmdline | Convert some values of kernel parameters into system parameters. Specifically, convert all **ohospara.xxx=valXXX** parameters to **ohos.boot.xxx=valXXX** parameters.|
    | OS system parameters| /system/etc/param/ohos_const/*.para | Load the definition file containing OS constants preferentially.                              |
    | Vendor parameters| /vendor/etc/param/*.para | Load the system parameters defined by vendors with the secondary priority.                            |
S
shawn_he 已提交
186 187
    | System parameters| /system/etc/param/*.para | Load the system parameters defined by each subsystem. A system parameter will be ignored if it already exists.|
    | Persistent parameters| /data/parameters/ | Load persistent parameters, if any, at last. Persistent parameters will overwrite the default system parameters that have been loaded.|
S
shawn_he 已提交
188

S
shawn_he 已提交
189

S
shawn_he 已提交
190
#### Tag File Size
S
shawn_he 已提交
191

S
shawn_he 已提交
192
If one tag is associated with more than five system parameters, you need to set the size of the system parameter tag file in **/base/startup/init/services/etc/param/ohos.para.size**. The default value is **512**.
S
shawn_he 已提交
193

S
shawn_he 已提交
194
The configuration rule is as follows:
S
shawn_he 已提交
195 196 197

System parameter tag = Size

S
shawn_he 已提交
198
For example:
S
shawn_he 已提交
199 200 201 202 203 204

```java
startup_init_param=40960
```


S
shawn_he 已提交
205 206
### Constraints

S
shawn_he 已提交
207
The parameter management subsystem is available only for the mini system and standard system.
S
shawn_he 已提交
208 209 210

## How to Develop

S
shawn_he 已提交
211
### Application Scenario
S
shawn_he 已提交
212

S
shawn_he 已提交
213 214
You can set specific system parameters as needed to meet your service demand.

S
shawn_he 已提交
215 216 217
### Implementation Method

The parameter management subsystem allows you to manage system parameters by running shell commands or calling **syspara** APIs.
S
shawn_he 已提交
218

S
shawn_he 已提交
219
  - Shell command mode
S
shawn_he 已提交
220

S
shawn_he 已提交
221
    You can operate system parameters directly by running shell commands. This operation mode is available only for the standard system. Table 6 is a list of the shell commands.
S
shawn_he 已提交
222 223 224 225 226 227 228

    **Table 6** Description of shell commands

    | Command| Description|
    | -------- | -------- |
    | param get [**key**] | Obtains the system parameter value of the specified key. If no key name is specified, all system parameter values will be returned.|
    | param set **key value** | Sets the specified value for the specified key.|
S
shawn_he 已提交
229
    | param wait **key** **value** | Waits for the system parameter value of the specified key to match the specified value. Fuzzy match is supported. For example, * indicates any value, and <strong>val</strong>* indicates matching of only the first three val characters.|
S
shawn_he 已提交
230
    | param watch | Watches for the value change of a system parameter asynchronously.|
S
shawn_he 已提交
231

S
shawn_he 已提交
232
  - syspara API mode
S
shawn_he 已提交
233

S
shawn_he 已提交
234
    Besides shell commands, you can use **syspara** APIs to manage system parameters. The return result is a constant string and the **free** operation is not supported. Table 7 is a list of the **syspara** APIs. 
S
shawn_he 已提交
235 236

    **Table 7** Description of syspara APIs
S
shawn_he 已提交
237

S
shawn_he 已提交
238 239 240 241 242
    | API| Description|
    | -------- | -------- |
    | int&nbsp;GetParameter(const&nbsp;char\*&nbsp;key,&nbsp;const&nbsp;char\*&nbsp;def,&nbsp;char\*&nbsp;value,&nbsp;unsigned&nbsp;int&nbsp;len) | Obtains system parameters.|
    | int&nbsp;SetParameter(const&nbsp;char\*&nbsp;key,&nbsp;const&nbsp;char\*&nbsp;value) | Sets or updates system parameters.|
    | const&nbsp;char\*&nbsp;GetDeviceType(void) | Obtains the device type.|
S
shawn_he 已提交
243 244 245 246
    | const&nbsp;char\*&nbsp;GetManufacture(void) | Obtains the device's manufacturer name.|
    | const&nbsp;char\*&nbsp;GetBrand(void) | Obtains the device's brand name.|
    | const&nbsp;char\*&nbsp;GetMarketName(void) | Obtains the device's marketing name.|
    | const&nbsp;char\*&nbsp;GetProductSeries(void) | Obtains the device's product series name.|
S
shawn_he 已提交
247
    | const&nbsp;char\*&nbsp;GetProductModel(void) | Obtains the device's product model.|
S
shawn_he 已提交
248 249 250 251 252 253 254 255 256 257 258 259
    | const&nbsp;char\*&nbsp;GetSoftwareModel(void) | Obtains the device's software model.|
    | const&nbsp;char\*&nbsp;GetHardwareModel(void) | Obtains the device's hardware model.|
    | const&nbsp;char\*&nbsp;GetHardwareProfile(void) | Obtains the device's hardware profile.|
    | const&nbsp;char\*&nbsp;GetSerial(void) | Obtains the device's serial number (SN).|
    | const&nbsp;char\*&nbsp;GetOSFullName(void) | Obtains the device's OS name.|
    | const&nbsp;char\*&nbsp;GetDisplayVersion(void) | Obtains the device's displayed software version.|
    | const&nbsp;char\*&nbsp;GetBootloaderVersion(void) | Obtains the device's bootloader version.|
    | const&nbsp;char\*&nbsp;GetSecurityPatchTag(void) | Obtains the device's security patch tag.|
    | const&nbsp;char\*&nbsp;GetAbiList(void) | Obtains the list of supported application binary interfaces (ABIs).|
    | int&nbsp;GetSdkApiVersion(void) | Obtains the SDK API version that matches the current system software.|
    | int&nbsp;GetFirstApiVersion(void) | Obtains the first SDK API version of the system software.|
    | const&nbsp;char\*&nbsp;GetIncrementalVersion(void) | Obtains the incremental version of the system software.|
S
shawn_he 已提交
260 261
    | const&nbsp;char\*&nbsp;GetVersionId(void) | Obtains the version ID.|
    | const&nbsp;char\*&nbsp;GetBuildType(void) | Obtains the build type.|
S
shawn_he 已提交
262 263
    | const&nbsp;char\*&nbsp;GetBuildUser(void) | Obtains the build user.|
    | const&nbsp;char\*&nbsp;GetBuildHost(void) | Obtains the build host.|
S
shawn_he 已提交
264
    | const&nbsp;char\*&nbsp;GetBuildTime(void) | Obtains the build time.|
S
shawn_he 已提交
265
    | const&nbsp;char\*&nbsp;GetBuildRootHash(void) | Obtains the buildroot hash value of the current version.|
S
shawn_he 已提交
266
    | const&nbsp;char\*&nbsp;GetOsReleaseType(void) | Obtains the system release type.|
S
shawn_he 已提交
267 268 269
    | int&nbsp;GetDevUdid(char&nbsp;\*udid,&nbsp;int&nbsp;size) | Obtains the device's unique device ID (UDID).|
    | const char *AclGetSerial(void); | Obtains the device's SN with ACL check.|
    | int AclGetDevUdid(char *udid, int size); | Obtains the device's UDID with ACL check.|
S
shawn_he 已提交
270

S
shawn_he 已提交
271
### Development Procedure
S
shawn_he 已提交
272

S
shawn_he 已提交
273
#### Parameter Definition
S
shawn_he 已提交
274

S
shawn_he 已提交
275
You can define default system parameters and implement permission control on them by configuring the <strong>.para</strong> and <strong>.para.dac</strong> files of the respective subsystem or product.
S
shawn_he 已提交
276

S
shawn_he 已提交
277
- On a standard system, use the <strong>ohos_prebuilt_para</strong> template to install the configuration file to the <strong>/etc/param/</strong> directory. The following is an example of the GN script:
S
shawn_he 已提交
278 279

    ```go
C
chengjinsong2 已提交
280
    import("//base/startup/init/services/etc/param/param_fixer.gni")
S
shawn_he 已提交
281 282

    ohos_prebuilt_para("ohos.para") {
C
chengjinsong2 已提交
283
        source = "//base/startup/init/services/etc/ohos.para"
S
shawn_he 已提交
284 285 286 287 288
        part_name = "init"
        module_install_dir = "etc/param"
    }

    ohos_prebuilt_para("ohos.para.dac") {
C
chengjinsong2 已提交
289
        source = "//base/startup/init/services/etc/ohos.para.dac"
S
shawn_he 已提交
290 291 292 293 294
        part_name = "init"
        module_install_dir = "etc/param"
    }
    ```

S
shawn_he 已提交
295
- On a small system, run the <strong>copy</strong> command to copy the corresponding system parameter definition file to the <strong>system/etc/param</strong> directory.
S
shawn_he 已提交
296 297
    ```go
    copy("ohos.para") {
C
chengjinsong2 已提交
298
      sources = [ "//base/startup/init/services/etc/param/ohos.para" ]
S
shawn_he 已提交
299 300 301
      outputs = [ "$root_out_dir/system/etc/param/ohos.para" ]
    }
    copy("ohos.para.dac") {
C
chengjinsong2 已提交
302
      sources = [ "//base/startup/init/services/etc/param/ohos.para.dac" ]
S
shawn_he 已提交
303 304 305
      outputs = [ "$root_out_dir/system/etc/param/ohos.para.dac" ]
    }
    ```
S
shawn_he 已提交
306
- On a mini system, convert all defined default system parameters into header files through **action** and compile them into the system.
S
shawn_he 已提交
307 308
    ```go
    action("lite_const_param_to") {
C
chengjinsong2 已提交
309
      script = "//base/startup/init/scripts/param_cfg_to_code.py"
S
shawn_he 已提交
310 311 312
      args = [
        "--source",
        rebase_path(
C
chengjinsong2 已提交
313
            "//base/startup/init/services/etc_lite/param/ohos_const/ohospara"),
S
shawn_he 已提交
314
        "--dest_dir",
C
chengjinsong2 已提交
315
        rebase_path("$root_out_dir/gen/init/"),
S
shawn_he 已提交
316 317 318 319 320 321
        "--priority",
        "0",
      ]
      outputs = [ "$target_gen_dir/${target_name}_param_cfg_to_code.log" ]
    }
    ```
S
shawn_he 已提交
322
#### Development Example
S
shawn_he 已提交
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
    ```
    // set && get
    char key1[] = "rw.sys.version";
    char value1[] = "10.1.0";
    int ret = SetParameter(key1, value1);
    char valueGet1[128] = {0};
    ret = GetParameter(key1, "version=10.1.0", valueGet1, 128);

    // get sysparm
    char* value1 = GetDeviceType();
    printf("Product type =%s\n", value1);

    char* value2 = GetManufacture();
    printf("Manufacture =%s\n", value2);

    char* value3 = GetBrand();
    printf("GetBrand =%s\n", value3);

    char* value4 = GetMarketName();
    printf("MarketName =%s\n", value4);

    char* value5 = GetProductSeries();
    printf("ProductSeries =%s\n", value5);

    char* value6 = GetProductModel();
    printf("ProductModel =%s\n", value6);

    char* value7 = GetSoftwareModel();
    printf("SoftwareModel =%s\n", value7);

    char* value8 = GetHardwareModel();
    printf("HardwareModel =%s\n", value8);

    char* value9 = GetHardwareProfile();
    printf("Software profile =%s\n", value9);

    char* value10 = GetSerial();
    printf("Serial =%s\n", value10);

    char* value11 = GetOSFullName();
    printf("OS name =%s\n", value11);

    char* value12 = GetDisplayVersion();
    printf("Display version =%s\n", value12);

    char* value13 = GetBootloaderVersion();
    printf("bootloader version =%s\n", value13);

    char* value14 = GetSecurityPatchTag();
    printf("secure patch level =%s\n", value14);

    char* value15 = GetAbiList();
    printf("abi list =%s\n", value15);

    int value16 = GetFirstApiVersion();
    printf("first api level =%d\n", value16);

    char* value17 = GetIncrementalVersion();
    printf("Incremental version = %s\n", value17);

    char* value18 = GetVersionId();
    printf("formal id =%s\n", value18);

    char* value19 = GetBuildType();
    printf("build type =%s\n", value19);

    char* value20 = GetBuildUser();
    printf("build user =%s\n", value20);

    char* value21 = GetBuildHost();
    printf("Build host = %s\n", value21);

    char* value22 = GetBuildTime();
    printf("build time =%s\n", value22);

    char* value23 = GetBuildRootHash();
    printf("build root later..., %s\n", value23);

    char* value24 = GetOsReleaseType();
    printf("OS release type =%s\n", value24);

    char* value25 = GetOsReleaseType();
    printf("OS release type =%s\n", value25);

    char value26[65] = {0};
    GetDevUdid(value26, 65);
    printf("device udid =%s\n", value26);
    ```