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 | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 7 | #include <sdbusplus/bus.hpp> |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 8 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 9 | namespace phosphor |
| 10 | { |
| 11 | namespace dump |
| 12 | { |
| 13 | |
Jayanth Othayoth | 671fc7f | 2017-06-14 08:01:41 -0500 | [diff] [blame] | 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 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 24 | /** @struct CustomFd |
| 25 | * |
| 26 | * RAII wrapper for file descriptor. |
| 27 | */ |
| 28 | struct CustomFd |
| 29 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 30 | private: |
| 31 | /** @brief File descriptor */ |
| 32 | int fd = -1; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 33 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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; |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 40 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 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 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 48 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 49 | ~CustomFd() |
| 50 | { |
| 51 | if (fd >= 0) |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 52 | { |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 53 | close(fd); |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 54 | } |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 55 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 56 | |
Jayanth Othayoth | cb65ffc | 2018-10-16 08:29:32 -0500 | [diff] [blame] | 57 | int operator()() const |
| 58 | { |
| 59 | return fd; |
| 60 | } |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 61 | }; |
| 62 | |
Jayanth Othayoth | d31be2c | 2020-02-04 02:56:45 -0600 | [diff] [blame] | 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 | |
Jayanth Othayoth | a320c7c | 2017-06-14 07:17:21 -0500 | [diff] [blame] | 74 | } // namespace dump |
| 75 | } // namespace phosphor |