source/io: Implement
diff --git a/.gitignore b/.gitignore
index 7d7cad2..9b8c3e2 100644
--- a/.gitignore
+++ b/.gitignore
@@ -50,4 +50,5 @@
/test/internal_utils
/test/source_base
/test/source_event
+/test/source_io
/test/source_time
diff --git a/src/Makefile.am b/src/Makefile.am
index 130c8e5..74f01f5 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -27,6 +27,9 @@
nobase_include_HEADERS += sdeventplus/source/event.hpp
libsdeventplus_la_SOURCES += sdeventplus/source/event.cpp
+nobase_include_HEADERS += sdeventplus/source/io.hpp
+libsdeventplus_la_SOURCES += sdeventplus/source/io.cpp
+
nobase_include_HEADERS += sdeventplus/source/time.hpp
libsdeventplus_la_SOURCES += sdeventplus/source/time.cpp
diff --git a/src/sdeventplus/internal/sdevent.cpp b/src/sdeventplus/internal/sdevent.cpp
index e7cd633..467a3c3 100644
--- a/src/sdeventplus/internal/sdevent.cpp
+++ b/src/sdeventplus/internal/sdevent.cpp
@@ -26,6 +26,14 @@
return ::sd_event_unref(event);
}
+int SdEventImpl::sd_event_add_io(sd_event* event, sd_event_source** source,
+ int fd, uint32_t events,
+ sd_event_io_handler_t callback,
+ void* userdata) const
+{
+ return ::sd_event_add_io(event, source, fd, events, callback, userdata);
+}
+
int SdEventImpl::sd_event_add_time(sd_event* event, sd_event_source** source,
clockid_t clock, uint64_t usec,
uint64_t accuracy,
@@ -172,6 +180,46 @@
return ::sd_event_source_set_enabled(source, enabled);
}
+int SdEventImpl::sd_event_source_get_io_fd(sd_event_source* source) const
+{
+ return ::sd_event_source_get_io_fd(source);
+}
+
+int SdEventImpl::sd_event_source_set_io_fd(sd_event_source* source,
+ int fd) const
+{
+ return ::sd_event_source_set_io_fd(source, fd);
+}
+
+int SdEventImpl::sd_event_source_get_io_fd_own(sd_event_source* source) const
+{
+ return ::sd_event_source_get_io_fd_own(source);
+}
+
+int SdEventImpl::sd_event_source_set_io_fd_own(sd_event_source* source,
+ int own) const
+{
+ return ::sd_event_source_set_io_fd_own(source, own);
+}
+
+int SdEventImpl::sd_event_source_get_io_events(sd_event_source* source,
+ uint32_t* events) const
+{
+ return ::sd_event_source_get_io_events(source, events);
+}
+
+int SdEventImpl::sd_event_source_set_io_events(sd_event_source* source,
+ uint32_t events) const
+{
+ return ::sd_event_source_set_io_events(source, events);
+}
+
+int SdEventImpl::sd_event_source_get_io_revents(sd_event_source* source,
+ uint32_t* revents) const
+{
+ return ::sd_event_source_get_io_revents(source, revents);
+}
+
int SdEventImpl::sd_event_source_get_time(sd_event_source* source,
uint64_t* usec) const
{
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index fb26fd2..62acbbe 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -17,6 +17,9 @@
virtual sd_event* sd_event_ref(sd_event* event) const = 0;
virtual sd_event* sd_event_unref(sd_event* event) const = 0;
+ virtual int sd_event_add_io(sd_event* event, sd_event_source** source,
+ int fd, uint32_t events, sd_event_io_handler_t,
+ void* userdata) const = 0;
virtual int sd_event_add_time(sd_event* event, sd_event_source** source,
clockid_t clock, uint64_t usec,
uint64_t accuracy,
@@ -72,6 +75,19 @@
int* enabled) const = 0;
virtual int sd_event_source_set_enabled(sd_event_source* source,
int enabled) const = 0;
+ virtual int sd_event_source_get_io_fd(sd_event_source* source) const = 0;
+ virtual int sd_event_source_set_io_fd(sd_event_source* source,
+ int fd) const = 0;
+ virtual int
+ sd_event_source_get_io_fd_own(sd_event_source* source) const = 0;
+ virtual int sd_event_source_set_io_fd_own(sd_event_source* source,
+ int own) const = 0;
+ virtual int sd_event_source_get_io_events(sd_event_source* source,
+ uint32_t* events) const = 0;
+ virtual int sd_event_source_set_io_events(sd_event_source* source,
+ uint32_t events) const = 0;
+ virtual int sd_event_source_get_io_revents(sd_event_source* source,
+ uint32_t* revents) const = 0;
virtual int sd_event_source_get_time(sd_event_source* source,
uint64_t* usec) const = 0;
virtual int sd_event_source_set_time(sd_event_source* source,
@@ -90,6 +106,9 @@
sd_event* sd_event_ref(sd_event* event) const override;
sd_event* sd_event_unref(sd_event* event) const override;
+ int sd_event_add_io(sd_event* event, sd_event_source** source, int fd,
+ uint32_t events, sd_event_io_handler_t callback,
+ void* userdata) const override;
int sd_event_add_time(sd_event* event, sd_event_source** source,
clockid_t clock, uint64_t usec, uint64_t accuracy,
sd_event_time_handler_t callback,
@@ -141,6 +160,18 @@
int* enabled) const override;
int sd_event_source_set_enabled(sd_event_source* source,
int enabled) const override;
+ int sd_event_source_get_io_fd(sd_event_source* source) const override;
+ int sd_event_source_set_io_fd(sd_event_source* source,
+ int fd) const override;
+ int sd_event_source_get_io_fd_own(sd_event_source* source) const override;
+ int sd_event_source_set_io_fd_own(sd_event_source* source,
+ int own) const override;
+ int sd_event_source_get_io_events(sd_event_source* source,
+ uint32_t* events) const override;
+ int sd_event_source_set_io_events(sd_event_source* source,
+ uint32_t events) const override;
+ int sd_event_source_get_io_revents(sd_event_source* source,
+ uint32_t* revents) const override;
int sd_event_source_get_time(sd_event_source* source,
uint64_t* usec) const override;
int sd_event_source_set_time(sd_event_source* source,
diff --git a/src/sdeventplus/source/io.cpp b/src/sdeventplus/source/io.cpp
new file mode 100644
index 0000000..2955a7d
--- /dev/null
+++ b/src/sdeventplus/source/io.cpp
@@ -0,0 +1,114 @@
+#include <sdeventplus/source/io.hpp>
+#include <type_traits>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+IO::IO(const Event& event, int fd, uint32_t events, Callback&& callback) :
+ Base(event, create_source(event, fd, events), std::false_type()),
+ callback(std::move(callback))
+{
+}
+
+int IO::get_fd() const
+{
+ int r = event.getSdEvent()->sd_event_source_get_io_fd(source.get());
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_get_io_fd");
+ }
+ return r;
+}
+
+void IO::set_fd(int fd) const
+{
+ int r = event.getSdEvent()->sd_event_source_set_io_fd(source.get(), fd);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_set_io_fd");
+ }
+}
+
+bool IO::get_fd_own() const
+{
+ int r = event.getSdEvent()->sd_event_source_get_io_fd_own(source.get());
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_get_io_fd_own");
+ }
+ return r;
+}
+
+void IO::set_fd_own(bool own) const
+{
+ int r =
+ event.getSdEvent()->sd_event_source_set_io_fd_own(source.get(), own);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_set_io_fd_own");
+ }
+}
+
+uint32_t IO::get_events() const
+{
+ uint32_t events;
+ int r = event.getSdEvent()->sd_event_source_get_io_events(source.get(),
+ &events);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_get_io_events");
+ }
+ return events;
+}
+
+void IO::set_events(uint32_t events) const
+{
+ int r =
+ event.getSdEvent()->sd_event_source_set_io_events(source.get(), events);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_set_io_events");
+ }
+}
+
+uint32_t IO::get_revents() const
+{
+ uint32_t revents;
+ int r = event.getSdEvent()->sd_event_source_get_io_revents(source.get(),
+ &revents);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_source_get_io_revents");
+ }
+ return revents;
+}
+
+const IO::Callback& IO::get_callback() const
+{
+ return callback;
+}
+
+sd_event_source* IO::create_source(const Event& event, int fd, uint32_t events)
+{
+ sd_event_source* source;
+ int r = event.getSdEvent()->sd_event_add_io(event.get(), &source, fd,
+ events, ioCallback, nullptr);
+ if (r < 0)
+ {
+ throw SdEventError(-r, "sd_event_add_io");
+ }
+ return source;
+}
+
+int IO::ioCallback(sd_event_source* source, int fd, uint32_t revents,
+ void* userdata)
+{
+ return sourceCallback<Callback, IO, &IO::get_callback>(
+ "ioCallback", source, userdata, fd, revents);
+}
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/source/io.hpp b/src/sdeventplus/source/io.hpp
new file mode 100644
index 0000000..f8547ec
--- /dev/null
+++ b/src/sdeventplus/source/io.hpp
@@ -0,0 +1,41 @@
+#pragma once
+
+#include <cstdint>
+#include <functional>
+#include <sdeventplus/source/base.hpp>
+#include <systemd/sd-event.h>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+class IO : public Base
+{
+ public:
+ using Callback = std::function<void(IO&, int fd, uint32_t revents)>;
+
+ IO(const Event& event, int fd, uint32_t events, Callback&& callback);
+
+ int get_fd() const;
+ void set_fd(int fd) const;
+ bool get_fd_own() const;
+ void set_fd_own(bool own) const;
+ uint32_t get_events() const;
+ void set_events(uint32_t events) const;
+ uint32_t get_revents() const;
+
+ private:
+ Callback callback;
+
+ const Callback& get_callback() const;
+
+ static sd_event_source* create_source(const Event& event, int fd,
+ uint32_t events);
+
+ static int ioCallback(sd_event_source* source, int fd, uint32_t revents,
+ void* userdata);
+};
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index ce386d6..28c484d 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -17,6 +17,9 @@
MOCK_CONST_METHOD1(sd_event_ref, sd_event*(sd_event*));
MOCK_CONST_METHOD1(sd_event_unref, sd_event*(sd_event*));
+ MOCK_CONST_METHOD6(sd_event_add_io,
+ int(sd_event*, sd_event_source**, int, uint32_t,
+ sd_event_io_handler_t, void*));
MOCK_CONST_METHOD7(sd_event_add_time,
int(sd_event*, sd_event_source**, clockid_t, uint64_t,
uint64_t, sd_event_time_handler_t, void*));
@@ -61,6 +64,17 @@
MOCK_CONST_METHOD2(sd_event_source_get_enabled,
int(sd_event_source*, int*));
MOCK_CONST_METHOD2(sd_event_source_set_enabled, int(sd_event_source*, int));
+ MOCK_CONST_METHOD1(sd_event_source_get_io_fd, int(sd_event_source*));
+ MOCK_CONST_METHOD2(sd_event_source_set_io_fd, int(sd_event_source*, int));
+ MOCK_CONST_METHOD1(sd_event_source_get_io_fd_own, int(sd_event_source*));
+ MOCK_CONST_METHOD2(sd_event_source_set_io_fd_own,
+ int(sd_event_source*, int));
+ MOCK_CONST_METHOD2(sd_event_source_get_io_events,
+ int(sd_event_source*, uint32_t*));
+ MOCK_CONST_METHOD2(sd_event_source_set_io_events,
+ int(sd_event_source*, uint32_t));
+ MOCK_CONST_METHOD2(sd_event_source_get_io_revents,
+ int(sd_event_source*, uint32_t*));
MOCK_CONST_METHOD2(sd_event_source_get_time,
int(sd_event_source*, uint64_t*));
MOCK_CONST_METHOD2(sd_event_source_set_time,
diff --git a/test/Makefile.am b/test/Makefile.am
index 49c7402..3d51b65 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -42,6 +42,11 @@
source_event_CPPFLAGS = $(gtest_cppflags)
source_event_LDADD = $(gtest_ldadd)
+check_PROGRAMS += source_io
+source_io_SOURCES = source/io.cpp
+source_io_CPPFLAGS = $(gtest_cppflags)
+source_io_LDADD = $(gtest_ldadd)
+
check_PROGRAMS += source_time
source_time_SOURCES = source/time.cpp
source_time_CPPFLAGS = $(gtest_cppflags)
diff --git a/test/source/io.cpp b/test/source/io.cpp
new file mode 100644
index 0000000..e3b5921
--- /dev/null
+++ b/test/source/io.cpp
@@ -0,0 +1,258 @@
+#include <cerrno>
+#include <functional>
+#include <gmock/gmock.h>
+#include <gtest/gtest.h>
+#include <memory>
+#include <sdeventplus/event.hpp>
+#include <sdeventplus/exception.hpp>
+#include <sdeventplus/source/io.hpp>
+#include <sdeventplus/test/sdevent.hpp>
+#include <systemd/sd-event.h>
+#include <type_traits>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+namespace
+{
+
+using testing::DoAll;
+using testing::Return;
+using testing::SaveArg;
+using testing::SetArgPointee;
+
+using UniqueEvent = std::unique_ptr<Event, std::function<void(Event*)>>;
+
+class IOTest : public testing::Test
+{
+ protected:
+ testing::StrictMock<test::SdEventMock> mock;
+ sd_event_source* const expected_source =
+ reinterpret_cast<sd_event_source*>(1234);
+ sd_event* const expected_event = reinterpret_cast<sd_event*>(2345);
+ UniqueEvent event = make_event(expected_event);
+
+ UniqueEvent make_event(sd_event* event)
+ {
+ auto deleter = [this, event](Event* e) {
+ EXPECT_CALL(this->mock, sd_event_unref(event))
+ .WillOnce(Return(nullptr));
+ delete e;
+ };
+ return UniqueEvent(new Event(event, std::false_type(), &mock), deleter);
+ }
+
+ void expect_destruct()
+ {
+ {
+ testing::InSequence sequence;
+ EXPECT_CALL(mock, sd_event_source_set_enabled(expected_source,
+ SD_EVENT_OFF))
+ .WillOnce(Return(0));
+ EXPECT_CALL(mock, sd_event_source_unref(expected_source))
+ .WillOnce(Return(nullptr));
+ }
+ EXPECT_CALL(mock, sd_event_unref(expected_event))
+ .WillOnce(Return(nullptr));
+ }
+};
+
+TEST_F(IOTest, ConstructSuccess)
+{
+ const int fd = 10;
+ const uint32_t events = EPOLLIN | EPOLLET;
+
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ sd_event_io_handler_t handler;
+ EXPECT_CALL(mock, sd_event_add_io(expected_event, testing::_, fd, events,
+ testing::_, nullptr))
+ .WillOnce(DoAll(SetArgPointee<1>(expected_source), SaveArg<4>(&handler),
+ Return(0)));
+ void* userdata;
+ EXPECT_CALL(mock, sd_event_source_set_userdata(expected_source, testing::_))
+ .WillOnce(DoAll(SaveArg<1>(&userdata), Return(nullptr)));
+ int completions = 0;
+ int return_fd;
+ uint32_t return_revents;
+ IO::Callback callback = [&](IO&, int fd, uint32_t revents) {
+ return_fd = fd;
+ return_revents = revents;
+ completions++;
+ };
+ IO io(*event, fd, events, std::move(callback));
+ EXPECT_FALSE(callback);
+ EXPECT_EQ(&io, userdata);
+ EXPECT_EQ(0, completions);
+
+ EXPECT_EQ(0, handler(nullptr, 5, EPOLLIN, &io));
+ EXPECT_EQ(1, completions);
+ EXPECT_EQ(5, return_fd);
+ EXPECT_EQ(EPOLLIN, return_revents);
+
+ expect_destruct();
+}
+
+TEST_F(IOTest, ConstructError)
+{
+ const int fd = 10;
+ const uint32_t events = EPOLLIN | EPOLLET;
+
+ EXPECT_CALL(mock, sd_event_add_io(expected_event, testing::_, fd, events,
+ testing::_, nullptr))
+ .WillOnce(Return(-EINVAL));
+ int completions = 0;
+ IO::Callback callback = [&completions](IO&, int, uint32_t) {
+ completions++;
+ };
+ EXPECT_THROW(IO(*event, fd, events, std::move(callback)), SdEventError);
+ EXPECT_TRUE(callback);
+ EXPECT_EQ(0, completions);
+}
+
+class IOMethodTest : public IOTest
+{
+ protected:
+ std::unique_ptr<IO> io;
+
+ void SetUp()
+ {
+ const int fd = 7;
+ const int events = EPOLLOUT;
+
+ EXPECT_CALL(mock, sd_event_ref(expected_event))
+ .WillOnce(Return(expected_event));
+ EXPECT_CALL(mock, sd_event_add_io(expected_event, testing::_, fd,
+ events, testing::_, nullptr))
+ .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
+ EXPECT_CALL(mock,
+ sd_event_source_set_userdata(expected_source, testing::_))
+ .WillOnce(Return(nullptr));
+ io =
+ std::make_unique<IO>(*event, fd, events, [](IO&, int, uint32_t) {});
+ }
+
+ void TearDown()
+ {
+ expect_destruct();
+ io.reset();
+ }
+};
+
+TEST_F(IOMethodTest, GetFdSuccess)
+{
+ const int fd = 5;
+ EXPECT_CALL(mock, sd_event_source_get_io_fd(expected_source))
+ .WillOnce(Return(fd));
+ EXPECT_EQ(fd, io->get_fd());
+}
+
+TEST_F(IOMethodTest, GetFdError)
+{
+ EXPECT_CALL(mock, sd_event_source_get_io_fd(expected_source))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->get_fd(), SdEventError);
+}
+
+TEST_F(IOMethodTest, SetFdSuccess)
+{
+ const int fd = 5;
+ EXPECT_CALL(mock, sd_event_source_set_io_fd(expected_source, fd))
+ .WillOnce(Return(0));
+ io->set_fd(fd);
+}
+
+TEST_F(IOMethodTest, SetFdError)
+{
+ const int fd = 3;
+ EXPECT_CALL(mock, sd_event_source_set_io_fd(expected_source, fd))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->set_fd(fd), SdEventError);
+}
+
+TEST_F(IOMethodTest, GetFdOwnSuccess)
+{
+ EXPECT_CALL(mock, sd_event_source_get_io_fd_own(expected_source))
+ .WillOnce(Return(3));
+ EXPECT_TRUE(io->get_fd_own());
+ EXPECT_CALL(mock, sd_event_source_get_io_fd_own(expected_source))
+ .WillOnce(Return(0));
+ EXPECT_FALSE(io->get_fd_own());
+}
+
+TEST_F(IOMethodTest, GetFdOwnError)
+{
+ EXPECT_CALL(mock, sd_event_source_get_io_fd_own(expected_source))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->get_fd_own(), SdEventError);
+}
+
+TEST_F(IOMethodTest, SetFdOwnSuccess)
+{
+ EXPECT_CALL(mock, sd_event_source_set_io_fd_own(expected_source, true))
+ .WillOnce(Return(0));
+ io->set_fd_own(true);
+}
+
+TEST_F(IOMethodTest, SetFdOwnError)
+{
+ EXPECT_CALL(mock, sd_event_source_set_io_fd_own(expected_source, false))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->set_fd_own(false), SdEventError);
+}
+
+TEST_F(IOMethodTest, GetEventsSuccess)
+{
+ const uint32_t events = EPOLLIN | EPOLLOUT;
+ EXPECT_CALL(mock,
+ sd_event_source_get_io_events(expected_source, testing::_))
+ .WillOnce(DoAll(SetArgPointee<1>(events), Return(0)));
+ EXPECT_EQ(events, io->get_events());
+}
+
+TEST_F(IOMethodTest, GetEventsError)
+{
+ EXPECT_CALL(mock,
+ sd_event_source_get_io_events(expected_source, testing::_))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->get_events(), SdEventError);
+}
+
+TEST_F(IOMethodTest, SetEventsSuccess)
+{
+ const uint32_t events = EPOLLIN | EPOLLOUT;
+ EXPECT_CALL(mock, sd_event_source_set_io_events(expected_source, events))
+ .WillOnce(Return(0));
+ io->set_events(events);
+}
+
+TEST_F(IOMethodTest, SetEventsError)
+{
+ const uint32_t events = EPOLLIN | EPOLLOUT;
+ EXPECT_CALL(mock, sd_event_source_set_io_events(expected_source, events))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->set_events(events), SdEventError);
+}
+
+TEST_F(IOMethodTest, GetREventsSuccess)
+{
+ const uint32_t revents = EPOLLOUT;
+ EXPECT_CALL(mock,
+ sd_event_source_get_io_revents(expected_source, testing::_))
+ .WillOnce(DoAll(SetArgPointee<1>(revents), Return(0)));
+ EXPECT_EQ(revents, io->get_revents());
+}
+
+TEST_F(IOMethodTest, GetREventsError)
+{
+ EXPECT_CALL(mock,
+ sd_event_source_get_io_revents(expected_source, testing::_))
+ .WillOnce(Return(-EINVAL));
+ EXPECT_THROW(io->get_revents(), SdEventError);
+}
+
+} // namespace
+} // namespace source
+} // namespace sdeventplus