blob: e985112bdfbf94d0e24bd063e45d9e04b449af81 [file] [log] [blame]
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05001#pragma once
2
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05003#include <memory>
4#include <unistd.h>
Jayanth Othayothd02153c2017-07-02 22:29:42 -05005#include <systemd/sd-event.h>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05006
Jayanth Othayotha320c7c2017-06-14 07:17:21 -05007namespace phosphor
8{
9namespace dump
10{
11
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050012/* Need a custom deleter for freeing up sd_event */
13struct EventDeleter
14{
15 void operator()(sd_event* event) const
16 {
17 event = sd_event_unref(event);
18 }
19};
20using EventPtr = std::unique_ptr<sd_event, EventDeleter>;
21
Jayanth Othayotha320c7c2017-06-14 07:17:21 -050022/** @struct CustomFd
23 *
24 * RAII wrapper for file descriptor.
25 */
26struct 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