1 /*
2 * Copyright (c) 2025 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 #include "resource_overlimit_mgr.h"
16
17 #include <unordered_set>
18 #include <application_context.h>
19 #include <hilog/log.h>
20 #include "file_util.h"
21 #include "hiappevent_base.h"
22
23 #undef LOG_DOMAIN
24 #define LOG_DOMAIN 0xD002DA0
25
26 #undef LOG_TAG
27 #define LOG_TAG "ResourceOverlimitMgr"
28
29 namespace {
30 inline constexpr const char* const RAWHEAP = "/rawheap";
31 }
32
33 namespace OHOS {
34 namespace HiviewDFX {
GetInstance()35 ResourceOverlimitMgr& ResourceOverlimitMgr::GetInstance()
36 {
37 static ResourceOverlimitMgr instance;
38 return instance;
39 }
40
GetRawheapDir() const41 std::string ResourceOverlimitMgr::GetRawheapDir() const
42 {
43 std::shared_ptr<OHOS::AbilityRuntime::ApplicationContext> context =
44 OHOS::AbilityRuntime::Context::GetApplicationContext();
45 if (context == nullptr) {
46 HILOG_ERROR(LOG_CORE, "Context is null.");
47 return "";
48 }
49 if (context->GetCacheDir().empty()) {
50 HILOG_ERROR(LOG_CORE, "cache dir obtained from context is empty.");
51 return "";
52 }
53 std::string path = context->GetCacheDir() + RAWHEAP;
54 if (!FileUtil::IsFileExists(path)) {
55 HILOG_ERROR(LOG_CORE, "rawheap file dosen't exist");
56 return "";
57 }
58
59 return path;
60 }
61
UpdateProperty(const std::string & property,const std::string & value) const62 int ResourceOverlimitMgr::UpdateProperty(const std::string& property, const std::string& value) const
63 {
64 if (runningId_.empty()) {
65 HILOG_ERROR(LOG_CORE, "empty runningId");
66 return ErrorCode::ERROR_UNKNOWN;
67 }
68 std::string newValue = runningId_ + "," + value;
69 std::string dir = GetRawheapDir();
70 if (dir.empty()) {
71 HILOG_ERROR(LOG_CORE, "failed to GetRawheapDir");
72 return ErrorCode::ERROR_UNKNOWN;
73 }
74 bool rtn = FileUtil::SetDirXattr(dir, property, newValue);
75 if (!rtn) {
76 HILOG_ERROR(LOG_CORE, "failed to SetDirXattr, dir: %{public}s, property: %{public}s, newValue: %{public}s,"
77 " err: %{public}s, errno: %{public}d",
78 dir.c_str(), property.c_str(), newValue.c_str(), strerror(errno), errno);
79 return ErrorCode::ERROR_UNKNOWN;
80 }
81 HILOG_INFO(LOG_CORE, "succeed to UpdateProperty, property: %{public}s, newValue: %{public}s",
82 property.c_str(), newValue.c_str());
83 return 0;
84 }
85
SetRunningId(const std::string & id)86 void ResourceOverlimitMgr::SetRunningId(const std::string& id)
87 {
88 runningId_ = id;
89 }
90
IsValid(const std::string & key,const std::string & value) const91 bool ResourceOverlimitMgr::IsValid(const std::string &key, const std::string &value) const
92 {
93 const std::map<std::string, std::set<std::string>> whiteList = {
94 {"js_heap_logtype", {"event", "event_rawheap"}}
95 };
96
97 auto it = whiteList.find(key);
98 if (it == whiteList.end()) {
99 HILOG_ERROR(LOG_CORE, "invalid config key: %{public}s", key.c_str());
100 return false;
101 }
102 if (it->second.find(value) == it->second.end()) {
103 HILOG_ERROR(LOG_CORE, "invalid value for key %{public}s: %{public}s", key.c_str(), value.c_str());
104 return false;
105 }
106
107 return true;
108 }
109
SetEventConfig(const std::map<std::string,std::string> & configMap) const110 int ResourceOverlimitMgr::SetEventConfig(const std::map<std::string, std::string>& configMap) const
111 {
112 int rtn = ErrorCode::ERROR_UNKNOWN;
113 for (const auto& [key, value] : configMap) {
114 if (!IsValid(key, value)) {
115 HILOG_WARN(LOG_CORE, "failed SetEventConfig, invalid key %{public}s, value: %{public}s",
116 key.c_str(), value.c_str());
117 continue;
118 }
119 if (key == "js_heap_logtype") {
120 rtn = UpdateProperty("user.event_config." + key, value); // key must start with 'user.'
121 }
122 }
123
124 return rtn;
125 }
126 } // HiviewDFX
127 } // OHOS
128