blob: da1ffad82a6d672a2f047f0260988b030f76ebf7 [file] [log] [blame]
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05001#pragma once
2
3#include <experimental/filesystem>
Brad Bishop78610792018-02-21 13:08:15 -05004#include <functional>
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -05005#include <systemd/sd-event.h>
6#include <sys/inotify.h>
7#include <map>
8
9#include "dump_utils.hpp"
10
11namespace phosphor
12{
13namespace dump
14{
15namespace inotify
16{
17
18namespace fs = std::experimental::filesystem;
19
Gunnar Mills95a72982017-10-25 17:00:14 -050020//User specific call back function input map(path:event) type.
Jayanth Othayoth671fc7f2017-06-14 08:01:41 -050021using UserMap = std::map<fs::path, uint32_t>;
22
23//User specific callback function wrapper type.
24using UserType = std::function<void(const UserMap&)>;
25
26/** @class Watch
27 *
28 * @brief Adds inotify watch on directory.
29 *
30 * The inotify watch is hooked up with sd-event, so that on call back,
31 * appropriate actions are taken to collect files from the directory
32 * initialized by the object.
33 */
34class Watch
35{
36 public:
37 /** @brief ctor - hook inotify watch with sd-event
38 *
39 * @param[in] eventObj - Event loop object
40 * @param[in] flags - inotify flags
41 * @param[in] mask - Mask of events
42 * @param[in] events - Events to be watched
43 * @param[in] path - File path to be watched
44 * @param[in] userFunc - User specific callback fnction wrapper.
45 *
46 */
47 Watch(const EventPtr& eventObj,
48 int flags,
49 uint32_t mask,
50 uint32_t events,
51 const fs::path& path,
52 UserType userFunc);
53
54 Watch(const Watch&) = delete;
55 Watch& operator=(const Watch&) = delete;
56 Watch(Watch&&) = default;
57 Watch& operator=(Watch&&) = default;
58
59 /* @brief dtor - remove inotify watch and close fd's */
60 ~Watch();
61
62 private:
63 /** @brief sd-event callback.
64 * @details Collects the files and event info and call the
65 * appropriate user function for further action.
66 *
67 * @param[in] s - event source, floating (unused) in our case
68 * @param[in] fd - inotify fd
69 * @param[in] revents - events that matched for fd
70 * @param[in] userdata - pointer to Watch object
71 *
72 * @returns 0 on success, -1 on fail
73 */
74 static int callback(sd_event_source* s,
75 int fd,
76 uint32_t revents,
77 void* userdata);
78
79 /** initialize an inotify instance and returns file descriptor */
80 int inotifyInit();
81
82 /** @brief inotify flags */
83 int flags;
84
85 /** @brief Mask of events */
86 uint32_t mask;
87
88 /** @brief Events to be watched */
89 uint32_t events;
90
91 /** @brief File path to be watched */
92 fs::path path;
93
94 /** @brief dump file directory watch descriptor */
95 int wd = -1;
96
97 /** @brief file descriptor manager */
98 CustomFd fd;
99
100 /** @brief The user level callback function wrapper */
101 UserType userFunc;
102};
103
104} // namespace inotify
105} // namespace dump
106} // namespace phosphor