1
2 /*
3 * Copyright (c) 2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16 #include "permission_grant_event.h"
17 #include <ctime>
18 #include <unistd.h>
19 #include "accesstoken_dfx_define.h"
20 #include "accesstoken_log.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 static constexpr int MS_PER_S = 1000;
26 static constexpr int NS_PER_MS = 1000000;
27
GetRealTimeStamp()28 static uint64_t GetRealTimeStamp()
29 {
30 struct timespec curTime;
31 if (clock_gettime(CLOCK_MONOTONIC, &curTime) != 0) {
32 return 0;
33 }
34
35 return curTime.tv_sec * MS_PER_S + curTime.tv_nsec / NS_PER_MS;
36 }
37
AddEvent(AccessTokenID tokenID,const std::string & permissionName,uint64_t & timestamp)38 void PermissionGrantEvent::AddEvent(AccessTokenID tokenID, const std::string& permissionName, uint64_t& timestamp)
39 {
40 std::unique_lock<std::mutex> lck(lock_);
41 struct GrantEvent event = {
42 .tokenID = tokenID,
43 .permissionName = permissionName,
44 .timeStamp = GetRealTimeStamp()
45 };
46
47 timestamp = event.timeStamp;
48 permGrantEventList_.emplace_back(event);
49 }
50
NotifyPermGrantStoreResult(bool isStoreSucc,uint64_t timestamp)51 void PermissionGrantEvent::NotifyPermGrantStoreResult(bool isStoreSucc, uint64_t timestamp)
52 {
53 std::unique_lock<std::mutex> lck(lock_);
54 for (auto iter = permGrantEventList_.begin(); iter != permGrantEventList_.end();) {
55 if (timestamp >= iter->timeStamp) {
56 if (!isStoreSucc) {
57 HiSysEventWrite(HiviewDFX::HiSysEvent::Domain::ACCESS_TOKEN, "PERMISSION_CHECK",
58 HiviewDFX::HiSysEvent::EventType::FAULT, "CODE", PERMISSION_STORE_ERROR,
59 "CALLER_TOKENID", iter->tokenID, "PERMISSION_NAME", iter->permissionName);
60 }
61 iter = permGrantEventList_.erase(iter);
62 } else {
63 iter++;
64 }
65 }
66 return;
67 }
68 } // namespace AccessToken
69 } // namespace Security
70 } // namespace OHOS
71