blob: bdea792c081b620e7855596a6c895c1337597206 [file] [log] [blame]
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -05001#pragma once
2
3#include <systemd/sd-event.h>
4#include <unistd.h>
5
6namespace openpower
7{
8namespace software
9{
10namespace updater
11{
12
13/* Need a custom deleter for freeing up sd_event_source */
14struct EventSourceDeleter
15{
16 void operator()(sd_event_source* eventSource) const
17 {
18 eventSource = sd_event_source_unref(eventSource);
19 }
20};
21using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>;
22
23/** @struct CustomFd
24 *
25 * RAII wrapper for file descriptor.
26 */
27struct CustomFd
28{
29 public:
30 CustomFd() = delete;
31 CustomFd(const CustomFd&) = delete;
32 CustomFd& operator=(const CustomFd&) = delete;
33 CustomFd(CustomFd&&) = delete;
34 CustomFd& operator=(CustomFd&&) = delete;
35
36 /** @brief Saves File descriptor and uses it to do file operation
37 *
38 * @param[in] fd - File descriptor
39 */
40 CustomFd(int fd) : fd(fd) {}
41
42 ~CustomFd()
43 {
44 if (fd >= 0)
45 {
46 close(fd);
47 }
48 }
49
50 int operator()() const
51 {
52 return fd;
53 }
54
55 private:
56 /** @brief File descriptor */
57 int fd = -1;
58};
59
60/** @class Watch
61 *
62 * @brief Adds inotify watch on PNOR symlinks file to monitor for changes in
63 * "running" PNOR version
64 *
65 * The inotify watch is hooked up with sd-event, so that on call back,
66 * appropriate actions related to a change in the "running" PNOR version
67 * can be taken.
68 */
69class Watch
70{
71 public:
72 /** @brief ctor - hook inotify watch with sd-event
73 *
74 * @param[in] loop - sd-event object
75 * @param[in] functionalCallback - The callback function for updating
76 * the functional associations.
77 */
78 Watch(sd_event* loop,
79 std::function<void(std::string&)> functionalCallback);
80
81 Watch(const Watch&) = delete;
82 Watch& operator=(const Watch&) = delete;
83 Watch(Watch&&) = delete;
84 Watch& operator=(Watch&&) = delete;
85
86 /** @brief dtor - remove inotify watch
87 */
88 ~Watch();
89
90 private:
91 /** @brief sd-event callback
92 *
93 * @param[in] s - event source, floating (unused) in our case
94 * @param[in] fd - inotify fd
95 * @param[in] revents - events that matched for fd
96 * @param[in] userdata - pointer to Watch object
97 * @returns 0 on success, -1 on fail
98 */
99 static int callback(sd_event_source* s,
100 int fd,
101 uint32_t revents,
102 void* userdata);
103
104 /** initialize an inotify instance and returns file descriptor */
105 int inotifyInit();
106
107 /** @brief PNOR symlink file watch descriptor */
108 int wd = -1;
109
110 /** @brief event source */
111 EventSourcePtr eventSource;
112
113 /** @brief The callback function for updating the
114 functional associations. */
115 std::function<void(std::string&)> functionalCallback;
116
117 /** @brief inotify file descriptor */
118 CustomFd fd;
119};
120
121} // namespace updater
122} // namespace software
123} // namespace openpower