blob: b45f53f8763fa82e2040e4dcd6011f5da87480ab [file] [log] [blame]
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +05301#pragma once
2
Patrick Venture189d44e2018-07-09 12:30:59 -07003#include "dns_updater.hpp"
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +05304#include "types.hpp"
Gunnar Mills57d9c502018-09-14 14:42:34 -05005#include "util.hpp"
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +05306
7#include <sys/inotify.h>
Patrick Venture189d44e2018-07-09 12:30:59 -07008#include <systemd/sd-event.h>
9
Manojkiran Edaa879baa2020-06-13 14:39:08 +053010#include <filesystem>
Patrick Venture189d44e2018-07-09 12:30:59 -070011#include <functional>
12#include <map>
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053013
14namespace phosphor
15{
16namespace network
17{
18namespace inotify
19{
20
Manojkiran Edaa879baa2020-06-13 14:39:08 +053021namespace fs = std::filesystem;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053022
23// Auxiliary callback to be invoked on inotify events
24using UserCallBack = std::function<void(const std::string&)>;
25
26/** @class Watch
27 *
28 * @brief Adds inotify watch on directory
29 *
30 * @details Calls back user function on matching events
31 */
32class Watch
33{
Gunnar Mills57d9c502018-09-14 14:42:34 -050034 public:
35 Watch() = delete;
36 Watch(const Watch&) = delete;
37 Watch& operator=(const Watch&) = delete;
38 Watch(Watch&&) = delete;
39 Watch& operator=(Watch&&) = delete;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053040
Gunnar Mills57d9c502018-09-14 14:42:34 -050041 /** @brief Hooks inotify watch with sd-event
42 *
43 * @param[in] eventPtr - Reference to sd_event wrapped in unique_ptr
44 * @param[in] path - File path to be watched
45 * @param[in] userFunc - User specific callback function on events
46 * @param[in] flags - Flags to be supplied to inotify
47 * @param[in] mask - Mask of events to be supplied to inotify
48 * @param[in] events - Events to be watched
49 */
50 Watch(phosphor::network::EventPtr& eventPtr, const fs::path path,
51 UserCallBack userFunc, int flags = IN_NONBLOCK,
52 uint32_t mask = IN_CLOSE_WRITE, uint32_t events = EPOLLIN);
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053053
Gunnar Mills57d9c502018-09-14 14:42:34 -050054 /** @brief Remove inotify watch and close fd's */
55 ~Watch()
56 {
57 if ((fd() >= 0) && (wd >= 0))
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053058 {
Gunnar Mills57d9c502018-09-14 14:42:34 -050059 inotify_rm_watch(fd(), wd);
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053060 }
Gunnar Mills57d9c502018-09-14 14:42:34 -050061 }
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053062
Gunnar Mills57d9c502018-09-14 14:42:34 -050063 private:
64 /** @brief Callback invoked when inotify event fires
65 *
66 * @details On a matching event, calls back into user supplied
67 * function if there is one registered
68 *
69 * @param[in] eventSource - Event source
70 * @param[in] fd - Inotify fd
71 * @param[in] retEvents - Events that matched for fd
72 * @param[in] userData - Pointer to Watch object
73 *
74 * @returns 0 on success, -1 on fail
75 */
76 static int processEvents(sd_event_source* eventSource, int fd,
77 uint32_t retEvents, void* userData);
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053078
Gunnar Mills57d9c502018-09-14 14:42:34 -050079 /** @brief Initializes an inotify instance
80 *
81 * @return Descriptor on success, -1 on failure
82 */
83 int inotifyInit();
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053084
Gunnar Mills57d9c502018-09-14 14:42:34 -050085 /** @brief File path to be watched */
86 const fs::path path;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053087
Gunnar Mills57d9c502018-09-14 14:42:34 -050088 /** @brief User callback function */
89 UserCallBack userFunc;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053090
Gunnar Mills57d9c502018-09-14 14:42:34 -050091 /** @brief Inotify flags */
92 int flags;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053093
Gunnar Mills57d9c502018-09-14 14:42:34 -050094 /** @brief Mask of events */
95 uint32_t mask;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053096
Gunnar Mills57d9c502018-09-14 14:42:34 -050097 /** @brief Events to be watched */
98 uint32_t events;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +053099
Gunnar Mills57d9c502018-09-14 14:42:34 -0500100 /** @brief Watch descriptor */
101 int wd = -1;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +0530102
Gunnar Mills57d9c502018-09-14 14:42:34 -0500103 /** @brief File descriptor manager */
104 phosphor::Descriptor fd;
Vishwanatha Subbannaca4ce1b2017-10-16 23:17:18 +0530105};
106
107} // namespace inotify
108} // namespace network
109} // namespace phosphor