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 "util/white_list.h"
16 #include "internal_inc/osal.h"
17 #include "util/ffrt_facade.h"
18
19 namespace {
20 constexpr char CONF_FILEPATH[] = "/etc/ffrt/ffrt_whitelist.conf";
21 constexpr int INDENT_SPACE_NUM = 4;
22 constexpr int PROCESS_NAME_BUFFER_LENGTH = 1024;
23 }
24
25 namespace ffrt {
WhiteList()26 WhiteList::WhiteList()
27 {
28 LoadFromFile();
29 }
30
GetInstance()31 WhiteList& WhiteList::GetInstance()
32 {
33 static WhiteList instance;
34 return instance;
35 }
36
IsEnabled(const std::string & functionName,bool defaultWhenAbnormal)37 bool WhiteList::IsEnabled(const std::string& functionName, bool defaultWhenAbnormal)
38 {
39 if (TryRefreshWhiteListOnInit()) {
40 auto it = whiteList_.find(functionName);
41 if (it != whiteList_.end()) {
42 return it->second;
43 }
44 } else {
45 std::unique_lock lock(whitelistMutex_);
46 auto it = whiteList_.find(functionName);
47 if (it != whiteList_.end()) {
48 return it->second;
49 }
50 }
51
52 // 若白名单加载失败或不在白名单中的默认返回值
53 return defaultWhenAbnormal;
54 }
55
LoadFromFile()56 void WhiteList::LoadFromFile()
57 {
58 char processNameChar[PROCESS_NAME_BUFFER_LENGTH] {};
59 GetProcessName(processNameChar, PROCESS_NAME_BUFFER_LENGTH);
60 if (strlen(processNameChar) == 0) {
61 FFRT_LOGW("Get process name failed.");
62 return;
63 }
64 std::string processName = std::string(processNameChar);
65 #ifdef OHOS_STANDARD_SYSTEM
66 std::string whiteProcess;
67 std::ifstream file(CONF_FILEPATH);
68 std::string functionName;
69 if (file.is_open()) {
70 while (std::getline(file, whiteProcess)) {
71 size_t pos = whiteProcess.find("{");
72 if (pos != std::string::npos) {
73 functionName = whiteProcess.substr(0, pos - 1);
74 whiteList_[functionName] = false;
75 } else if ((whiteProcess != "}" && whiteProcess != "") &&
76 processName.find(whiteProcess.substr(INDENT_SPACE_NUM)) != std::string::npos) {
77 whiteList_[functionName] = true;
78 }
79 }
80 } else {
81 // 当文件不存在或者无权限时默认都关
82 FFRT_LOGW("white_list.conf does not exist or file permission denied");
83 }
84 #else
85 whiteList_["IsInSFFRTList"] = false;
86 if (processName.find("zygote") != std::string::npos) {
87 whiteList_["IsInSFFRTList"] = true;
88 }
89 if (processName.find("CameraDaemon") != std::string::npos) {
90 whiteList_["SetThreadAttr"] = true;
91 whiteList_["CreateCPUWorker"] = true;
92 whiteList_["HandleTaskNotifyConservative"] = true;
93 }
94 #endif // OHOS_STANDARD_SYSTEM
95 }
96
TryRefreshWhiteListOnInit()97 bool WhiteList::TryRefreshWhiteListOnInit()
98 {
99 if (FFRT_LIKELY(whitelistInited_)) {
100 return true;
101 }
102
103 // 确保在 init 与 facade 初始化之间和之后各构造一次白名单
104 if (!ffrt::GetInitFlag()) {
105 return false;
106 }
107
108 std::lock_guard lock(whitelistMutex_);
109 if (!whitelistInited_) {
110 LoadFromFile();
111 whitelistInited_ = true;
112 }
113 return true;
114 }
115 } // namespace ffrt