blob: fa9441a50df953da8138779bb01f6864592ed3cc [file] [log] [blame]
Gunnar Millsf6ed5892018-09-07 17:08:02 -05001#include "config.h"
2
3#include "watch.hpp"
4
Lei YUbee51402019-02-26 11:36:34 +08005#include "item_updater_ubi.hpp"
Gunnar Millsf6ed5892018-09-07 17:08:02 -05006
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -05007#include <sys/inotify.h>
8#include <unistd.h>
Gunnar Millsf6ed5892018-09-07 17:08:02 -05009
Brad Bishop8facccf2020-11-04 09:44:58 -050010#include <phosphor-logging/log.hpp>
11
Gunnar Millsf6ed5892018-09-07 17:08:02 -050012#include <cstddef>
13#include <cstring>
Brad Bishop9f44c992020-11-06 14:48:46 -050014#include <filesystem>
Gunnar Millsf6ed5892018-09-07 17:08:02 -050015#include <functional>
Gunnar Millsf6ed5892018-09-07 17:08:02 -050016#include <stdexcept>
17#include <string>
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050018
19namespace openpower
20{
21namespace software
22{
23namespace updater
24{
25
26using namespace phosphor::logging;
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050027
28Watch::Watch(sd_event* loop,
Lei YUf3ce4332019-02-21 14:09:49 +080029 std::function<void(const std::string&)> functionalCallback) :
Patrick Williamsf8e02422024-08-16 15:19:59 -040030 functionalCallback(functionalCallback), fd(inotifyInit())
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050031
32{
33 // Create PNOR_ACTIVE_PATH if doesn't exist.
Brad Bishop9f44c992020-11-06 14:48:46 -050034 if (!std::filesystem::is_directory(PNOR_ACTIVE_PATH))
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050035 {
Brad Bishop9f44c992020-11-06 14:48:46 -050036 std::filesystem::create_directories(PNOR_ACTIVE_PATH);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050037 }
38
39 wd = inotify_add_watch(fd(), PNOR_ACTIVE_PATH, IN_CREATE);
40 if (-1 == wd)
41 {
42 auto error = errno;
Adriana Kobylak70dcb632018-02-27 15:46:52 -060043 throw std::system_error(error, std::generic_category(),
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050044 "Error occurred during the inotify_init1");
45 }
46
47 decltype(eventSource.get()) sourcePtr = nullptr;
Adriana Kobylak70dcb632018-02-27 15:46:52 -060048 auto rc = sd_event_add_io(loop, &sourcePtr, fd(), EPOLLIN, callback, this);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050049
50 eventSource.reset(sourcePtr);
51
52 if (0 > rc)
53 {
Adriana Kobylak70dcb632018-02-27 15:46:52 -060054 throw std::system_error(-rc, std::generic_category(),
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050055 "Error occurred during the inotify_init1");
56 }
57}
58
59Watch::~Watch()
60{
61 if ((-1 != fd()) && (-1 != wd))
62 {
63 inotify_rm_watch(fd(), wd);
64 }
65}
66
Brad Bishopc8f22502020-11-06 14:42:09 -050067int Watch::callback(sd_event_source*, int fd, uint32_t revents, void* userdata)
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050068{
69 if (!(revents & EPOLLIN))
70 {
71 return 0;
72 }
73
74 constexpr auto maxBytes = 1024;
75 uint8_t buffer[maxBytes];
76 auto bytes = read(fd, buffer, maxBytes);
77 if (0 > bytes)
78 {
79 auto error = errno;
Adriana Kobylak70dcb632018-02-27 15:46:52 -060080 throw std::system_error(error, std::generic_category(),
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050081 "failed to read inotify event");
82 }
83
84 auto offset = 0;
85 while (offset < bytes)
86 {
87 auto event = reinterpret_cast<inotify_event*>(&buffer[offset]);
88 // Update the functional association on a RO
89 // active image symlink change
Brad Bishop9f44c992020-11-06 14:48:46 -050090 std::filesystem::path path(PNOR_ACTIVE_PATH);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050091 path /= event->name;
Brad Bishop9f44c992020-11-06 14:48:46 -050092 if (std::filesystem::equivalent(path, PNOR_RO_ACTIVE_PATH))
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050093 {
Lei YUbee51402019-02-26 11:36:34 +080094 auto id = ItemUpdaterUbi::determineId(path);
Lei YUf3ce4332019-02-21 14:09:49 +080095 static_cast<Watch*>(userdata)->functionalCallback(id);
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -050096 }
97 offset += offsetof(inotify_event, name) + event->len;
98 }
99
100 return 0;
101}
102
103int Watch::inotifyInit()
104{
105 auto fd = inotify_init1(IN_NONBLOCK);
106
107 if (-1 == fd)
108 {
109 auto error = errno;
Adriana Kobylak70dcb632018-02-27 15:46:52 -0600110 throw std::system_error(error, std::generic_category(),
Gunnar Mills6bd6d7b2017-09-18 09:22:36 -0500111 "Error occurred during the inotify_init1");
112 }
113
114 return fd;
115}
116
117} // namespace updater
118} // namespace software
119} // namespace openpower