blob: ba351e118e14b0dbca4ac1c28ec74e196ccff844 [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 Othayoth671fc7f2017-06-14 08:01:41 -05007
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05008namespace phosphor
9{
10namespace dump
11{
12
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050013/* Need a custom deleter for freeing up sd_event */
14struct EventDeleter
15{
16 void operator()(sd_event* event) const
17 {
18 event = sd_event_unref(event);
19 }
20};
21using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
22
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050023/** @struct CustomFd
24 *
25 * RAII wrapper for file descriptor.
26 */
27struct CustomFd
28{
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050029 private:
30 /** @brief File descriptor */
31 int fd = -1;
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050032
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050033 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 Othayotha320c7c2017-06-14 07:17:21 -050039
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050040 /** @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 Othayotha320c7c2017-06-14 07:17:21 -050047
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050048 ~CustomFd()
49 {
50 if (fd >= 0)
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050051 {
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050052 close(fd);
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050053 }
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050054 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050055
Jayanth Othayothcb65ffc2018-10-16 08:29:32 -050056 int operator()() const
57 {
58 return fd;
59 }
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050060};
61
62} // namespace dump
63} // namespace phosphor