1 /*
2 * Copyright (C) 2024 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 <filesystem>
16 #include <iostream>
17 #include "include/FileDescriptor.h"
18 #include "include/sp_utils.h"
19 #include "include/sp_log.h"
20
21 namespace fs = std::filesystem;
22 namespace OHOS {
23 namespace SmartPerf {
24 const size_t FDSRESERVE_SIZE = 512;
25 const size_t FDTOTALSRESERVE_SIZE = 1024;
ItemData()26 std::map<std::string, std::string> FileDescriptor::ItemData()
27 {
28 std::map<std::string, std::string> result;
29 std::string& fds = result["fds"];
30 std::string& fdTotal = result["fdTotal"];
31 fds.reserve(FDSRESERVE_SIZE);
32 fdTotal.reserve(FDTOTALSRESERVE_SIZE);
33 idNum = processId.size();
34 for (size_t i = 0; i < idNum; i++) {
35 GetFds(processId[i], fds, fdTotal);
36 }
37 #ifndef FUZZ_TEST
38 LOGD("FileDescriptor::ItemData %s %s", fds.c_str(), fdTotal.c_str());
39 #endif
40 return result;
41 }
42
SetPackageName(const std::string & pName)43 void FileDescriptor::SetPackageName(const std::string &pName)
44 {
45 packageName = pName;
46 }
47
SetProcessId(const std::string & pid)48 void FileDescriptor::SetProcessId(const std::string &pid)
49 {
50 processId.clear();
51 SPUtils::StrSplit(pid, " ", processId);
52 }
53
GetFds(const std::string & pid,std::string & fds,std::string & fdTotal)54 void FileDescriptor::GetFds(const std::string &pid, std::string &fds, std::string &fdTotal)
55 {
56 std::string directoryPath = "/proc/";
57 directoryPath.append(pid).append("/fd/");
58 int cnt = 0;
59 std::error_code ec;
60 if (fs::exists(directoryPath) && fs::is_directory(directoryPath)) {
61 fs::directory_iterator dir_iter(directoryPath, ec);
62 if (ec) {
63 #ifndef FUZZ_TEST
64 LOGD("Get fds info fail (%s)", ec.message().c_str());
65 fds.append(pid).append(":").append("0");
66 fdTotal.append(pid).append(":").append("0").append("|");
67 #endif
68 return;
69 }
70 fds.append(pid).append(":");
71 for (const auto &entry : dir_iter) {
72 std::string fileSymlink = fs::read_symlink(directoryPath + entry.path().filename().string(), ec);
73 if (ec) {
74 #ifndef FUZZ_TEST
75 LOGD("Get (%s) info fail (%s)", entry.path().c_str(), ec.message().c_str());
76 break;
77 #endif
78 }
79 ++cnt;
80 fds.append(entry.path().filename().string()).append("->").append(fileSymlink).append(" ");
81 }
82 fdTotal.append(pid).append(":").append(std::to_string(cnt));
83 if (idNum > 1) {
84 fds.append("|");
85 fdTotal.append("|");
86 }
87 } else {
88 processId.erase(std::remove(processId.begin(), processId.end(), pid), processId.end());
89 #ifndef FUZZ_TEST
90 LOGD("(%s) Not exist.", directoryPath.c_str());
91 #endif
92 }
93 }
94
SetProcessIdForFuzzTest(const std::vector<std::string> & pid)95 void FileDescriptor::SetProcessIdForFuzzTest(const std::vector<std::string> &pid)
96 {
97 processId = pid;
98 }
99 }
100 }