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