source/child: Implement
diff --git a/.gitignore b/.gitignore
index 3c5419c..e7fd756 100644
--- a/.gitignore
+++ b/.gitignore
@@ -49,6 +49,7 @@
 /test/internal_sdref
 /test/internal_utils
 /test/source_base
+/test/source_child
 /test/source_event
 /test/source_io
 /test/source_signal
diff --git a/src/Makefile.am b/src/Makefile.am
index 236e347..518ac1f 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -24,6 +24,9 @@
 nobase_include_HEADERS += sdeventplus/source/base.hpp
 libsdeventplus_la_SOURCES += sdeventplus/source/base.cpp
 
+nobase_include_HEADERS += sdeventplus/source/child.hpp
+libsdeventplus_la_SOURCES += sdeventplus/source/child.cpp
+
 nobase_include_HEADERS += sdeventplus/source/event.hpp
 libsdeventplus_la_SOURCES += sdeventplus/source/event.cpp
 
diff --git a/src/sdeventplus/internal/sdevent.cpp b/src/sdeventplus/internal/sdevent.cpp
index ad5114c..b5e4542 100644
--- a/src/sdeventplus/internal/sdevent.cpp
+++ b/src/sdeventplus/internal/sdevent.cpp
@@ -249,6 +249,12 @@
     return ::sd_event_source_get_signal(source);
 }
 
+int SdEventImpl::sd_event_source_get_child_pid(sd_event_source* source,
+                                               pid_t* pid) const
+{
+    return ::sd_event_source_get_child_pid(source, pid);
+}
+
 SdEventImpl sdevent_impl;
 
 } // namespace internal
diff --git a/src/sdeventplus/internal/sdevent.hpp b/src/sdeventplus/internal/sdevent.hpp
index 42427eb..3151981 100644
--- a/src/sdeventplus/internal/sdevent.hpp
+++ b/src/sdeventplus/internal/sdevent.hpp
@@ -28,6 +28,10 @@
     virtual int sd_event_add_signal(sd_event* event, sd_event_source** source,
                                     int sig, sd_event_signal_handler_t callback,
                                     void* userdata) const = 0;
+    virtual int sd_event_add_child(sd_event* event, sd_event_source** source,
+                                   pid_t, int options,
+                                   sd_event_child_handler_t callback,
+                                   void* userdata) const = 0;
     virtual int sd_event_add_defer(sd_event* event, sd_event_source** source,
                                    sd_event_handler_t callback,
                                    void* userdata) const = 0;
@@ -100,6 +104,8 @@
     virtual int sd_event_source_set_time_accuracy(sd_event_source* source,
                                                   uint64_t usec) const = 0;
     virtual int sd_event_source_get_signal(sd_event_source*) const = 0;
+    virtual int sd_event_source_get_child_pid(sd_event_source*,
+                                              pid_t* pid) const = 0;
 };
 
 class SdEventImpl : public SdEvent
@@ -125,6 +131,14 @@
         return ::sd_event_add_signal(event, source, sig, callback, userdata);
     }
 
+    int sd_event_add_child(sd_event* event, sd_event_source** source, pid_t pid,
+                           int options, sd_event_child_handler_t callback,
+                           void* userdata) const override
+    {
+        return ::sd_event_add_child(event, source, pid, options, callback,
+                                    userdata);
+    }
+
     int sd_event_add_defer(sd_event* event, sd_event_source** source,
                            sd_event_handler_t callback,
                            void* userdata) const override;
@@ -193,6 +207,8 @@
     int sd_event_source_set_time_accuracy(sd_event_source* source,
                                           uint64_t usec) const override;
     int sd_event_source_get_signal(sd_event_source*) const override;
+    int sd_event_source_get_child_pid(sd_event_source*,
+                                      pid_t* pid) const override;
 };
 
 extern SdEventImpl sdevent_impl;
diff --git a/src/sdeventplus/source/child.cpp b/src/sdeventplus/source/child.cpp
new file mode 100644
index 0000000..cda067b
--- /dev/null
+++ b/src/sdeventplus/source/child.cpp
@@ -0,0 +1,55 @@
+#include <sdeventplus/exception.hpp>
+#include <sdeventplus/source/child.hpp>
+#include <type_traits>
+#include <utility>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+Child::Child(const Event& event, pid_t pid, int options, Callback&& callback) :
+    Base(event, create_source(event, pid, options), std::false_type()),
+    callback(std::move(callback))
+{
+}
+
+pid_t Child::get_pid() const
+{
+    pid_t pid;
+    int r =
+        event.getSdEvent()->sd_event_source_get_child_pid(source.get(), &pid);
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_source_get_child_pid");
+    }
+    return pid;
+}
+
+const Child::Callback& Child::get_callback() const
+{
+    return callback;
+}
+
+sd_event_source* Child::create_source(const Event& event, pid_t pid,
+                                      int options)
+{
+    sd_event_source* source;
+    int r = event.getSdEvent()->sd_event_add_child(
+        event.get(), &source, pid, options, childCallback, nullptr);
+    if (r < 0)
+    {
+        throw SdEventError(-r, "sd_event_add_child");
+    }
+    return source;
+}
+
+int Child::childCallback(sd_event_source* source, const siginfo_t* si,
+                         void* userdata)
+{
+    return sourceCallback<Callback, Child, &Child::get_callback>(
+        "childCallback", source, userdata, si);
+}
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/source/child.hpp b/src/sdeventplus/source/child.hpp
new file mode 100644
index 0000000..5af359a
--- /dev/null
+++ b/src/sdeventplus/source/child.hpp
@@ -0,0 +1,34 @@
+#pragma once
+
+#include <functional>
+#include <sdeventplus/source/base.hpp>
+#include <signal.h>
+
+namespace sdeventplus
+{
+namespace source
+{
+
+class Child : public Base
+{
+  public:
+    using Callback = std::function<void(Child& source, const siginfo_t* si)>;
+
+    Child(const Event& event, pid_t pid, int options, Callback&& callback);
+
+    pid_t get_pid() const;
+
+  private:
+    Callback callback;
+
+    const Callback& get_callback() const;
+
+    static sd_event_source* create_source(const Event& event, pid_t pid,
+                                          int options);
+
+    static int childCallback(sd_event_source* source, const siginfo_t* si,
+                             void* userdata);
+};
+
+} // namespace source
+} // namespace sdeventplus
diff --git a/src/sdeventplus/test/sdevent.hpp b/src/sdeventplus/test/sdevent.hpp
index 37b4464..1d87174 100644
--- a/src/sdeventplus/test/sdevent.hpp
+++ b/src/sdeventplus/test/sdevent.hpp
@@ -26,6 +26,9 @@
     MOCK_CONST_METHOD5(sd_event_add_signal,
                        int(sd_event*, sd_event_source**, int,
                            sd_event_signal_handler_t, void*));
+    MOCK_CONST_METHOD6(sd_event_add_child,
+                       int(sd_event*, sd_event_source**, pid_t, int,
+                           sd_event_child_handler_t, void*));
     MOCK_CONST_METHOD4(sd_event_add_defer, int(sd_event*, sd_event_source**,
                                                sd_event_handler_t, void*));
     MOCK_CONST_METHOD4(sd_event_add_post, int(sd_event*, sd_event_source**,
@@ -87,6 +90,8 @@
     MOCK_CONST_METHOD2(sd_event_source_set_time_accuracy,
                        int(sd_event_source*, uint64_t));
     MOCK_CONST_METHOD1(sd_event_source_get_signal, int(sd_event_source*));
+    MOCK_CONST_METHOD2(sd_event_source_get_child_pid,
+                       int(sd_event_source*, pid_t*));
 };
 
 } // namespace test
diff --git a/test/Makefile.am b/test/Makefile.am
index 5a6c1bb..6a5fd9e 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -37,6 +37,11 @@
 source_base_CPPFLAGS = $(gtest_cppflags)
 source_base_LDADD = $(gtest_ldadd)
 
+check_PROGRAMS += source_child
+source_child_SOURCES = source/child.cpp
+source_child_CPPFLAGS = $(gtest_cppflags)
+source_child_LDADD = $(gtest_ldadd)
+
 check_PROGRAMS += source_event
 source_event_SOURCES = source/event.cpp
 source_event_CPPFLAGS = $(gtest_cppflags)
diff --git a/test/source/child.cpp b/test/source/child.cpp
new file mode 100644
index 0000000..e1e1322
--- /dev/null
+++ b/test/source/child.cpp
@@ -0,0 +1,163 @@
+#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/child.hpp>
+#include <sdeventplus/test/sdevent.hpp>
+#include <sys/wait.h>
+#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 ChildTest : 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(ChildTest, ConstructSuccess)
+{
+    const pid_t pid = 50;
+    const int options = WEXITED;
+
+    EXPECT_CALL(mock, sd_event_ref(expected_event))
+        .WillOnce(Return(expected_event));
+    sd_event_child_handler_t handler;
+    EXPECT_CALL(mock, sd_event_add_child(expected_event, testing::_, pid,
+                                         options, 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;
+    const siginfo_t* return_si;
+    Child::Callback callback = [&](Child&, const siginfo_t* si) {
+        return_si = si;
+        completions++;
+    };
+    Child child(*event, pid, options, std::move(callback));
+    EXPECT_FALSE(callback);
+    EXPECT_EQ(&child, userdata);
+    EXPECT_EQ(0, completions);
+
+    const siginfo_t* expected_si = reinterpret_cast<siginfo_t*>(865);
+    EXPECT_EQ(0, handler(nullptr, expected_si, &child));
+    EXPECT_EQ(1, completions);
+    EXPECT_EQ(expected_si, return_si);
+
+    expect_destruct();
+}
+
+TEST_F(ChildTest, ConstructError)
+{
+    const pid_t pid = 50;
+    const int options = WEXITED;
+
+    EXPECT_CALL(mock, sd_event_add_child(expected_event, testing::_, pid,
+                                         options, testing::_, nullptr))
+        .WillOnce(Return(-EINVAL));
+    int completions = 0;
+    Child::Callback callback = [&completions](Child&, const siginfo_t*) {
+        completions++;
+    };
+    EXPECT_THROW(Child(*event, pid, options, std::move(callback)),
+                 SdEventError);
+    EXPECT_TRUE(callback);
+    EXPECT_EQ(0, completions);
+}
+
+class ChildMethodTest : public ChildTest
+{
+  protected:
+    std::unique_ptr<Child> child;
+
+    void SetUp()
+    {
+        const pid_t pid = 50;
+        const int options = WEXITED;
+
+        EXPECT_CALL(mock, sd_event_ref(expected_event))
+            .WillOnce(Return(expected_event));
+        EXPECT_CALL(mock, sd_event_add_child(expected_event, testing::_, pid,
+                                             options, testing::_, nullptr))
+            .WillOnce(DoAll(SetArgPointee<1>(expected_source), Return(0)));
+        EXPECT_CALL(mock,
+                    sd_event_source_set_userdata(expected_source, testing::_))
+            .WillOnce(Return(nullptr));
+        child = std::make_unique<Child>(*event, pid, options,
+                                        [](Child&, const siginfo_t*) {});
+    }
+
+    void TearDown()
+    {
+        expect_destruct();
+        child.reset();
+    }
+};
+
+TEST_F(ChildMethodTest, GetPidSuccess)
+{
+    const pid_t pid = 32;
+    EXPECT_CALL(mock,
+                sd_event_source_get_child_pid(expected_source, testing::_))
+        .WillOnce(DoAll(SetArgPointee<1>(pid), Return(0)));
+    EXPECT_EQ(pid, child->get_pid());
+}
+
+TEST_F(ChildMethodTest, GetPidError)
+{
+    EXPECT_CALL(mock,
+                sd_event_source_get_child_pid(expected_source, testing::_))
+        .WillOnce(Return(-EINVAL));
+    EXPECT_THROW(child->get_pid(), SdEventError);
+}
+
+} // namespace
+} // namespace source
+} // namespace sdeventplus