github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/util/proc_util.cc (about) 1 // Copyright 2018 The gVisor Authors. 2 // 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 "test/util/proc_util.h" 16 17 #include <algorithm> 18 #include <iostream> 19 #include <vector> 20 21 #include "absl/strings/ascii.h" 22 #include "absl/strings/str_cat.h" 23 #include "absl/strings/str_split.h" 24 #include "absl/strings/string_view.h" 25 #include "test/util/fs_util.h" 26 #include "test/util/test_util.h" 27 28 namespace gvisor { 29 namespace testing { 30 31 // Parses a single line from /proc/<xxx>/maps. 32 PosixErrorOr<ProcMapsEntry> ParseProcMapsLine(absl::string_view line) { 33 ProcMapsEntry map_entry = {}; 34 35 // Limit splitting to 6 parts so that if there is a file path and it contains 36 // spaces, the file path is not split. 37 std::vector<std::string> parts = 38 absl::StrSplit(line, absl::MaxSplits(' ', 5), absl::SkipEmpty()); 39 40 // parts.size() should be 6 if there is a file name specified, and 5 41 // otherwise. 42 if (parts.size() < 5) { 43 return PosixError(EINVAL, absl::StrCat("Invalid line: ", line)); 44 } 45 46 // Address range in the form X-X where X are hex values without leading 0x. 47 std::vector<std::string> addresses = absl::StrSplit(parts[0], '-'); 48 if (addresses.size() != 2) { 49 return PosixError(EINVAL, 50 absl::StrCat("Invalid address range: ", parts[0])); 51 } 52 ASSIGN_OR_RETURN_ERRNO(map_entry.start, AtoiBase(addresses[0], 16)); 53 ASSIGN_OR_RETURN_ERRNO(map_entry.end, AtoiBase(addresses[1], 16)); 54 55 // Permissions are four bytes of the form rwxp or - if permission not set. 56 if (parts[1].size() != 4) { 57 return PosixError(EINVAL, 58 absl::StrCat("Invalid permission field: ", parts[1])); 59 } 60 61 map_entry.readable = parts[1][0] == 'r'; 62 map_entry.writable = parts[1][1] == 'w'; 63 map_entry.executable = parts[1][2] == 'x'; 64 map_entry.priv = parts[1][3] == 'p'; 65 66 ASSIGN_OR_RETURN_ERRNO(map_entry.offset, AtoiBase(parts[2], 16)); 67 68 std::vector<std::string> device = absl::StrSplit(parts[3], ':'); 69 if (device.size() != 2) { 70 return PosixError(EINVAL, absl::StrCat("Invalid device: ", parts[3])); 71 } 72 ASSIGN_OR_RETURN_ERRNO(map_entry.major, AtoiBase(device[0], 16)); 73 ASSIGN_OR_RETURN_ERRNO(map_entry.minor, AtoiBase(device[1], 16)); 74 75 ASSIGN_OR_RETURN_ERRNO(map_entry.inode, Atoi<int64_t>(parts[4])); 76 if (parts.size() == 6) { 77 // A filename is present. However, absl::StrSplit retained the whitespace 78 // between the inode number and the filename. 79 map_entry.filename = 80 std::string(absl::StripLeadingAsciiWhitespace(parts[5])); 81 } 82 83 return map_entry; 84 } 85 86 PosixErrorOr<std::vector<ProcMapsEntry>> ParseProcMaps( 87 absl::string_view contents) { 88 std::vector<ProcMapsEntry> entries; 89 auto lines = absl::StrSplit(contents, '\n', absl::SkipEmpty()); 90 for (const auto& l : lines) { 91 std::cout << "line: " << l << std::endl; 92 ASSIGN_OR_RETURN_ERRNO(auto entry, ParseProcMapsLine(l)); 93 entries.push_back(entry); 94 } 95 return entries; 96 } 97 98 PosixErrorOr<bool> IsVsyscallEnabled() { 99 ASSIGN_OR_RETURN_ERRNO(auto contents, GetContents("/proc/self/maps")); 100 ASSIGN_OR_RETURN_ERRNO(auto maps, ParseProcMaps(contents)); 101 return std::any_of(maps.begin(), maps.end(), [](const ProcMapsEntry& e) { 102 return e.filename == "[vsyscall]"; 103 }); 104 } 105 106 } // namespace testing 107 } // namespace gvisor