Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 1 | #pragma once |
| 2 | |
| 3 | /* NOTE: IIRC, wak@ is working on exposing some of this in stdplus, so we can |
| 4 | * transition when that's ready. |
| 5 | * |
| 6 | * Copied some from gpioplus to enable unit-testing of lpc nuvoton and later |
| 7 | * other pieces. |
| 8 | */ |
| 9 | |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 10 | #include <netdb.h> |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 11 | #include <poll.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 12 | #include <sys/mman.h> |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 13 | #include <sys/socket.h> |
| 14 | #include <sys/types.h> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 15 | |
| 16 | #include <cinttypes> |
| 17 | #include <cstddef> |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 18 | #include <cstdint> |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 19 | #include <system_error> |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 20 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 21 | namespace internal |
| 22 | { |
| 23 | |
| 24 | /** |
| 25 | * @class Sys |
| 26 | * @brief Overridable direct syscall interface |
| 27 | */ |
| 28 | class Sys |
| 29 | { |
| 30 | public: |
| 31 | virtual ~Sys() = default; |
| 32 | |
| 33 | virtual int open(const char* pathname, int flags) const = 0; |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 34 | virtual int read(int fd, void* buf, std::size_t count) const = 0; |
Brandon Kim | 493b3af | 2019-11-05 16:28:40 -0800 | [diff] [blame] | 35 | virtual int pread(int fd, void* buf, std::size_t count, |
| 36 | off_t offset) const = 0; |
| 37 | virtual int pwrite(int fd, const void* buf, std::size_t count, |
| 38 | off_t offset) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 39 | virtual int close(int fd) const = 0; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 40 | virtual void* mmap(void* addr, std::size_t length, int prot, int flags, |
| 41 | int fd, off_t offset) const = 0; |
| 42 | virtual int munmap(void* addr, std::size_t length) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 43 | virtual int getpagesize() const = 0; |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 44 | virtual int ioctl(int fd, unsigned long request, void* param) const = 0; |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 45 | virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0; |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 46 | virtual int socket(int domain, int type, int protocol) const = 0; |
| 47 | virtual int connect(int sockfd, const struct sockaddr* addr, |
| 48 | socklen_t addrlen) const = 0; |
| 49 | virtual ssize_t sendfile(int out_fd, int in_fd, off_t* offset, |
| 50 | size_t count) const = 0; |
| 51 | virtual int getaddrinfo(const char* node, const char* service, |
| 52 | const struct addrinfo* hints, |
| 53 | struct addrinfo** res) const = 0; |
| 54 | virtual void freeaddrinfo(struct addrinfo* res) const = 0; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 55 | virtual std::int64_t getSize(const char* pathname) const = 0; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 56 | }; |
| 57 | |
| 58 | /** |
| 59 | * @class SysImpl |
| 60 | * @brief syscall concrete implementation |
| 61 | * @details Passes through all calls to the normal linux syscalls |
| 62 | */ |
| 63 | class SysImpl : public Sys |
| 64 | { |
| 65 | public: |
| 66 | int open(const char* pathname, int flags) const override; |
Patrick Venture | cec0490 | 2019-01-15 13:04:44 -0800 | [diff] [blame] | 67 | int read(int fd, void* buf, std::size_t count) const override; |
Brandon Kim | 493b3af | 2019-11-05 16:28:40 -0800 | [diff] [blame] | 68 | int pread(int fd, void* buf, std::size_t count, |
| 69 | off_t offset) const override; |
| 70 | int pwrite(int fd, const void* buf, std::size_t count, |
| 71 | off_t offset) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 72 | int close(int fd) const override; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 73 | void* mmap(void* addr, std::size_t length, int prot, int flags, int fd, |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 74 | off_t offset) const override; |
Patrick Venture | 28abae7 | 2018-12-14 09:44:02 -0800 | [diff] [blame] | 75 | int munmap(void* addr, std::size_t length) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 76 | int getpagesize() const override; |
Patrick Venture | 7b91cbc | 2018-11-28 14:24:41 -0800 | [diff] [blame] | 77 | int ioctl(int fd, unsigned long request, void* param) const override; |
Patrick Venture | 7b78aa2 | 2018-12-14 13:56:15 -0800 | [diff] [blame] | 78 | int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override; |
Benjamin Fair | f7ccadb | 2019-10-11 17:55:27 -0700 | [diff] [blame] | 79 | int socket(int domain, int type, int protocol) const override; |
| 80 | int connect(int sockfd, const struct sockaddr* addr, |
| 81 | socklen_t addrlen) const override; |
| 82 | ssize_t sendfile(int out_fd, int in_fd, off_t* offset, |
| 83 | size_t count) const override; |
| 84 | int getaddrinfo(const char* node, const char* service, |
| 85 | const struct addrinfo* hints, |
| 86 | struct addrinfo** res) const override; |
| 87 | void freeaddrinfo(struct addrinfo* res) const override; |
Patrick Venture | cf9b219 | 2019-06-27 12:09:52 -0700 | [diff] [blame] | 88 | /* returns 0 on failure, or if the file is zero bytes. */ |
| 89 | std::int64_t getSize(const char* pathname) const override; |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 90 | }; |
| 91 | |
Benjamin Fair | c04c2c5 | 2020-06-05 09:14:44 -0700 | [diff] [blame^] | 92 | inline std::system_error errnoException(const std::string& message) |
| 93 | { |
| 94 | return std::system_error(errno, std::generic_category(), message); |
| 95 | } |
| 96 | |
Patrick Venture | 8b58856 | 2018-11-18 08:44:33 -0800 | [diff] [blame] | 97 | /** @brief Default instantiation of sys */ |
| 98 | extern SysImpl sys_impl; |
| 99 | |
| 100 | } // namespace internal |