1 #include <sdeventplus/internal/cexec.hpp>
2 #include <sdeventplus/internal/sdevent.hpp>
3 #include <sdeventplus/source/child.hpp>
4 #include <sdeventplus/types.hpp>
5
6 #include <memory>
7 #include <type_traits>
8 #include <utility>
9
10 namespace sdeventplus
11 {
12 namespace source
13 {
14
Child(const Event & event,pid_t pid,int options,Callback && callback)15 Child::Child(const Event& event, pid_t pid, int options, Callback&& callback) :
16 Base(event, create_source(event, pid, options), std::false_type())
17 {
18 set_userdata(
19 std::make_unique<detail::ChildData>(*this, std::move(callback)));
20 }
21
Child(const Child & other,sdeventplus::internal::NoOwn)22 Child::Child(const Child& other, sdeventplus::internal::NoOwn) :
23 Base(other, sdeventplus::internal::NoOwn())
24 {}
25
set_callback(Callback && callback)26 void Child::set_callback(Callback&& callback)
27 {
28 get_userdata().callback = std::move(callback);
29 }
30
get_pid() const31 pid_t Child::get_pid() const
32 {
33 pid_t pid;
34 SDEVENTPLUS_CHECK(
35 "sd_event_source_get_child_pid",
36 event.getSdEvent()->sd_event_source_get_child_pid(get(), &pid));
37 return pid;
38 }
39
get_userdata() const40 detail::ChildData& Child::get_userdata() const
41 {
42 return static_cast<detail::ChildData&>(Base::get_userdata());
43 }
44
get_callback()45 Child::Callback& Child::get_callback()
46 {
47 return get_userdata().callback;
48 }
49
create_source(const Event & event,pid_t pid,int options)50 sd_event_source* Child::create_source(const Event& event, pid_t pid,
51 int options)
52 {
53 sd_event_source* source;
54 SDEVENTPLUS_CHECK(
55 "sd_event_add_child",
56 event.getSdEvent()->sd_event_add_child(
57 event.get(), &source, pid, options, childCallback, nullptr));
58 return source;
59 }
60
childCallback(sd_event_source * source,const siginfo_t * si,void * userdata)61 int Child::childCallback(sd_event_source* source, const siginfo_t* si,
62 void* userdata)
63 {
64 return sourceCallback<Callback, detail::ChildData, &Child::get_callback>(
65 "childCallback", source, userdata, si);
66 }
67
68 namespace detail
69 {
70
ChildData(const Child & base,Child::Callback && callback)71 ChildData::ChildData(const Child& base, Child::Callback&& callback) :
72 Child(base, sdeventplus::internal::NoOwn()), BaseData(base),
73 callback(std::move(callback))
74 {}
75
76 } // namespace detail
77
78 } // namespace source
79 } // namespace sdeventplus
80