blob: eef2b2a8b8810e919092da144969fe993fa3c1a4 [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 */
Patrick Williams7fb6c342023-05-10 07:50:18 -050044 explicit CustomFd(int fd) : fd(fd) {}
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050045
Adriana Kobylak70dcb632018-02-27 15:46:52 -060046 ~CustomFd()
47 {
48 if (fd >= 0)
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050049 {
Adriana Kobylak70dcb632018-02-27 15:46:52 -060050 close(fd);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050051 }
Adriana Kobylak70dcb632018-02-27 15:46:52 -060052 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050053
Adriana Kobylak70dcb632018-02-27 15:46:52 -060054 int operator()() const
55 {
56 return fd;
57 }
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050058
Adriana Kobylak70dcb632018-02-27 15:46:52 -060059 private:
60 /** @brief File descriptor */
61 int fd = -1;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050062};
63
64/** @class Watch
65 *
66 * @brief Adds inotify watch on PNOR symlinks file to monitor for changes in
67 * "running" PNOR version
68 *
69 * The inotify watch is hooked up with sd-event, so that on call back,
70 * appropriate actions related to a change in the "running" PNOR version
71 * can be taken.
72 */
73class Watch
74{
Adriana Kobylak70dcb632018-02-27 15:46:52 -060075 public:
76 /** @brief ctor - hook inotify watch with sd-event
77 *
78 * @param[in] loop - sd-event object
79 * @param[in] functionalCallback - The callback function for updating
80 * the functional associations.
81 */
Lei YUf3ce4332019-02-21 14:09:49 +080082 Watch(sd_event* loop,
83 std::function<void(const 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