• 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_TRIE_H
17 #define BASE_STARTUP_PARAM_TRIE_H
18 #include <stdatomic.h>
19 #include <stdio.h>
20 #include <string.h>
21 #include <sys/syscall.h>
22 
23 #include "param_security.h"
24 #include "securec.h"
25 #include "sys_param.h"
26 
27 #ifndef __NR_futex
28 #define PARAM_NR_FUTEX 202 /* syscall number */
29 #else
30 #define PARAM_NR_FUTEX __NR_futex
31 #endif
32 
33 #if defined FUTEX_WAIT || defined FUTEX_WAKE
34 #include <linux/futex.h>
35 #else
36 #define FUTEX_WAIT 0
37 #define FUTEX_WAKE 1
38 
39 #define PARAM_FUTEX(ftx, op, value, timeout, bitset)                         \
40     do {                                                                   \
41         struct timespec d_timeout = { 0, 1000 * 1000 * (timeout) };        \
42         syscall(PARAM_NR_FUTEX, ftx, op, value, &d_timeout, NULL, bitset); \
43     } while (0)
44 
45 #define futex_wake(ftx, count) PARAM_FUTEX(ftx, FUTEX_WAKE, count, 0, 0)
46 #define futex_wait(ftx, value) PARAM_FUTEX(ftx, FUTEX_WAIT, value, 100, 0)
47 #endif
48 
49 #ifdef __cplusplus
50 #if __cplusplus
51 extern "C" {
52 #endif
53 #endif
54 
55 #define PARAM_WORKSPACE_MAX (80 * 1024)
56 #define FILENAME_LEN_MAX 255
57 typedef struct {
58     uint32_t left;
59     uint32_t right;
60     uint32_t child;
61     uint32_t labelIndex;
62     uint32_t dataIndex;
63     uint16_t length;
64     char key[0];
65 } ParamTrieNode;
66 
67 #define PARAM_FLAGS_MODIFY 0x80000000
68 #define PARAM_FLAGS_TRIGGED 0x40000000
69 #define PARAM_FLAGS_WAITED 0x20000000
70 #define PARAM_FLAGS_COMMITID 0x0000ffff
71 
72 typedef struct {
73     atomic_uint commitId;
74     uint16_t keyLength;
75     uint16_t valueLength;
76     char data[0];
77 } ParamNode;
78 
79 typedef struct {
80     uid_t uid;
81     gid_t gid;
82     uint16_t mode;
83     uint16_t length;
84     char data[0];
85 } ParamSecruityNode;
86 
87 typedef struct {
88     uint32_t trieNodeCount;
89     uint32_t paramNodeCount;
90     uint32_t securityNodeCount;
91     uint32_t currOffset;
92     uint32_t firstNode;
93     uint32_t dataSize;
94     uint32_t reserved_[28];
95     char data[0];
96 } ParamTrieHeader;
97 
98 struct WorkSpace_;
99 typedef struct WorkSpace_ {
100     char fileName[FILENAME_LEN_MAX + 1];
101     uint32_t (*allocTrieNode)(struct WorkSpace_ *workSpace, const char *key, uint32_t keyLen);
102     int (*compareTrieNode)(const ParamTrieNode *node, const char *key2, uint32_t key2Len);
103     ParamTrieHeader *area;
104 } WorkSpace;
105 
106 int InitWorkSpace(const char *fileName, WorkSpace *workSpace, int onlyRead);
107 void CloseWorkSpace(WorkSpace *workSpace);
108 
109 ParamTrieNode *GetTrieNode(const WorkSpace *workSpace, uint32_t offset);
110 void SaveIndex(uint32_t *index, uint32_t offset);
111 
112 ParamTrieNode *AddTrieNode(WorkSpace *workSpace, const char *key, uint32_t keyLen);
113 ParamTrieNode *FindTrieNode(const WorkSpace *workSpace, const char *key, uint32_t keyLen, uint32_t *matchLabel);
114 
115 typedef int (*TraversalTrieNodePtr)(const WorkSpace *workSpace, const ParamTrieNode *node, void *cookie);
116 int TraversalTrieNode(const WorkSpace *workSpace,
117     const ParamTrieNode *subTrie, TraversalTrieNodePtr walkFunc, void *cookie);
118 
119 uint32_t AddParamSecruityNode(WorkSpace *workSpace, const ParamAuditData *auditData);
120 uint32_t AddParamNode(WorkSpace *workSpace, const char *key, uint32_t keyLen, const char *value, uint32_t valueLen);
121 #ifdef __cplusplus
122 #if __cplusplus
123 }
124 #endif
125 #endif
126 #endif // BASE_STARTUP_PARAM_TRIE_H