1 /*
2  * Copyright (c) 2023 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_COMMON_H
17 #define BASE_STARTUP_PARAM_COMMON_H
18 #include <stdio.h>
19 #include <stdint.h>
20 #include <stdarg.h>
21 #ifndef __LITEOS_M__
22 #include <pthread.h>
23 #endif
24 #include "param_atomic.h"
25 #ifdef __cplusplus
26 #if __cplusplus
27 extern "C" {
28 #endif
29 #endif
30 
31 // support mutex
32 #ifndef __LITEOS_M__
33 typedef struct {
34     pthread_rwlock_t rwlock;
35 } ParamRWMutex;
36 
37 typedef struct {
38     pthread_mutex_t mutex;
39 } ParamMutex;
40 #else
41 typedef struct {
42     uint32_t mutex;
43 } ParamRWMutex;
44 
45 typedef struct {
46     uint32_t mutex;
47 } ParamMutex;
48 #endif
49 
50 typedef struct {
51     int shmid;
52 } MemHandle;
53 
54 typedef struct {
55     uint32_t left;
56     uint32_t right;
57     uint32_t child;
58     uint32_t labelIndex;
59     uint32_t dataIndex;
60     uint16_t selinuxLabel;
61     uint16_t length;
62     char key[0];
63 } ParamTrieNode;
64 
65 #define PARAM_FLAGS_MODIFY 0x80000000
66 #define PARAM_FLAGS_TRIGGED 0x40000000
67 #define PARAM_FLAGS_WAITED 0x20000000
68 #define PARAM_FLAGS_PERSIST 0x10000000
69 #define PARAM_FLAGS_COMMITID 0x0000ffff
70 
71 #define PARAM_TYPE_MASK   0x0f
72 #define PARAM_TYPE_STRING 0x00
73 #define PARAM_TYPE_INT    0x01
74 #define PARAM_TYPE_BOOL   0x02
75 
76 typedef struct {
77     ATOMIC_UINT32 commitId;
78     uint8_t type;
79     uint8_t keyLength;
80     uint16_t valueLength;
81     char data[0];
82 } ParamNode;
83 
84 typedef struct {
85     uid_t uid;
86     gid_t gid;
87     uint32_t selinuxIndex;
88     uint16_t mode;
89     uint8_t type;
90     uint8_t length;
91     uint32_t memberNum;
92     uid_t members[0];
93 } ParamSecurityNode;
94 
95 typedef struct {
96     ATOMIC_LLONG commitId;
97     ATOMIC_LLONG commitPersistId;
98     uint32_t trieNodeCount;
99     uint32_t paramNodeCount;
100     uint32_t securityNodeCount;
101     uint32_t currOffset;
102     uint32_t spaceSizeOffset;
103     uint32_t firstNode;
104     uint32_t dataSize;
105     char data[0];
106 } ParamTrieHeader;
107 
108 typedef struct WorkSpace_ {
109     unsigned int flags;
110     MemHandle memHandle;
111     ParamTrieHeader *area;
112     ATOMIC_UINT32 rwSpaceLock;
113     uint32_t spaceSize;
114     uint32_t spaceIndex;
115     ParamRWMutex rwlock;
116     char fileName[0];
117 } WorkSpace;
118 
119 typedef struct {
120     uint8_t updaterMode;
121     void (*logFunc)(int logLevel, uint32_t domain, const char *tag, const char *fmt, va_list vargs);
122     int (*setfilecon)(const char *name, const char *content);
123     int (*getServiceGroupIdByPid)(pid_t pid, gid_t *gids, uint32_t gidSize);
124 } PARAM_WORKSPACE_OPS;
125 
126 typedef struct CachedParameter_ {
127     struct WorkSpace_ *workspace;
128     const char *(*cachedParameterCheck)(struct CachedParameter_ *param, int *changed);
129     long long spaceCommitId;
130     uint32_t dataCommitId;
131     uint32_t dataIndex;
132     uint32_t bufferLen;
133     uint32_t nameLen;
134     char *paramValue;
135     char data[0];
136 } CachedParameter;
137 
138 typedef void *CachedHandle;
139 
140 typedef struct _SpaceSize {
141     uint32_t maxLabelIndex;
142     uint32_t spaceSize[0];
143 } WorkSpaceSize;
144 
145 #ifdef __cplusplus
146 #if __cplusplus
147 }
148 #endif
149 #endif
150 #endif  // BASE_STARTUP_PARAM_COMMON_H