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