summaryrefslogtreecommitdiffstats
path: root/dump_utils.hpp
blob: e985112bdfbf94d0e24bd063e45d9e04b449af81 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#pragma once

#include <memory>
#include <unistd.h>
#include <systemd/sd-event.h>

namespace phosphor
{
namespace dump
{

/* Need a custom deleter for freeing up sd_event */
struct EventDeleter
{
    void operator()(sd_event* event) const
    {
        event = sd_event_unref(event);
    }
};
using EventPtr = std::unique_ptr<sd_event, EventDeleter>;

/** @struct CustomFd
 *
 *  RAII wrapper for file descriptor.
 */
struct CustomFd
{
    private:
        /** @brief File descriptor */
        int fd = -1;

    public:
        CustomFd() = delete;
        CustomFd(const CustomFd&) = delete;
        CustomFd& operator=(const CustomFd&) = delete;
        CustomFd(CustomFd&&) = delete;
        CustomFd& operator=(CustomFd&&) = delete;

        /** @brief Saves File descriptor and uses it to do file operation
          *
          *  @param[in] fd - File descriptor
          */
        CustomFd(int fd) : fd(fd) {}

        ~CustomFd()
        {
            if (fd >= 0)
            {
                close(fd);
            }
        }

        int operator()() const
        {
            return fd;
        }
};

} // namespace dump
} // namespace phosphor
OpenPOWER on IntegriCloud