blob: 0ce5d0ed314d3420d085ce3832f569f7cfa7e7fe [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{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060029 public:
30 CustomFd() = delete;
31 CustomFd(const CustomFd&) = delete;
32 CustomFd& operator=(const CustomFd&) = delete;
33 CustomFd(CustomFd&&) = delete;
34 CustomFd& operator=(CustomFd&&) = delete;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050035
Adriana Kobylak70dcb632018-02-27 15:46:52 -060036 /** @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 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050043
Adriana Kobylak70dcb632018-02-27 15:46:52 -060044 ~CustomFd()
45 {
46 if (fd >= 0)
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050047 {
Adriana Kobylak70dcb632018-02-27 15:46:52 -060048 close(fd);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050049 }
Adriana Kobylak70dcb632018-02-27 15:46:52 -060050 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050051
Adriana Kobylak70dcb632018-02-27 15:46:52 -060052 int operator()() const
53 {
54 return fd;
55 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050056
Adriana Kobylak70dcb632018-02-27 15:46:52 -060057 private:
58 /** @brief File descriptor */
59 int fd = -1;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050060};
61
62/** @class Watch
63 *
64 * @brief Adds inotify watch on PNOR symlinks file to monitor for changes in
65 * "running" PNOR version
66 *
67 * The inotify watch is hooked up with sd-event, so that on call back,
68 * appropriate actions related to a change in the "running" PNOR version
69 * can be taken.
70 */
71class Watch
72{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060073 public:
74 /** @brief ctor - hook inotify watch with sd-event
75 *
76 * @param[in] loop - sd-event object
77 * @param[in] functionalCallback - The callback function for updating
78 * the functional associations.
79 */
80 Watch(sd_event* loop, std::function<void(std::string&)> functionalCallback);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050081
Adriana Kobylak70dcb632018-02-27 15:46:52 -060082 Watch(const Watch&) = delete;
83 Watch& operator=(const Watch&) = delete;
84 Watch(Watch&&) = delete;
85 Watch& operator=(Watch&&) = delete;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050086
Adriana Kobylak70dcb632018-02-27 15:46:52 -060087 /** @brief dtor - remove inotify watch
88 */
89 ~Watch();
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050090
Adriana Kobylak70dcb632018-02-27 15:46:52 -060091 private:
92 /** @brief sd-event callback
93 *
94 * @param[in] s - event source, floating (unused) in our case
95 * @param[in] fd - inotify fd
96 * @param[in] revents - events that matched for fd
97 * @param[in] userdata - pointer to Watch object
98 * @returns 0 on success, -1 on fail
99 */
100 static int callback(sd_event_source* s, int fd, uint32_t revents,
101 void* userdata);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500102
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600103 /** initialize an inotify instance and returns file descriptor */
104 int inotifyInit();
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500105
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600106 /** @brief PNOR symlink file watch descriptor */
107 int wd = -1;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500108
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600109 /** @brief event source */
110 EventSourcePtr eventSource;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500111
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600112 /** @brief The callback function for updating the
113 functional associations. */
114 std::function<void(std::string&)> functionalCallback;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500115
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600116 /** @brief inotify file descriptor */
117 CustomFd fd;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500118};
119
120} // namespace updater
121} // namespace software
122} // namespace openpower