blob: 2628e938e8ca90836e88a19382127dfaf4c8e990 [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>
Andrew Geisslerab139ce2020-05-16 13:22:09 -05008#include <string>
Gunnar Millsf6ed5892018-09-07 17:08:02 -05009
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050010namespace openpower
11{
12namespace software
13{
14namespace updater
15{
16
17/* Need a custom deleter for freeing up sd_event_source */
18struct EventSourceDeleter
19{
20 void operator()(sd_event_source* eventSource) const
21 {
Lei YU1db9adf2019-03-05 16:02:31 +080022 sd_event_source_unref(eventSource);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050023 }
24};
25using EventSourcePtr = std::unique_ptr<sd_event_source, EventSourceDeleter>;
26
27/** @struct CustomFd
28 *
29 * RAII wrapper for file descriptor.
30 */
31struct CustomFd
32{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060033 public:
34 CustomFd() = delete;
35 CustomFd(const CustomFd&) = delete;
36 CustomFd& operator=(const CustomFd&) = delete;
37 CustomFd(CustomFd&&) = delete;
38 CustomFd& operator=(CustomFd&&) = delete;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050039
Adriana Kobylak70dcb632018-02-27 15:46:52 -060040 /** @brief Saves File descriptor and uses it to do file operation
41 *
42 * @param[in] fd - File descriptor
43 */
Lei YU1db9adf2019-03-05 16:02:31 +080044 explicit CustomFd(int fd) : fd(fd)
Brad Bishop8facccf2020-11-04 09:44:58 -050045 {}
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 */
Lei YUf3ce4332019-02-21 14:09:49 +080083 Watch(sd_event* loop,
84 std::function<void(const std::string&)> functionalCallback);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050085
Adriana Kobylak70dcb632018-02-27 15:46:52 -060086 Watch(const Watch&) = delete;
87 Watch& operator=(const Watch&) = delete;
88 Watch(Watch&&) = delete;
89 Watch& operator=(Watch&&) = delete;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050090
Adriana Kobylak70dcb632018-02-27 15:46:52 -060091 /** @brief dtor - remove inotify watch
92 */
93 ~Watch();
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050094
Adriana Kobylak70dcb632018-02-27 15:46:52 -060095 private:
96 /** @brief sd-event callback
97 *
98 * @param[in] s - event source, floating (unused) in our case
99 * @param[in] fd - inotify fd
100 * @param[in] revents - events that matched for fd
101 * @param[in] userdata - pointer to Watch object
102 * @returns 0 on success, -1 on fail
103 */
104 static int callback(sd_event_source* s, int fd, uint32_t revents,
105 void* userdata);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500106
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600107 /** initialize an inotify instance and returns file descriptor */
108 int inotifyInit();
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500109
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600110 /** @brief PNOR symlink file watch descriptor */
111 int wd = -1;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500112
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600113 /** @brief event source */
114 EventSourcePtr eventSource;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500115
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600116 /** @brief The callback function for updating the
117 functional associations. */
118 std::function<void(std::string&)> functionalCallback;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500119
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600120 /** @brief inotify file descriptor */
121 CustomFd fd;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500122};
123
124} // namespace updater
125} // namespace software
126} // namespace openpower