1 #pragma once
2 
3 #include <functional>
4 #include <sdeventplus/source/base.hpp>
5 #include <signal.h>
6 
7 namespace sdeventplus
8 {
9 namespace source
10 {
11 
12 /** @class Child
13  *  @brief A wrapper around the sd_event_source child type
14  *         See sd_event_add_child(3) for more information
15  */
16 class Child : public Base
17 {
18   public:
19     using Callback = std::function<void(Child& source, const siginfo_t* si)>;
20 
21     /** @brief Adds a new child source handler to the Event
22      *         This type of source defaults to Enabled::Oneshot, and needs to be
23      *         reconfigured upon each callback.
24      *
25      *  @param[in] event    - The event to attach the handler
26      *  @param[in] pid      - The pid of the child to monitor
27      *  @param[in] options  - An OR-ed mask that determines triggers
28      *                        See waitid(2) for further information
29      *  @param[in] callback - The function executed on event dispatch
30      *  @throws SdEventError for underlying sd_event errors
31      */
32     Child(const Event& event, pid_t pid, int options, Callback&& callback);
33 
34     /** @brief Gets the pid of the child process being watched
35      *
36      *  @return The child pid
37      *  @throws SdEventError for underlying sd_event errors
38      */
39     pid_t get_pid() const;
40 
41   private:
42     Callback callback;
43 
44     /** @brief Returns a reference to the callback executed for this source
45      *
46      *  @return A reference to the callback
47      */
48     const Callback& get_callback() const;
49 
50     /** @brief Creates a new child source attached to the Event
51      *
52      *  @param[in] event   - The event to attach the handler
53      *  @param[in] pid     - The pid of the child to monitor
54      *  @param[in] options - An OR-ed mask that determines triggers
55      *  @throws SdEventError for underlying sd_event errors
56      *  @return A new sd_event_source
57      */
58     static sd_event_source* create_source(const Event& event, pid_t pid,
59                                           int options);
60 
61     /** @brief A wrapper around the callback that can be called from sd-event
62      *
63      *  @param[in] source   - The sd_event_source associated with the call
64      *  @param[in] userdata - The provided userdata for the source
65      *  @return 0 on success or a negative errno otherwise
66      */
67     static int childCallback(sd_event_source* source, const siginfo_t* si,
68                              void* userdata);
69 };
70 
71 } // namespace source
72 } // namespace sdeventplus
73