1 /*
2  * Copyright 2018 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "sys.hpp"
18 
19 #include <fcntl.h>
20 #include <sys/ioctl.h>
21 #include <sys/mman.h>
22 #include <sys/sendfile.h>
23 #include <sys/stat.h>
24 #include <sys/types.h>
25 #include <unistd.h>
26 
27 #include <cstdint>
28 
29 namespace internal
30 {
31 
getSize(const char * pathname) const32 std::int64_t SysImpl::getSize(const char* pathname) const
33 {
34     struct stat results;
35     int rc = ::stat(pathname, &results);
36     if (rc)
37     {
38         return 0;
39     }
40 
41     return static_cast<std::int64_t>(results.st_size);
42 }
43 
open(const char * pathname,int flags) const44 int SysImpl::open(const char* pathname, int flags) const
45 {
46     return ::open(pathname, flags);
47 }
48 
read(int fd,void * buf,std::size_t count) const49 int SysImpl::read(int fd, void* buf, std::size_t count) const
50 {
51     return static_cast<int>(::read(fd, buf, count));
52 }
53 
pread(int fd,void * buf,std::size_t count,off_t offset) const54 int SysImpl::pread(int fd, void* buf, std::size_t count, off_t offset) const
55 {
56     return static_cast<int>(::pread(fd, buf, count, offset));
57 }
58 
pwrite(int fd,const void * buf,std::size_t count,off_t offset) const59 int SysImpl::pwrite(int fd, const void* buf, std::size_t count,
60                     off_t offset) const
61 {
62     return static_cast<int>(::pwrite(fd, buf, count, offset));
63 }
64 
close(int fd) const65 int SysImpl::close(int fd) const
66 {
67     return ::close(fd);
68 }
69 
mmap(void * addr,std::size_t length,int prot,int flags,int fd,off_t offset) const70 void* SysImpl::mmap(void* addr, std::size_t length, int prot, int flags, int fd,
71                     off_t offset) const
72 {
73     return ::mmap(addr, length, prot, flags, fd, offset);
74 }
75 
munmap(void * addr,std::size_t length) const76 int SysImpl::munmap(void* addr, std::size_t length) const
77 {
78     return ::munmap(addr, length);
79 }
80 
getpagesize() const81 int SysImpl::getpagesize() const
82 {
83     return ::getpagesize();
84 }
85 
ioctl(int fd,unsigned long request,void * param) const86 int SysImpl::ioctl(int fd, unsigned long request, void* param) const
87 {
88     return ::ioctl(fd, request, param);
89 }
90 
poll(struct pollfd * fds,nfds_t nfds,int timeout) const91 int SysImpl::poll(struct pollfd* fds, nfds_t nfds, int timeout) const
92 {
93     return ::poll(fds, nfds, timeout);
94 }
95 
socket(int domain,int type,int protocol) const96 int SysImpl::socket(int domain, int type, int protocol) const
97 {
98     return ::socket(domain, type, protocol);
99 }
100 
connect(int sockfd,const struct sockaddr * addr,socklen_t addrlen) const101 int SysImpl::connect(int sockfd, const struct sockaddr* addr,
102                      socklen_t addrlen) const
103 {
104     return ::connect(sockfd, addr, addrlen);
105 }
106 
send(int sockfd,const void * buf,size_t len,int flags) const107 ssize_t SysImpl::send(int sockfd, const void* buf, size_t len, int flags) const
108 {
109     return ::send(sockfd, buf, len, flags);
110 }
111 
sendfile(int out_fd,int in_fd,off_t * offset,size_t count) const112 ssize_t SysImpl::sendfile(int out_fd, int in_fd, off_t* offset,
113                           size_t count) const
114 {
115     return ::sendfile(out_fd, in_fd, offset, count);
116 }
117 
getaddrinfo(const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** res) const118 int SysImpl::getaddrinfo(const char* node, const char* service,
119                          const struct addrinfo* hints,
120                          struct addrinfo** res) const
121 {
122     return ::getaddrinfo(node, service, hints, res);
123 }
124 
freeaddrinfo(struct addrinfo * res) const125 void SysImpl::freeaddrinfo(struct addrinfo* res) const
126 {
127     ::freeaddrinfo(res);
128 }
129 
130 SysImpl sys_impl;
131 
132 } // namespace internal
133