Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 1 | #pragma once |
| 2 | |
Jayanth Othayoth | d02153c | 2017-07-02 22:29:42 -0500 | [diff] [blame] | 3 | #include <systemd/sd-event.h> |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 4 | #include <unistd.h> |
| 5 | |
| 6 | #include <memory> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 7 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 8 | namespace phosphor |
| 9 | { |
| 10 | namespace dump |
| 11 | { |
| 12 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 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 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 23 | /** @struct CustomFd |
| 24 | * |
| 25 | * RAII wrapper for file descriptor. |
| 26 | */ |
| 27 | struct CustomFd |
| 28 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 29 | private: |
| 30 | /** @brief File descriptor */ |
| 31 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 32 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 39 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 47 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 48 | ~CustomFd() |
| 49 | { |
| 50 | if (fd >= 0) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 51 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 52 | close(fd); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 53 | } |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 54 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 55 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 56 | int operator()() const |
| 57 | { |
| 58 | return fd; |
| 59 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 60 | }; |
| 61 | |
| 62 | } // namespace dump |
| 63 | } // namespace phosphor |