blob: a2c26043d8f99d0239a676d3fb6c5346dcf57cb8 [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#pragma once
2
Jayanth Othayothd02153c2017-07-02 22:29:42 -05003#include <systemd/sd-event.h>
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -05004#include <unistd.h>
5
6#include <memory>
Jayanth Othayothd31be2c2020-02-04 02:56:45 -06007#include <sdbusplus/bus.hpp>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05008
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05009namespace phosphor
10{
11namespace dump
12{
13
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050014/* Need a custom deleter for freeing up sd_event */
15struct EventDeleter
16{
17 void operator()(sd_event* event) const
18 {
19 event = sd_event_unref(event);
20 }
21};
22using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
23
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050024/** @struct CustomFd
25 *
26 * RAII wrapper for file descriptor.
27 */
28struct CustomFd
29{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050030 private:
31 /** @brief File descriptor */
32 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050033
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050034 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 Othayotha320c7c2017-06-14 07:17:21 -050040
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050041 /** @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 Othayotha320c7c2017-06-14 07:17:21 -050048
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050049 ~CustomFd()
50 {
51 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050052 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050053 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050054 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050055 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050056
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050057 int operator()() const
58 {
59 return fd;
60 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050061};
62
Jayanth Othayothd31be2c2020-02-04 02:56:45 -060063/**
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 **/
71std::string getService(sdbusplus::bus::bus& bus, const std::string& path,
72 const std::string& interface);
73
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050074} // namespace dump
75} // namespace phosphor