1 /*
2  * Copyright (c) 2020 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 #ifndef LITE_SA_STORE_H
16 #define LITE_SA_STORE_H
17 
18 #include <ohos_types.h>
19 #include <serializer.h>
20 #include <unistd.h>
21 
22 #ifdef __cplusplus
23 #if __cplusplus
24 extern "C" {
25 #endif
26 #endif
27 
28 #define MAX_NAME_LEN 16
29 typedef struct ServiceInfo ServiceInfo;
30 typedef struct FeatureNode FeatureNode;
31 typedef struct SAStore SAStore;
32 typedef struct ListNode ListNode;
33 typedef struct PidHandle PidHandle;
34 struct ServiceInfo {
35     char name[MAX_NAME_LEN];
36     uint32 handle;
37     uintptr_t cookie;
38     FeatureNode *head;
39 };
40 
41 struct FeatureNode {
42     char name[MAX_NAME_LEN];
43     uint32 isDefault;
44     uint32 token;
45     FeatureNode *next;
46 };
47 
48 struct SAStore {
49     int saSize;
50     ListNode *root;
51     int16 mapSize;
52     int16 mapTop;
53     PidHandle *maps;
54 };
55 
56 struct ListNode {
57     ListNode *next;
58     ServiceInfo info;
59 };
60 
61 struct PidHandle {
62     pid_t pid;
63     uid_t uid;
64     uint32 handle;
65     uintptr_t cookie;
66     uint32 deadId;
67 };
68 
SASTORA_Init(SAStore * saStore)69 static inline void SASTORA_Init(SAStore *saStore)
70 {
71     saStore->saSize = 0;
72     saStore->root = NULL;
73     saStore->mapTop = 0;
74     saStore->mapSize = 0;
75     saStore->maps = NULL;
76 }
77 
78 int SASTORA_Save(SAStore *saStore, const char *service, const char *feature, const SvcIdentity *identity);
79 int SASTORA_SaveHandleByPid(SAStore *saStore, PidHandle handle);
80 int SASTORA_FindHandleByPid(SAStore *saStore, pid_t callingPid, PidHandle *handle);
81 int SASTORA_FindHandleByUidPid(SAStore *saStore, uid_t callingUid, pid_t callingPid, PidHandle *handle);
82 SvcIdentity SASTORA_Find(SAStore *saStore, const char *service, const char *feature);
83 int SASTORA_ClearByPid(SAStore *saStore, pid_t pid);
84 PidHandle SASTORA_FindPidHandleByIpcHandle(const SAStore *saStore, uint32 handle);
85 
86 #ifdef __cplusplus
87 #if __cplusplus
88 }
89 #endif
90 #endif
91 #endif // LITE_SA_STORE_H
92