github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/util/file_descriptor.h (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 #ifndef GVISOR_TEST_UTIL_FILE_DESCRIPTOR_H_ 16 #define GVISOR_TEST_UTIL_FILE_DESCRIPTOR_H_ 17 18 #include <fcntl.h> 19 #include <sys/stat.h> 20 #include <sys/types.h> 21 #include <unistd.h> 22 23 #include <algorithm> 24 #include <string> 25 26 #include "gmock/gmock.h" 27 #include "absl/strings/str_cat.h" 28 #include "absl/strings/str_format.h" 29 #include "test/util/logging.h" 30 #include "test/util/posix_error.h" 31 #include "test/util/save_util.h" 32 33 namespace gvisor { 34 namespace testing { 35 36 // FileDescriptor is an RAII type class which takes ownership of a file 37 // descriptor. It will close the FD when this object goes out of scope. 38 class FileDescriptor { 39 public: 40 // Constructs an empty FileDescriptor (one that does not own a file 41 // descriptor). 42 FileDescriptor() = default; 43 44 // Constructs a FileDescriptor that owns fd. If fd is negative, constructs an 45 // empty FileDescriptor. 46 explicit FileDescriptor(int fd) { set_fd(fd); } 47 48 FileDescriptor(FileDescriptor&& orig) : fd_(orig.release()) {} 49 50 FileDescriptor& operator=(FileDescriptor&& orig) { 51 reset(orig.release()); 52 return *this; 53 } 54 55 PosixErrorOr<FileDescriptor> Dup() const { 56 if (fd_ < 0) { 57 return PosixError(EINVAL, "Attempting to Dup unset fd"); 58 } 59 60 int fd = dup(fd_); 61 if (fd < 0) { 62 return PosixError(errno, absl::StrCat("dup ", fd_)); 63 } 64 MaybeSave(); 65 return FileDescriptor(fd); 66 } 67 68 FileDescriptor(FileDescriptor const& other) = delete; 69 FileDescriptor& operator=(FileDescriptor const& other) = delete; 70 71 ~FileDescriptor() { reset(); } 72 73 // If this object is non-empty, returns the owned file descriptor. (Ownership 74 // is retained by the FileDescriptor.) Otherwise returns -1. 75 int get() const { return fd_; } 76 77 // If this object is non-empty, transfers ownership of the file descriptor to 78 // the caller and returns it. Otherwise returns -1. 79 int release() { 80 int const fd = fd_; 81 fd_ = -1; 82 return fd; 83 } 84 85 // If this object is non-empty, closes the owned file descriptor (recording a 86 // test failure if the close fails). 87 void reset() { reset(-1); } 88 89 // Like no-arg reset(), but the FileDescriptor takes ownership of fd after 90 // closing its existing file descriptor. 91 void reset(int fd) { 92 if (fd_ >= 0) { 93 TEST_PCHECK(close(fd_) == 0); 94 MaybeSave(); 95 } 96 set_fd(fd); 97 } 98 99 private: 100 // Wrapper that coerces negative fd values other than -1 to -1 so that get() 101 // etc. return -1. 102 void set_fd(int fd) { fd_ = std::max(fd, -1); } 103 104 int fd_ = -1; 105 }; 106 107 // Wrapper around open(2) that returns a FileDescriptor. 108 inline PosixErrorOr<FileDescriptor> Open(std::string const& path, int flags, 109 mode_t mode = 0) { 110 int fd = open(path.c_str(), flags, mode); 111 if (fd < 0) { 112 return PosixError(errno, absl::StrFormat("open(%s, %#x, %#o)", path.c_str(), 113 flags, mode)); 114 } 115 MaybeSave(); 116 return FileDescriptor(fd); 117 } 118 119 // Wrapper around openat(2) that returns a FileDescriptor. 120 inline PosixErrorOr<FileDescriptor> OpenAt(int dirfd, std::string const& path, 121 int flags, mode_t mode = 0) { 122 int fd = openat(dirfd, path.c_str(), flags, mode); 123 if (fd < 0) { 124 return PosixError(errno, absl::StrFormat("openat(%d, %s, %#x, %#o)", dirfd, 125 path, flags, mode)); 126 } 127 MaybeSave(); 128 return FileDescriptor(fd); 129 } 130 131 } // namespace testing 132 } // namespace gvisor 133 134 #endif // GVISOR_TEST_UTIL_FILE_DESCRIPTOR_H_