diff --git a/include/envoy/api/os_sys_calls.h b/include/envoy/api/os_sys_calls.h index 9378b35e3cc05..ae76568a2f80b 100644 --- a/include/envoy/api/os_sys_calls.h +++ b/include/envoy/api/os_sys_calls.h @@ -17,12 +17,12 @@ namespace Api { /** * SysCallResult holds the rc and errno values resulting from a system call. */ -struct SysCallResult { +template struct SysCallResult { /** * The return code from the system call. */ - int rc_; + T rc_; /** * The errno value as captured after the system call. @@ -30,6 +30,10 @@ struct SysCallResult { int errno_; }; +typedef SysCallResult SysCallIntResult; +typedef SysCallResult SysCallSizeResult; +typedef SysCallResult SysCallPtrResult; + class OsSysCalls { public: virtual ~OsSysCalls() {} @@ -37,86 +41,88 @@ class OsSysCalls { /** * @see bind (man 2 bind) */ - virtual int bind(int sockfd, const sockaddr* addr, socklen_t addrlen) PURE; + virtual SysCallIntResult bind(int sockfd, const sockaddr* addr, socklen_t addrlen) PURE; /** * @see ioctl (man 2 ioctl) */ - virtual int ioctl(int sockfd, unsigned long int request, void* argp) PURE; + virtual SysCallIntResult ioctl(int sockfd, unsigned long int request, void* argp) PURE; /** * Open file by full_path with given flags and mode. * @return file descriptor. */ - virtual int open(const std::string& full_path, int flags, int mode) PURE; + virtual SysCallIntResult open(const std::string& full_path, int flags, int mode) PURE; /** * Write num_bytes to fd from buffer. * @return number of bytes written if non negative, otherwise error code. */ - virtual ssize_t write(int fd, const void* buffer, size_t num_bytes) PURE; + virtual SysCallSizeResult write(int fd, const void* buffer, size_t num_bytes) PURE; /** * @see writev (man 2 writev) */ - virtual ssize_t writev(int fd, const iovec* iovec, int num_iovec) PURE; + virtual SysCallSizeResult writev(int fd, const iovec* iovec, int num_iovec) PURE; /** * @see readv (man 2 readv) */ - virtual ssize_t readv(int fd, const iovec* iovec, int num_iovec) PURE; + virtual SysCallSizeResult readv(int fd, const iovec* iovec, int num_iovec) PURE; /** * @see recv (man 2 recv) */ - virtual ssize_t recv(int socket, void* buffer, size_t length, int flags) PURE; + virtual SysCallSizeResult recv(int socket, void* buffer, size_t length, int flags) PURE; /** * Release all resources allocated for fd. * @return zero on success, -1 returned otherwise. */ - virtual int close(int fd) PURE; + virtual SysCallIntResult close(int fd) PURE; /** * @see shm_open (man 3 shm_open) */ - virtual int shmOpen(const char* name, int oflag, mode_t mode) PURE; + virtual SysCallIntResult shmOpen(const char* name, int oflag, mode_t mode) PURE; /** * @see shm_unlink (man 3 shm_unlink) */ - virtual int shmUnlink(const char* name) PURE; + virtual SysCallIntResult shmUnlink(const char* name) PURE; /** * @see man 2 ftruncate */ - virtual int ftruncate(int fd, off_t length) PURE; + virtual SysCallIntResult ftruncate(int fd, off_t length) PURE; /** * @see man 2 mmap */ - virtual void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) PURE; + virtual SysCallPtrResult mmap(void* addr, size_t length, int prot, int flags, int fd, + off_t offset) PURE; /** * @see man 2 stat */ - virtual int stat(const char* pathname, struct stat* buf) PURE; + virtual SysCallIntResult stat(const char* pathname, struct stat* buf) PURE; /** * @see man 2 setsockopt */ - virtual int setsockopt(int sockfd, int level, int optname, const void* optval, - socklen_t optlen) PURE; + virtual SysCallIntResult setsockopt(int sockfd, int level, int optname, const void* optval, + socklen_t optlen) PURE; /** * @see man 2 getsockopt */ - virtual int getsockopt(int sockfd, int level, int optname, void* optval, socklen_t* optlen) PURE; + virtual SysCallIntResult getsockopt(int sockfd, int level, int optname, void* optval, + socklen_t* optlen) PURE; /** * @see man 2 socket */ - virtual int socket(int domain, int type, int protocol) PURE; + virtual SysCallIntResult socket(int domain, int type, int protocol) PURE; }; typedef std::unique_ptr OsSysCallsPtr; diff --git a/include/envoy/buffer/buffer.h b/include/envoy/buffer/buffer.h index 4340604dfd9fd..c7bebd9ba7f14 100644 --- a/include/envoy/buffer/buffer.h +++ b/include/envoy/buffer/buffer.h @@ -158,10 +158,10 @@ class Instance { * Read from a file descriptor directly into the buffer. * @param fd supplies the descriptor to read from. * @param max_length supplies the maximum length to read. - * @return a Api::SysCallResult with rc_ = the number of bytes read if successful, or rc_ = -1 + * @return a Api::SysCallIntResult with rc_ = the number of bytes read if successful, or rc_ = -1 * for failure. If the call is successful, errno_ shouldn't be used. */ - virtual Api::SysCallResult read(int fd, uint64_t max_length) PURE; + virtual Api::SysCallIntResult read(int fd, uint64_t max_length) PURE; /** * Reserve space in the buffer. @@ -190,10 +190,10 @@ class Instance { /** * Write the buffer out to a file descriptor. * @param fd supplies the descriptor to write to. - * @return a Api::SysCallResult with rc_ = the number of bytes written if successful, or rc_ = -1 - * for failure. If the call is successful, errno_ shouldn't be used. + * @return a Api::SysCallIntResult with rc_ = the number of bytes written if successful, or rc_ = + * -1 for failure. If the call is successful, errno_ shouldn't be used. */ - virtual Api::SysCallResult write(int fd) PURE; + virtual Api::SysCallIntResult write(int fd) PURE; }; typedef std::unique_ptr InstancePtr; diff --git a/include/envoy/network/address.h b/include/envoy/network/address.h index 13004a1f19b03..f5d5374e735b4 100644 --- a/include/envoy/network/address.h +++ b/include/envoy/network/address.h @@ -129,19 +129,19 @@ class Instance { * Bind a socket to this address. The socket should have been created with a call to socket() on * an Instance of the same address family. * @param fd supplies the platform socket handle. - * @return a Api::SysCallResult with rc_ = 0 for success and rc_ = -1 for failure. If the call + * @return a Api::SysCallIntResult with rc_ = 0 for success and rc_ = -1 for failure. If the call * is successful, errno_ shouldn't be used. */ - virtual Api::SysCallResult bind(int fd) const PURE; + virtual Api::SysCallIntResult bind(int fd) const PURE; /** * Connect a socket to this address. The socket should have been created with a call to socket() * on this object. * @param fd supplies the platform socket handle. - * @return a Api::SysCallResult with rc_ = 0 for success and rc_ = -1 for failure. If the call + * @return a Api::SysCallIntResult with rc_ = 0 for success and rc_ = -1 for failure. If the call * is successful, errno_ shouldn't be used. */ - virtual Api::SysCallResult connect(int fd) const PURE; + virtual Api::SysCallIntResult connect(int fd) const PURE; /** * @return the IP address information IFF type() == Type::Ip, otherwise nullptr. diff --git a/source/common/api/os_sys_calls_impl.cc b/source/common/api/os_sys_calls_impl.cc index 80cfd24bf602c..c6e6a43c17e15 100644 --- a/source/common/api/os_sys_calls_impl.cc +++ b/source/common/api/os_sys_calls_impl.cc @@ -1,5 +1,6 @@ #include "common/api/os_sys_calls_impl.h" +#include #include #include #include @@ -7,62 +8,87 @@ namespace Envoy { namespace Api { -int OsSysCallsImpl::bind(int sockfd, const sockaddr* addr, socklen_t addrlen) { - return ::bind(sockfd, addr, addrlen); +SysCallIntResult OsSysCallsImpl::bind(int sockfd, const sockaddr* addr, socklen_t addrlen) { + const int rc = ::bind(sockfd, addr, addrlen); + return {rc, errno}; } -int OsSysCallsImpl::ioctl(int sockfd, unsigned long int request, void* argp) { - return ::ioctl(sockfd, request, argp); +SysCallIntResult OsSysCallsImpl::ioctl(int sockfd, unsigned long int request, void* argp) { + const int rc = ::ioctl(sockfd, request, argp); + return {rc, errno}; } -int OsSysCallsImpl::open(const std::string& full_path, int flags, int mode) { - return ::open(full_path.c_str(), flags, mode); +SysCallIntResult OsSysCallsImpl::open(const std::string& full_path, int flags, int mode) { + const int rc = ::open(full_path.c_str(), flags, mode); + return {rc, errno}; } -int OsSysCallsImpl::close(int fd) { return ::close(fd); } +SysCallIntResult OsSysCallsImpl::close(int fd) { + const int rc = ::close(fd); + return {rc, errno}; +} -ssize_t OsSysCallsImpl::write(int fd, const void* buffer, size_t num_bytes) { - return ::write(fd, buffer, num_bytes); +SysCallSizeResult OsSysCallsImpl::write(int fd, const void* buffer, size_t num_bytes) { + const ssize_t rc = ::write(fd, buffer, num_bytes); + return {rc, errno}; } -ssize_t OsSysCallsImpl::writev(int fd, const iovec* iovec, int num_iovec) { - return ::writev(fd, iovec, num_iovec); +SysCallSizeResult OsSysCallsImpl::writev(int fd, const iovec* iovec, int num_iovec) { + const ssize_t rc = ::writev(fd, iovec, num_iovec); + return {rc, errno}; } -ssize_t OsSysCallsImpl::readv(int fd, const iovec* iovec, int num_iovec) { - return ::readv(fd, iovec, num_iovec); +SysCallSizeResult OsSysCallsImpl::readv(int fd, const iovec* iovec, int num_iovec) { + const ssize_t rc = ::readv(fd, iovec, num_iovec); + return {rc, errno}; } -ssize_t OsSysCallsImpl::recv(int socket, void* buffer, size_t length, int flags) { - return ::recv(socket, buffer, length, flags); +SysCallSizeResult OsSysCallsImpl::recv(int socket, void* buffer, size_t length, int flags) { + const ssize_t rc = ::recv(socket, buffer, length, flags); + return {rc, errno}; } -int OsSysCallsImpl::shmOpen(const char* name, int oflag, mode_t mode) { - return ::shm_open(name, oflag, mode); +SysCallIntResult OsSysCallsImpl::shmOpen(const char* name, int oflag, mode_t mode) { + const int rc = ::shm_open(name, oflag, mode); + return {rc, errno}; } -int OsSysCallsImpl::shmUnlink(const char* name) { return ::shm_unlink(name); } +SysCallIntResult OsSysCallsImpl::shmUnlink(const char* name) { + const int rc = ::shm_unlink(name); + return {rc, errno}; +} -int OsSysCallsImpl::ftruncate(int fd, off_t length) { return ::ftruncate(fd, length); } +SysCallIntResult OsSysCallsImpl::ftruncate(int fd, off_t length) { + const int rc = ::ftruncate(fd, length); + return {rc, errno}; +} -void* OsSysCallsImpl::mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) { - return ::mmap(addr, length, prot, flags, fd, offset); +SysCallPtrResult OsSysCallsImpl::mmap(void* addr, size_t length, int prot, int flags, int fd, + off_t offset) { + void* rc = ::mmap(addr, length, prot, flags, fd, offset); + return {rc, errno}; } -int OsSysCallsImpl::stat(const char* pathname, struct stat* buf) { return ::stat(pathname, buf); } +SysCallIntResult OsSysCallsImpl::stat(const char* pathname, struct stat* buf) { + const int rc = ::stat(pathname, buf); + return {rc, errno}; +} -int OsSysCallsImpl::setsockopt(int sockfd, int level, int optname, const void* optval, - socklen_t optlen) { - return ::setsockopt(sockfd, level, optname, optval, optlen); +SysCallIntResult OsSysCallsImpl::setsockopt(int sockfd, int level, int optname, const void* optval, + socklen_t optlen) { + const int rc = ::setsockopt(sockfd, level, optname, optval, optlen); + return {rc, errno}; } -int OsSysCallsImpl::getsockopt(int sockfd, int level, int optname, void* optval, - socklen_t* optlen) { - return ::getsockopt(sockfd, level, optname, optval, optlen); +SysCallIntResult OsSysCallsImpl::getsockopt(int sockfd, int level, int optname, void* optval, + socklen_t* optlen) { + const int rc = ::getsockopt(sockfd, level, optname, optval, optlen); + return {rc, errno}; } -int OsSysCallsImpl::socket(int domain, int type, int protocol) { - return ::socket(domain, type, protocol); +SysCallIntResult OsSysCallsImpl::socket(int domain, int type, int protocol) { + const int rc = ::socket(domain, type, protocol); + return {rc, errno}; } } // namespace Api diff --git a/source/common/api/os_sys_calls_impl.h b/source/common/api/os_sys_calls_impl.h index db325862367df..eed6d1798645f 100644 --- a/source/common/api/os_sys_calls_impl.h +++ b/source/common/api/os_sys_calls_impl.h @@ -10,22 +10,25 @@ namespace Api { class OsSysCallsImpl : public OsSysCalls { public: // Api::OsSysCalls - int bind(int sockfd, const sockaddr* addr, socklen_t addrlen) override; - int ioctl(int sockfd, unsigned long int request, void* argp) override; - int open(const std::string& full_path, int flags, int mode) override; - ssize_t write(int fd, const void* buffer, size_t num_bytes) override; - ssize_t writev(int fd, const iovec* iovec, int num_iovec) override; - ssize_t readv(int fd, const iovec* iovec, int num_iovec) override; - ssize_t recv(int socket, void* buffer, size_t length, int flags) override; - int close(int fd) override; - int shmOpen(const char* name, int oflag, mode_t mode) override; - int shmUnlink(const char* name) override; - int ftruncate(int fd, off_t length) override; - void* mmap(void* addr, size_t length, int prot, int flags, int fd, off_t offset) override; - int stat(const char* pathname, struct stat* buf) override; - int setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen) override; - int getsockopt(int sockfd, int level, int optname, void* optval, socklen_t* optlen) override; - int socket(int domain, int type, int protocol) override; + SysCallIntResult bind(int sockfd, const sockaddr* addr, socklen_t addrlen) override; + SysCallIntResult ioctl(int sockfd, unsigned long int request, void* argp) override; + SysCallIntResult open(const std::string& full_path, int flags, int mode) override; + SysCallSizeResult write(int fd, const void* buffer, size_t num_bytes) override; + SysCallSizeResult writev(int fd, const iovec* iovec, int num_iovec) override; + SysCallSizeResult readv(int fd, const iovec* iovec, int num_iovec) override; + SysCallSizeResult recv(int socket, void* buffer, size_t length, int flags) override; + SysCallIntResult close(int fd) override; + SysCallIntResult shmOpen(const char* name, int oflag, mode_t mode) override; + SysCallIntResult shmUnlink(const char* name) override; + SysCallIntResult ftruncate(int fd, off_t length) override; + SysCallPtrResult mmap(void* addr, size_t length, int prot, int flags, int fd, + off_t offset) override; + SysCallIntResult stat(const char* pathname, struct stat* buf) override; + SysCallIntResult setsockopt(int sockfd, int level, int optname, const void* optval, + socklen_t optlen) override; + SysCallIntResult getsockopt(int sockfd, int level, int optname, void* optval, + socklen_t* optlen) override; + SysCallIntResult socket(int domain, int type, int protocol) override; }; typedef ThreadSafeSingleton OsSysCallsSingleton; diff --git a/source/common/buffer/buffer_impl.cc b/source/common/buffer/buffer_impl.cc index 1f04211944aa6..a62b5931b15a2 100644 --- a/source/common/buffer/buffer_impl.cc +++ b/source/common/buffer/buffer_impl.cc @@ -106,7 +106,7 @@ void OwnedImpl::move(Instance& rhs, uint64_t length) { static_cast(rhs).postProcess(); } -Api::SysCallResult OwnedImpl::read(int fd, uint64_t max_length) { +Api::SysCallIntResult OwnedImpl::read(int fd, uint64_t max_length) { if (max_length == 0) { return {0, 0}; } @@ -126,13 +126,13 @@ Api::SysCallResult OwnedImpl::read(int fd, uint64_t max_length) { ASSERT(num_slices_to_read <= MaxSlices); ASSERT(num_bytes_to_read <= max_length); auto& os_syscalls = Api::OsSysCallsSingleton::get(); - const ssize_t rc = os_syscalls.readv(fd, iov, static_cast(num_slices_to_read)); - const int error = errno; - if (rc < 0) { - return {static_cast(rc), error}; + const Api::SysCallSizeResult result = + os_syscalls.readv(fd, iov, static_cast(num_slices_to_read)); + if (result.rc_ < 0) { + return {static_cast(result.rc_), result.errno_}; } uint64_t num_slices_to_commit = 0; - uint64_t bytes_to_commit = rc; + uint64_t bytes_to_commit = result.rc_; ASSERT(bytes_to_commit <= max_length); while (bytes_to_commit != 0) { slices[num_slices_to_commit].len_ = @@ -143,7 +143,7 @@ Api::SysCallResult OwnedImpl::read(int fd, uint64_t max_length) { } ASSERT(num_slices_to_commit <= num_slices); commit(slices, num_slices_to_commit); - return {static_cast(rc), error}; + return {static_cast(result.rc_), result.errno_}; } uint64_t OwnedImpl::reserve(uint64_t length, RawSlice* iovecs, uint64_t num_iovecs) { @@ -164,7 +164,7 @@ ssize_t OwnedImpl::search(const void* data, uint64_t size, size_t start) const { return result_ptr.pos; } -Api::SysCallResult OwnedImpl::write(int fd) { +Api::SysCallIntResult OwnedImpl::write(int fd) { constexpr uint64_t MaxSlices = 16; RawSlice slices[MaxSlices]; const uint64_t num_slices = std::min(getRawSlices(slices, MaxSlices), MaxSlices); @@ -181,12 +181,11 @@ Api::SysCallResult OwnedImpl::write(int fd) { return {0, 0}; } auto& os_syscalls = Api::OsSysCallsSingleton::get(); - const ssize_t rc = os_syscalls.writev(fd, iov, num_slices_to_write); - const int error = errno; - if (rc > 0) { - drain(static_cast(rc)); + const Api::SysCallSizeResult result = os_syscalls.writev(fd, iov, num_slices_to_write); + if (result.rc_ > 0) { + drain(static_cast(result.rc_)); } - return {static_cast(rc), error}; + return {static_cast(result.rc_), result.errno_}; } OwnedImpl::OwnedImpl() : buffer_(evbuffer_new()) {} diff --git a/source/common/buffer/buffer_impl.h b/source/common/buffer/buffer_impl.h index da8a96ee05e31..bf566c7c78b55 100644 --- a/source/common/buffer/buffer_impl.h +++ b/source/common/buffer/buffer_impl.h @@ -82,10 +82,10 @@ class OwnedImpl : public LibEventInstance { void* linearize(uint32_t size) override; void move(Instance& rhs) override; void move(Instance& rhs, uint64_t length) override; - Api::SysCallResult read(int fd, uint64_t max_length) override; + Api::SysCallIntResult read(int fd, uint64_t max_length) override; uint64_t reserve(uint64_t length, RawSlice* iovecs, uint64_t num_iovecs) override; ssize_t search(const void* data, uint64_t size, size_t start) const override; - Api::SysCallResult write(int fd) override; + Api::SysCallIntResult write(int fd) override; void postProcess() override {} std::string toString() const override; diff --git a/source/common/buffer/watermark_buffer.cc b/source/common/buffer/watermark_buffer.cc index edb660d654148..01d6d159cbd5b 100644 --- a/source/common/buffer/watermark_buffer.cc +++ b/source/common/buffer/watermark_buffer.cc @@ -50,8 +50,8 @@ void WatermarkBuffer::move(Instance& rhs, uint64_t length) { checkHighWatermark(); } -Api::SysCallResult WatermarkBuffer::read(int fd, uint64_t max_length) { - Api::SysCallResult result = OwnedImpl::read(fd, max_length); +Api::SysCallIntResult WatermarkBuffer::read(int fd, uint64_t max_length) { + Api::SysCallIntResult result = OwnedImpl::read(fd, max_length); checkHighWatermark(); return result; } @@ -62,8 +62,8 @@ uint64_t WatermarkBuffer::reserve(uint64_t length, RawSlice* iovecs, uint64_t nu return bytes_reserved; } -Api::SysCallResult WatermarkBuffer::write(int fd) { - Api::SysCallResult result = OwnedImpl::write(fd); +Api::SysCallIntResult WatermarkBuffer::write(int fd) { + Api::SysCallIntResult result = OwnedImpl::write(fd); checkLowWatermark(); return result; } diff --git a/source/common/buffer/watermark_buffer.h b/source/common/buffer/watermark_buffer.h index 869463061c361..6d4808bb401e7 100644 --- a/source/common/buffer/watermark_buffer.h +++ b/source/common/buffer/watermark_buffer.h @@ -30,9 +30,9 @@ class WatermarkBuffer : public OwnedImpl { void drain(uint64_t size) override; void move(Instance& rhs) override; void move(Instance& rhs, uint64_t length) override; - Api::SysCallResult read(int fd, uint64_t max_length) override; + Api::SysCallIntResult read(int fd, uint64_t max_length) override; uint64_t reserve(uint64_t length, RawSlice* iovecs, uint64_t num_iovecs) override; - Api::SysCallResult write(int fd) override; + Api::SysCallIntResult write(int fd) override; void postProcess() override { checkLowWatermark(); } void setWatermarks(uint32_t watermark) { setWatermarks(watermark / 2, watermark); } diff --git a/source/common/filesystem/filesystem_impl.cc b/source/common/filesystem/filesystem_impl.cc index 378d21bb349a6..8c362b0860417 100644 --- a/source/common/filesystem/filesystem_impl.cc +++ b/source/common/filesystem/filesystem_impl.cc @@ -107,11 +107,12 @@ FileImpl::FileImpl(const std::string& path, Event::Dispatcher& dispatcher, } void FileImpl::open() { - fd_ = os_sys_calls_.open(path_.c_str(), O_RDWR | O_APPEND | O_CREAT, - S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); - + Api::SysCallIntResult result = os_sys_calls_.open(path_.c_str(), O_RDWR | O_APPEND | O_CREAT, + S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); + fd_ = result.rc_; if (-1 == fd_) { - throw EnvoyException(fmt::format("unable to open file '{}': {}", path_, strerror(errno))); + throw EnvoyException( + fmt::format("unable to open file '{}': {}", path_, strerror(result.errno_))); } } @@ -154,8 +155,8 @@ void FileImpl::doWrite(Buffer::Instance& buffer) { { Thread::LockGuard lock(file_lock_); for (Buffer::RawSlice& slice : slices) { - ssize_t rc = os_sys_calls_.write(fd_, slice.mem_, slice.len_); - ASSERT(rc == static_cast(slice.len_)); + const Api::SysCallSizeResult result = os_sys_calls_.write(fd_, slice.mem_, slice.len_); + ASSERT(result.rc_ == static_cast(slice.len_)); stats_.write_completed_.inc(); } } diff --git a/source/common/network/address_impl.cc b/source/common/network/address_impl.cc index 6f1c5f18c75d0..b5fae7869975f 100644 --- a/source/common/network/address_impl.cc +++ b/source/common/network/address_impl.cc @@ -47,11 +47,11 @@ void validateIpv6Supported(const std::string& address) { // Check if an IP family is supported on this machine. bool ipFamilySupported(int domain) { Api::OsSysCalls& os_sys_calls = Api::OsSysCallsSingleton::get(); - const int fd = os_sys_calls.socket(domain, SOCK_STREAM, 0); - if (fd >= 0) { - RELEASE_ASSERT(os_sys_calls.close(fd) == 0, ""); + const Api::SysCallIntResult result = os_sys_calls.socket(domain, SOCK_STREAM, 0); + if (result.rc_ >= 0) { + RELEASE_ASSERT(os_sys_calls.close(result.rc_).rc_ == 0, ""); } - return fd != -1; + return result.rc_ != -1; } Address::InstanceConstSharedPtr addressFromSockAddr(const sockaddr_storage& ss, socklen_t ss_len, @@ -214,13 +214,13 @@ bool Ipv4Instance::operator==(const Instance& rhs) const { (ip_.port() == rhs_casted->ip_.port())); } -Api::SysCallResult Ipv4Instance::bind(int fd) const { +Api::SysCallIntResult Ipv4Instance::bind(int fd) const { const int rc = ::bind(fd, reinterpret_cast(&ip_.ipv4_.address_), sizeof(ip_.ipv4_.address_)); return {rc, errno}; } -Api::SysCallResult Ipv4Instance::connect(int fd) const { +Api::SysCallIntResult Ipv4Instance::connect(int fd) const { const int rc = ::connect(fd, reinterpret_cast(&ip_.ipv4_.address_), sizeof(ip_.ipv4_.address_)); return {rc, errno}; @@ -279,13 +279,13 @@ bool Ipv6Instance::operator==(const Instance& rhs) const { (ip_.port() == rhs_casted->ip_.port())); } -Api::SysCallResult Ipv6Instance::bind(int fd) const { +Api::SysCallIntResult Ipv6Instance::bind(int fd) const { const int rc = ::bind(fd, reinterpret_cast(&ip_.ipv6_.address_), sizeof(ip_.ipv6_.address_)); return {rc, errno}; } -Api::SysCallResult Ipv6Instance::connect(int fd) const { +Api::SysCallIntResult Ipv6Instance::connect(int fd) const { const int rc = ::connect(fd, reinterpret_cast(&ip_.ipv6_.address_), sizeof(ip_.ipv6_.address_)); return {rc, errno}; @@ -338,7 +338,7 @@ PipeInstance::PipeInstance(const std::string& pipe_path) : InstanceBase(Type::Pi bool PipeInstance::operator==(const Instance& rhs) const { return asString() == rhs.asString(); } -Api::SysCallResult PipeInstance::bind(int fd) const { +Api::SysCallIntResult PipeInstance::bind(int fd) const { if (abstract_namespace_) { const int rc = ::bind(fd, reinterpret_cast(&address_), offsetof(struct sockaddr_un, sun_path) + address_length_); @@ -352,7 +352,7 @@ Api::SysCallResult PipeInstance::bind(int fd) const { return {rc, errno}; } -Api::SysCallResult PipeInstance::connect(int fd) const { +Api::SysCallIntResult PipeInstance::connect(int fd) const { if (abstract_namespace_) { const int rc = ::connect(fd, reinterpret_cast(&address_), offsetof(struct sockaddr_un, sun_path) + address_length_); diff --git a/source/common/network/address_impl.h b/source/common/network/address_impl.h index 1e691ddee48ad..01fdb13513fbe 100644 --- a/source/common/network/address_impl.h +++ b/source/common/network/address_impl.h @@ -97,8 +97,8 @@ class Ipv4Instance : public InstanceBase { // Network::Address::Instance bool operator==(const Instance& rhs) const override; - Api::SysCallResult bind(int fd) const override; - Api::SysCallResult connect(int fd) const override; + Api::SysCallIntResult bind(int fd) const override; + Api::SysCallIntResult connect(int fd) const override; const Ip* ip() const override { return &ip_; } int socket(SocketType type) const override; @@ -157,8 +157,8 @@ class Ipv6Instance : public InstanceBase { // Network::Address::Instance bool operator==(const Instance& rhs) const override; - Api::SysCallResult bind(int fd) const override; - Api::SysCallResult connect(int fd) const override; + Api::SysCallIntResult bind(int fd) const override; + Api::SysCallIntResult connect(int fd) const override; const Ip* ip() const override { return &ip_; } int socket(SocketType type) const override; @@ -214,8 +214,8 @@ class PipeInstance : public InstanceBase { // Network::Address::Instance bool operator==(const Instance& rhs) const override; - Api::SysCallResult bind(int fd) const override; - Api::SysCallResult connect(int fd) const override; + Api::SysCallIntResult bind(int fd) const override; + Api::SysCallIntResult connect(int fd) const override; const Ip* ip() const override { return nullptr; } int socket(SocketType type) const override; diff --git a/source/common/network/connection_impl.cc b/source/common/network/connection_impl.cc index aa921fa4593f9..f7869f6396172 100644 --- a/source/common/network/connection_impl.cc +++ b/source/common/network/connection_impl.cc @@ -553,7 +553,7 @@ ClientConnectionImpl::ClientConnectionImpl( } if (source_address != nullptr) { - const Api::SysCallResult result = source_address->bind(fd()); + const Api::SysCallIntResult result = source_address->bind(fd()); if (result.rc_ < 0) { ENVOY_LOG_MISC(debug, "Bind failure. Failed to bind to {}: {}", source_address->asString(), strerror(result.errno_)); @@ -570,7 +570,7 @@ ClientConnectionImpl::ClientConnectionImpl( void ClientConnectionImpl::connect() { ENVOY_CONN_LOG(debug, "connecting to {}", *this, socket_->remoteAddress()->asString()); - const Api::SysCallResult result = socket_->remoteAddress()->connect(fd()); + const Api::SysCallIntResult result = socket_->remoteAddress()->connect(fd()); if (result.rc_ == 0) { // write will become ready. ASSERT(connecting_); diff --git a/source/common/network/listen_socket_impl.cc b/source/common/network/listen_socket_impl.cc index bc7e8a7858d97..e1eb72e45a703 100644 --- a/source/common/network/listen_socket_impl.cc +++ b/source/common/network/listen_socket_impl.cc @@ -16,7 +16,7 @@ namespace Envoy { namespace Network { void ListenSocketImpl::doBind() { - const Api::SysCallResult result = local_address_->bind(fd_); + const Api::SysCallIntResult result = local_address_->bind(fd_); if (result.rc_ == -1) { close(); throw EnvoyException( diff --git a/source/common/network/raw_buffer_socket.cc b/source/common/network/raw_buffer_socket.cc index 699313042386e..6987797052ecc 100644 --- a/source/common/network/raw_buffer_socket.cc +++ b/source/common/network/raw_buffer_socket.cc @@ -17,7 +17,7 @@ IoResult RawBufferSocket::doRead(Buffer::Instance& buffer) { bool end_stream = false; do { // 16K read is arbitrary. TODO(mattklein123) PERF: Tune the read size. - Api::SysCallResult result = buffer.read(callbacks_->fd(), 16384); + Api::SysCallIntResult result = buffer.read(callbacks_->fd(), 16384); ENVOY_CONN_LOG(trace, "read returns: {}", callbacks_->connection(), result.rc_); if (result.rc_ == 0) { @@ -58,7 +58,7 @@ IoResult RawBufferSocket::doWrite(Buffer::Instance& buffer, bool end_stream) { action = PostIoAction::KeepOpen; break; } - Api::SysCallResult result = buffer.write(callbacks_->fd()); + Api::SysCallIntResult result = buffer.write(callbacks_->fd()); ENVOY_CONN_LOG(trace, "write returns: {}", callbacks_->connection(), result.rc_); if (result.rc_ == -1) { diff --git a/source/common/network/socket_option_impl.cc b/source/common/network/socket_option_impl.cc index a0b8c3f6cff98..a3d9ced31524a 100644 --- a/source/common/network/socket_option_impl.cc +++ b/source/common/network/socket_option_impl.cc @@ -13,7 +13,8 @@ namespace Network { bool SocketOptionImpl::setOption(Socket& socket, envoy::api::v2::core::SocketOption::SocketState state) const { if (in_state_ == state) { - const Api::SysCallResult result = SocketOptionImpl::setSocketOption(socket, optname_, value_); + const Api::SysCallIntResult result = + SocketOptionImpl::setSocketOption(socket, optname_, value_); if (result.rc_ != 0) { ENVOY_LOG(warn, "Setting option on socket failed: {}", strerror(result.errno_)); return false; @@ -24,17 +25,16 @@ bool SocketOptionImpl::setOption(Socket& socket, bool SocketOptionImpl::isSupported() const { return optname_.has_value(); } -Api::SysCallResult SocketOptionImpl::setSocketOption(Socket& socket, - Network::SocketOptionName optname, - const absl::string_view value) { +Api::SysCallIntResult SocketOptionImpl::setSocketOption(Socket& socket, + Network::SocketOptionName optname, + const absl::string_view value) { if (!optname.has_value()) { return {-1, ENOTSUP}; } auto& os_syscalls = Api::OsSysCallsSingleton::get(); - const int rc = os_syscalls.setsockopt(socket.fd(), optname.value().first, optname.value().second, - value.data(), value.size()); - return {rc, errno}; + return os_syscalls.setsockopt(socket.fd(), optname.value().first, optname.value().second, + value.data(), value.size()); } } // namespace Network diff --git a/source/common/network/socket_option_impl.h b/source/common/network/socket_option_impl.h index c40b530c0628b..cd233e109c5bf 100644 --- a/source/common/network/socket_option_impl.h +++ b/source/common/network/socket_option_impl.h @@ -108,11 +108,11 @@ class SocketOptionImpl : public Socket::Option, Logger::Loggable= PROXY_PROTO_V2_HEADER_LEN + addr_len) { - ssize_t lread; ssize_t missing = (PROXY_PROTO_V2_HEADER_LEN + addr_len) - buf_off_; - lread = os_syscalls.recv(fd, buf_ + buf_off_, missing, 0); - if (lread != missing) { + const Api::SysCallSizeResult read_result = + os_syscalls.recv(fd, buf_ + buf_off_, missing, 0); + if (read_result.rc_ != missing) { throw EnvoyException("failed to read proxy protocol (remote closed)"); } - buf_off_ += lread; + buf_off_ += read_result.rc_; parseV2Header(buf_); // The TLV remain, they are read/discard in parseExtensions() which is called from the // parent (if needed). return true; } else { - nread = os_syscalls.recv(fd, buf_ + buf_off_, nread, 0); + const Api::SysCallSizeResult result = os_syscalls.recv(fd, buf_ + buf_off_, nread, 0); + nread = result.rc_; if (nread < 0) { throw EnvoyException("failed to read proxy protocol (remote closed)"); } @@ -355,7 +357,8 @@ bool Filter::readProxyHeader(int fd) { ntoread = search_index_ - buf_off_; } - nread = os_syscalls.recv(fd, buf_ + buf_off_, ntoread, 0); + const Api::SysCallSizeResult result = os_syscalls.recv(fd, buf_ + buf_off_, ntoread, 0); + nread = result.rc_; ASSERT(size_t(nread) == ntoread); buf_off_ += nread; diff --git a/source/extensions/filters/listener/tls_inspector/tls_inspector.cc b/source/extensions/filters/listener/tls_inspector/tls_inspector.cc index 06a2ef6a5dcc4..16090aaaec7b7 100644 --- a/source/extensions/filters/listener/tls_inspector/tls_inspector.cc +++ b/source/extensions/filters/listener/tls_inspector/tls_inspector.cc @@ -143,13 +143,13 @@ void Filter::onRead() { // TODO(ggreenway): write an integration test to ensure the events work as expected on all // platforms. auto& os_syscalls = Api::OsSysCallsSingleton::get(); - ssize_t n = os_syscalls.recv(cb_->socket().fd(), buf_, config_->maxClientHelloSize(), MSG_PEEK); - const int error = errno; // Latch errno right after the recv call. - ENVOY_LOG(trace, "tls inspector: recv: {}", n); + const Api::SysCallSizeResult result = + os_syscalls.recv(cb_->socket().fd(), buf_, config_->maxClientHelloSize(), MSG_PEEK); + ENVOY_LOG(trace, "tls inspector: recv: {}", result.rc_); - if (n == -1 && error == EAGAIN) { + if (result.rc_ == -1 && result.errno_ == EAGAIN) { return; - } else if (n < 0) { + } else if (result.rc_ < 0) { config_->stats().read_error_.inc(); done(false); return; @@ -157,10 +157,10 @@ void Filter::onRead() { // Because we're doing a MSG_PEEK, data we've seen before gets returned every time, so // skip over what we've already processed. - if (static_cast(n) > read_) { + if (static_cast(result.rc_) > read_) { const uint8_t* data = buf_ + read_; - const size_t len = n - read_; - read_ = n; + const size_t len = result.rc_ - read_; + read_ = result.rc_; parseClientHello(data, len); } } diff --git a/source/extensions/stat_sinks/common/statsd/statsd.cc b/source/extensions/stat_sinks/common/statsd/statsd.cc index 6478616e42bb5..6882d10c68033 100644 --- a/source/extensions/stat_sinks/common/statsd/statsd.cc +++ b/source/extensions/stat_sinks/common/statsd/statsd.cc @@ -24,7 +24,7 @@ Writer::Writer(Network::Address::InstanceConstSharedPtr address) { fd_ = address->socket(Network::Address::SocketType::Datagram); ASSERT(fd_ != -1); - const Api::SysCallResult result = address->connect(fd_); + const Api::SysCallIntResult result = address->connect(fd_); ASSERT(result.rc_ != -1); } diff --git a/source/server/hot_restart_impl.cc b/source/server/hot_restart_impl.cc index a4be237f272b2..9d92877e511c1 100644 --- a/source/server/hot_restart_impl.cc +++ b/source/server/hot_restart_impl.cc @@ -55,19 +55,21 @@ SharedMemory& SharedMemory::initialize(uint64_t stats_set_size, Options& options os_sys_calls.shmUnlink(shmem_name.c_str()); } - int shmem_fd = os_sys_calls.shmOpen(shmem_name.c_str(), flags, S_IRUSR | S_IWUSR); - if (shmem_fd == -1) { + const Api::SysCallIntResult result = + os_sys_calls.shmOpen(shmem_name.c_str(), flags, S_IRUSR | S_IWUSR); + if (result.rc_ == -1) { PANIC(fmt::format("cannot open shared memory region {} check user permissions. Error: {}", - shmem_name, strerror(errno))); + shmem_name, strerror(result.errno_))); } if (options.restartEpoch() == 0) { - int rc = os_sys_calls.ftruncate(shmem_fd, total_size); - RELEASE_ASSERT(rc != -1, ""); + const Api::SysCallIntResult truncateRes = os_sys_calls.ftruncate(result.rc_, total_size); + RELEASE_ASSERT(truncateRes.rc_ != -1, ""); } - SharedMemory* shmem = reinterpret_cast( - os_sys_calls.mmap(nullptr, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, shmem_fd, 0)); + const Api::SysCallPtrResult mmapRes = + os_sys_calls.mmap(nullptr, total_size, PROT_READ | PROT_WRITE, MAP_SHARED, result.rc_, 0); + SharedMemory* shmem = reinterpret_cast(mmapRes.rc_); RELEASE_ASSERT(shmem != MAP_FAILED, ""); RELEASE_ASSERT((reinterpret_cast(shmem) % alignof(decltype(shmem))) == 0, ""); @@ -184,8 +186,9 @@ int HotRestartImpl::bindDomainSocket(uint64_t id) { // easily read single messages. int fd = socket(AF_UNIX, SOCK_DGRAM | SOCK_NONBLOCK, 0); sockaddr_un address = createDomainSocketAddress(id); - int rc = os_sys_calls.bind(fd, reinterpret_cast(&address), sizeof(address)); - if (rc != 0) { + Api::SysCallIntResult result = + os_sys_calls.bind(fd, reinterpret_cast(&address), sizeof(address)); + if (result.rc_ != 0) { throw EnvoyException( fmt::format("unable to bind domain socket with id={} (see --base-id option)", id)); } diff --git a/test/common/buffer/owned_impl_test.cc b/test/common/buffer/owned_impl_test.cc index 3a6c93aff9823..0cbbd7f97d77b 100644 --- a/test/common/buffer/owned_impl_test.cc +++ b/test/common/buffer/owned_impl_test.cc @@ -120,28 +120,28 @@ TEST_F(OwnedImplTest, Write) { Buffer::OwnedImpl buffer; buffer.add("example"); - EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(7)); - Api::SysCallResult result = buffer.write(-1); + EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{7, 0})); + Api::SysCallIntResult result = buffer.write(-1); EXPECT_EQ(7, result.rc_); EXPECT_EQ(0, buffer.length()); buffer.add("example"); - EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(6)); + EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{6, 0})); result = buffer.write(-1); EXPECT_EQ(6, result.rc_); EXPECT_EQ(1, buffer.length()); - EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(0)); + EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{0, 0})); result = buffer.write(-1); EXPECT_EQ(0, result.rc_); EXPECT_EQ(1, buffer.length()); - EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{-1, 0})); result = buffer.write(-1); EXPECT_EQ(-1, result.rc_); EXPECT_EQ(1, buffer.length()); - EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(1)); + EXPECT_CALL(os_sys_calls, writev(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{1, 0})); result = buffer.write(-1); EXPECT_EQ(1, result.rc_); EXPECT_EQ(0, buffer.length()); @@ -157,12 +157,12 @@ TEST_F(OwnedImplTest, Read) { TestThreadsafeSingletonInjector os_calls(&os_sys_calls); Buffer::OwnedImpl buffer; - EXPECT_CALL(os_sys_calls, readv(_, _, _)).WillOnce(Return(0)); - Api::SysCallResult result = buffer.read(-1, 100); + EXPECT_CALL(os_sys_calls, readv(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{0, 0})); + Api::SysCallIntResult result = buffer.read(-1, 100); EXPECT_EQ(0, result.rc_); EXPECT_EQ(0, buffer.length()); - EXPECT_CALL(os_sys_calls, readv(_, _, _)).WillOnce(Return(-1)); + EXPECT_CALL(os_sys_calls, readv(_, _, _)).WillOnce(Return(Api::SysCallSizeResult{-1, 0})); result = buffer.read(-1, 100); EXPECT_EQ(-1, result.rc_); EXPECT_EQ(0, buffer.length()); diff --git a/test/common/buffer/watermark_buffer_test.cc b/test/common/buffer/watermark_buffer_test.cc index 8a21b854591e2..e7f45ea56b8f3 100644 --- a/test/common/buffer/watermark_buffer_test.cc +++ b/test/common/buffer/watermark_buffer_test.cc @@ -178,7 +178,7 @@ TEST_F(WatermarkBufferTest, WatermarkFdFunctions) { int bytes_written_total = 0; while (bytes_written_total < 20) { - Api::SysCallResult result = buffer_.write(pipe_fds[1]); + Api::SysCallIntResult result = buffer_.write(pipe_fds[1]); if (result.rc_ < 0) { ASSERT_EQ(EAGAIN, result.errno_); } else { @@ -191,7 +191,7 @@ TEST_F(WatermarkBufferTest, WatermarkFdFunctions) { int bytes_read_total = 0; while (bytes_read_total < 20) { - Api::SysCallResult result = buffer_.read(pipe_fds[0], 20); + Api::SysCallIntResult result = buffer_.read(pipe_fds[0], 20); bytes_read_total += result.rc_; } EXPECT_EQ(2, times_high_watermark_called_); diff --git a/test/common/network/address_impl_test.cc b/test/common/network/address_impl_test.cc index 839f4437699cd..690083062a2c1 100644 --- a/test/common/network/address_impl_test.cc +++ b/test/common/network/address_impl_test.cc @@ -64,7 +64,7 @@ void testSocketBindAndConnect(Network::Address::IpVersion ip_version, bool v6onl } // Bind the socket to the desired address and port. - const Api::SysCallResult result = addr_port->bind(listen_fd); + const Api::SysCallIntResult result = addr_port->bind(listen_fd); ASSERT_EQ(result.rc_, 0) << addr_port->asString() << "\nerror: " << strerror(result.errno_) << "\nerrno: " << result.errno_; @@ -85,7 +85,7 @@ void testSocketBindAndConnect(Network::Address::IpVersion ip_version, bool v6onl makeFdBlocking(client_fd); // Connect to the server. - const Api::SysCallResult result = addr_port->connect(client_fd); + const Api::SysCallIntResult result = addr_port->connect(client_fd); ASSERT_EQ(result.rc_, 0) << addr_port->asString() << "\nerror: " << strerror(result.errno_) << "\nerrno: " << result.errno_; }; @@ -314,7 +314,7 @@ TEST(PipeInstanceTest, UnlinksExistingFile) { ASSERT_GE(listen_fd, 0) << address.asString(); ScopedFdCloser closer(listen_fd); - const Api::SysCallResult result = address.bind(listen_fd); + const Api::SysCallIntResult result = address.bind(listen_fd); ASSERT_EQ(result.rc_, 0) << address.asString() << "\nerror: " << strerror(result.errno_) << "\nerrno: " << result.errno_; }; diff --git a/test/common/network/connection_impl_test.cc b/test/common/network/connection_impl_test.cc index 598638d704ebf..abb352e02f06a 100644 --- a/test/common/network/connection_impl_test.cc +++ b/test/common/network/connection_impl_test.cc @@ -677,10 +677,11 @@ TEST_P(ConnectionImplTest, WriteWithWatermarks) { EXPECT_CALL(*client_write_buffer_, move(_)) .WillRepeatedly(DoAll(AddBufferToStringWithoutDraining(&data_written), Invoke(client_write_buffer_, &MockWatermarkBuffer::baseMove))); - EXPECT_CALL(*client_write_buffer_, write(_)).WillOnce(Invoke([&](int fd) -> Api::SysCallResult { - dispatcher_->exit(); - return client_write_buffer_->failWrite(fd); - })); + EXPECT_CALL(*client_write_buffer_, write(_)) + .WillOnce(Invoke([&](int fd) -> Api::SysCallIntResult { + dispatcher_->exit(); + return client_write_buffer_->failWrite(fd); + })); // The write() call on the connection will buffer enough data to bring the connection above the // high watermark and as the data will not flush it should not return below the watermark. EXPECT_CALL(client_callbacks_, onAboveWriteBufferHighWatermark()); @@ -763,7 +764,7 @@ TEST_P(ConnectionImplTest, WatermarkFuzzing) { .WillOnce(Invoke(client_write_buffer_, &MockWatermarkBuffer::baseMove)); EXPECT_CALL(*client_write_buffer_, write(_)) .WillOnce(DoAll(Invoke([&](int) -> void { client_write_buffer_->drain(bytes_to_flush); }), - Return(Api::SysCallResult{bytes_to_flush, 0}))) + Return(Api::SysCallIntResult{bytes_to_flush, 0}))) .WillRepeatedly(testing::Invoke(client_write_buffer_, &MockWatermarkBuffer::failWrite)); client_connection_->write(buffer_to_write, false); dispatcher_->run(Event::Dispatcher::RunType::NonBlock); diff --git a/test/common/network/dns_impl_test.cc b/test/common/network/dns_impl_test.cc index 567be4f909f47..026cfa58bc9c6 100644 --- a/test/common/network/dns_impl_test.cc +++ b/test/common/network/dns_impl_test.cc @@ -358,8 +358,8 @@ class CustomInstance : public Address::Instance { } const std::string& asString() const override { return antagonistic_name_; } const std::string& logicalName() const override { return antagonistic_name_; } - Api::SysCallResult bind(int fd) const override { return instance_.bind(fd); } - Api::SysCallResult connect(int fd) const override { return instance_.connect(fd); } + Api::SysCallIntResult bind(int fd) const override { return instance_.bind(fd); } + Api::SysCallIntResult connect(int fd) const override { return instance_.connect(fd); } const Address::Ip* ip() const override { return instance_.ip(); } int socket(Address::SocketType type) const override { return instance_.socket(type); } Address::Type type() const override { return instance_.type(); } diff --git a/test/common/network/socket_option_impl_test.cc b/test/common/network/socket_option_impl_test.cc index 9f804c3b18f0a..d3fffd89b35b7 100644 --- a/test/common/network/socket_option_impl_test.cc +++ b/test/common/network/socket_option_impl_test.cc @@ -8,7 +8,7 @@ class SocketOptionImplTest : public SocketOptionTest {}; TEST_F(SocketOptionImplTest, BadFd) { absl::string_view zero("\0\0\0\0", 4); - Api::SysCallResult result = SocketOptionImpl::setSocketOption(socket_, {}, zero); + Api::SysCallIntResult result = SocketOptionImpl::setSocketOption(socket_, {}, zero); EXPECT_EQ(-1, result.rc_); EXPECT_EQ(ENOTSUP, result.errno_); } diff --git a/test/common/runtime/runtime_impl_test.cc b/test/common/runtime/runtime_impl_test.cc index d141c40162d8a..6606e2b57badd 100644 --- a/test/common/runtime/runtime_impl_test.cc +++ b/test/common/runtime/runtime_impl_test.cc @@ -77,8 +77,10 @@ class DiskBackedLoaderImplTest : public testing::Test { os_sys_calls_ = new NiceMock; ON_CALL(*os_sys_calls_, stat(_, _)) - .WillByDefault( - Invoke([](const char* filename, struct stat* stat) { return ::stat(filename, stat); })); + .WillByDefault(Invoke([](const char* filename, struct stat* stat) { + const int rc = ::stat(filename, stat); + return Api::SysCallIntResult{rc, errno}; + })); } void run(const std::string& primary_dir, const std::string& override_dir) { @@ -159,7 +161,7 @@ TEST_F(DiskBackedLoaderImplTest, BadDirectory) { TEST_F(DiskBackedLoaderImplTest, BadStat) { setup(); - EXPECT_CALL(*os_sys_calls_, stat(_, _)).WillOnce(Return(-1)); + EXPECT_CALL(*os_sys_calls_, stat(_, _)).WillOnce(Return(Api::SysCallIntResult{-1, 0})); run("test/common/runtime/test_data/current", "envoy_override"); EXPECT_EQ(store.counter("runtime.load_error").value(), 1); // We should still have the admin layer diff --git a/test/extensions/filters/listener/proxy_protocol/proxy_protocol_test.cc b/test/extensions/filters/listener/proxy_protocol/proxy_protocol_test.cc index a5ce01b42fb4e..57916b0bb4938 100644 --- a/test/extensions/filters/listener/proxy_protocol/proxy_protocol_test.cc +++ b/test/extensions/filters/listener/proxy_protocol/proxy_protocol_test.cc @@ -257,20 +257,27 @@ TEST_P(ProxyProtocolTest, errorRecv_2) { 'r', 'e', ' ', 'd', 'a', 't', 'a'}; Api::MockOsSysCalls os_sys_calls; TestThreadsafeSingletonInjector os_calls(&os_sys_calls); - EXPECT_CALL(os_sys_calls, recv(_, _, _, _)).Times(AnyNumber()).WillOnce(Return((errno = 0, -1))); + EXPECT_CALL(os_sys_calls, recv(_, _, _, _)) + .Times(AnyNumber()) + .WillOnce(Return(Api::SysCallSizeResult{-1, 0})); EXPECT_CALL(os_sys_calls, ioctl(_, _, _)) .Times(AnyNumber()) .WillRepeatedly(Invoke([](int fd, unsigned long int request, void* argp) { - return ::ioctl(fd, request, argp); + const int rc = ::ioctl(fd, request, argp); + return Api::SysCallIntResult{rc, errno}; })); EXPECT_CALL(os_sys_calls, writev(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::writev(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::writev(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); EXPECT_CALL(os_sys_calls, readv(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::readv(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::readv(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); connect(false); write(buffer, sizeof(buffer)); @@ -287,15 +294,19 @@ TEST_P(ProxyProtocolTest, errorFIONREAD_1) { 'r', 'e', ' ', 'd', 'a', 't', 'a'}; Api::MockOsSysCalls os_sys_calls; TestThreadsafeSingletonInjector os_calls(&os_sys_calls); - EXPECT_CALL(os_sys_calls, ioctl(_, FIONREAD, _)).WillOnce(Return(-1)); + EXPECT_CALL(os_sys_calls, ioctl(_, FIONREAD, _)).WillOnce(Return(Api::SysCallIntResult{-1, 0})); EXPECT_CALL(os_sys_calls, writev(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::writev(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::writev(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); EXPECT_CALL(os_sys_calls, readv(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::readv(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::readv(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); connect(false); write(buffer, sizeof(buffer)); @@ -481,25 +492,31 @@ TEST_P(ProxyProtocolTest, v2ParseExtensionsIoctlError) { .WillRepeatedly(Invoke([](int fd, unsigned long int request, void* argp) { int x = ::ioctl(fd, request, argp); if (x == 0 && *static_cast(argp) == sizeof(tlv)) { - return -1; + return Api::SysCallIntResult{-1, errno}; } else { - return x; + return Api::SysCallIntResult{x, errno}; } })); EXPECT_CALL(os_sys_calls, recv(_, _, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, void* buf, size_t len, int flags) { return ::recv(fd, buf, len, flags); })); + .WillRepeatedly(Invoke([](int fd, void* buf, size_t len, int flags) { + const ssize_t rc = ::recv(fd, buf, len, flags); + return Api::SysCallSizeResult{rc, errno}; + })); EXPECT_CALL(os_sys_calls, writev(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::writev(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::writev(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); EXPECT_CALL(os_sys_calls, readv(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::readv(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::readv(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); connect(false); write(buffer, sizeof(buffer)); @@ -603,23 +620,32 @@ TEST_P(ProxyProtocolTest, v2Fragmented3Error) { EXPECT_CALL(os_sys_calls, recv(_, _, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, void* buf, size_t len, int flags) { return ::recv(fd, buf, len, flags); })); - EXPECT_CALL(os_sys_calls, recv(_, _, 1, _)).Times(AnyNumber()).WillOnce(Return(-1)); + .WillRepeatedly(Invoke([](int fd, void* buf, size_t len, int flags) { + const ssize_t rc = ::recv(fd, buf, len, flags); + return Api::SysCallSizeResult{rc, errno}; + })); + EXPECT_CALL(os_sys_calls, recv(_, _, 1, _)) + .Times(AnyNumber()) + .WillOnce(Return(Api::SysCallSizeResult{-1, 0})); EXPECT_CALL(os_sys_calls, ioctl(_, _, _)) .Times(AnyNumber()) .WillRepeatedly(Invoke([](int fd, unsigned long int request, void* argp) { - return ::ioctl(fd, request, argp); + const int rc = ::ioctl(fd, request, argp); + return Api::SysCallIntResult{rc, errno}; })); EXPECT_CALL(os_sys_calls, writev(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::writev(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::writev(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); EXPECT_CALL(os_sys_calls, readv(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::readv(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::readv(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); connect(false); write(buffer, 17); @@ -640,23 +666,32 @@ TEST_P(ProxyProtocolTest, v2Fragmented4Error) { EXPECT_CALL(os_sys_calls, recv(_, _, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, void* buf, size_t len, int flags) { return ::recv(fd, buf, len, flags); })); - EXPECT_CALL(os_sys_calls, recv(_, _, 4, _)).Times(AnyNumber()).WillOnce(Return(-1)); + .WillRepeatedly(Invoke([](int fd, void* buf, size_t len, int flags) { + const ssize_t rc = ::recv(fd, buf, len, flags); + return Api::SysCallSizeResult{rc, errno}; + })); + EXPECT_CALL(os_sys_calls, recv(_, _, 4, _)) + .Times(AnyNumber()) + .WillOnce(Return(Api::SysCallSizeResult{-1, 0})); EXPECT_CALL(os_sys_calls, ioctl(_, _, _)) .Times(AnyNumber()) .WillRepeatedly(Invoke([](int fd, unsigned long int request, void* argp) { - return ::ioctl(fd, request, argp); + const int rc = ::ioctl(fd, request, argp); + return Api::SysCallIntResult{rc, errno}; })); EXPECT_CALL(os_sys_calls, writev(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::writev(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::writev(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); EXPECT_CALL(os_sys_calls, readv(_, _, _)) .Times(AnyNumber()) - .WillRepeatedly(Invoke( - [](int fd, const struct iovec* iov, int iovcnt) { return ::readv(fd, iov, iovcnt); })); + .WillRepeatedly(Invoke([](int fd, const struct iovec* iov, int iovcnt) { + const ssize_t rc = ::readv(fd, iov, iovcnt); + return Api::SysCallSizeResult{rc, errno}; + })); connect(false); write(buffer, 10); diff --git a/test/extensions/filters/listener/tls_inspector/tls_inspector_benchmark.cc b/test/extensions/filters/listener/tls_inspector/tls_inspector_benchmark.cc index a7697248e5705..c4d23873317f4 100644 --- a/test/extensions/filters/listener/tls_inspector/tls_inspector_benchmark.cc +++ b/test/extensions/filters/listener/tls_inspector/tls_inspector_benchmark.cc @@ -62,10 +62,10 @@ class FastMockOsSysCalls : public Api::MockOsSysCalls { public: FastMockOsSysCalls(const std::vector& client_hello) : client_hello_(client_hello) {} - ssize_t recv(int, void* buffer, size_t length, int) override { + Api::SysCallSizeResult recv(int, void* buffer, size_t length, int) override { RELEASE_ASSERT(length >= client_hello_.size(), ""); memcpy(buffer, client_hello_.data(), client_hello_.size()); - return client_hello_.size(); + return Api::SysCallSizeResult{ssize_t(client_hello_.size()), 0}; } const std::vector client_hello_; diff --git a/test/extensions/filters/listener/tls_inspector/tls_inspector_test.cc b/test/extensions/filters/listener/tls_inspector/tls_inspector_test.cc index a6a385fcf32c6..0833711f39f4e 100644 --- a/test/extensions/filters/listener/tls_inspector/tls_inspector_test.cc +++ b/test/extensions/filters/listener/tls_inspector/tls_inspector_test.cc @@ -83,8 +83,7 @@ TEST_F(TlsInspectorTest, Timeout) { TEST_F(TlsInspectorTest, ReadError) { init(); EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)).WillOnce(InvokeWithoutArgs([]() { - errno = ENOTSUP; - return -1; + return Api::SysCallSizeResult{ssize_t(-1), ENOTSUP}; })); EXPECT_CALL(cb_, continueFilterChain(false)); file_event_callback_(Event::FileReadyType::Read); @@ -97,11 +96,12 @@ TEST_F(TlsInspectorTest, SniRegistered) { const std::string servername("example.com"); std::vector client_hello = Tls::Test::generateClientHello(servername, ""); EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) - .WillOnce(Invoke([&client_hello](int, void* buffer, size_t length, int) -> int { - ASSERT(length >= client_hello.size()); - memcpy(buffer, client_hello.data(), client_hello.size()); - return client_hello.size(); - })); + .WillOnce( + Invoke([&client_hello](int, void* buffer, size_t length, int) -> Api::SysCallSizeResult { + ASSERT(length >= client_hello.size()); + memcpy(buffer, client_hello.data(), client_hello.size()); + return Api::SysCallSizeResult{ssize_t(client_hello.size()), 0}; + })); EXPECT_CALL(socket_, setRequestedServerName(Eq(servername))); EXPECT_CALL(socket_, setRequestedApplicationProtocols(_)).Times(0); EXPECT_CALL(socket_, setDetectedTransportProtocol(absl::string_view("tls"))); @@ -119,11 +119,12 @@ TEST_F(TlsInspectorTest, AlpnRegistered) { absl::string_view("http/1.1")}; std::vector client_hello = Tls::Test::generateClientHello("", "\x02h2\x08http/1.1"); EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) - .WillOnce(Invoke([&client_hello](int, void* buffer, size_t length, int) -> int { - ASSERT(length >= client_hello.size()); - memcpy(buffer, client_hello.data(), client_hello.size()); - return client_hello.size(); - })); + .WillOnce( + Invoke([&client_hello](int, void* buffer, size_t length, int) -> Api::SysCallSizeResult { + ASSERT(length >= client_hello.size()); + memcpy(buffer, client_hello.data(), client_hello.size()); + return Api::SysCallSizeResult{ssize_t(client_hello.size()), 0}; + })); EXPECT_CALL(socket_, setRequestedServerName(_)).Times(0); EXPECT_CALL(socket_, setRequestedApplicationProtocols(alpn_protos)); EXPECT_CALL(socket_, setDetectedTransportProtocol(absl::string_view("tls"))); @@ -142,17 +143,18 @@ TEST_F(TlsInspectorTest, MultipleReads) { std::vector client_hello = Tls::Test::generateClientHello(servername, "\x02h2"); { InSequence s; - EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)).WillOnce(InvokeWithoutArgs([]() -> int { - errno = EAGAIN; - return -1; - })); + EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) + .WillOnce(InvokeWithoutArgs([]() -> Api::SysCallSizeResult { + return Api::SysCallSizeResult{ssize_t(-1), EAGAIN}; + })); for (size_t i = 1; i <= client_hello.size(); i++) { EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) - .WillOnce(Invoke([&client_hello, i](int, void* buffer, size_t length, int) -> int { - ASSERT(length >= client_hello.size()); - memcpy(buffer, client_hello.data(), client_hello.size()); - return i; - })); + .WillOnce(Invoke( + [&client_hello, i](int, void* buffer, size_t length, int) -> Api::SysCallSizeResult { + ASSERT(length >= client_hello.size()); + memcpy(buffer, client_hello.data(), client_hello.size()); + return Api::SysCallSizeResult{ssize_t(i), 0}; + })); } } @@ -176,11 +178,12 @@ TEST_F(TlsInspectorTest, NoExtensions) { init(); std::vector client_hello = Tls::Test::generateClientHello("", ""); EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) - .WillOnce(Invoke([&client_hello](int, void* buffer, size_t length, int) -> int { - ASSERT(length >= client_hello.size()); - memcpy(buffer, client_hello.data(), client_hello.size()); - return client_hello.size(); - })); + .WillOnce( + Invoke([&client_hello](int, void* buffer, size_t length, int) -> Api::SysCallSizeResult { + ASSERT(length >= client_hello.size()); + memcpy(buffer, client_hello.data(), client_hello.size()); + return Api::SysCallSizeResult{ssize_t(client_hello.size()), 0}; + })); EXPECT_CALL(socket_, setRequestedServerName(_)).Times(0); EXPECT_CALL(socket_, setRequestedApplicationProtocols(_)).Times(0); EXPECT_CALL(socket_, setDetectedTransportProtocol(absl::string_view("tls"))); @@ -200,11 +203,12 @@ TEST_F(TlsInspectorTest, ClientHelloTooBig) { ASSERT(client_hello.size() > max_size); init(); EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) - .WillOnce(Invoke([&client_hello](int, void* buffer, size_t length, int) -> int { - ASSERT(length == max_size); - memcpy(buffer, client_hello.data(), length); - return length; - })); + .WillOnce( + Invoke([&client_hello](int, void* buffer, size_t length, int) -> Api::SysCallSizeResult { + ASSERT(length == max_size); + memcpy(buffer, client_hello.data(), length); + return Api::SysCallSizeResult{ssize_t(length), 0}; + })); EXPECT_CALL(cb_, continueFilterChain(false)); file_event_callback_(Event::FileReadyType::Read); EXPECT_EQ(1, cfg_->stats().client_hello_too_large_.value()); @@ -219,10 +223,10 @@ TEST_F(TlsInspectorTest, NotSsl) { data.resize(100); EXPECT_CALL(os_sys_calls_, recv(42, _, _, MSG_PEEK)) - .WillOnce(Invoke([&data](int, void* buffer, size_t length, int) -> int { + .WillOnce(Invoke([&data](int, void* buffer, size_t length, int) -> Api::SysCallSizeResult { ASSERT(length >= data.size()); memcpy(buffer, data.data(), data.size()); - return data.size(); + return Api::SysCallSizeResult{ssize_t(data.size()), 0}; })); EXPECT_CALL(cb_, continueFilterChain(true)); file_event_callback_(Event::FileReadyType::Read); diff --git a/test/mocks/api/mocks.cc b/test/mocks/api/mocks.cc index d88015199ddeb..6373bf33d6610 100644 --- a/test/mocks/api/mocks.cc +++ b/test/mocks/api/mocks.cc @@ -20,41 +20,41 @@ MockOsSysCalls::MockOsSysCalls() { num_writes_ = num_open_ = 0; } MockOsSysCalls::~MockOsSysCalls() {} -int MockOsSysCalls::open(const std::string& full_path, int flags, int mode) { +SysCallIntResult MockOsSysCalls::open(const std::string& full_path, int flags, int mode) { Thread::LockGuard lock(open_mutex_); - int result = open_(full_path, flags, mode); + int rc = open_(full_path, flags, mode); num_open_++; open_event_.notifyOne(); - return result; + return SysCallIntResult{rc, errno}; } -ssize_t MockOsSysCalls::write(int fd, const void* buffer, size_t num_bytes) { +SysCallSizeResult MockOsSysCalls::write(int fd, const void* buffer, size_t num_bytes) { Thread::LockGuard lock(write_mutex_); - ssize_t result = write_(fd, buffer, num_bytes); + ssize_t rc = write_(fd, buffer, num_bytes); num_writes_++; write_event_.notifyOne(); - return result; + return SysCallSizeResult{rc, errno}; } -int MockOsSysCalls::setsockopt(int sockfd, int level, int optname, const void* optval, - socklen_t optlen) { +SysCallIntResult MockOsSysCalls::setsockopt(int sockfd, int level, int optname, const void* optval, + socklen_t optlen) { ASSERT(optlen == sizeof(int)); // Allow mocking system call failure. if (setsockopt_(sockfd, level, optname, optval, optlen) != 0) { - return -1; + return SysCallIntResult{-1, 0}; } boolsockopts_[SockOptKey(sockfd, level, optname)] = !!*reinterpret_cast(optval); - return 0; + return SysCallIntResult{0, 0}; }; -int MockOsSysCalls::getsockopt(int sockfd, int level, int optname, void* optval, - socklen_t* optlen) { +SysCallIntResult MockOsSysCalls::getsockopt(int sockfd, int level, int optname, void* optval, + socklen_t* optlen) { ASSERT(*optlen == sizeof(int)); int val = 0; const auto& it = boolsockopts_.find(SockOptKey(sockfd, level, optname)); @@ -63,10 +63,10 @@ int MockOsSysCalls::getsockopt(int sockfd, int level, int optname, void* optval, } // Allow mocking system call failure. if (getsockopt_(sockfd, level, optname, optval, optlen) != 0) { - return -1; + return {-1, 0}; } *reinterpret_cast(optval) = val; - return 0; + return {0, 0}; } } // namespace Api diff --git a/test/mocks/api/mocks.h b/test/mocks/api/mocks.h index 3c58d705ead6b..66898518b17a1 100644 --- a/test/mocks/api/mocks.h +++ b/test/mocks/api/mocks.h @@ -43,30 +43,33 @@ class MockOsSysCalls : public OsSysCallsImpl { ~MockOsSysCalls(); // Api::OsSysCalls - ssize_t write(int fd, const void* buffer, size_t num_bytes) override; - int open(const std::string& full_path, int flags, int mode) override; - int setsockopt(int sockfd, int level, int optname, const void* optval, socklen_t optlen) override; - int getsockopt(int sockfd, int level, int optname, void* optval, socklen_t* optlen) override; - - MOCK_METHOD3(bind, int(int sockfd, const sockaddr* addr, socklen_t addrlen)); - MOCK_METHOD3(ioctl, int(int sockfd, unsigned long int request, void* argp)); - MOCK_METHOD1(close, int(int)); + SysCallSizeResult write(int fd, const void* buffer, size_t num_bytes) override; + SysCallIntResult open(const std::string& full_path, int flags, int mode) override; + SysCallIntResult setsockopt(int sockfd, int level, int optname, const void* optval, + socklen_t optlen) override; + SysCallIntResult getsockopt(int sockfd, int level, int optname, void* optval, + socklen_t* optlen) override; + + MOCK_METHOD3(bind, SysCallIntResult(int sockfd, const sockaddr* addr, socklen_t addrlen)); + MOCK_METHOD3(ioctl, SysCallIntResult(int sockfd, unsigned long int request, void* argp)); + MOCK_METHOD1(close, SysCallIntResult(int)); MOCK_METHOD3(open_, int(const std::string& full_path, int flags, int mode)); MOCK_METHOD3(write_, ssize_t(int, const void*, size_t)); - MOCK_METHOD3(writev, ssize_t(int, const iovec*, int)); - MOCK_METHOD3(readv, ssize_t(int, const iovec*, int)); - MOCK_METHOD4(recv, ssize_t(int socket, void* buffer, size_t length, int flags)); - - MOCK_METHOD3(shmOpen, int(const char*, int, mode_t)); - MOCK_METHOD1(shmUnlink, int(const char*)); - MOCK_METHOD2(ftruncate, int(int fd, off_t length)); - MOCK_METHOD6(mmap, void*(void* addr, size_t length, int prot, int flags, int fd, off_t offset)); - MOCK_METHOD2(stat, int(const char* name, struct stat* stat)); + MOCK_METHOD3(writev, SysCallSizeResult(int, const iovec*, int)); + MOCK_METHOD3(readv, SysCallSizeResult(int, const iovec*, int)); + MOCK_METHOD4(recv, SysCallSizeResult(int socket, void* buffer, size_t length, int flags)); + + MOCK_METHOD3(shmOpen, SysCallIntResult(const char*, int, mode_t)); + MOCK_METHOD1(shmUnlink, SysCallIntResult(const char*)); + MOCK_METHOD2(ftruncate, SysCallIntResult(int fd, off_t length)); + MOCK_METHOD6(mmap, SysCallPtrResult(void* addr, size_t length, int prot, int flags, int fd, + off_t offset)); + MOCK_METHOD2(stat, SysCallIntResult(const char* name, struct stat* stat)); MOCK_METHOD5(setsockopt_, int(int sockfd, int level, int optname, const void* optval, socklen_t optlen)); MOCK_METHOD5(getsockopt_, int(int sockfd, int level, int optname, void* optval, socklen_t* optlen)); - MOCK_METHOD3(socket, int(int domain, int type, int protocol)); + MOCK_METHOD3(socket, SysCallIntResult(int domain, int type, int protocol)); size_t num_writes_; size_t num_open_; diff --git a/test/mocks/buffer/mocks.h b/test/mocks/buffer/mocks.h index 669e4c710e89a..6f83465209c72 100644 --- a/test/mocks/buffer/mocks.h +++ b/test/mocks/buffer/mocks.h @@ -18,7 +18,7 @@ template class MockBufferBase : public BaseClass { MockBufferBase(); MockBufferBase(std::function below_low, std::function above_high); - MOCK_METHOD1(write, Api::SysCallResult(int fd)); + MOCK_METHOD1(write, Api::SysCallIntResult(int fd)); MOCK_METHOD1(move, void(Buffer::Instance& rhs)); MOCK_METHOD2(move, void(Buffer::Instance& rhs, uint64_t length)); MOCK_METHOD1(drain, void(uint64_t size)); @@ -26,8 +26,8 @@ template class MockBufferBase : public BaseClass { void baseMove(Buffer::Instance& rhs) { BaseClass::move(rhs); } void baseDrain(uint64_t size) { BaseClass::drain(size); } - Api::SysCallResult trackWrites(int fd) { - Api::SysCallResult result = BaseClass::write(fd); + Api::SysCallIntResult trackWrites(int fd) { + Api::SysCallIntResult result = BaseClass::write(fd); if (result.rc_ > 0) { bytes_written_ += result.rc_; } @@ -40,7 +40,7 @@ template class MockBufferBase : public BaseClass { } // A convenience function to invoke on write() which fails the write with EAGAIN. - Api::SysCallResult failWrite(int) { return {-1, EAGAIN}; } + Api::SysCallIntResult failWrite(int) { return {-1, EAGAIN}; } int bytes_written() const { return bytes_written_; } uint64_t bytes_drained() const { return bytes_drained_; } diff --git a/test/mocks/network/mocks.h b/test/mocks/network/mocks.h index d39c6538bc6a1..651da87c4174b 100644 --- a/test/mocks/network/mocks.h +++ b/test/mocks/network/mocks.h @@ -410,8 +410,8 @@ class MockResolvedAddress : public Address::Instance { return asString() == other.asString(); } - MOCK_CONST_METHOD1(bind, Api::SysCallResult(int)); - MOCK_CONST_METHOD1(connect, Api::SysCallResult(int)); + MOCK_CONST_METHOD1(bind, Api::SysCallIntResult(int)); + MOCK_CONST_METHOD1(connect, Api::SysCallIntResult(int)); MOCK_CONST_METHOD0(ip, Address::Ip*()); MOCK_CONST_METHOD1(socket, int(Address::SocketType)); MOCK_CONST_METHOD0(type, Address::Type()); diff --git a/test/server/hot_restart_impl_test.cc b/test/server/hot_restart_impl_test.cc index e7b1c1ae59f4c..c5ed4e40fd6e9 100644 --- a/test/server/hot_restart_impl_test.cc +++ b/test/server/hot_restart_impl_test.cc @@ -29,10 +29,10 @@ class HotRestartImplTest : public testing::Test { EXPECT_CALL(os_sys_calls_, shmOpen(_, _, _)); EXPECT_CALL(os_sys_calls_, ftruncate(_, _)).WillOnce(WithArg<1>(Invoke([this](off_t size) { buffer_.resize(size); - return 0; + return Api::SysCallIntResult{0, 0}; }))); EXPECT_CALL(os_sys_calls_, mmap(_, _, _, _, _, _)).WillOnce(InvokeWithoutArgs([this]() { - return buffer_.data(); + return Api::SysCallPtrResult{buffer_.data(), 0}; })); EXPECT_CALL(os_sys_calls_, bind(_, _, _)); EXPECT_CALL(options_, statsOptions()).WillRepeatedly(ReturnRef(stats_options_)); @@ -142,7 +142,8 @@ TEST_F(HotRestartImplTest, crossAlloc) { EXPECT_CALL(options_, restartEpoch()).WillRepeatedly(Return(1)); EXPECT_CALL(os_sys_calls_, shmOpen(_, _, _)); - EXPECT_CALL(os_sys_calls_, mmap(_, _, _, _, _, _)).WillOnce(Return(buffer_.data())); + EXPECT_CALL(os_sys_calls_, mmap(_, _, _, _, _, _)) + .WillOnce(Return(Api::SysCallPtrResult{buffer_.data(), 0})); EXPECT_CALL(os_sys_calls_, bind(_, _, _)); HotRestartImpl hot_restart2(options_); Stats::RawStatData* stat1_prime = hot_restart2.alloc("stat1"); diff --git a/test/server/listener_manager_impl_test.cc b/test/server/listener_manager_impl_test.cc index b969ade6bad03..4c113784eddf6 100644 --- a/test/server/listener_manager_impl_test.cc +++ b/test/server/listener_manager_impl_test.cc @@ -426,8 +426,8 @@ TEST_F(ListenerManagerImplTest, AddListenerOnIpv4OnlySetups) { ListenerHandle* listener_foo = expectListenerCreate(false); - EXPECT_CALL(os_sys_calls, socket(AF_INET, _, 0)).WillOnce(Return(5)); - EXPECT_CALL(os_sys_calls, socket(AF_INET6, _, 0)).WillOnce(Return(-1)); + EXPECT_CALL(os_sys_calls, socket(AF_INET, _, 0)).WillOnce(Return(Api::SysCallIntResult{5, 0})); + EXPECT_CALL(os_sys_calls, socket(AF_INET6, _, 0)).WillOnce(Return(Api::SysCallIntResult{-1, 0})); EXPECT_CALL(listener_factory_, createListenSocket(_, _, true)); @@ -456,8 +456,8 @@ TEST_F(ListenerManagerImplTest, AddListenerOnIpv6OnlySetups) { ListenerHandle* listener_foo = expectListenerCreate(false); - EXPECT_CALL(os_sys_calls, socket(AF_INET, _, 0)).WillOnce(Return(-1)); - EXPECT_CALL(os_sys_calls, socket(AF_INET6, _, 0)).WillOnce(Return(5)); + EXPECT_CALL(os_sys_calls, socket(AF_INET, _, 0)).WillOnce(Return(Api::SysCallIntResult{-1, 0})); + EXPECT_CALL(os_sys_calls, socket(AF_INET6, _, 0)).WillOnce(Return(Api::SysCallIntResult{5, 0})); EXPECT_CALL(listener_factory_, createListenSocket(_, _, true)); diff --git a/test/test_common/network_utility.cc b/test/test_common/network_utility.cc index 76e190170ddb1..d21d2d8332989 100644 --- a/test/test_common/network_utility.cc +++ b/test/test_common/network_utility.cc @@ -31,7 +31,7 @@ Address::InstanceConstSharedPtr findOrCheckFreePort(Address::InstanceConstShared // Not setting REUSEADDR, therefore if the address has been recently used we won't reuse it here. // However, because we're going to use the address while checking if it is available, we'll need // to set REUSEADDR on listener sockets created by tests using an address validated by this means. - Api::SysCallResult result = addr_port->bind(fd); + Api::SysCallIntResult result = addr_port->bind(fd); int err; const char* failing_fn = nullptr; if (result.rc_ != 0) { @@ -156,7 +156,7 @@ std::pair bindFreeLoopbackPort(Address::Ip Address::SocketType type) { Address::InstanceConstSharedPtr addr = getCanonicalLoopbackAddress(version); const int fd = addr->socket(type); - Api::SysCallResult result = addr->bind(fd); + Api::SysCallIntResult result = addr->bind(fd); if (0 != result.rc_) { close(fd); std::string msg = fmt::format("bind failed for address {} with error: {} ({})",