xref: /openbmc/phosphor-ipmi-flash/internal/sys.cpp (revision 9bb21e3eb89ab6d590dfb0f677b6bf912a68941b)
18b58856fSPatrick Venture /*
28b58856fSPatrick Venture  * Copyright 2018 Google Inc.
38b58856fSPatrick Venture  *
48b58856fSPatrick Venture  * Licensed under the Apache License, Version 2.0 (the "License");
58b58856fSPatrick Venture  * you may not use this file except in compliance with the License.
68b58856fSPatrick Venture  * You may obtain a copy of the License at
78b58856fSPatrick Venture  *
88b58856fSPatrick Venture  *     http://www.apache.org/licenses/LICENSE-2.0
98b58856fSPatrick Venture  *
108b58856fSPatrick Venture  * Unless required by applicable law or agreed to in writing, software
118b58856fSPatrick Venture  * distributed under the License is distributed on an "AS IS" BASIS,
128b58856fSPatrick Venture  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138b58856fSPatrick Venture  * See the License for the specific language governing permissions and
148b58856fSPatrick Venture  * limitations under the License.
158b58856fSPatrick Venture  */
168b58856fSPatrick Venture 
178b58856fSPatrick Venture #include "sys.hpp"
188b58856fSPatrick Venture 
198b58856fSPatrick Venture #include <fcntl.h>
207b91cbc1SPatrick Venture #include <sys/ioctl.h>
218b58856fSPatrick Venture #include <sys/mman.h>
22f7ccadb0SBenjamin Fair #include <sys/sendfile.h>
23cf9b2195SPatrick Venture #include <sys/stat.h>
24cf9b2195SPatrick Venture #include <sys/types.h>
258b58856fSPatrick Venture #include <unistd.h>
268b58856fSPatrick Venture 
27cf9b2195SPatrick Venture #include <cstdint>
28cf9b2195SPatrick Venture 
298b58856fSPatrick Venture namespace internal
308b58856fSPatrick Venture {
318b58856fSPatrick Venture 
getSize(const char * pathname) const32cf9b2195SPatrick Venture std::int64_t SysImpl::getSize(const char* pathname) const
33cf9b2195SPatrick Venture {
34cf9b2195SPatrick Venture     struct stat results;
35cf9b2195SPatrick Venture     int rc = ::stat(pathname, &results);
36cf9b2195SPatrick Venture     if (rc)
37cf9b2195SPatrick Venture     {
38cf9b2195SPatrick Venture         return 0;
39cf9b2195SPatrick Venture     }
40cf9b2195SPatrick Venture 
41cf9b2195SPatrick Venture     return static_cast<std::int64_t>(results.st_size);
42cf9b2195SPatrick Venture }
43cf9b2195SPatrick Venture 
open(const char * pathname,int flags) const448b58856fSPatrick Venture int SysImpl::open(const char* pathname, int flags) const
458b58856fSPatrick Venture {
468b58856fSPatrick Venture     return ::open(pathname, flags);
478b58856fSPatrick Venture }
488b58856fSPatrick Venture 
read(int fd,void * buf,std::size_t count) const49cec04901SPatrick Venture int SysImpl::read(int fd, void* buf, std::size_t count) const
50cec04901SPatrick Venture {
51cec04901SPatrick Venture     return static_cast<int>(::read(fd, buf, count));
52cec04901SPatrick Venture }
53cec04901SPatrick Venture 
pread(int fd,void * buf,std::size_t count,off_t offset) const54493b3af0SBrandon Kim int SysImpl::pread(int fd, void* buf, std::size_t count, off_t offset) const
55493b3af0SBrandon Kim {
56493b3af0SBrandon Kim     return static_cast<int>(::pread(fd, buf, count, offset));
57493b3af0SBrandon Kim }
58493b3af0SBrandon Kim 
pwrite(int fd,const void * buf,std::size_t count,off_t offset) const59493b3af0SBrandon Kim int SysImpl::pwrite(int fd, const void* buf, std::size_t count,
60493b3af0SBrandon Kim                     off_t offset) const
61493b3af0SBrandon Kim {
62493b3af0SBrandon Kim     return static_cast<int>(::pwrite(fd, buf, count, offset));
63493b3af0SBrandon Kim }
64493b3af0SBrandon Kim 
close(int fd) const658b58856fSPatrick Venture int SysImpl::close(int fd) const
668b58856fSPatrick Venture {
678b58856fSPatrick Venture     return ::close(fd);
688b58856fSPatrick Venture }
698b58856fSPatrick Venture 
mmap(void * addr,std::size_t length,int prot,int flags,int fd,off_t offset) const7028abae7cSPatrick Venture void* SysImpl::mmap(void* addr, std::size_t length, int prot, int flags, int fd,
718b58856fSPatrick Venture                     off_t offset) const
728b58856fSPatrick Venture {
738b58856fSPatrick Venture     return ::mmap(addr, length, prot, flags, fd, offset);
748b58856fSPatrick Venture }
758b58856fSPatrick Venture 
munmap(void * addr,std::size_t length) const7628abae7cSPatrick Venture int SysImpl::munmap(void* addr, std::size_t length) const
778b58856fSPatrick Venture {
788b58856fSPatrick Venture     return ::munmap(addr, length);
798b58856fSPatrick Venture }
808b58856fSPatrick Venture 
getpagesize() const818b58856fSPatrick Venture int SysImpl::getpagesize() const
828b58856fSPatrick Venture {
838b58856fSPatrick Venture     return ::getpagesize();
848b58856fSPatrick Venture }
858b58856fSPatrick Venture 
ioctl(int fd,unsigned long request,void * param) const867b91cbc1SPatrick Venture int SysImpl::ioctl(int fd, unsigned long request, void* param) const
877b91cbc1SPatrick Venture {
887b91cbc1SPatrick Venture     return ::ioctl(fd, request, param);
897b91cbc1SPatrick Venture }
907b91cbc1SPatrick Venture 
poll(struct pollfd * fds,nfds_t nfds,int timeout) const917b78aa2cSPatrick Venture int SysImpl::poll(struct pollfd* fds, nfds_t nfds, int timeout) const
927b78aa2cSPatrick Venture {
937b78aa2cSPatrick Venture     return ::poll(fds, nfds, timeout);
947b78aa2cSPatrick Venture }
957b78aa2cSPatrick Venture 
socket(int domain,int type,int protocol) const96f7ccadb0SBenjamin Fair int SysImpl::socket(int domain, int type, int protocol) const
97f7ccadb0SBenjamin Fair {
98f7ccadb0SBenjamin Fair     return ::socket(domain, type, protocol);
99f7ccadb0SBenjamin Fair }
100f7ccadb0SBenjamin Fair 
connect(int sockfd,const struct sockaddr * addr,socklen_t addrlen) const101f7ccadb0SBenjamin Fair int SysImpl::connect(int sockfd, const struct sockaddr* addr,
102f7ccadb0SBenjamin Fair                      socklen_t addrlen) const
103f7ccadb0SBenjamin Fair {
104f7ccadb0SBenjamin Fair     return ::connect(sockfd, addr, addrlen);
105f7ccadb0SBenjamin Fair }
106f7ccadb0SBenjamin Fair 
send(int sockfd,const void * buf,size_t len,int flags) const107*9bb21e3eSWilliam A. Kennington III ssize_t SysImpl::send(int sockfd, const void* buf, size_t len, int flags) const
108*9bb21e3eSWilliam A. Kennington III {
109*9bb21e3eSWilliam A. Kennington III     return ::send(sockfd, buf, len, flags);
110*9bb21e3eSWilliam A. Kennington III }
111*9bb21e3eSWilliam A. Kennington III 
sendfile(int out_fd,int in_fd,off_t * offset,size_t count) const112f7ccadb0SBenjamin Fair ssize_t SysImpl::sendfile(int out_fd, int in_fd, off_t* offset,
113f7ccadb0SBenjamin Fair                           size_t count) const
114f7ccadb0SBenjamin Fair {
115f7ccadb0SBenjamin Fair     return ::sendfile(out_fd, in_fd, offset, count);
116f7ccadb0SBenjamin Fair }
117f7ccadb0SBenjamin Fair 
getaddrinfo(const char * node,const char * service,const struct addrinfo * hints,struct addrinfo ** res) const118f7ccadb0SBenjamin Fair int SysImpl::getaddrinfo(const char* node, const char* service,
119f7ccadb0SBenjamin Fair                          const struct addrinfo* hints,
120f7ccadb0SBenjamin Fair                          struct addrinfo** res) const
121f7ccadb0SBenjamin Fair {
122f7ccadb0SBenjamin Fair     return ::getaddrinfo(node, service, hints, res);
123f7ccadb0SBenjamin Fair }
124f7ccadb0SBenjamin Fair 
freeaddrinfo(struct addrinfo * res) const125f7ccadb0SBenjamin Fair void SysImpl::freeaddrinfo(struct addrinfo* res) const
126f7ccadb0SBenjamin Fair {
127f7ccadb0SBenjamin Fair     ::freeaddrinfo(res);
128f7ccadb0SBenjamin Fair }
129f7ccadb0SBenjamin Fair 
1308b58856fSPatrick Venture SysImpl sys_impl;
1318b58856fSPatrick Venture 
1328b58856fSPatrick Venture } // namespace internal
133