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 #include "running_lock_proxy.h"
17
18 #include <cmath>
19 #include "power_log.h"
20
21 namespace OHOS {
22 namespace PowerMgr {
AddRunningLock(pid_t pid,pid_t uid,const sptr<IRemoteObject> & remoteObj)23 void RunningLockProxy::AddRunningLock(pid_t pid, pid_t uid, const sptr<IRemoteObject>& remoteObj)
24 {
25 std::string proxyKey = AssembleProxyKey(pid, uid);
26 auto proxyIter = proxyMap_.find(proxyKey);
27 if (proxyIter == proxyMap_.end()) {
28 std::vector<sptr<IRemoteObject>> tmpLockList {};
29 tmpLockList.push_back(remoteObj);
30 std::tie(proxyIter, std::ignore) = proxyMap_.emplace(proxyKey, std::make_pair(tmpLockList, 0));
31 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Add runninglock proxy, proxyKey=%{public}s", proxyKey.c_str());
32 } else {
33 auto& remoteObjList = proxyIter->second.first;
34 if (std::find(remoteObjList.begin(), remoteObjList.end(), remoteObj) != remoteObjList.end()) {
35 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Runninglock is existed, proxyKey=%{public}s", proxyKey.c_str());
36 return;
37 }
38 remoteObjList.push_back(remoteObj);
39 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Insert runninglock, proxyKey=%{public}s", proxyKey.c_str());
40 }
41 }
42
RemoveRunningLock(pid_t pid,pid_t uid,const sptr<IRemoteObject> & remoteObj)43 void RunningLockProxy::RemoveRunningLock(pid_t pid, pid_t uid, const sptr<IRemoteObject>& remoteObj)
44 {
45 std::string proxyKey = AssembleProxyKey(pid, uid);
46 auto proxyIter = proxyMap_.find(proxyKey);
47 if (proxyIter == proxyMap_.end()) {
48 POWER_HILOGI(FEATURE_RUNNING_LOCK,
49 "Runninglock proxyKey is not existed, proxyKey=%{public}s", proxyKey.c_str());
50 return;
51 }
52 auto& remoteObjList = proxyIter->second.first;
53 auto remoteObjIter = std::find(remoteObjList.begin(), remoteObjList.end(), remoteObj);
54 if (remoteObjIter == remoteObjList.end()) {
55 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Runninglock is not existed, proxyKey=%{public}s", proxyKey.c_str());
56 return;
57 }
58 remoteObjList.erase(remoteObjIter);
59 if (remoteObjList.empty()) {
60 proxyMap_.erase(proxyKey);
61 POWER_HILOGD(FEATURE_RUNNING_LOCK, "Runninglock list is empty, earse proxyKey=%{public}s", proxyKey.c_str());
62 }
63 }
64
GetRemoteObjectList(pid_t pid,pid_t uid)65 std::vector<sptr<IRemoteObject>> RunningLockProxy::GetRemoteObjectList(pid_t pid, pid_t uid)
66 {
67 std::string proxyKey = AssembleProxyKey(pid, uid);
68 auto proxyIter = proxyMap_.find(proxyKey);
69 if (proxyIter != proxyMap_.end()) {
70 return proxyIter->second.first;
71 }
72 return std::vector<sptr<IRemoteObject>>();
73 }
74
IsProxied(pid_t pid,pid_t uid)75 bool RunningLockProxy::IsProxied(pid_t pid, pid_t uid)
76 {
77 std::string proxyKey = AssembleProxyKey(pid, uid);
78 auto proxyIter = proxyMap_.find(proxyKey);
79 if (proxyIter == proxyMap_.end()) {
80 return false;
81 }
82 return proxyIter->second.second != 0;
83 }
84
IncreaseProxyCnt(pid_t pid,pid_t uid,const std::function<void (void)> & proxyRunningLock)85 bool RunningLockProxy::IncreaseProxyCnt(pid_t pid, pid_t uid, const std::function<void(void)>& proxyRunningLock)
86 {
87 std::string proxyKey = AssembleProxyKey(pid, uid);
88 auto proxyIter = proxyMap_.find(proxyKey);
89 if (proxyIter == proxyMap_.end()) {
90 std::tie(proxyIter, std::ignore) = proxyMap_.emplace(proxyKey,
91 std::make_pair<std::vector<sptr<IRemoteObject>>, int32_t>({}, 0));
92 }
93 proxyIter->second.second++;
94 POWER_HILOGI(FEATURE_RUNNING_LOCK, "IncreaseProxyCnt proxykey=%{public}s proxycnt=%{public}d",
95 proxyKey.c_str(), proxyIter->second.second);
96 if (proxyIter->second.second > 1) {
97 return false;
98 }
99 proxyRunningLock();
100 return true;
101 }
102
DecreaseProxyCnt(pid_t pid,pid_t uid,const std::function<void (void)> & unProxyRunningLock)103 bool RunningLockProxy::DecreaseProxyCnt(pid_t pid, pid_t uid, const std::function<void(void)>& unProxyRunningLock)
104 {
105 std::string proxyKey = AssembleProxyKey(pid, uid);
106 auto proxyIter = proxyMap_.find(proxyKey);
107 if (proxyIter == proxyMap_.end()) {
108 return false;
109 }
110 proxyIter->second.second = std::max(0, proxyIter->second.second - 1);
111 POWER_HILOGI(FEATURE_RUNNING_LOCK, "DecreaseProxyCnt proxykey=%{public}s proxycnt=%{public}d",
112 proxyKey.c_str(), proxyIter->second.second);
113 if (proxyIter->second.second > 0) {
114 return false;
115 }
116 if (proxyIter->second.first.empty()) {
117 proxyMap_.erase(proxyIter);
118 }
119 unProxyRunningLock();
120 return true;
121 }
122
DumpProxyInfo()123 std::string RunningLockProxy::DumpProxyInfo()
124 {
125 std::string result {""};
126 int index = 0;
127 for (const auto& [key, value] : proxyMap_) {
128 index++;
129 result.append(" index=").append(std::to_string(index))
130 .append(" pid_uid=").append(key)
131 .append(" lock_cnt=").append(std::to_string(value.first.size()))
132 .append(" proxy_cnt=").append(std::to_string(value.second)).append("\n");
133 }
134 return result;
135 }
136
ResetRunningLocks()137 void RunningLockProxy::ResetRunningLocks()
138 {
139 POWER_HILOGI(FEATURE_RUNNING_LOCK, "reset proxycnt");
140 for (auto proxyIter = proxyMap_.begin(); proxyIter != proxyMap_.end();) {
141 proxyIter->second.second = 0;
142 if (proxyIter->second.first.empty()) {
143 proxyMap_.erase(proxyIter++);
144 } else {
145 proxyIter++;
146 }
147 }
148 }
149
Clear()150 void RunningLockProxy::Clear()
151 {
152 proxyMap_.clear();
153 }
154
AssembleProxyKey(pid_t pid,pid_t uid)155 std::string RunningLockProxy::AssembleProxyKey(pid_t pid, pid_t uid)
156 {
157 return std::to_string(pid) + "_" + std::to_string(uid);
158 }
159 } // namespace PowerMgr
160 } // namespace OHOS
161
162