Rolf Badorek | ef2bf51 | 2019-08-20 11:17:15 +0300 | [diff] [blame] | 1 | /* |
| 2 | Copyright (c) 2018-2019 Nokia. |
| 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 "private/system.hpp" |
| 18 | #include <system_error> |
| 19 | #include <cerrno> |
| 20 | #include <cstring> |
| 21 | #include <sstream> |
| 22 | #include <sys/epoll.h> |
| 23 | #include <sys/timerfd.h> |
| 24 | #include <sys/eventfd.h> |
| 25 | #include "private/abort.hpp" |
| 26 | #include "private/createlogger.hpp" |
| 27 | |
| 28 | using namespace shareddatalayer; |
| 29 | |
| 30 | int System::poll(struct pollfd *fds, nfds_t nfds, int timeout) |
| 31 | { |
| 32 | const int ret(::poll(fds, nfds, timeout)); |
| 33 | if (ret == -1 && errno != EINTR) |
| 34 | throw std::system_error(errno, std::system_category(), "poll"); |
| 35 | return ret; |
| 36 | } |
| 37 | |
| 38 | int System::epoll_create1(int flags) |
| 39 | { |
| 40 | const int ret(::epoll_create1(flags)); |
| 41 | if (ret == -1) |
| 42 | throw std::system_error(errno, std::system_category(), "epoll_create1"); |
| 43 | return ret; |
| 44 | } |
| 45 | |
| 46 | void System::epoll_ctl(int epfd, int op, int fd, epoll_event* event) |
| 47 | { |
| 48 | const int ret(::epoll_ctl(epfd, op, fd, event)); |
| 49 | if (ret == -1) |
| 50 | throw std::system_error(errno, std::system_category(), "epoll_ctl"); |
| 51 | } |
| 52 | |
| 53 | int System::epoll_wait(int epfd, epoll_event* events, int maxevents, int timeout) |
| 54 | { |
| 55 | const int ret(::epoll_wait(epfd, events, maxevents, timeout)); |
| 56 | if ((ret == -1) && (errno != EINTR)) |
| 57 | throw std::system_error(errno, std::system_category(), "epoll_wait"); |
| 58 | return ret; |
| 59 | } |
| 60 | |
| 61 | int System::timerfd_create(int clockid, int flags) |
| 62 | { |
| 63 | const int ret(::timerfd_create(clockid, flags)); |
| 64 | if (ret == -1) |
| 65 | throw std::system_error(errno, std::system_category(), "timerfd_create"); |
| 66 | return ret; |
| 67 | } |
| 68 | |
| 69 | void System::timerfd_settime(int fd, int flags, const itimerspec* new_value, itimerspec* old_value) |
| 70 | { |
| 71 | const int ret(::timerfd_settime(fd, flags, new_value, old_value)); |
| 72 | if (ret == -1) |
| 73 | throw std::system_error(errno, std::system_category(), "timerfd_settime"); |
| 74 | } |
| 75 | |
| 76 | ssize_t System::read(int fd, void* buf, size_t count) |
| 77 | { |
| 78 | const ssize_t ret(::read(fd, buf, count)); |
| 79 | if ((ret == -1) && (errno != EINTR) && (errno != EAGAIN)) |
| 80 | throw std::system_error(errno, std::system_category(), "read"); |
| 81 | return ret; |
| 82 | } |
| 83 | |
| 84 | int System::eventfd(unsigned int initval, int flags) |
| 85 | { |
| 86 | const int ret(::eventfd(initval, flags)); |
| 87 | if (ret == -1) |
| 88 | throw std::system_error(errno, std::system_category(), "eventfd"); |
| 89 | return ret; |
| 90 | } |
| 91 | |
| 92 | ssize_t System::write(int fd, const void* buf, size_t count) |
| 93 | { |
| 94 | ssize_t ret; |
| 95 | do |
| 96 | ret = ::write(fd, buf, count); |
| 97 | while ((ret == -1) && (errno == EINTR)); |
| 98 | if (ret == -1) |
| 99 | throw std::system_error(errno, std::system_category(), "write"); |
| 100 | return ret; |
| 101 | } |
| 102 | |
| 103 | std::chrono::steady_clock::duration System::time_since_epoch() |
| 104 | { |
| 105 | return std::chrono::steady_clock::now().time_since_epoch(); |
| 106 | } |
| 107 | |
| 108 | void System::close(int fd) |
| 109 | { |
| 110 | if (::close(fd) == -1) |
| 111 | { |
| 112 | int errno_saved = errno; |
| 113 | std::ostringstream msg; |
| 114 | msg << "close(" << fd << ") failed: " << strerror(errno_saved); |
| 115 | logErrorOnce(msg.str()); |
| 116 | SHAREDDATALAYER_ABORT("close failed"); |
| 117 | } |
| 118 | } |
| 119 | |
| 120 | const char* System::getenv(const char* name) |
| 121 | { |
| 122 | return ::getenv(name); |
| 123 | } |
| 124 | |
| 125 | System& System::getSystem() noexcept |
| 126 | { |
| 127 | static System system; |
| 128 | return system; |
| 129 | } |