blob: f7ae6afe7765d96da09c5bd42820dc127915eab5 [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
Gunnar Millsf6ed5892018-09-07 17:08:02 -05006#include <functional>
7#include <memory>
8
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -05009namespace openpower
10{
11namespace software
12{
13namespace updater
14{
15
16/* Need a custom deleter for freeing up sd_event_source */
17struct EventSourceDeleter
18{
19 void operator()(sd_event_source* eventSource) const
20 {
21 eventSource = sd_event_source_unref(eventSource);
22 }
23};
24using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>;
25
26/** @struct CustomFd
27 *
28 * RAII wrapper for file descriptor.
29 */
30struct CustomFd
31{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060032 public:
33 CustomFd() = delete;
34 CustomFd(const CustomFd&) = delete;
35 CustomFd& operator=(const CustomFd&) = delete;
36 CustomFd(CustomFd&&) = delete;
37 CustomFd& operator=(CustomFd&&) = delete;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050038
Adriana Kobylak70dcb632018-02-27 15:46:52 -060039 /** @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 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050046
Adriana Kobylak70dcb632018-02-27 15:46:52 -060047 ~CustomFd()
48 {
49 if (fd >= 0)
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050050 {
Adriana Kobylak70dcb632018-02-27 15:46:52 -060051 close(fd);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050052 }
Adriana Kobylak70dcb632018-02-27 15:46:52 -060053 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050054
Adriana Kobylak70dcb632018-02-27 15:46:52 -060055 int operator()() const
56 {
57 return fd;
58 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050059
Adriana Kobylak70dcb632018-02-27 15:46:52 -060060 private:
61 /** @brief File descriptor */
62 int fd = -1;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050063};
64
65/** @class Watch
66 *
67 * @brief Adds inotify watch on PNOR symlinks file to monitor for changes in
68 * "running" PNOR version
69 *
70 * The inotify watch is hooked up with sd-event, so that on call back,
71 * appropriate actions related to a change in the "running" PNOR version
72 * can be taken.
73 */
74class Watch
75{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060076 public:
77 /** @brief ctor - hook inotify watch with sd-event
78 *
79 * @param[in] loop - sd-event object
80 * @param[in] functionalCallback - The callback function for updating
81 * the functional associations.
82 */
83 Watch(sd_event* loop, std::function<void(std::string&)> functionalCallback);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050084
Adriana Kobylak70dcb632018-02-27 15:46:52 -060085 Watch(const Watch&) = delete;
86 Watch& operator=(const Watch&) = delete;
87 Watch(Watch&&) = delete;
88 Watch& operator=(Watch&&) = delete;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050089
Adriana Kobylak70dcb632018-02-27 15:46:52 -060090 /** @brief dtor - remove inotify watch
91 */
92 ~Watch();
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050093
Adriana Kobylak70dcb632018-02-27 15:46:52 -060094 private:
95 /** @brief sd-event callback
96 *
97 * @param[in] s - event source, floating (unused) in our case
98 * @param[in] fd - inotify fd
99 * @param[in] revents - events that matched for fd
100 * @param[in] userdata - pointer to Watch object
101 * @returns 0 on success, -1 on fail
102 */
103 static int callback(sd_event_source* s, int fd, uint32_t revents,
104 void* userdata);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500105
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600106 /** initialize an inotify instance and returns file descriptor */
107 int inotifyInit();
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500108
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600109 /** @brief PNOR symlink file watch descriptor */
110 int wd = -1;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500111
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600112 /** @brief event source */
113 EventSourcePtr eventSource;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500114
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600115 /** @brief The callback function for updating the
116 functional associations. */
117 std::function<void(std::string&)> functionalCallback;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500118
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600119 /** @brief inotify file descriptor */
120 CustomFd fd;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500121};
122
123} // namespace updater
124} // namespace software
125} // namespace openpower