blob: 023d1d058620ba478b90f1cc92250e8e0c203c66 [file] [log] [blame]
Patrick Venture8b588562018-11-18 08:44:33 -08001#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 Fairf7ccadb2019-10-11 17:55:27 -070010#include <netdb.h>
Patrick Venture7b78aa22018-12-14 13:56:15 -080011#include <poll.h>
Patrick Venture8b588562018-11-18 08:44:33 -080012#include <sys/mman.h>
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070013#include <sys/socket.h>
14#include <sys/types.h>
Patrick Venture8b588562018-11-18 08:44:33 -080015
16#include <cinttypes>
17#include <cstddef>
Patrick Venturecf9b2192019-06-27 12:09:52 -070018#include <cstdint>
Benjamin Fairc04c2c52020-06-05 09:14:44 -070019#include <system_error>
Patrick Venture8b588562018-11-18 08:44:33 -080020
Patrick Venture8b588562018-11-18 08:44:33 -080021namespace internal
22{
23
24/**
25 * @class Sys
26 * @brief Overridable direct syscall interface
27 */
28class Sys
29{
30 public:
31 virtual ~Sys() = default;
32
33 virtual int open(const char* pathname, int flags) const = 0;
Patrick Venturecec04902019-01-15 13:04:44 -080034 virtual int read(int fd, void* buf, std::size_t count) const = 0;
Brandon Kim493b3af2019-11-05 16:28:40 -080035 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 Venture8b588562018-11-18 08:44:33 -080039 virtual int close(int fd) const = 0;
Patrick Venture28abae72018-12-14 09:44:02 -080040 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 Venture8b588562018-11-18 08:44:33 -080043 virtual int getpagesize() const = 0;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080044 virtual int ioctl(int fd, unsigned long request, void* param) const = 0;
Patrick Venture7b78aa22018-12-14 13:56:15 -080045 virtual int poll(struct pollfd* fds, nfds_t nfds, int timeout) const = 0;
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070046 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;
William A. Kennington III9bb21e32022-04-08 14:28:50 -070049 virtual ssize_t send(int sockfd, const void* buf, size_t len,
50 int flags) const = 0;
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070051 virtual ssize_t sendfile(int out_fd, int in_fd, off_t* offset,
52 size_t count) const = 0;
53 virtual int getaddrinfo(const char* node, const char* service,
54 const struct addrinfo* hints,
55 struct addrinfo** res) const = 0;
56 virtual void freeaddrinfo(struct addrinfo* res) const = 0;
Patrick Venturecf9b2192019-06-27 12:09:52 -070057 virtual std::int64_t getSize(const char* pathname) const = 0;
Patrick Venture8b588562018-11-18 08:44:33 -080058};
59
60/**
61 * @class SysImpl
62 * @brief syscall concrete implementation
63 * @details Passes through all calls to the normal linux syscalls
64 */
65class SysImpl : public Sys
66{
67 public:
68 int open(const char* pathname, int flags) const override;
Patrick Venturecec04902019-01-15 13:04:44 -080069 int read(int fd, void* buf, std::size_t count) const override;
Brandon Kim493b3af2019-11-05 16:28:40 -080070 int pread(int fd, void* buf, std::size_t count,
71 off_t offset) const override;
72 int pwrite(int fd, const void* buf, std::size_t count,
73 off_t offset) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080074 int close(int fd) const override;
Patrick Venture28abae72018-12-14 09:44:02 -080075 void* mmap(void* addr, std::size_t length, int prot, int flags, int fd,
Patrick Venture8b588562018-11-18 08:44:33 -080076 off_t offset) const override;
Patrick Venture28abae72018-12-14 09:44:02 -080077 int munmap(void* addr, std::size_t length) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080078 int getpagesize() const override;
Patrick Venture7b91cbc2018-11-28 14:24:41 -080079 int ioctl(int fd, unsigned long request, void* param) const override;
Patrick Venture7b78aa22018-12-14 13:56:15 -080080 int poll(struct pollfd* fds, nfds_t nfds, int timeout) const override;
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070081 int socket(int domain, int type, int protocol) const override;
82 int connect(int sockfd, const struct sockaddr* addr,
83 socklen_t addrlen) const override;
William A. Kennington III9bb21e32022-04-08 14:28:50 -070084 ssize_t send(int sockfd, const void* buf, size_t len,
85 int flags) const override;
Benjamin Fairf7ccadb2019-10-11 17:55:27 -070086 ssize_t sendfile(int out_fd, int in_fd, off_t* offset,
87 size_t count) const override;
88 int getaddrinfo(const char* node, const char* service,
89 const struct addrinfo* hints,
90 struct addrinfo** res) const override;
91 void freeaddrinfo(struct addrinfo* res) const override;
Patrick Venturecf9b2192019-06-27 12:09:52 -070092 /* returns 0 on failure, or if the file is zero bytes. */
93 std::int64_t getSize(const char* pathname) const override;
Patrick Venture8b588562018-11-18 08:44:33 -080094};
95
Benjamin Fairc04c2c52020-06-05 09:14:44 -070096inline std::system_error errnoException(const std::string& message)
97{
98 return std::system_error(errno, std::generic_category(), message);
99}
100
Patrick Venture8b588562018-11-18 08:44:33 -0800101/** @brief Default instantiation of sys */
102extern SysImpl sys_impl;
103
104} // namespace internal