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_TRIE_H 17 #define BASE_STARTUP_PARAM_TRIE_H 18 #include <stdio.h> 19 20 #include "init_hashmap.h" 21 #include "init_param.h" 22 #include "list.h" 23 #include "param_osadp.h" 24 #include "param_security.h" 25 26 #ifdef __cplusplus 27 #if __cplusplus 28 extern "C" { 29 #endif 30 #endif 31 32 #ifdef PARAM_SUPPORT_SELINUX 33 #define HASH_BUTT 32 34 #else 35 #define HASH_BUTT 1 36 #endif 37 38 typedef struct { 39 uint32_t left; 40 uint32_t right; 41 uint32_t child; 42 uint32_t labelIndex; 43 uint32_t dataIndex; 44 uint16_t length; 45 char key[0]; 46 } ParamTrieNode; 47 48 #define PARAM_FLAGS_MODIFY 0x80000000 49 #define PARAM_FLAGS_TRIGGED 0x40000000 50 #define PARAM_FLAGS_WAITED 0x20000000 51 #define PARAM_FLAGS_COMMITID 0x0000ffff 52 53 #define PARAM_TYPE_MASK 0x0f 54 #define PARAM_TYPE_STRING 0x00 55 #define PARAM_TYPE_INT 0x01 56 #define PARAM_TYPE_BOOL 0x02 57 58 typedef struct { 59 ATOMIC_UINT32 commitId; 60 uint8_t type; 61 uint8_t keyLength; 62 uint16_t valueLength; 63 char data[0]; 64 } ParamNode; 65 66 typedef struct { 67 uid_t uid; 68 gid_t gid; 69 uint16_t mode; 70 uint8_t type; 71 uint8_t length; 72 char data[0]; 73 } ParamSecurityNode; 74 75 typedef struct { 76 ATOMIC_LLONG commitId; 77 ATOMIC_LLONG commitPersistId; 78 uint32_t trieNodeCount; 79 uint32_t paramNodeCount; 80 uint32_t securityNodeCount; 81 uint32_t startIndex; 82 uint32_t currOffset; 83 uint32_t firstNode; 84 uint32_t dataSize; 85 char data[0]; 86 } ParamTrieHeader; 87 88 struct WorkSpace_; 89 typedef struct WorkSpace_ { 90 unsigned int flags; 91 HashNode hashNode; 92 ListNode node; 93 uint32_t (*allocTrieNode)(struct WorkSpace_ *workSpace, const char *key, uint32_t keyLen); 94 int (*compareTrieNode)(const ParamTrieNode *node, const char *key2, uint32_t key2Len); 95 MemHandle memHandle; 96 ParamTrieHeader *area; 97 #ifdef WORKSPACE_AREA_NEED_MUTEX 98 ParamRWMutex rwlock; 99 #endif 100 char fileName[0]; 101 } WorkSpace; 102 103 INIT_LOCAL_API int InitWorkSpace(WorkSpace *workSpace, int onlyRead, uint32_t spaceSize); 104 INIT_LOCAL_API void CloseWorkSpace(WorkSpace *workSpace); 105 106 INIT_LOCAL_API ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset); 107 INIT_LOCAL_API void SaveIndex(uint32_t *index, uint32_t offset); 108 109 INIT_LOCAL_API ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen); 110 INIT_LOCAL_API ParamTrieNode *FindTrieNode( 111 WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel); 112 113 typedef int (*TraversalTrieNodePtr)(const WorkSpace *workSpace, const ParamTrieNode *node, const void *cookie); 114 INIT_LOCAL_API int TraversalTrieNode(const WorkSpace *workSpace, 115 const ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, const void *cookie); 116 117 INIT_LOCAL_API uint32_t AddParamSecurityNode(WorkSpace *workSpace, const ParamAuditData *auditData); 118 INIT_LOCAL_API uint32_t AddParamNode(WorkSpace *workSpace, uint8_t type, 119 const char *key, uint32_t keyLen, const char *value, uint32_t valueLen); 120 121 INIT_LOCAL_API uint32_t GetParamMaxLen(uint8_t type); 122 INIT_LOCAL_API ParamNode *GetParamNode(const char *spaceName, const char *name); 123 #ifdef __cplusplus 124 #if __cplusplus 125 } 126 #endif 127 #endif 128 #endif // BASE_STARTUP_PARAM_TRIE_H