param_trie.h 3.8 KB
Newer Older
Z
zhong_ning 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
/*
 * Copyright (c) 2020 Huawei Device Co., Ltd.
 * 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 21 22
#include <stdatomic.h>
#include <stdio.h>
#include <string.h>
#include <sys/syscall.h>

S
sun_fan 已提交
23
#include "param_security.h"
Z
zhong_ning 已提交
24
#include "securec.h"
S
sun_fan 已提交
25
#include "sys_param.h"
Z
zhong_ning 已提交
26

S
sun_fan 已提交
27 28 29 30
#ifndef __NR_futex
#define PARAM_NR_FUTEX 202 /* syscall number */
#else
#define PARAM_NR_FUTEX __NR_futex
Z
zhong_ning 已提交
31 32
#endif

S
sun_fan 已提交
33 34 35 36 37
#if defined FUTEX_WAIT || defined FUTEX_WAKE
#include <linux/futex.h>
#else
#define FUTEX_WAIT 0
#define FUTEX_WAKE 1
Z
zhong_ning 已提交
38

S
sun_fan 已提交
39 40 41 42 43
#define PARAM_FUTEX(ftx, op, value, timeout, bitset)                         \
    do {                                                                   \
        struct timespec d_timeout = { 0, 1000 * 1000 * (timeout) };        \
        syscall(PARAM_NR_FUTEX, ftx, op, value, &d_timeout, NULL, bitset); \
    } while (0)
Z
zhong_ning 已提交
44

S
sun_fan 已提交
45 46 47
#define futex_wake(ftx, count) PARAM_FUTEX(ftx, FUTEX_WAKE, count, 0, 0)
#define futex_wait(ftx, value) PARAM_FUTEX(ftx, FUTEX_WAIT, value, 100, 0)
#endif
Z
zhong_ning 已提交
48

S
sun_fan 已提交
49 50 51 52 53
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
Z
zhong_ning 已提交
54

S
sun_fan 已提交
55 56
#define PARAM_WORKSPACE_MAX (80 * 1024)
#define FILENAME_LEN_MAX 255
Z
zhong_ning 已提交
57
typedef struct {
S
sun_fan 已提交
58 59 60 61 62 63
    uint32_t left;
    uint32_t right;
    uint32_t child;
    uint32_t labelIndex;
    uint32_t dataIndex;
    uint16_t length;
Z
zhong_ning 已提交
64
    char key[0];
S
sun_fan 已提交
65 66 67 68 69 70
} ParamTrieNode;

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

typedef struct {
S
sun_fan 已提交
73 74 75 76 77
    atomic_uint commitId;
    uint16_t keyLength;
    uint16_t valueLength;
    char data[0];
} ParamNode;
Z
zhong_ning 已提交
78 79

typedef struct {
S
sun_fan 已提交
80 81 82 83
    uid_t uid;
    gid_t gid;
    uint16_t mode;
    uint16_t length;
Z
zhong_ning 已提交
84
    char data[0];
S
sun_fan 已提交
85
} ParamSecruityNode;
Z
zhong_ning 已提交
86 87

typedef struct {
S
sun_fan 已提交
88 89 90 91 92 93 94
    uint32_t trieNodeCount;
    uint32_t paramNodeCount;
    uint32_t securityNodeCount;
    uint32_t currOffset;
    uint32_t firstNode;
    uint32_t dataSize;
    uint32_t reserved_[28];
Z
zhong_ning 已提交
95
    char data[0];
S
sun_fan 已提交
96
} ParamTrieHeader;
Z
zhong_ning 已提交
97 98 99 100

struct WorkSpace_;
typedef struct WorkSpace_ {
    char fileName[FILENAME_LEN_MAX + 1];
S
sun_fan 已提交
101
    uint32_t (*allocTrieNode)(struct WorkSpace_ *workSpace, const char *key, uint32_t keyLen);
4
411148299@qq.com 已提交
102
    int (*compareTrieNode)(const ParamTrieNode *node, const char *key2, uint32_t key2Len);
S
sun_fan 已提交
103
    ParamTrieHeader *area;
Z
zhong_ning 已提交
104 105 106 107 108
} WorkSpace;

int InitWorkSpace(const char *fileName, WorkSpace *workSpace, int onlyRead);
void CloseWorkSpace(WorkSpace *workSpace);

4
411148299@qq.com 已提交
109
ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset);
S
sun_fan 已提交
110 111 112
void SaveIndex(uint32_t *index, uint32_t offset);

ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen);
4
411148299@qq.com 已提交
113
ParamTrieNode *FindTrieNode(const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel);
Z
zhong_ning 已提交
114

4
411148299@qq.com 已提交
115
typedef int (*TraversalTrieNodePtr)(const WorkSpace *workSpace, const ParamTrieNode *node, void *cookie);
4
411148299@qq.com 已提交
116 117
int TraversalTrieNode(const WorkSpace *workSpace,
    const ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, void *cookie);
Z
zhong_ning 已提交
118

S
sun_fan 已提交
119 120
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 已提交
121 122 123 124 125
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
Z
zhong_ning 已提交
126
#endif // BASE_STARTUP_PARAM_TRIE_H