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 #ifndef WATCHER_MANAGER_H_
16 #define WATCHER_MANAGER_H_
17 #include <atomic>
18 #include <iostream>
19 #include <map>
20 #include <mutex>
21 #include <thread>
22 #include <vector>
23
24 #include "iremote_stub.h"
25 #include "iwatcher.h"
26 #include "list.h"
27 #include "message_parcel.h"
28 #include "param_utils.h"
29 #include "parcel.h"
30 #include "param_message.h"
31 #include "system_ability.h"
32 #include "watcher_manager_stub.h"
33
34 namespace OHOS {
35 namespace init_param {
36 class WatcherNode;
37 class ParamWatcherList;
38 class WatcherGroup;
39 class RemoteWatcher;
40 using WatcherGroupPtr = WatcherGroup *;
41 using RemoteWatcherPtr = RemoteWatcher *;
42 using WatcherNodePtr = WatcherNode *;
43 using ListNodePtr = ListNode *;
44 using ListHeadPtr = ListHead *;
45 using ParamWatcherListPtr = ParamWatcherList *;
46 using ParamWatcherProcessor = std::function<void(ParamWatcherListPtr list, WatcherNodePtr node, uint32_t index)>;
47
48 class WatcherManager : public SystemAbility, public WatcherManagerStub {
49 public:
50 friend class WatcherGroup;
51 DECLARE_SYSTEM_ABILITY(WatcherManager);
52 DISALLOW_COPY_AND_MOVE(WatcherManager);
53 explicit WatcherManager(int32_t systemAbilityId, bool runOnCreate = true)
SystemAbility(systemAbilityId,runOnCreate)54 : SystemAbility(systemAbilityId, runOnCreate)
55 {
56 }
57 ~WatcherManager() override;
58
59 // For death event procession
60 class DeathRecipient final : public IRemoteObject::DeathRecipient {
61 public:
DeathRecipient(WatcherManager * manager)62 explicit DeathRecipient(WatcherManager *manager) : manager_(manager) {}
63 ~DeathRecipient() final = default;
64 DISALLOW_COPY_AND_MOVE(DeathRecipient);
OnRemoteDied(const wptr<IRemoteObject> & remote)65 void OnRemoteDied(const wptr<IRemoteObject> &remote) final
66 {
67 manager_->OnRemoteDied(remote);
68 }
69 private:
70 WatcherManager *manager_;
71 };
GetDeathRecipient(void)72 sptr<IRemoteObject::DeathRecipient> GetDeathRecipient(void)
73 {
74 return deathRecipient_;
75 }
76 int32_t AddRemoteWatcher(uint32_t id, uint32_t &watcherId, const sptr<IWatcher> &watcher) override;
77 int32_t DelRemoteWatcher(uint32_t remoteWatcherId) override;
78 int32_t AddWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) override;
79 int32_t DelWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) override;
80 int32_t RefreshWatcher(const std::string &keyPrefix, uint32_t remoteWatcherId) override;
81 protected:
82 void OnStart() override;
83 void OnStop() override;
84 int Dump(int fd, const std::vector<std::u16string>& args) override;
85 private:
86 void RunLoop();
87 void StartLoop();
88 void StopLoop();
89 void SendLocalChange(const std::string &keyPrefix, uint32_t remoteWatcherId);
90 int SendMessage(WatcherGroupPtr group, int type);
91 int GetServerFd(bool retry);
92 int GetRemoteWatcherId(uint32_t &remoteWatcherId);
93 int GetGroupId(uint32_t &groupId);
94 // for remote watcher
95 int AddRemoteWatcher(RemoteWatcherPtr remoteWatcher);
96 RemoteWatcherPtr GetRemoteWatcher(uint32_t remoteWatcherId);
97 RemoteWatcherPtr GetRemoteWatcher(const wptr<IRemoteObject> &remote);
98 void DelRemoteWatcher(RemoteWatcherPtr remoteWatcher);
99 // for group
100 WatcherGroupPtr AddWatcherGroup(const std::string &keyPrefix);
101 void AddRealWatcherGroup(const std::string &keyPrefix, int type);
102 WatcherGroupPtr GetWatcherGroup(const std::string &keyPrefix);
103 WatcherGroupPtr GetWatcherGroup(uint32_t groupId);
104 void DelWatcherGroup(WatcherGroupPtr group);
105 // for param watcher
106 int AddParamWatcher(WatcherGroupPtr group, RemoteWatcherPtr remoteWatcher);
107 int DelParamWatcher(WatcherGroupPtr group, RemoteWatcherPtr remoteWatcher);
108 // for app param watcher permission
109 int32_t CheckAppWatchPermission(const std::string &keyPrefix);
110 // for process message form init
111 void ProcessWatcherMessage(const ParamMessage *msg);
112 // for client died
113 void OnRemoteDied(const wptr<IRemoteObject> &remote);
114 void OnRemoteDied(RemoteWatcherPtr remoteWatcher);
115 // clear
116 void Clear(void);
117 // dump
118 void DumpAllGroup(int fd, ParamWatcherProcessor dumpHandle);
119 private:
120 std::atomic<uint32_t> remoteWatcherId_ { 0 };
121 std::atomic<uint32_t> groupId_ { 0 };
122 std::mutex mutex_;
123 std::mutex watcherMutex_;
124 int serverFd_ { -1 };
125 std::thread *pRecvThread_ { nullptr };
126 std::atomic<bool> stop_ { false };
127 std::map<std::string, WatcherGroupPtr> groupMap_ {};
128 std::map<std::string, uint32_t> groupRealMap_ {};
129 sptr<IRemoteObject::DeathRecipient> deathRecipient_ {};
130 ParamWatcherListPtr watcherGroups_ {};
131 ParamWatcherListPtr remoteWatchers_ {};
132 };
133
134 class WatcherNode {
135 public:
WatcherNode(uint32_t nodeId)136 explicit WatcherNode(uint32_t nodeId) : nodeId_(nodeId)
137 {
138 OH_ListInit(&node_);
139 }
140 virtual ~WatcherNode(void) = default;
GetNodeId(void)141 uint32_t GetNodeId(void) const
142 {
143 return nodeId_;
144 }
145
146 void AddToList(ListHeadPtr list);
147 void RemoveFromList(ListHeadPtr list);
148 WatcherNodePtr GetNext(ListHeadPtr list);
149 static WatcherNodePtr GetFromList(ListHeadPtr list, uint32_t nodeId);
150 static WatcherNodePtr GetNextFromList(ListHeadPtr list, uint32_t nodeId);
ConvertNodeToBase(ListNodePtr node)151 static WatcherNodePtr ConvertNodeToBase(ListNodePtr node)
152 {
153 return reinterpret_cast<WatcherNodePtr>((char *)node - (char*)(&(((WatcherNodePtr)0)->node_)));
154 }
GetListNode()155 ListNodePtr GetListNode()
156 {
157 return &node_;
158 }
159 private:
160 static int CompareNode(ListNodePtr node, ListNodePtr newNode);
161 static int CompareData(ListNodePtr node, void *data);
162 static int Greater(ListNodePtr node, void *data);
163 ListNode node_;
164 uint32_t nodeId_;
165 };
166
167 template<typename T>
ConvertTo(WatcherNodePtr node)168 inline T *ConvertTo(WatcherNodePtr node)
169 {
170 #ifdef PARAM_WATCHER_RTTI_ENABLE
171 // when -frtti is enabled, we use dynamic cast directly
172 // to achieve the correct base class side-to-side conversion.
173 T *obj = dynamic_cast<T *>(node);
174 #else
175 // adjust pointer position when multiple inheritance.
176 void *tmp = reinterpret_cast<void *>(node);
177 // when -frtti is not enable, we use static cast.
178 // static cast is not safe enough, but we have checked before we get here.
179 T *obj = static_cast<T *>(tmp);
180 #endif
181 return obj;
182 }
183
184 class ParamWatcher : public WatcherNode {
185 public:
ParamWatcher(uint32_t watcherId)186 explicit ParamWatcher(uint32_t watcherId) : WatcherNode(watcherId) {}
~ParamWatcher(void)187 ~ParamWatcher(void) override {}
188 };
189
190 class ParamWatcherList {
191 public:
ParamWatcherList(void)192 ParamWatcherList(void)
193 {
194 OH_ListInit(&nodeList_);
195 }
196 ~ParamWatcherList(void) = default;
197
Empty(void)198 bool Empty(void) const
199 {
200 return nodeList_.next == &nodeList_;
201 }
GetNodeCount(void)202 uint32_t GetNodeCount(void) const
203 {
204 return nodeCount_;
205 }
206 int AddNode(WatcherNodePtr node);
207 int RemoveNode(WatcherNodePtr node);
208 WatcherNodePtr GetNode(uint32_t nodeId);
209 WatcherNodePtr GetNextNodeSafe(WatcherNodePtr node);
210 WatcherNodePtr GetNextNode(WatcherNodePtr node);
211 void TraversalNode(ParamWatcherProcessor handle);
212 void TraversalNodeSafe(ParamWatcherProcessor processor);
213 public:
214 ListHead nodeList_ {};
215 uint32_t nodeCount_ = 0;
216 };
217
218 class RemoteWatcher : public WatcherNode, public ParamWatcherList {
219 public:
RemoteWatcher(uint32_t watcherId,const sptr<IWatcher> & watcher)220 RemoteWatcher(uint32_t watcherId, const sptr<IWatcher> &watcher)
221 : WatcherNode(watcherId), ParamWatcherList(), watcher_(watcher) {}
222 ~RemoteWatcher(void) override;
223
GetRemoteWatcherId(void)224 uint32_t GetRemoteWatcherId(void) const
225 {
226 return GetNodeId();
227 }
GetAgentId(void)228 uint32_t GetAgentId(void) const
229 {
230 return id_;
231 }
CheckAgent(pid_t calling)232 bool CheckAgent(pid_t calling) const
233 {
234 return id_ == static_cast<uint32_t>(calling);
235 }
SetAgentId(uint32_t id)236 void SetAgentId(uint32_t id)
237 {
238 id_ = id;
239 }
ProcessParameterChange(const std::string & prefix,const std::string & name,const std::string & value)240 void ProcessParameterChange(const std::string &prefix, const std::string &name, const std::string &value)
241 {
242 watcher_->OnParameterChange(prefix, name, value);
243 }
GetWatcher(void)244 sptr<IWatcher> GetWatcher(void)
245 {
246 return watcher_;
247 }
248 private:
249 uint32_t id_ = { 0 };
250 sptr<IWatcher> watcher_ {};
251 };
252
253 class WatcherGroup : public WatcherNode, public ParamWatcherList {
254 public:
WatcherGroup(uint32_t groupId,const std::string & key)255 WatcherGroup(uint32_t groupId, const std::string &key)
256 : WatcherNode(groupId), ParamWatcherList(), keyPrefix_(key) {}
257 ~WatcherGroup(void) override;
258
259 void ProcessParameterChange(WatcherManager *manager, const std::string &name, const std::string &value);
GetKeyPrefix()260 const std::string GetKeyPrefix() const
261 {
262 return keyPrefix_;
263 }
GetGroupId()264 uint32_t GetGroupId() const
265 {
266 return GetNodeId();
267 }
268 private:
269 std::string keyPrefix_ { };
270 };
271 } // namespace init_param
272 } // namespace OHOS
273 #endif // WATCHER_MANAGER_H_