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