Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 25 additions & 19 deletions include/envoy/api/os_sys_calls.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,106 +17,112 @@ namespace Api {
/**
* SysCallResult holds the rc and errno values resulting from a system call.
*/
struct SysCallResult {
template <typename T> struct SysCallResult {

/**
* The return code from the system call.
*/
int rc_;
T rc_;

/**
* The errno value as captured after the system call.
*/
int errno_;
};

typedef SysCallResult<int> SysCallIntResult;
typedef SysCallResult<ssize_t> SysCallSizeResult;
typedef SysCallResult<void*> SysCallPtrResult;

class OsSysCalls {
public:
virtual ~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<OsSysCalls> OsSysCallsPtr;
Expand Down
10 changes: 5 additions & 5 deletions include/envoy/buffer/buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Instance> InstancePtr;
Expand Down
8 changes: 4 additions & 4 deletions include/envoy/network/address.h
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
86 changes: 56 additions & 30 deletions source/common/api/os_sys_calls_impl.cc
Original file line number Diff line number Diff line change
@@ -1,68 +1,94 @@
#include "common/api/os_sys_calls_impl.h"

#include <errno.h>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>

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
Expand Down
35 changes: 19 additions & 16 deletions source/common/api/os_sys_calls_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<OsSysCallsImpl> OsSysCallsSingleton;
Expand Down
Loading