1 /* 2 * Copyright (c) 2021-2022 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_MANAGER_H 17 #define BASE_STARTUP_PARAM_MANAGER_H 18 #include <pthread.h> 19 #include <stdio.h> 20 #include <string.h> 21 22 #include "init_param.h" 23 #include "list.h" 24 #include "param_base.h" 25 #include "param_osadp.h" 26 #include "param_persist.h" 27 #include "param_security.h" 28 #include "param_trie.h" 29 #include "param_utils.h" 30 31 #ifdef __cplusplus 32 #if __cplusplus 33 extern "C" { 34 #endif 35 #endif 36 37 #define WORKSPACE_NAME_DAC "param_sec_dac" 38 #define WORKSPACE_NAME_DEF_SELINUX "u:object_r:default_param:s0" 39 #ifndef PARAM_SUPPORT_SELINUX 40 #define WORKSPACE_NAME_NORMAL "param_storage" 41 #else 42 #define WORKSPACE_NAME_NORMAL WORKSPACE_NAME_DEF_SELINUX 43 #endif 44 45 #define PARAM_NEED_CHECK_IN_SERVICE 0x2 46 #define PARAM_CTRL_SERVICE 0x1 47 48 #define PARAM_WORKSPACE_CHECK(space, exper, ...) \ 49 if (((*space).flags & WORKSPACE_FLAGS_INIT) != WORKSPACE_FLAGS_INIT) { \ 50 PARAM_LOGE(__VA_ARGS__); \ 51 exper; \ 52 } 53 54 typedef struct { 55 uint32_t flags; 56 ParamSecurityLabel securityLabel; 57 ParamSecurityOps paramSecurityOps[PARAM_SECURITY_MAX]; 58 HashMapHandle workSpaceHashHandle; 59 ListHead workSpaceList; 60 #ifdef PARAMWORKSPACE_NEED_MUTEX 61 ParamRWMutex rwlock; 62 #endif 63 PARAM_WORKSPACE_OPS ops; 64 #ifdef PARAM_SUPPORT_SELINUX 65 SelinuxSpace selinuxSpace; 66 #endif 67 } ParamWorkSpace; 68 69 typedef struct { 70 ParamTaskPtr serverTask; 71 ParamTaskPtr timer; 72 ParamTaskPtr watcherTask; 73 } ParamService; 74 75 typedef struct { 76 uint32_t flags; 77 long long commitId; 78 ParamTaskPtr saveTimer; 79 time_t lastSaveTimer; 80 PersistParamOps persistParamOps; 81 } ParamPersistWorkSpace; 82 83 typedef struct { 84 char realKey[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX + 1]; 85 char cmdName[32]; 86 uint32_t valueOffset; 87 uint8_t ctrlParam; 88 } ServiceCtrlInfo; 89 90 typedef void (*TraversalParamPtr)(ParamHandle handle, void *context); 91 typedef struct { 92 TraversalParamPtr traversalParamPtr; 93 void *context; 94 char *prefix; 95 } ParamTraversalContext; 96 97 INIT_LOCAL_API int AddWorkSpace(const char *name, int onlyRead, uint32_t spacesize); 98 INIT_LOCAL_API WorkSpace *GetFirstWorkSpace(void); 99 INIT_LOCAL_API WorkSpace *GetNextWorkSpace(WorkSpace *curr); 100 INIT_LOCAL_API WorkSpace *GetWorkSpace(const char *name); 101 INIT_LOCAL_API ParamTrieNode *GetTrieNodeByHandle(ParamHandle handle); 102 INIT_LOCAL_API int ReadParamWithCheck(const char *name, uint32_t op, ParamHandle *handle); 103 INIT_LOCAL_API int CheckParamValue(const ParamTrieNode *node, const char *name, const char *value, uint8_t paramType); 104 INIT_LOCAL_API int CheckParamName(const char *name, int paramInfo); 105 INIT_LOCAL_API uint8_t GetParamValueType(const char *name); 106 107 INIT_LOCAL_API ParamNode *SystemCheckMatchParamWait(const char *name, const char *value); 108 INIT_LOCAL_API int WriteParam(const char *name, const char *value, uint32_t *dataIndex, int onlyAdd); 109 INIT_LOCAL_API int AddSecurityLabel(const ParamAuditData *auditData); 110 INIT_LOCAL_API ParamSecurityLabel *GetParamSecurityLabel(void); 111 112 INIT_LOCAL_API void LoadParamFromBuild(void); 113 INIT_LOCAL_API int LoadParamFromCmdLine(void); 114 INIT_LOCAL_API void LoadParamAreaSize(void); 115 INIT_LOCAL_API int InitPersistParamWorkSpace(void); 116 INIT_LOCAL_API void ClosePersistParamWorkSpace(void); 117 INIT_LOCAL_API int WritePersistParam(const char *name, const char *value); 118 119 INIT_LOCAL_API uint32_t ReadCommitId(ParamNode *entry); 120 INIT_LOCAL_API int ReadParamName(ParamHandle handle, char *name, uint32_t length); 121 INIT_LOCAL_API int CheckParameterSet(const char *name, const char *value, 122 const ParamSecurityLabel *srcLabel, int *ctrlService); 123 INIT_LOCAL_API ParamHandle GetParamHandle(const WorkSpace *workSpace, uint32_t index, const char *name); 124 INIT_LOCAL_API int CheckParamPermission(const ParamSecurityLabel *srcLabel, const char *name, uint32_t mode); 125 126 INIT_LOCAL_API int SysCheckParamExist(const char *name); 127 INIT_LOCAL_API int GenerateKeyHasCode(const char *buff, size_t len); 128 129 INIT_INNER_API ParamWorkSpace *GetParamWorkSpace(void); 130 INIT_INNER_API int GetParamSecurityAuditData(const char *name, int type, ParamAuditData *auditData); 131 INIT_LOCAL_API int GetServiceCtrlInfo(const char *name, const char *value, ServiceCtrlInfo **ctrlInfo); 132 133 #ifdef STARTUP_INIT_TEST 134 ParamService *GetParamService(); 135 #endif 136 #ifdef __cplusplus 137 #if __cplusplus 138 } 139 #endif 140 #endif 141 #endif