1 #pragma once
2 
3 #include <systemd/sd-event.h>
4 #include <unistd.h>
5 
6 #include <memory>
7 #include <sdbusplus/bus.hpp>
8 
9 namespace phosphor
10 {
11 namespace dump
12 {
13 
14 /* Need a custom deleter for freeing up sd_event */
15 struct EventDeleter
16 {
17     void operator()(sd_event* event) const
18     {
19         event = sd_event_unref(event);
20     }
21 };
22 using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
23 
24 /** @struct CustomFd
25  *
26  *  RAII wrapper for file descriptor.
27  */
28 struct CustomFd
29 {
30   private:
31     /** @brief File descriptor */
32     int fd = -1;
33 
34   public:
35     CustomFd() = delete;
36     CustomFd(const CustomFd&) = delete;
37     CustomFd& operator=(const CustomFd&) = delete;
38     CustomFd(CustomFd&&) = delete;
39     CustomFd& operator=(CustomFd&&) = delete;
40 
41     /** @brief Saves File descriptor and uses it to do file operation
42      *
43      *  @param[in] fd - File descriptor
44      */
45     CustomFd(int fd) : fd(fd)
46     {
47     }
48 
49     ~CustomFd()
50     {
51         if (fd >= 0)
52         {
53             close(fd);
54         }
55     }
56 
57     int operator()() const
58     {
59         return fd;
60     }
61 };
62 
63 /**
64  * @brief Get the bus service
65  *
66  * @param[in] bus - Bus to attach to.
67  * @param[in] path - D-Bus path name.
68  * @param[in] interface - D-Bus interface name.
69  * @return the bus service as a string
70  **/
71 std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
72                        const std::string& interface);
73 
74 } // namespace dump
75 } // namespace phosphor
76