param_trie.h 3.4 KB
Newer Older
Z
zhong_ning 已提交
1
/*
Z
zhong_ning 已提交
2
 * Copyright (c) 2021 Huawei Device Co., Ltd.
Z
zhong_ning 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

Z
zhong_ning 已提交
16 17
#ifndef BASE_STARTUP_PARAM_TRIE_H
#define BASE_STARTUP_PARAM_TRIE_H
Z
zhong_ning 已提交
18 19 20
#include <stdatomic.h>
#include <stdio.h>

M
Mupceet 已提交
21 22 23 24
#include "init_hashmap.h"
#include "init_param.h"
#include "list.h"
#include "param_osadp.h"
S
sun_fan 已提交
25
#include "param_security.h"
Z
zhong_ning 已提交
26 27
#include "securec.h"

M
Mupceet 已提交
28 29 30 31
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
Z
zhong_ning 已提交
32 33
#endif

M
Mupceet 已提交
34 35
#ifdef PARAM_SUPPORT_SELINUX
#define HASH_BUTT 32
S
sun_fan 已提交
36
#else
M
Mupceet 已提交
37
#define HASH_BUTT 1
S
sun_fan 已提交
38
#endif
Z
zhong_ning 已提交
39 40

typedef struct {
S
sun_fan 已提交
41 42 43 44 45 46
    uint32_t left;
    uint32_t right;
    uint32_t child;
    uint32_t labelIndex;
    uint32_t dataIndex;
    uint16_t length;
Z
zhong_ning 已提交
47
    char key[0];
S
sun_fan 已提交
48 49 50 51 52 53
} ParamTrieNode;

#define PARAM_FLAGS_MODIFY 0x80000000
#define PARAM_FLAGS_TRIGGED 0x40000000
#define PARAM_FLAGS_WAITED 0x20000000
#define PARAM_FLAGS_COMMITID 0x0000ffff
Z
zhong_ning 已提交
54 55

typedef struct {
S
sun_fan 已提交
56 57 58 59 60
    atomic_uint commitId;
    uint16_t keyLength;
    uint16_t valueLength;
    char data[0];
} ParamNode;
Z
zhong_ning 已提交
61 62

typedef struct {
S
sun_fan 已提交
63 64 65 66
    uid_t uid;
    gid_t gid;
    uint16_t mode;
    uint16_t length;
Z
zhong_ning 已提交
67
    char data[0];
S
sun_fan 已提交
68
} ParamSecruityNode;
Z
zhong_ning 已提交
69 70

typedef struct {
X
xionglei6 已提交
71
    atomic_llong commitId;
M
Mupceet 已提交
72
    atomic_llong commitPersistId;
S
sun_fan 已提交
73 74 75
    uint32_t trieNodeCount;
    uint32_t paramNodeCount;
    uint32_t securityNodeCount;
M
Mupceet 已提交
76
    uint32_t startIndex;
S
sun_fan 已提交
77 78 79
    uint32_t currOffset;
    uint32_t firstNode;
    uint32_t dataSize;
Z
zhong_ning 已提交
80
    char data[0];
S
sun_fan 已提交
81
} ParamTrieHeader;
Z
zhong_ning 已提交
82 83 84

struct WorkSpace_;
typedef struct WorkSpace_ {
M
Mupceet 已提交
85 86 87
    int flags;
    HashNode hashNode;
    ListNode node;
S
sun_fan 已提交
88
    uint32_t (*allocTrieNode)(struct WorkSpace_ *workSpace, const char *key, uint32_t keyLen);
4
411148299@qq.com 已提交
89
    int (*compareTrieNode)(const ParamTrieNode *node, const char *key2, uint32_t key2Len);
M
Mupceet 已提交
90
    MemHandle memHandle;
S
sun_fan 已提交
91
    ParamTrieHeader *area;
M
Mupceet 已提交
92 93 94 95
#ifdef WORKSPACE_AREA_NEED_MUTEX
    ParamRWMutex rwlock;
#endif
    char fileName[0];
Z
zhong_ning 已提交
96 97
} WorkSpace;

M
Mupceet 已提交
98
int InitWorkSpace(WorkSpace *workSpace, int onlyRead, uint32_t spaceSize);
Z
zhong_ning 已提交
99 100
void CloseWorkSpace(WorkSpace *workSpace);

4
411148299@qq.com 已提交
101
ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset);
S
sun_fan 已提交
102 103 104
void SaveIndex(uint32_t *index, uint32_t offset);

ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen);
M
Mupceet 已提交
105
ParamTrieNode *FindTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel);
Z
zhong_ning 已提交
106

M
Mupceet 已提交
107
typedef int (*TraversalTrieNodePtr)(const WorkSpace *workSpace, const ParamTrieNode *node, const void *cookie);
4
411148299@qq.com 已提交
108
int TraversalTrieNode(const WorkSpace *workSpace,
M
Mupceet 已提交
109
    const ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, const void *cookie);
Z
zhong_ning 已提交
110

S
sun_fan 已提交
111 112
uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData);
uint32_t AddParamNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, const char *value, uint32_t valueLen);
Z
zhong_ning 已提交
113 114 115 116 117
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
M
Mupceet 已提交
118
#endif  // BASE_STARTUP_PARAM_TRIE_H