• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #ifndef BASE_STARTUP_PARAM_UTILS_H
17 #define BASE_STARTUP_PARAM_UTILS_H
18 #include <stddef.h>
19 #include <stdint.h>
20 
21 #include "init_log.h"
22 #include "securec.h"
23 #include "sys_param.h"
24 
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 typedef enum {
32     PARAM_CODE_NOT_INIT = PARAM_CODE_MAX + 1,
33     PARAM_CODE_ERROR_MAP_FILE,
34 } PARAM_INNER_CODE;
35 
36 #define MS_UNIT 1000
37 #define UNUSED(x) (void)(x)
38 #define PARAM_ALIGN(len) (((len) + 0x03) & (~0x03))
39 #define PARAM_ENTRY(ptr, type, member) (type *)((char *)(ptr)-offsetof(type, member))
40 
41 #define IS_READY_ONLY(name) \
42     ((strncmp((name), "const.", strlen("const.")) == 0) || (strncmp((name), "ro.", strlen("ro.")) == 0))
43 #define PARAM_PERSIST_PREFIX "persist."
44 
45 #define SYS_POWER_CTRL "ohos.startup.powerctrl="
46 #define OHOS_CTRL_START "ohos.ctl.start="
47 #define OHOS_CTRL_STOP "ohos.ctl.stop="
48 #define OHOS_SERVICE_CTRL_PREFIX "ohos.servicectrl."
49 #define OHOS_BOOT "ohos.boot."
50 
51 #define CLIENT_PIPE_NAME "/dev/unix/socket/paramservice"
52 #define CLIENT_PARAM_STORAGE_PATH "/dev/__parameters__/param_storage"
53 
54 #ifdef STARTUP_INIT_TEST
55 #define PARAM_STATIC
56 #define PARAM_DEFAULT_PATH "/data/init_ut"
57 #define PIPE_NAME PARAM_DEFAULT_PATH"/param/paramservice"
58 #define PARAM_STORAGE_PATH PARAM_DEFAULT_PATH "/__parameters__/param_storage"
59 #define PARAM_PERSIST_SAVE_PATH PARAM_DEFAULT_PATH "/param/persist_parameters"
60 #define PARAM_PERSIST_SAVE_TMP_PATH PARAM_DEFAULT_PATH "/param/tmp_persist_parameters"
61 #else
62 #define PARAM_DEFAULT_PATH ""
63 #define PARAM_STATIC static
64 #define PIPE_NAME "/dev/unix/socket/paramservice"
65 #define PARAM_STORAGE_PATH "/dev/__parameters__/param_storage"
66 #define PARAM_PERSIST_SAVE_PATH "/data/parameters/persist_parameters"
67 #define PARAM_PERSIST_SAVE_TMP_PATH "/data/parameters/tmp_persist_parameters"
68 #endif
69 
70 #define PARAM_CMD_LINE "/proc/cmdline"
71 #define GROUP_FILE_PATH "/etc/group"
72 #define USER_FILE_PATH "/etc/passwd"
73 
74 #define WORKSPACE_FLAGS_INIT 0x01
75 #define WORKSPACE_FLAGS_LOADED 0x02
76 #define WORKSPACE_FLAGS_UPDATE 0x04
77 #define WORKSPACE_FLAGS_LABEL_LOADED 0x08
78 
79 #define PARAM_SET_FLAG(node, flag) ((node) |= (flag))
80 #define PARAM_CLEAR_FLAG(node, flag) ((node) &= ~(flag))
81 #define PARAM_TEST_FLAG(node, flag) (((node) & (flag)) == (flag))
82 
83 #define PARAN_LOG_FILE "param.log"
84 #define PARAN_LABEL "PARAM"
85 #define PARAM_LOGI(fmt, ...) STARTUP_LOGI(PARAN_LOG_FILE, PARAN_LABEL, fmt, ##__VA_ARGS__)
86 #define PARAM_LOGE(fmt, ...) STARTUP_LOGE(PARAN_LOG_FILE, PARAN_LABEL, fmt, ##__VA_ARGS__)
87 #define PARAM_LOGV(fmt, ...) STARTUP_LOGV(PARAN_LOG_FILE, PARAN_LABEL, fmt, ##__VA_ARGS__)
88 
89 #define PARAM_CHECK(retCode, exper, ...) \
90     if (!(retCode)) {                \
91         PARAM_LOGE(__VA_ARGS__);     \
92         exper;                       \
93     }
94 
95 #ifdef INIT_AGENT
96 #define PARAM_DUMP printf
97 #else
98 #define PARAM_DUMP PARAM_LOGI
99 #endif
100 
101 #define MAX_LABEL_LEN 256
102 #define PARAM_BUFFER_SIZE 256
103 
104 #define SUBSTR_INFO_NAME 0
105 #define SUBSTR_INFO_VALUE 1
106 #ifdef PARAM_SUPPORT_SELINUX
107 #define SUBSTR_INFO_LABEL 1
108 #define SUBSTR_INFO_DAC 2
109 #else
110 #define SUBSTR_INFO_LABEL 1
111 #define SUBSTR_INFO_DAC 1
112 #endif
113 
114 typedef struct {
115     int length;
116     char value[PARAM_BUFFER_SIZE];
117 } SubStringInfo;
118 
119 void CheckAndCreateDir(const char *fileName);
120 int GetSubStringInfo(const char *buff, uint32_t buffLen, char delimiter, SubStringInfo *info, int subStrNumber);
121 #ifdef __cplusplus
122 #if __cplusplus
123 }
124 #endif
125 #endif
126 #endif